Skip to content

Commit

Permalink
feat(linter) add suggestion fix for oxc missing throw (oxc-project#4806)
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 committed Aug 10, 2024
1 parent d2734f3 commit d7fce58
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions crates/oxc_linter/src/rules/oxc/missing_throw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ declare_oxc_lint!(
/// ```
MissingThrow,
correctness,
pending // TODO: add a suggestion that adds `throw`
suggestion
);

impl Rule for MissingThrow {
Expand All @@ -35,7 +35,9 @@ impl Rule for MissingThrow {
return;
};
if new_expr.callee.is_specific_id("Error") && Self::has_missing_throw(node, ctx) {
ctx.diagnostic(missing_throw_diagnostic(new_expr.span));
ctx.diagnostic_with_suggestion(missing_throw_diagnostic(new_expr.span), |fixer| {
fixer.insert_text_before(node, "throw ")
});
}
}
}
Expand Down Expand Up @@ -80,5 +82,10 @@ fn test() {
let fail =
vec![("function foo() { new Error() }", None), ("const foo = () => { new Error() }", None)];

Tester::new(MissingThrow::NAME, pass, fail).test_and_snapshot();
let fix = vec![
("function foo() { new Error() }", "function foo() { throw new Error() }"),
("const foo = () => { new Error() }", "const foo = () => { throw new Error() }"),
];

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

0 comments on commit d7fce58

Please sign in to comment.