fix(linter) Fix prefer type error with parenthesis (#1240)

This commit is contained in:
Cameron 2023-11-12 16:05:58 +00:00 committed by GitHub
parent 9f653881bb
commit f25b496eea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View file

@ -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();

View file

@ -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(...)`