fix(linter) Fix prefer logical operator error with parenthesis (#1241)

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

View file

@ -88,6 +88,12 @@ fn is_same_node(left: &Expression, right: &Expression, ctx: &LintContext) -> boo
Expression::UnaryExpression(right_await_expr),
) => return is_same_node(&left_await_expr.argument, &right_await_expr.argument, ctx),
(Expression::UpdateExpression(_), Expression::UpdateExpression(_)) => return false,
(Expression::ParenthesizedExpression(left_paren_expr), _) => {
return is_same_node(&left_paren_expr.expression, right, ctx)
}
(_, Expression::ParenthesizedExpression(right_paren_expr)) => {
return is_same_node(left, &right_paren_expr.expression, ctx);
}
_ => {}
}
@ -201,6 +207,9 @@ fn test() {
"a ?? b ? a ?? b : bar",
"foo ? foo : await a",
"await a ? await a : foo",
"await a ? (await (a)) : (foo)",
"(await a) ? await (a) : (foo)",
"(await a) ? (await (a)) : (foo)",
];
Tester::new_without_config(PreferLogicalOperatorOverTernary::NAME, pass, fail)

View file

@ -107,4 +107,25 @@ expression: prefer_logical_operator_over_ternary
╰────
help: Switch to "||" or "??" operator
⚠ eslint-plugin-unicorn(prefer-logical-operator-over-ternary): Prefer using a logical operator over a ternary.
╭─[prefer_logical_operator_over_ternary.tsx:1:1]
1 │ await a ? (await (a)) : (foo)
· ─────────────────────────────
╰────
help: Switch to "||" or "??" operator
⚠ eslint-plugin-unicorn(prefer-logical-operator-over-ternary): Prefer using a logical operator over a ternary.
╭─[prefer_logical_operator_over_ternary.tsx:1:1]
1 │ (await a) ? await (a) : (foo)
· ─────────────────────────────
╰────
help: Switch to "||" or "??" operator
⚠ eslint-plugin-unicorn(prefer-logical-operator-over-ternary): Prefer using a logical operator over a ternary.
╭─[prefer_logical_operator_over_ternary.tsx:1:1]
1 │ (await a) ? (await (a)) : (foo)
· ───────────────────────────────
╰────
help: Switch to "||" or "??" operator