Skip to content

Bug: [prefer-nullish-coalescing] has various bugs around multi-part nullishness checks #10733

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

Closed
4 tasks done
kirkwaiblinger opened this issue Jan 28, 2025 · 10 comments
Closed
4 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@kirkwaiblinger
Copy link
Member

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.7.2&fileType=.tsx&code=CYUwxgNghgTiAEYD2A7AzgF3igrhCA8jAQEYBW4GAXNnhPAD7wDe8UNmMAligObwBfANwAoEcnRYMITAEZ4AXlr4ipCmCwBCBUpwpQAMx4hg8AGRnl9bUtz4R8eAH4rq8pQfwaAFgBMo8VRMeGlMX0VXYncNeBt4AyQkc0s7ax0rTxdUt3UMTx9-IA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUFvcgAQBcBPABxQGNoBLEggWhXioDsCB6E6RAM0WjuYImyABZ1yAewCGSZORYBzdFADuU6M0jgwAXxA6gA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

declare const nullOrObject: null | { a: string };

const test1 = nullOrObject !== undefined && null !== null
  ? nullOrObject
  : 42;

const test2 = nullOrObject !== foo && null !== null
  ? nullOrObject
  : 42;

ESLint Config

module.exports = {
  "rules": {
        "@typescript-eslint/prefer-nullish-coalescing": "warn"
  }
}

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

Expected Result

I expect neither test should report

Actual Result

both do

Additional Info

Relates to #10724, but this is an existing bug, not a regression from recent work

cc @OlivierZal if you're interested

@kirkwaiblinger kirkwaiblinger added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Jan 28, 2025
@kirkwaiblinger
Copy link
Member Author

We may want to look into sharing logic with prefer-optional-chain since that has lots of analysis in similar area

@OlivierZal
Copy link
Contributor

Indeed I'm interested!

@kirkwaiblinger
Copy link
Member Author

kirkwaiblinger commented Jan 28, 2025

More realistic test case with false positive (playground)

const test1 = nullOrObject !== foo && bar != null
  ? nullOrObject
  : 42;

Actually - I think this was a case of #10724 and is fixed in #10732

@kirkwaiblinger kirkwaiblinger added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Jan 28, 2025
@OlivierZal
Copy link
Contributor

OlivierZal commented Feb 24, 2025

Also to consider:

declare let x: { a: string } | null

x?.['a'] ? x.a : 'foo'
x?.a ? x.['a'] : 'foo'

// see `prefer-optional-chain`
x !== null && x.a ? x.a : 'foo'

@kirkwaiblinger
Copy link
Member Author

kirkwaiblinger commented Feb 25, 2025

Also to consider:

declare let x: { a: string } | null

x?.['a'] ? x.a : 'foo'
x?.a ? x.['a'] : 'foo'

I raise you this abomination (false positive) 😁:

const a = 'b';

declare let x: { a: string, b: string } | null

x?.a != null ? x[a] : 'foo'

Let's spin off a separate issue for that (both would fall under the umbrella that we should potentially use the static member access utils to compare the property being accessed... or maybe we just need to be careful about checking for computed member access in isNodeEqual?)

// see `prefer-optional-chain`
x !== null && x.a ? x.a : 'foo'

Hmmmm. I guess yeah this could technically be converted (subject to ignorePrimitives) but it seems ok to me if it's first flagged by prefer-optional-chain to simplify it into something that this rule can better analyze... But, maybe we can reuse some of the analyzeChain code and get this type of case somewhat "for free" in prefer-nullish-coalescing? I'm not sure 🤷‍♂

@OlivierZal
Copy link
Contributor

I raise you this abomination (false positive) 😁:

😱

I’ll file an issue.

// see prefer-optional-chain
x !== null && x.a ? x.a : 'foo'

Hmmmm. I guess yeah this could technically be converted (subject to ignorePrimitives) but it seems ok to me if it's first flagged by prefer-optional-chain to simplify it into something that this rule can better analyze... But, maybe we can reuse some of the analyzeChain code and get this type of case somewhat "for free" in prefer-nullish-coalescing? I'm not sure 🤷‍♂

At first I thought going for the "for free" option, but maybe someone disabling the prefer-optional-chain on purpose would be unhappy with it, although it’s true that the ones enabling it would benefit (on a 2-step basis).

@kirkwaiblinger
Copy link
Member Author

At first I thought going for the "for free" option, but maybe someone disabling the prefer-optional-chain on purpose would be unhappy with it, although it’s true that the ones enabling it would benefit (on a 2-step basis).

Oh, yeah, that's a good point. I think, since you can't actually fix it without optional chaining (e.g. (x !== null && x.a) ?? 'foo' has wrong semantics) maybe yeah we should leave that case up to the prefer-nullish-coalescing entirely. But good spot!

@OlivierZal
Copy link
Contributor

OlivierZal commented Mar 24, 2025

@kirkwaiblinger, did #10861 address everything you had in mind? If so I think the issue can be closed.

Cc @JoshuaKGoldberg

@kirkwaiblinger
Copy link
Member Author

Yep, looks like it! Thanks again for your work on this! ❤

@github-actions github-actions bot added the locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. label Apr 1, 2025
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 1, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy