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
Reported:
total = total + price;
user.visits = user.visits + 1;
area = width * area;
Not reported:
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.
total += price;
total = total + price;
cache ??= new Map();