Skip to content

[prefer-nullish-coalescing] Rule is hard too use without major refactors #1313

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
milesj opened this issue Dec 6, 2019 · 1 comment
Closed
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended

Comments

@milesj
Copy link
Contributor

milesj commented Dec 6, 2019

Wasn't sure how to title this and will preface this by saying I understand the semantics of nullish coalescing, and why the prefer-nullish-coalescing exists, but in it's current state, it's quite troublesome to enable. My story:

  • I recently applied this rule to our codebase, as a warning, which revealed hundreds of call sites. Pretty awesome stuff that it takes the types into account and flags them correctly.
  • I then ran --fix on them all, which worked. Ok cool, this is even more awesome.
  • I then ran our test suite, which started failing over the place. Uh oh, this isn't going to be fun...

The crux is that we use empty string/JSX fall-through patterns a lot, for example providedMessage || "Fallback message". The rule however will flag this (rightly so since the value isn't null or undefined, but a string) and convert it to providedMessage ?? "Fallback message", which is entirely incorrect as it will break our expectation/render.

I hope you see the problem here. In it's current state, we have the following options, none of which are ideal/easily solved.

  • Enable the rule and rewrite our code to use verbose checks instead of fall-through logic.
  • Enable the rule and add eslint-disable-next-line comments all over the source for all these string (and number) call sites.
  • Disable/not use the rule and lose all insights.

The 1st option is probably the best past forward, but is also a lot of work, sometimes too much work. The 2nd option is the worst for obvious reasons. The 3rd option is "acceptable" but we lose so many benefits provided by this rule.

I'm not really looking for a solution here, but hopefully spark some conversation on this, as I'm sure others will run into this.

Possible solutions

  • Not flag || usage where the left and right sides are both the same type (strings, numbers, etc).
  • Have the fixer not convert patterns where the left side is a string.
  • Add some rule options to avoid flagging it if the left side is a string (or number)?
@milesj milesj added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Dec 6, 2019
@bradzacher
Copy link
Member

Not flag || usage where the left and right sides are both the same type (strings, numbers, etc).

This will lead to the rule being pretty well useless, as one of the most used case is for doing things like: nullishString || 'proper message', nullishNumber || 10, nullishBoolean || true.

  • Have the fixer not convert patterns where the left side is a string.
  • Add some rule options to avoid flagging it if the left side is a string (or number)?

It's not a good idea to special case strings, as they are no different to numbers or booleans, both of which have falsey values.


The unfortunate thing is that so many people use and rely upon the implicit falsey conversion for primitive types.
It does make it hard to use this rule, as running the fixer will break things - there are probably places that your code is relying upon the implicit falsey conversion without even realising it.

This is why I added this option last week: forceSuggestionFixer.
This will convert all cases of the fixer to a suggestion. This allows you to turn the rule on as a warning, and slowly migrate your codebase without worrying about someone running --fix and breaking it.

@bradzacher bradzacher added working as intended Issues that are closed as they are working as intended and removed triage Waiting for team members to take a look labels Dec 7, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended
Projects
None yet
Development

No branches or pull requests

2 participants