# operator-assignment

> Require or disallow shorthand assignment operators such as `+=`

`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.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

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

Every option, set to its default:

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

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 3} */}
```js
total = total + price;
user.visits = user.visits + 1;
area = width * area;
```

Not reported:

{/* good */}
```js
total += price;
user.visits += 1;
area *= width;

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

## Options

```toml 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.

{/* bad {"options": ["never"]} */}
```js
total += price;
```

{/* good {"options": ["never"]} */}
```js
total = total + price;
cache ??= new Map();
```
