question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

prefer-nullish-coalescing rule

See original GitHub issue

Hi there,

Please describe what the rule should do: Almost same as typescript-eslint’s prefer-nullish-coalescing.

https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.md

I think that could be included in the eslint feature. It seems to depend on the type of information of typescript though.

What new ECMAScript feature does this rule relate to?

https://github.com/tc39/proposal-nullish-coalescing

What category of rule is this? (place an “X” next to just one item)

[ ] Warns about a potential error (problem) [X] Suggests an alternate way of doing something (suggestion) [ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about:

a || b; // warn
a ?? b;

if (a || b) {} // warn if ignoreConditionalTests = false
if (a ?? b) {}

(a && b) || c; // warn if ignoreMixedLogicalExpressions = false
(a && b) ?? c; 

Why should this rule be included in ESLint (instead of a plugin)? Since Nullish Coalescing operator became a feature of EcmaScript not only typescript.

Are you willing to submit a pull request to implement this rule? Not for now.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
mdjermanoviccommented, Jan 26, 2021

Thanks for the update!

a || b; // warn
a ?? b;

So the rule would basically disallow || in favor of ??? I’m not sure if it makes sense without type information, the rule would be probably unusable due to a lot of false positives.

2reactions
mdjermanoviccommented, Feb 23, 2021

There are probably many places in a codebase where || can (and should) be replaced with ??, but sometimes || has to be || and I don’t think we can reliably detect that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript-eslint/prefer-nullish-coalescing.md at main - GitHub
Because the nullish coalescing operator only coalesces when the original value is null or undefined , it is much safer than relying upon...
Read more >
no-non-null-asserted-nullish-coalescing | typescript-eslint
Disallow non-null assertions in the left operand of a nullish coalescing operator. Extending "plugin:@typescript-eslint/strict" in an ESLint configuration ...
Read more >
Typescript: use the nullish coalescing operator to prevent bugs
Typescript has released the nullish coalescing operator. You should use it to prevent bugs due to default values. This article explains why.
Read more >
Enforce the usage of the nullish coalescing operator ... - GitLab
prefer-nullish-coalescing.md 4.75 KiB ... Rule Details. This rule aims enforce the usage of the safer operator.
Read more >
Eslint fails to parse and red-highlights optional chaining ...
Nullish coalescing operator is natively supported starting from eslint>=7.5.0 . The easiest is set ES2020 in your package.json:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found