-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(eslint-plugin): [prefer-optional-chain] skip optional chaining when it could change the result #11702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Thanks for the PR, @mdm317! typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community. The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately. Thanks again! 🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. |
✅ Deploy Preview for typescript-eslint ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
View your CI Pipeline Execution ↗ for commit fd98e65
☁️ Nx Cloud last updated this comment at |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #11702 +/- ##
==========================================
- Coverage 90.67% 90.66% -0.01%
==========================================
Files 518 518
Lines 52466 52415 -51
Branches 8694 8679 -15
==========================================
- Hits 47571 47520 -51
Misses 4881 4881
Partials 14 14
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
| `, | ||
| ` | ||
| declare const foo: { bar: number }; | ||
| foo != null && foo.bar !== x; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like x is undeclared here, so this is an error type. Might be good to make it explicit?
| 'foo != null && foo.bar != x;', | ||
| ` | ||
| declare const foo: { bar: number }; | ||
| foo && foo.bar == x; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's declare x with a type here? Unless we're intentionally testing undeclared variables?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn’t declare it because I wanted to test that it doesn’t convert to an optional chain when we don’t know x’s type.
But thinking about what you said, testing with an error type feels odd and doesn’t clearly show the test’s purpose.
How about declaring x with the type any instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think both are potentially valid tests! But I would just make the intention explicit like:
declare const foo: { bar: number };
declare const anee: any;
foo && foo.bar == anee;// intentionally tests that an undeclared variable doesn't trigger an autofix
declare const foo: { bar: number };
foo && foo.bar == undeclaredVar;There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like there's a minor bug somewhere with:
declare const foo: { bar: number | null } | undefined;
if (foo === undefined || foo.bar === null) { // suggests to foo?.bar === null, which is not equivalent.
} else {
foo.bar.toExponential(); // after accepting fix, this crashes if `foo` is `undefined`.
}I guess this should either not report or it could maybe try to use foo?.bar == null? But this seems kind of bad since it is impossible to work with for an === nullishValue-prefering user
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typescript-eslint/packages/eslint-plugin/src/rules/prefer-optional-chain-utils/analyzeChain.ts
Lines 277 to 284 in 3f5fbf6
if ( includesType(parserServices, operand.comparedName, ts.TypeFlags.Null) ) { // we know the next operand is not a `null` check and that this // operand includes `null` - which means that making this an // optional chain would change the runtime behavior of the expression return null; }
I believe this bug didn’t originate from this PR — it seems to have existed beforehand.
Since I wasn’t the original author, it might take some time to fix it properly.
If possible, we could apply a quick hotfix for the current issue first and then open a new issue to track it, but it’s just a suggestion — feel free to decide as you think best.
In any case, I’ll start digging into it now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kirkwaiblinger
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it's on the right track, but minor touchups and a question about one bug that I saw.
Thanks for looking at this so quickly! ❤️
| datasource | package | from | to | | ---------- | -------------------------------- | ------ | ------ | | npm | @typescript-eslint/eslint-plugin | 8.46.1 | 8.46.2 | | npm | @typescript-eslint/parser | 8.46.1 | 8.46.2 | ## [v8.46.2](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8462-2025-10-20) ##### 🩹 Fixes - **eslint-plugin:** \[prefer-optional-chain] skip optional chaining when it could change the result ([#11702](typescript-eslint/typescript-eslint#11702)) ##### ❤️ Thank You - mdm317 You can read about our [versioning strategy](https://typescript-eslint.io/users/versioning) and [releases](https://typescript-eslint.io/users/releases) on our website.
| datasource | package | from | to | | ---------- | -------------------------------- | ------ | ------ | | npm | @typescript-eslint/eslint-plugin | 8.46.1 | 8.46.2 | | npm | @typescript-eslint/parser | 8.46.1 | 8.46.2 | ## [v8.46.2](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8462-2025-10-20) ##### 🩹 Fixes - **eslint-plugin:** \[prefer-optional-chain] skip optional chaining when it could change the result ([#11702](typescript-eslint/typescript-eslint#11702)) ##### ❤️ Thank You - mdm317 You can read about our [versioning strategy](https://typescript-eslint.io/users/versioning) and [releases](https://typescript-eslint.io/users/releases) on our website.
| datasource | package | from | to | | ---------- | -------------------------------- | ------ | ------ | | npm | @typescript-eslint/eslint-plugin | 8.46.1 | 8.46.2 | | npm | @typescript-eslint/parser | 8.46.1 | 8.46.2 | ## [v8.46.2](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8462-2025-10-20) ##### 🩹 Fixes - **eslint-plugin:** \[prefer-optional-chain] skip optional chaining when it could change the result ([#11702](typescript-eslint/typescript-eslint#11702)) ##### ❤️ Thank You - mdm317 You can read about our [versioning strategy](https://typescript-eslint.io/users/versioning) and [releases](https://typescript-eslint.io/users/releases) on our website.
| datasource | package | from | to | | ---------- | -------------------------------- | ------ | ------ | | npm | @typescript-eslint/eslint-plugin | 8.46.1 | 8.46.2 | | npm | @typescript-eslint/parser | 8.46.1 | 8.46.2 | ## [v8.46.2](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8462-2025-10-20) ##### 🩹 Fixes - **eslint-plugin:** \[prefer-optional-chain] skip optional chaining when it could change the result ([#11702](typescript-eslint/typescript-eslint#11702)) ##### ❤️ Thank You - mdm317 You can read about our [versioning strategy](https://typescript-eslint.io/users/versioning) and [releases](https://typescript-eslint.io/users/releases) on our website.
| datasource | package | from | to | | ---------- | -------------------------------- | ------ | ------ | | npm | @typescript-eslint/eslint-plugin | 8.46.1 | 8.46.2 | | npm | @typescript-eslint/parser | 8.46.1 | 8.46.2 | ## [v8.46.2](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8462-2025-10-20) ##### 🩹 Fixes - **eslint-plugin:** \[prefer-optional-chain] skip optional chaining when it could change the result ([#11702](typescript-eslint/typescript-eslint#11702)) ##### ❤️ Thank You - mdm317 You can read about our [versioning strategy](https://typescript-eslint.io/users/versioning) and [releases](https://typescript-eslint.io/users/releases) on our website.
| datasource | package | from | to | | ---------- | -------------------------------- | ------ | ------ | | npm | @typescript-eslint/eslint-plugin | 8.46.1 | 8.46.3 | | npm | @typescript-eslint/parser | 8.46.1 | 8.46.3 | ## [v8.46.3](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8463-2025-11-03) ##### 🩹 Fixes - **eslint-plugin:** \[no-duplicate-enum-values] support signed numbers ([#11722](typescript-eslint/typescript-eslint#11722), [#11723](typescript-eslint/typescript-eslint#11723)) - **eslint-plugin:** \[no-misused-promises] expand union type to retrieve target property ([#11706](typescript-eslint/typescript-eslint#11706)) ##### ❤️ Thank You - Evgeny Stepanovych [@undsoft](https://github.com/undsoft) - tao You can read about our [versioning strategy](https://typescript-eslint.io/users/versioning) and [releases](https://typescript-eslint.io/users/releases) on our website. ## [v8.46.2](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8462-2025-10-20) ##### 🩹 Fixes - **eslint-plugin:** \[prefer-optional-chain] skip optional chaining when it could change the result ([#11702](typescript-eslint/typescript-eslint#11702)) ##### ❤️ Thank You - mdm317 You can read about our [versioning strategy](https://typescript-eslint.io/users/versioning) and [releases](https://typescript-eslint.io/users/releases) on our website.
| datasource | package | from | to | | ---------- | -------------------------------- | ------ | ------ | | npm | @typescript-eslint/eslint-plugin | 8.46.1 | 8.46.3 | | npm | @typescript-eslint/parser | 8.46.1 | 8.46.3 | ## [v8.46.3](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8463-2025-11-03) ##### 🩹 Fixes - **eslint-plugin:** \[no-duplicate-enum-values] support signed numbers ([#11722](typescript-eslint/typescript-eslint#11722), [#11723](typescript-eslint/typescript-eslint#11723)) - **eslint-plugin:** \[no-misused-promises] expand union type to retrieve target property ([#11706](typescript-eslint/typescript-eslint#11706)) ##### ❤️ Thank You - Evgeny Stepanovych [@undsoft](https://github.com/undsoft) - tao You can read about our [versioning strategy](https://typescript-eslint.io/users/versioning) and [releases](https://typescript-eslint.io/users/releases) on our website. ## [v8.46.2](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8462-2025-10-20) ##### 🩹 Fixes - **eslint-plugin:** \[prefer-optional-chain] skip optional chaining when it could change the result ([#11702](typescript-eslint/typescript-eslint#11702)) ##### ❤️ Thank You - mdm317 You can read about our [versioning strategy](https://typescript-eslint.io/users/versioning) and [releases](https://typescript-eslint.io/users/releases) on our website.

PR Checklist
When applying optional chaining, if the transformation has the potential to alter the evaluation result,
the previous operand should not be included, even when it appears useless.
Conversely, if the transformation is guaranteed to preserve the result,
the previous operand should be included in the chain, even when it’s useless.
Not convert to optional-chain
Conver to optional-chain
see Algorithm