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.

New rule: Prefer logical assignment operators

See original GitHub issue

Please describe what the rule should do:

Suggest the use of logical assignment operators.

What new ECMAScript feature does this rule relate to?

Logical 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:

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:closed
  • Created 3 years ago
  • Reactions:23
  • Comments:14 (13 by maintainers)

github_iconTop GitHub Comments

6reactions
nzakascommented, Mar 30, 2022

@mdjermanovic whats the next steps on this?

4reactions
ljharbcommented, Jun 27, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

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