operator-assignment

Require or disallow shorthand assignment operators such as +=

fixable

total = total + price names total twice, and you have to compare both sides to see that it's an update. total += price says it once. This rule keeps a codebase to one of the two forms.

--fix rewrites the assignment when the target is a variable or a plain property such as user.visits or this.count. area = width * area is reported but not fixed.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"operator-assignment" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"operator-assignment" = ["error", "always"] # "always" or "never"

Examples#

example.js3 problems
total = total + price;
user.visits = user.visits + 1;
area = width * area;
example.jsNo problems
total += price;
user.visits += 1;
area *= width;

// Not the same as `label += prefix`
label = prefix + label;

Options#

bunfig.toml
[lint.rules]
operator-assignment = ["error", "never"]
OptionWhat it does
"always" (default)Report an assignment that can use the shorthand.
"never"Report the shorthand instead.

With "never", the logical operators &&=, ||= and ??= are still allowed.

example.jswith "never"1 problem
total += price;
example.jswith "never"No problems
total = total + price;
cache ??= new Map();