From d7fce589f84ca3eb88696bead107ec05f18e5e4f Mon Sep 17 00:00:00 2001 From: camc314 <18101008+camc314@users.noreply.github.com> Date: Sat, 10 Aug 2024 16:13:44 +0000 Subject: [PATCH] feat(linter) add suggestion fix for oxc missing throw (#4806) --- crates/oxc_linter/src/rules/oxc/missing_throw.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/oxc_linter/src/rules/oxc/missing_throw.rs b/crates/oxc_linter/src/rules/oxc/missing_throw.rs index 27fe633f8ba49..52bb3a609450c 100644 --- a/crates/oxc_linter/src/rules/oxc/missing_throw.rs +++ b/crates/oxc_linter/src/rules/oxc/missing_throw.rs @@ -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(); }