Skip to content

Commit

Permalink
feat: add contional_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shulaoda committed Sep 28, 2024
1 parent 78c01ef commit 1961f35
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions crates/oxc_linter/src/rules/eslint/no_throw_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ declare_oxc_lint!(
/// ```
NoThrowLiteral,
correctness,
conditional_fix,
);

impl Rule for NoThrowLiteral {
Expand All @@ -76,11 +77,24 @@ impl Rule for NoThrowLiteral {

let expr = &stmt.argument;

if !Self::could_be_error(expr) {
ctx.diagnostic(no_throw_literal_diagnostic(expr.span(), false));
} else if matches!(expr, Expression::Identifier(id) if id.name == "undefined") {
ctx.diagnostic(no_throw_literal_diagnostic(expr.span(), true));
};
match expr.get_inner_expression() {
Expression::StringLiteral(_) | Expression::TemplateLiteral(_) => {
let span = expr.span();
ctx.diagnostic_with_fix(no_throw_literal_diagnostic(span, false), |fixer| {
fixer.replace(
span,
format!("new Error({})", span.source_text(ctx.source_text())),
)
});
}
_ => {
if !Self::could_be_error(expr) {
ctx.diagnostic(no_throw_literal_diagnostic(expr.span(), false));
} else if matches!(expr, Expression::Identifier(id) if id.name == "undefined") {
ctx.diagnostic(no_throw_literal_diagnostic(expr.span(), true));
};
}
}
}
}

Expand Down Expand Up @@ -195,5 +209,11 @@ fn test() {
"throw 'error' satisfies Error",
];

Tester::new(NoThrowLiteral::NAME, pass, fail).test_and_snapshot();
let fix = vec![
("throw 'error';", "throw new Error('error');"),
("throw `${err}`;", "throw new Error(`${err}`);"),
("throw 'error' satisfies Error", "throw new Error('error' satisfies Error)"),
];

Tester::new(NoThrowLiteral::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
}

0 comments on commit 1961f35

Please sign in to comment.