Skip to content

New rule: Prefer logical assignment operators #13689

@glen-84

Description

@glen-84

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.

Metadata

Metadata

Assignees

Labels

acceptedThere is consensus among the team that this change meets the criteria for inclusionarchived due to ageThis issue has been archived; please open a new issue for any further discussionfeatureThis change adds a new feature to ESLintruleRelates to ESLint's core rules

Type

No type

Projects

Status

Complete

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions