no-multi-assign
Disallow chained assignments such as a = b = c
In const width = height = 100, only width is declared. height is a plain assignment. It throws in strict mode if nothing declares it, and creates a global variable otherwise. Even when every name is declared, the second assignment is easy to overlook.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"no-multi-assign" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"no-multi-assign" = ["error", {
ignoreNonDeclaration = false,
}]Examples#
example.js1 problem
const width = height = 100Unexpected chained assignment.;example.js1 problem
let first, last;
first = last = items[0]Unexpected chained assignment.;example.js1 problem
class Box {
width = this.height = 100Unexpected chained assignment.;
}example.jsNo problems
const width = 100;
const height = 100;
let first, last;
first = items[0];
last = first;Options#
| Option | Default | What it does |
|---|---|---|
ignoreNonDeclaration | false | Only report chains in a var, let or const declaration or a class field. Allow them between variables that already exist. |
example.jswith
{ "ignoreNonDeclaration": true }No problemslet first, last;
first = last = items[0];example.jswith
{ "ignoreNonDeclaration": true }1 problemconst width = height = 100Unexpected chained assignment.;