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.

Rule proposal: `prefer-logical-nullish-assignment`

See original GitHub issue

I searched and didn’t see something like that requested…

Please describe what the rule should do:

if (x == null) {
  x = 1;
}

Could be rewritten as:

x ??= 1;

What new ECMAScript feature does this rule relate to?

Logical nullish assignment.

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:

if (x.y == null) {
  x.y = 1;
}
const object = {};
if (!('x' in object)) {
  object.x = [];
}

Why should this rule be included in ESLint (instead of a plugin)?

Because it improves JS code by using core JS features.

Are you willing to submit a pull request to implement this rule?

No.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
btmillscommented, Jun 27, 2021

We have operator-shorthand for the mathematical operators. Would #13689 cover the logical operator request here?

0reactions
btmillscommented, Jul 5, 2021

Closing to combine into #13689. If you’ve left a 👍 here, feel free to add one there as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

prefer-nullish-coalescing - TypeScript ESLint
Because the nullish coalescing operator only coalesces when the original value is null or undefined , it is much safer than relying upon...
Read more >
Nullish coalescing assignment (??=) - JavaScript | MDN
No assignment is performed if the left-hand side is not nullish, due to short-circuiting of the nullish coalescing 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 >
@babel/plugin-proposal-logical-assignment-operators | Yarn
@babel/plugin-proposal-logical-assignment-operators ... Transforms logical assignment operators into short-circuited assignments. babel-plugin ...
Read more >
3 New JavaScript Operators | by Edgar Rodríguez | Medium
Logical nullish assignment (??=) ... Today you'll do something like: ... Let me remind you that !a checks falsy values like false ,...
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