fix(prefer-optional-chain): false positive on tricky condition#831
Conversation
How to use the Graphite Merge QueueAdd the label 0-merge to this PR to add it to the merge queue. You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
Merge activity
|
913ec5d to
1052b09
Compare
There was a problem hiding this comment.
Pull request overview
This PR fixes a false positive in the prefer-optional-chain rule for && chains ending in a comparison where rewriting to optional chaining would change boolean semantics (notably making undefined === undefined possible and returning true where the original expression returned false). It aligns tsgolint behavior with the expected typescript-eslint behavior for the reported pattern in issue #829.
Changes:
- Add a regression test covering the “tricky condition” where the suggested optional-chain rewrite would change results when both sides can be nullish.
- Improve trailing-comparison safety detection by identifying the actual comparison “value” side using
comparedExprstructural matching (instead of a simple “is access expression” heuristic). - Replace literal/identifier heuristics for trailing comparison safety with type-based checks using new
TypeInfohelpers (nullish/undefined-like and always-nullish/undefined-like).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
internal/rules/prefer_optional_chain/prefer_optional_chain.go |
Refines unsafe trailing-comparison detection to avoid behavior-changing optional-chain suggestions. |
internal/rules/prefer_optional_chain/prefer_optional_chain_test.go |
Adds a regression test ensuring the reported false positive is not flagged. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

fixes #829