From 4c0861f694f821fa499a255a66cf9df87e59edf3 Mon Sep 17 00:00:00 2001 From: camc314 <18101008+camc314@users.noreply.github.com> Date: Sat, 31 Aug 2024 05:17:47 +0000 Subject: [PATCH] feat(linter/unicorn): add fixer for `prefer-type-error` (#5311) --- .../src/rules/unicorn/prefer_type_error.rs | 60 ++++++++++++++++++- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/crates/oxc_linter/src/rules/unicorn/prefer_type_error.rs b/crates/oxc_linter/src/rules/unicorn/prefer_type_error.rs index 707c0bdc1e60c..6119b08e208d6 100644 --- a/crates/oxc_linter/src/rules/unicorn/prefer_type_error.rs +++ b/crates/oxc_linter/src/rules/unicorn/prefer_type_error.rs @@ -43,7 +43,7 @@ declare_oxc_lint!( /// ``` PreferTypeError, pedantic, - pending + fix ); impl Rule for PreferTypeError { @@ -82,7 +82,10 @@ impl Rule for PreferTypeError { }; if is_type_checking_expr(&if_stmt.test) { - ctx.diagnostic(prefer_type_error_diagnostic(new_expr.callee.span())); + ctx.diagnostic_with_fix( + prefer_type_error_diagnostic(new_expr.callee.span()), + |fixer| fixer.replace(new_expr.callee.span(), "TypeError"), + ); } } } @@ -468,5 +471,56 @@ fn test() { ", ]; - Tester::new(PreferTypeError::NAME, pass, fail).test_and_snapshot(); + let fix = vec![ + ( + r"if (!isFinite(foo)) { throw new Error(); }", + r"if (!isFinite(foo)) { throw new TypeError(); }", + ), + ( + r"if (isNaN(foo) === false) { throw new Error(); }", + r"if (isNaN(foo) === false) { throw new TypeError(); }", + ), + ( + r"if (Array.isArray(foo)) { throw new Error('foo is an Array'); }", + r"if (Array.isArray(foo)) { throw new TypeError('foo is an Array'); }", + ), + ( + r"if (foo instanceof bar) { throw new Error(foobar); }", + r"if (foo instanceof bar) { throw new TypeError(foobar); }", + ), + ( + r"if (_.isElement(foo)) { throw new Error(); }", + r"if (_.isElement(foo)) { throw new TypeError(); }", + ), + ( + r"if (_.isElement(foo)) { throw new Error; }", + r"if (_.isElement(foo)) { throw new TypeError; }", + ), + ( + r"if (wrapper._.isElement(foo)) { throw new Error; }", + r"if (wrapper._.isElement(foo)) { throw new TypeError; }", + ), + ( + r"if (typeof foo == 'Foo' || 'Foo' === typeof foo) { throw new Error(); }", + r"if (typeof foo == 'Foo' || 'Foo' === typeof foo) { throw new TypeError(); }", + ), + ( + r"if (Number.isFinite(foo) && Number.isSafeInteger(foo) && Number.isInteger(foo)) { throw new Error(); }", + r"if (Number.isFinite(foo) && Number.isSafeInteger(foo) && Number.isInteger(foo)) { throw new TypeError(); }", + ), + ( + r"if (wrapper.n.isFinite(foo) && wrapper.n.isSafeInteger(foo) && wrapper.n.isInteger(foo)) { throw new Error(); }", + r"if (wrapper.n.isFinite(foo) && wrapper.n.isSafeInteger(foo) && wrapper.n.isInteger(foo)) { throw new TypeError(); }", + ), + ( + r"if (wrapper.f.g.n.isFinite(foo) && wrapper.g.n.isSafeInteger(foo) && wrapper.n.isInteger(foo)) { throw new Error(); }", + r"if (wrapper.f.g.n.isFinite(foo) && wrapper.g.n.isSafeInteger(foo) && wrapper.n.isInteger(foo)) { throw new TypeError(); }", + ), + ( + r"if (_.isElement(foo)) { throw (new Error()); }", + r"if (_.isElement(foo)) { throw (new TypeError()); }", + ), + ]; + + Tester::new(PreferTypeError::NAME, pass, fail).expect_fix(fix).test_and_snapshot(); }