Skip to content

Commit c5e31f7

Browse files
committed
fix(prefer-optional-chain): ignore non-access OR comparison (#872)
fixes oxc-project/oxc#21077
1 parent ba92094 commit c5e31f7

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

internal/rules/prefer_optional_chain/prefer_optional_chain.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,8 +1589,16 @@ outer3:
15891589
}
15901590

15911591
comparedExpr := left
1592+
hasPropertyAccess := leftIsAccess
15921593
if rightIsAccess {
15931594
comparedExpr = right
1595+
hasPropertyAccess = true
1596+
}
1597+
// OR chains can only safely extend through comparisons that actually inspect
1598+
// an access expression. Non-nullish identifier comparisons like `b === a`
1599+
// should not seed an optional-chain candidate such as `b === a || b.foo()`.
1600+
if !hasPropertyAccess {
1601+
return Operand{typ: OperandTypeInvalid, node: node}
15941602
}
15951603
return Operand{typ: OperandTypeComparison, node: node, comparedExpr: comparedExpr}
15961604
}

internal/rules/prefer_optional_chain/prefer_optional_chain_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,14 @@ func TestPreferOptionalChainRule(t *testing.T) {
10001000
10011001
left !== shared.name && right.prop === shared.name;
10021002
`},
1003+
{
1004+
Code: `const a: string | null = '';
1005+
const b: string = '';
1006+
1007+
if (!a || b === a || b.indexOf('input, button, a')) {
1008+
console.log('Hello, World');
1009+
}`,
1010+
},
10031011
}
10041012

10051013
invalidCases := []rule_tester.InvalidTestCase{

0 commit comments

Comments
 (0)