feat(linter) add suggestion fix for oxc missing throw (#4806)

This commit is contained in:
camc314 2024-08-10 16:13:44 +00:00
parent d2734f3bc4
commit d7fce589f8

View file

@ -26,7 +26,7 @@ declare_oxc_lint!(
/// ```
MissingThrow,
correctness,
pending // TODO: add a suggestion that adds `throw`
suggestion
);
impl Rule for MissingThrow {
@ -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 ")
});
}
}
}
@ -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();
}