# logical-assignment-operators

> Require or disallow the logical assignment operators `||=`, `&&=` and `??=`

`options = options || {}` names the variable twice, and it assigns even when `options` keeps its value. `options ||= {}` says the same thing with one name and only assigns when it needs to.

`--fix` rewrites the code when the target is a plain variable. A property can have a getter or a setter, and the rewrite can change how often they run, so `user.name = user.name || "guest"` gets a suggestion instead of a fix.

{/* 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]
"logical-assignment-operators" = "error" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"logical-assignment-operators" = [
  "error",
  "always", # "always" or "never"
  {
    enforceForIfStatements = false,
  },
]
```

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

## Examples

Reported:

{/* bad {"count": 5} */}
```js
options = options || {};
cache = cache ?? new Map();
enabled = enabled && isReady();
title || (title = "Untitled");
user.name = user.name || "guest";
```

Not reported:

{/* good */}
```js
options ||= {};
cache ??= new Map();
enabled &&= isReady();
title ||= "Untitled";
user.name ||= "guest";
```

## Options

```toml bunfig.toml
[lint.rules]
logical-assignment-operators = ["error", "always", {
  enforceForIfStatements = true,
}]
```

| Option | What it does |
| --- | --- |
| `"always"` (default) | Report code that could use a logical assignment operator. |
| `"never"` | Report every logical assignment operator. |

| Option | Default | What it does |
| --- | --- | --- |
| `enforceForIfStatements` | `false` | With `"always"`, also report an `if` that only assigns to the value it tests. |

{/* bad {"options": ["never"]} */}
```js
options ||= {};
```

{/* good {"options": ["never"]} */}
```js
options = options || {};
```

### `enforceForIfStatements`

{/* good */}
```js
if (!title) {
  title = "Untitled";
}

if (cache == null) cache = new Map();
```

{/* bad {"options": ["always", {"enforceForIfStatements": true}], "count": 2} */}
```js
if (!title) {
  title = "Untitled";
}

if (cache == null) cache = new Map();
```
