-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(eslint-plugin): [prefer-optional-chain] include mixed "nullish comparison style" chains in checks #11533
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
fix(eslint-plugin): [prefer-optional-chain] include mixed "nullish comparison style" chains in checks #11533
Conversation
…perator consistency based on logical operator - When parent operator is `&&`, enforce `==` or `===` as the last operand operator. - When parent operator is `||`, enforce `!=` or `!==` as the last operand operator.
- Already check isTruthyOperand and isValidLastChainOperand so dowmstream error not occur when ComparisonType - And !==, != check not nullish
some occur in chain when != null is last chain typescript-eslint#7654
a != null && a.b != null a?.b != null always same when a != null is true
a !== undefined && a.b a?.b always same when a is not nullish
a || b is always b when a is false
|
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. |
|
| Command | Status | Duration | Result |
|---|---|---|---|
nx run-many -t typecheck |
❌ Failed | 2m 2s | View ↗ |
nx test eslint-plugin --coverage=false |
✅ Succeeded | 5m 13s | View ↗ |
nx test typescript-estree --coverage=false |
✅ Succeeded | 2s | View ↗ |
nx test eslint-plugin-internal --coverage=false |
✅ Succeeded | 3s | View ↗ |
nx run types:build |
✅ Succeeded | 5s | View ↗ |
nx run integration-tests:test |
✅ Succeeded | 4s | View ↗ |
nx run generate-configs |
✅ Succeeded | 6s | View ↗ |
nx run-many --target=build --parallel --exclude... |
✅ Succeeded | 16s | View ↗ |
Additional runs (28) |
✅ Succeeded | ... | View ↗ |
☁️ Nx Cloud last updated this comment at 2025-10-03 12:48:53 UTC
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #11533 +/- ##
==========================================
+ Coverage 90.74% 90.78% +0.04%
==========================================
Files 516 516
Lines 51989 52222 +233
Branches 8588 8646 +58
==========================================
+ Hits 47176 47409 +233
Misses 4799 4799
Partials 14 14
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
check lastOperand always evaluates to true cases: - case `!= null` - case `!== null && !== undefined`
| // eslint-disable-next-line @typescript-eslint/prefer-optional-chain | ||
| if (declarations == null || declarations.length !== 1) { |
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.
const declarations: ts.Declaration[] | undefined
When the new logic was applied, this code
declarations == null || declarations.length !== 1
was changed to
declarations?.length !== 1
But I think the original code is more readable and intuitive.
For cases like foo == null || <equaliy check>, should we avoid optional chaining?
What do you think?
example new playground
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.
If the choices are these:
- Original:
if (declations == null || declations.length !== 1) { - Updated:
if (declations?.length !== 1) {
Personal anecodtal thoughts: I find the updated form more readable. I think when ?. was new I would probably have preferred the original for being more familiar. But at this point I'm pretty comfortable with ?. and ??, and it feels natural to me.
| return false; | ||
| } | ||
|
|
||
| function isAlwaysTruthyOperand( |
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'm not convinced this function is necessary.
This function checks if the previous operand is always true:
foo != null && foo.bar == somevalue→foo?.bar == somevalue(same result when foo is not nullish)foo && foo.bar == somevalue→foo?.bar == somevalue(same result when foo is truthy)
But if we know foo is not nullish, we can simply write foo.bar == somevalue without optional chaining at all.
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.
Removing it fails some unit tests (that have legitimate cases, I believe). E.g.:
declare const foo: { bar: string } | null;
foo !== null && foo.bar !== null;| chain, | ||
| ) => { | ||
| switch (operand.comparisonType) { | ||
| case NullishComparisonType.Boolean: { |
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.
This logic moved to line 700.
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.
Thanks for these tips, they made reviewing this (already, by necessity, complex) logic easier. 🙂
| case NullishComparisonType.StrictEqualNull: | ||
| case NullishComparisonType.NotStrictEqualNull: { |
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.
When the last parameter is StrictEqualNull like foo && foo.bar !== null, we can't convert to optional chaining because:
foo && foo.bar !== nullreturnsfalsewhen foo is nullishfoo?.bar !== nullreturnstruewhen foo is nullish
Also StrictEqualNull case foo == null | foo.bar !== null
foo == null || foo.bar !== nullreturnstruewhen foo is nullishfoo?.bar !== nullreturnsfalsewhen foo is nullish
This issue is documented here.
But as I mentioned above, I'm still curious whether this logic is necessary at all.
| lastOperand.comparisonType, | ||
| parserServices, | ||
| ) || | ||
| isValidLastChainOperand( |
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.
This function checks if the last operand is valid as outlined in the table.
| left.type === AST_NODE_TYPES.MemberExpression; | ||
| const isRightMemberExpression = | ||
| right.type === AST_NODE_TYPES.MemberExpression; | ||
| if (isLeftMemberExpression && !isRightMemberExpression) { |
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.
Only checks MemberExpression because equality checks cannot be the first chain element.
| | null | ||
| | undefined; | ||
| }; | ||
| foo.bar?.() !== null && |
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.
If foo.bar is guaranteed to be non-nullish, we can change it to optional chaining.
| | undefined; | ||
| }; | ||
| foo.bar === undefined || | ||
| foo.bar() === undefined || |
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.
Since operand is a nullish check,
operand || foo.bar() === undefined can be converted to optional chaining.
JoshuaKGoldberg
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.
I have to be honest, this rule is one of my least favorites to review -- purely because of how intricate and nuanced its logic is. Each time I review I think I get to maybe 80% comprehension of it. Very Pareto Principle. If someone like @bradzacher has time to take a deeper look that'd probably be best... but the rule is quite well-tested, and from what I had time to parse out this is excellent.
I have no suggestions for changes. Just the one request to remove an eslint-disable internally as you'd brought a comment up on.
Awesome work. Really, thank you for diving so much into this beast of a rule! 🔥
| // eslint-disable-next-line @typescript-eslint/prefer-optional-chain | ||
| if (declarations == null || declarations.length !== 1) { |
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.
If the choices are these:
- Original:
if (declations == null || declations.length !== 1) { - Updated:
if (declations?.length !== 1) {
Personal anecodtal thoughts: I find the updated form more readable. I think when ?. was new I would probably have preferred the original for being more familiar. But at this point I'm pretty comfortable with ?. and ??, and it feels natural to me.
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.
[Praise] Such thorough testing! Lovely. 👏 🔥
| // x == something :( | ||
| // x === something :( | ||
| // x != something :( | ||
| // x !== something :( |
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.
[Praise] The :(s made me chuckle. 😄
|
This PR is too aggressive and suggests optional chaining in cases where it would change behavior. Few cases from mikro-orm/mikro-orm#6924 (when applying the suggested fixes, the better half of the test suite breaks). - if (meta.properties[propertyName] && meta.properties[propertyName].kind !== reference) {
+ if (meta.properties[propertyName]?.kind !== reference) {- if (!meta.embeddable && (!meta.primaryKeys || meta.primaryKeys.length === 0)) {
+ if (!meta.embeddable && (meta.primaryKeys?.length === 0)) {- if (!prop.ownColumns || prop.ownColumns.length === 0) {
+ if (prop.ownColumns?.length === 0) {- if (!last || last[0] !== entityName || last[1] !== prop) {
+ if (last?.[0] !== entityName || last[1] !== prop) {Basically, all the changes that the PR suggests are wrong. |
|
@B4nan We ask that you please file a new issue rather than commenting on a closed PR, see https://typescript-eslint.io/contributing/issues#commenting. I'm not able to reproduce what you're seeing in the playground based on my best guess at creating a reproduction... This code does not result in a lint report: declare const a: { b?: number[] };
if (!a.b || a.b.length === 0) {
}Can you please help us help you by creating an isolated reproduction we can try, and filing a new issue with it? Thanks! |
| datasource | package | from | to | | ---------- | -------------------------------- | ------ | ------ | | npm | @typescript-eslint/eslint-plugin | 8.46.0 | 8.46.1 | | npm | @typescript-eslint/parser | 8.46.0 | 8.46.1 | ## [v8.46.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8461-2025-10-13) ##### 🩹 Fixes - **eslint-plugin:** \[no-misused-promises] special-case `.finally` not to report when a promise returning function is provided as an argument ([#11667](typescript-eslint/typescript-eslint#11667)) - **eslint-plugin:** \[prefer-optional-chain] include mixed "nullish comparison style" chains in checks ([#11533](typescript-eslint/typescript-eslint#11533)) ##### ❤️ Thank You - mdm317 - Ronen Amiel 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.0 | 8.46.1 | | npm | @typescript-eslint/parser | 8.46.0 | 8.46.1 | ## [v8.46.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8461-2025-10-13) ##### 🩹 Fixes - **eslint-plugin:** \[no-misused-promises] special-case `.finally` not to report when a promise returning function is provided as an argument ([#11667](typescript-eslint/typescript-eslint#11667)) - **eslint-plugin:** \[prefer-optional-chain] include mixed "nullish comparison style" chains in checks ([#11533](typescript-eslint/typescript-eslint#11533)) ##### ❤️ Thank You - mdm317 - Ronen Amiel 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
Also fixes Bug: [prefer-optional-chain] Unsoundly fixes
a !== undefined && a.b !== nulltoa?.b !== null#10484 to not report that case.Also fixes Bug: [prefer-optional-chain] Unsoundly fixes
a !== undefined && a.b !== nulltoa?.b !== null#10484 not report that caseOverview
In this PR, I decided not to include boolean checks within optional chaining.
The reason is that the boolean outcome may differ between the original code and the optional chaining, which may lead to type inference issues (for example, as described in issue 7654
So I only handle equality operators (==, ===, !=, !==) at the end of a chain, and only when the boolean result is unchanged after converting to optional chaining.
Algorithm
AND (&&) versions
a != null && …)a?.b …)nullorundefined)OR (||) versions
a == null || …)a?.b …)Test cases
Valid (no report)
We do not report when converting to optional chaining could change runtime behavior — especially with truthy/falsy values.
Example from issue 7654:
Error
Conversion to optional chaining either produces the same result as before, or introduces a type allowed by optional chaining.
Suggestion
Optional chaining introduces an additional undefined type, which changes the result type but preserves the same truthy/falsy behavior.
Currently, even when the type changes, especially truthy and false some cases report as a suggestion.
examples
I think we should fix these cases not to be reported as suggestions.
repro