New rule: Prefer logical assignment operators
See original GitHub issuePlease describe what the rule should do:
Suggest the use of logical assignment operators.
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:
Bad:
opts.foo = opts.foo ?? 'bar'
Good:
opts.foo ??= 'bar'
(same for ||=
and &&=
)
– Optional extras –
Bad:
if (!a) {
a = 'foo';
}
Good:
a ||= 'foo';
Bad:
opts.baz ?? (opts.baz = 'qux');
Good:
opts.baz ??= 'qux';
Why should this rule be included in ESLint (instead of a plugin)?
It’s a general syntax suggestion.
Are you willing to submit a pull request to implement this rule?
Not at this time.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:23
- Comments:14 (13 by maintainers)
Top Results From Across the Web
logical-assignment-operators - Pluggable JavaScript Linter
Rule Details. This rule requires or disallows logical assignment operator shorthand. Options. This rule has a string and an object option. String option:....
Read more >ES 12/2021 introduces new logical assignment operators
Logical AND assignment operator assigns a value to a variable if it is currently truthy. This helps us simplify some of the checks...
Read more >Expressions and operators - JavaScript - MDN Web Docs
This chapter describes JavaScript's expressions and operators, including assignment, comparison, arithmetic, bitwise, logical, string, ternary ...
Read more >Operator Precedence - Visual Basic | Microsoft Learn
All comparison operators have equal precedence, and all have greater precedence than the logical and bitwise operators, but lower precedence ...
Read more >JavaScript for Beginners #4 - Logical and Assignment Operators
In this javascript for beginners tutorial I will talking about logical and assignment operators. These operators are used to perform ...
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
@mdjermanovic whats the next steps on this?
Using no-restricted-syntax isn’t helpful because it can’t autofix in the reverse direction.
In other words, it seems pretty useful to have a rule that can be configured to autofix from the long form to the logical operator form, or the inverse.