Skip to content

fix(prefer-optional-chain): false positive on tricky condition#831

Merged
graphite-app[bot] merged 1 commit into
mainfrom
c/03-24-fix_prefer-optional-chain_false_positive_on_tricky_condition
Mar 24, 2026
Merged

fix(prefer-optional-chain): false positive on tricky condition#831
graphite-app[bot] merged 1 commit into
mainfrom
c/03-24-fix_prefer-optional-chain_false_positive_on_tricky_condition

Conversation

@camc314
Copy link
Copy Markdown
Contributor

@camc314 camc314 commented Mar 24, 2026

fixes #829

Copy link
Copy Markdown
Contributor Author

camc314 commented Mar 24, 2026


How to use the Graphite Merge Queue

Add 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.

@camc314 camc314 changed the title fix prefer-optional-chain false positive on tricky condition fix(prefer-optional-chain): false positive on tricky condition Mar 24, 2026
@camc314 camc314 self-assigned this Mar 24, 2026
@camc314 camc314 marked this pull request as ready for review March 24, 2026 17:38
Copilot AI review requested due to automatic review settings March 24, 2026 17:38
Copy link
Copy Markdown
Contributor Author

camc314 commented Mar 24, 2026

Merge activity

@graphite-app graphite-app Bot force-pushed the c/03-24-fix_prefer-optional-chain_false_positive_on_tricky_condition branch from 913ec5d to 1052b09 Compare March 24, 2026 17:40
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 comparedExpr structural matching (instead of a simple “is access expression” heuristic).
  • Replace literal/identifier heuristics for trailing comparison safety with type-based checks using new TypeInfo helpers (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.

@graphite-app graphite-app Bot merged commit 1052b09 into main Mar 24, 2026
7 of 8 checks passed
@graphite-app graphite-app Bot deleted the c/03-24-fix_prefer-optional-chain_false_positive_on_tricky_condition branch March 24, 2026 17:47
@graphite-app graphite-app Bot removed the 0-merge label Mar 24, 2026
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.

prefer-optional-chain: false positive on tricky condition

2 participants