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#
example.js2 problems
count++,Unexpected use of comma operator. save();
while (item = queue.shift(),Unexpected use of comma operator. item) {
handle(item);
}example.jsNo problems
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.
example.jsNo problems
const last = (step(), result());
while ((item = queue.shift(), item)) {
handle(item);
}
const log = (value) => ((console.log(value), value));example.js1 problem
const log = (value) => (console.log(value),Unexpected use of comma operator. value);example.jswith
{ "allowInParentheses": false }1 problemconst last = (step(),Unexpected use of comma operator. result());