]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # Disallow Use of Chained Assignment Expressions (no-multi-assign) |
2 | ||
3 | Chaining the assignment of variables can lead to unexpected results and be difficult to read. | |
4 | ||
5 | ```js | |
6 | (function() { | |
7 | const foo = bar = 0; // Did you mean `foo = bar == 0`? | |
8 | bar = 1; // This will not fail since `bar` is not constant. | |
9 | })(); | |
10 | console.log(bar); // This will output 1 since `bar` is not scoped. | |
11 | ``` | |
12 | ||
13 | ## Rule Details | |
14 | ||
15 | This rule disallows using multiple assignments within a single statement. | |
16 | ||
17 | Examples of **incorrect** code for this rule: | |
18 | ||
19 | ```js | |
20 | /*eslint no-multi-assign: "error"*/ | |
21 | ||
22 | var a = b = c = 5; | |
23 | ||
24 | const foo = bar = "baz"; | |
25 | ||
26 | let a = | |
27 | b = | |
28 | c; | |
29 | ``` | |
30 | ||
31 | Examples of **correct** code for this rule: | |
32 | ||
33 | ```js | |
34 | /*eslint no-multi-assign: "error"*/ | |
35 | var a = 5; | |
36 | var b = 5; | |
37 | var c = 5; | |
38 | ||
39 | const foo = "baz"; | |
40 | const bar = "baz"; | |
41 | ||
42 | let a = c; | |
43 | let b = c; | |
44 | ``` | |
45 | ||
5422a9cc TL |
46 | ## Options |
47 | ||
48 | This rule has an object option: | |
49 | ||
50 | * `"ignoreNonDeclaration"`: When set to `true`, the rule allows chains that don't include initializing a variable in a declaration. Default is `false`. | |
51 | ||
52 | ### ignoreNonDeclaration | |
53 | ||
54 | Examples of **correct** code for the `{ "ignoreNonDeclaration": true }` option: | |
55 | ||
56 | ```js | |
57 | /*eslint no-multi-assign: ["error", { "ignoreNonDeclaration": true }]*/ | |
58 | ||
59 | let a; | |
60 | let b; | |
61 | a = b = "baz"; | |
62 | ||
63 | const x = {}; | |
64 | const y = {}; | |
65 | x.one = y.one = 1; | |
66 | ``` | |
67 | ||
68 | Examples of **incorrect** code for the `{ "ignoreNonDeclaration": true }` option: | |
69 | ||
70 | ```js | |
71 | /*eslint no-multi-assign: ["error", { "ignoreNonDeclaration": true }]*/ | |
72 | ||
73 | let a = b = "baz"; | |
74 | ||
75 | const foo = bar = 1; | |
76 | ``` | |
77 | ||
eb39fafa DC |
78 | ## Related Rules |
79 | ||
80 | * [max-statements-per-line](max-statements-per-line.md) |