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 + priceAssignment (=) can be replaced with operator assignment (+=).;
user.visits = user.visits + 1Assignment (=) can be replaced with operator assignment (+=).;
area = width * areaAssignment (=) can be replaced with operator assignment (*=).;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"]| Option | What 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 problemtotal += priceUnexpected operator assignment (+=) shorthand.;example.jswith
"never"No problemstotal = total + price;
cache ??= new Map();