Linter · Rules
no-sequences
Disallow the comma operator
a, b evaluates both expressions and keeps only the value of the last one. In if (load(), ready) the result of load() is thrown away, which is easy to miss. And if you type a comma instead of a . or a ;, the code still parses.
The first and last parts of a for header are always allowed, since for (i = 0, j = 10; i < j; i++, j--) is the usual way to drive two counters.
Configure
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"no-sequences" = "error" # or "warn"
Every option, set to its default:
bunfig.toml
[lint.rules]
"no-sequences" = ["error", {
allowInParentheses = true,
}]
Examples
Reported:
count++, save();
while (item = queue.shift(), item) {
handle(item);
}
Not reported:
count++;
save();
for (let i = 0, j = items.length - 1; i < j; i++, j--) {
swap(items, i, j);
}
Options
| Option | Default | What it does |
|---|---|---|
allowInParentheses | true | Allow a sequence that's wrapped in its own parentheses. |
The parentheses show that the comma is on purpose. Parentheses that belong to the statement, as in while (..), don't count, so you need a second pair there. The same goes for an arrow function's body.
const last = (step(), result());
while ((item = queue.shift(), item)) {
handle(item);
}
const log = (value) => ((console.log(value), value));
const log = (value) => (console.log(value), value);
const last = (step(), result());