From 9f64fe0638c6cd888d075df68c4b33d46f2f3406 Mon Sep 17 00:00:00 2001 From: Cameron Clark Date: Fri, 23 Aug 2024 21:42:35 +0100 Subject: [PATCH] feat(linter) add fixer to `throw-new-error` --- .../oxc_linter/src/rules/unicorn/throw_new_error.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/oxc_linter/src/rules/unicorn/throw_new_error.rs b/crates/oxc_linter/src/rules/unicorn/throw_new_error.rs index 166caa67b9bc5..64bb7bca5e23b 100644 --- a/crates/oxc_linter/src/rules/unicorn/throw_new_error.rs +++ b/crates/oxc_linter/src/rules/unicorn/throw_new_error.rs @@ -48,7 +48,7 @@ declare_oxc_lint!( /// ``` ThrowNewError, style, - pending + fix ); impl Rule for ThrowNewError { @@ -85,7 +85,9 @@ impl Rule for ThrowNewError { _ => return, } - ctx.diagnostic(throw_new_error_diagnostic(call_expr.span)); + ctx.diagnostic_with_fix(throw_new_error_diagnostic(call_expr.span), |fixer| { + fixer.insert_text_before_range(call_expr.span, "new ") + }); } } @@ -143,5 +145,10 @@ fn test() { ("throw (( getGlobalThis().Error ))()", None), ]; - Tester::new(ThrowNewError::NAME, pass, fail).test_and_snapshot(); + let fix = vec![ + ("throw Error()", "throw new Error()"), + ("throw (( getGlobalThis().Error ))()", "throw new (( getGlobalThis().Error ))()"), + ]; + + Tester::new(ThrowNewError::NAME, pass, fail).expect_fix(fix).test_and_snapshot(); }