From f25b496eea764d8182b1fe67ec316177ffaf70e1 Mon Sep 17 00:00:00 2001 From: Cameron Date: Sun, 12 Nov 2023 16:05:58 +0000 Subject: [PATCH] fix(linter) Fix prefer type error with parenthesis (#1240) --- .../oxc_linter/src/rules/unicorn/prefer_type_error.rs | 10 +++++++++- crates/oxc_linter/src/snapshots/prefer_type_error.snap | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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 5924a024e..38fdf0678 100644 --- a/crates/oxc_linter/src/rules/unicorn/prefer_type_error.rs +++ b/crates/oxc_linter/src/rules/unicorn/prefer_type_error.rs @@ -49,7 +49,10 @@ impl Rule for PreferTypeError { fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { let AstKind::ThrowStatement(throw_stmt) = node.kind() else { return }; - let Expression::NewExpression(new_expr) = &throw_stmt.argument else { return }; + let Expression::NewExpression(new_expr) = &throw_stmt.argument.without_parenthesized() + else { + return; + }; if !new_expr.callee.is_specific_id("Error") { return; @@ -445,6 +448,11 @@ fn test() { throw new Error(); } "#, + r#" + if (_.isElement(foo)) { + throw (new Error()); + } + "#, ]; Tester::new_without_config(PreferTypeError::NAME, pass, fail).test_and_snapshot(); diff --git a/crates/oxc_linter/src/snapshots/prefer_type_error.snap b/crates/oxc_linter/src/snapshots/prefer_type_error.snap index 6f61910e8..48c63a355 100644 --- a/crates/oxc_linter/src/snapshots/prefer_type_error.snap +++ b/crates/oxc_linter/src/snapshots/prefer_type_error.snap @@ -100,4 +100,13 @@ expression: prefer_type_error ╰──── help: Change to `throw new TypeError(...)` + ⚠ eslint-plugin-unicorn(prefer-type-error): Prefer throwing a `TypeError` over a generic `Error` after a type checking if-statement + ╭─[prefer_type_error.tsx:2:1] + 2 │ if (_.isElement(foo)) { + 3 │ throw (new Error()); + · ───── + 4 │ } + ╰──── + help: Change to `throw new TypeError(...)` +