fix(prefer-optional-chain): avoid false positives for instanceof and redundant nullish checks#753
Merged
camc314 merged 2 commits intooxc-project:mainfrom Mar 17, 2026
Merged
Conversation
Contributor
|
@wagenet mind rebasing? thanks |
Contributor
|
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 |
cb78a5e to
fca5e11
Compare
in/instanceof and redundant nullish checks…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>
fca5e11 to
7e5c99c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
instanceoffalse positive: patterns like(!a.b || foo instanceof a.b)and(a.b && foo instanceof a.b)were incorrectly flagged. AddedKindInstanceOfKeywordto the invalid-operator switches in both the AND-chain and OR-chain branches ofparseOperand—instanceofhas no optional-chain equivalent.Redundant nullish-check false positive:
a.b === null || a.b === undefinedwas falsely reported because both operands check the exact same expression with no deeper member access to convert. Added anallOperandsCheckSameExpressionguard tovalidateOrChainForReporting, mirroring the identical guard already present invalidateAndChainForReporting.Test plan
(!a.b || foo instanceof a.b)— no violation(a.b && foo instanceof a.b)— no violationrequest.payload === null || request.payload === undefined— no violationrequest.payload === undefined || request.payload === null— no violationgo test ./internal/rules/prefer_optional_chain/...passes🤖 Generated with Claude Code