Rule proposal: `prefer-logical-nullish-assignment`
See original GitHub issueI 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?
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:
- Created 2 years ago
- Reactions:4
- Comments:9 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We have
operator-shorthand
for the mathematical operators. Would #13689 cover the logical operator request here?Closing to combine into #13689. If you’ve left a 👍 here, feel free to add one there as well.