From e7a81f89166d675a716ce761291a75f225f37aac Mon Sep 17 00:00:00 2001 From: Cameron Clark Date: Sun, 12 Nov 2023 15:33:24 +0000 Subject: [PATCH] fix(linter) Fix prefer logical operator error with parenthesis --- .../prefer_logical_operator_over_ternary.rs | 9 ++++++++ .../prefer_logical_operator_over_ternary.snap | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/crates/oxc_linter/src/rules/unicorn/prefer_logical_operator_over_ternary.rs b/crates/oxc_linter/src/rules/unicorn/prefer_logical_operator_over_ternary.rs index 9dbaadb441c8..33ebb637b2d5 100644 --- a/crates/oxc_linter/src/rules/unicorn/prefer_logical_operator_over_ternary.rs +++ b/crates/oxc_linter/src/rules/unicorn/prefer_logical_operator_over_ternary.rs @@ -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) diff --git a/crates/oxc_linter/src/snapshots/prefer_logical_operator_over_ternary.snap b/crates/oxc_linter/src/snapshots/prefer_logical_operator_over_ternary.snap index ade2edf6facc..cf5d7bbd3aba 100644 --- a/crates/oxc_linter/src/snapshots/prefer_logical_operator_over_ternary.snap +++ b/crates/oxc_linter/src/snapshots/prefer_logical_operator_over_ternary.snap @@ -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 +