Linter · Rules
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.
Configure
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"logical-assignment-operators" = "error" # or "warn"
Every option, set to its default:
bunfig.toml
[lint.rules]
"logical-assignment-operators" = [
"error",
"always", # "always" or "never"
{
enforceForIfStatements = false,
},
]
Examples
Reported:
options = options || {};
cache = cache ?? new Map();
enabled = enabled && isReady();
title || (title = "Untitled");
user.name = user.name || "guest";
Not reported:
options ||= {};
cache ??= new Map();
enabled &&= isReady();
title ||= "Untitled";
user.name ||= "guest";
Options
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. |
options ||= {};
options = options || {};
enforceForIfStatements
if (!title) {
title = "Untitled";
}
if (cache == null) cache = new Map();
if (!title) {
title = "Untitled";
}
if (cache == null) cache = new Map();