Skip to content

fix(prefer-optional-chain): avoid false positives for instanceof and redundant nullish checks#753

Merged
camc314 merged 2 commits intooxc-project:mainfrom
wagenet:fix-optional-chain
Mar 17, 2026
Merged

fix(prefer-optional-chain): avoid false positives for instanceof and redundant nullish checks#753
camc314 merged 2 commits intooxc-project:mainfrom
wagenet:fix-optional-chain

Conversation

@wagenet
Copy link
Copy Markdown
Contributor

@wagenet wagenet commented Feb 26, 2026

Summary

  • instanceof false positive: patterns like (!a.b || foo instanceof a.b) and (a.b && foo instanceof a.b) were incorrectly flagged. Added KindInstanceOfKeyword to the invalid-operator switches in both the AND-chain and OR-chain branches of parseOperandinstanceof has no optional-chain equivalent.

  • Redundant nullish-check false positive: a.b === null || a.b === undefined was falsely reported because both operands check the exact same expression with no deeper member access to convert. Added an allOperandsCheckSameExpression guard to validateOrChainForReporting, mirroring the identical guard already present in validateAndChainForReporting.

Test plan

  • (!a.b || foo instanceof a.b) — no violation
  • (a.b && foo instanceof a.b) — no violation
  • request.payload === null || request.payload === undefined — no violation
  • request.payload === undefined || request.payload === null — no violation
  • go test ./internal/rules/prefer_optional_chain/... passes

🤖 Generated with Claude Code

@camc314 camc314 marked this pull request as draft February 26, 2026 12:16
@camc314
Copy link
Copy Markdown
Contributor

camc314 commented Feb 26, 2026

@wagenet mind rebasing? thanks

@camc314 camc314 self-assigned this Feb 26, 2026
@kalvenschraut
Copy link
Copy Markdown
Contributor

kalvenschraut commented Feb 26, 2026

May want to double check main has these fixed, looks pretty similar to my other PR that was merged. AI wanted to add instanceof also but those weren't actually being triggered at least the ones I tried

#732

@wagenet wagenet changed the title fix(prefer-optional-chain): avoid false positives for in/instanceof and redundant nullish checks fix(prefer-optional-chain): avoid false positives for instanceof and redundant nullish checks Feb 26, 2026
@wagenet wagenet marked this pull request as ready for review February 26, 2026 17:01
Comment thread internal/rules/prefer_optional_chain/prefer_optional_chain.go Outdated
Comment thread internal/rules/prefer_optional_chain/prefer_optional_chain_test.go
…f` and redundant nullish checks

Two false positives are fixed:

1. `in`/`instanceof` operators: expressions like `(!a.b || key in a.b)` or
   `(a.b && foo instanceof a.b)` were incorrectly flagged because `in` and
   `instanceof` have no optional-chain equivalent. Add `KindInKeyword` and
   `KindInstanceOfKeyword` to the invalid-operator switches in both the
   AND-chain and OR-chain branches of `parseOperand`.

2. Redundant nullish checks: `a.b === null || a.b === undefined` was falsely
   reported because the chain passed `hasPropertyAccessInChain` (a.b is an
   access expression) even though both operands check the exact same
   expression with no deeper member access to convert. Add an
   `allOperandsCheckSameExpression` guard to `validateOrChainForReporting`,
   mirroring the identical guard already present in
   `validateAndChainForReporting`.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@camc314 camc314 force-pushed the fix-optional-chain branch from fca5e11 to 7e5c99c Compare March 16, 2026 19:56
@camc314 camc314 enabled auto-merge (squash) March 17, 2026 11:05
Copy link
Copy Markdown
Contributor

@camc314 camc314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@camc314 camc314 merged commit 991b141 into oxc-project:main Mar 17, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants