]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-sequences.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / no-sequences.md
1 ---
2 title: no-sequences
3 rule_type: suggestion
4 ---
5
6
7 The comma operator includes multiple expressions where only one is expected. It evaluates each operand from left to right and returns the value of the last operand. However, this frequently obscures side effects, and its use is often an accident. Here are some examples of sequences:
8
9 ```js
10 var a = (3, 5); // a = 5
11
12 a = b += 5, a + b;
13
14 while (a = next(), a && a.length);
15
16 (0, eval)("doSomething();");
17 ```
18
19 ## Rule Details
20
21 This rule forbids the use of the comma operator, with the following exceptions:
22
23 * In the initialization or update portions of a `for` statement.
24 * By default, if the expression sequence is explicitly wrapped in parentheses. This exception can be removed with the `allowInParentheses` option.
25
26 Examples of **incorrect** code for this rule:
27
28 ::: incorrect
29
30 ```js
31 /*eslint no-sequences: "error"*/
32
33 foo = doSomething(), val;
34
35 0, eval("doSomething();");
36
37 do {} while (doSomething(), !!test);
38
39 for (; doSomething(), !!test; );
40
41 if (doSomething(), !!test);
42
43 switch (val = foo(), val) {}
44
45 while (val = foo(), val < 42);
46
47 with (doSomething(), val) {}
48 ```
49
50 :::
51
52 Examples of **correct** code for this rule:
53
54 ::: correct
55
56 ```js
57 /*eslint no-sequences: "error"*/
58
59 foo = (doSomething(), val);
60
61 (0, eval)("doSomething();");
62
63 do {} while ((doSomething(), !!test));
64
65 for (i = 0, j = 10; i < j; i++, j--);
66
67 if ((doSomething(), !!test));
68
69 switch ((val = foo(), val)) {}
70
71 while ((val = foo(), val < 42));
72
73 with ((doSomething(), val)) {}
74 ```
75
76 :::
77
78 ### Note about arrow function bodies
79
80 If an arrow function body is a statement rather than a block, and that statement contains a sequence, you need to use double parentheses around the statement to indicate that the sequence is intentional.
81
82 Examples of **incorrect** code for arrow functions:
83
84 ::: incorrect
85
86 ```js
87 /*eslint no-sequences: "error"*/
88 const foo = (val) => (console.log('bar'), val);
89
90 const foo = () => ((bar = 123), 10);
91
92 const foo = () => { return (bar = 123), 10 }
93 ```
94
95 :::
96
97 Examples of **correct** code for arrow functions:
98
99 ::: correct
100
101 ```js
102 /*eslint no-sequences: "error"*/
103 const foo = (val) => ((console.log('bar'), val));
104
105 const foo = () => (((bar = 123), 10));
106
107 const foo = () => { return ((bar = 123), 10) }
108 ```
109
110 :::
111
112 ## Options
113
114 This rule takes one option, an object, with the following properties:
115
116 * `"allowInParentheses"`: If set to `true` (default), this rule allows expression sequences that are explicitly wrapped in parentheses.
117
118 ### allowInParentheses
119
120 Examples of **incorrect** code for this rule with the `{ "allowInParentheses": false }` option:
121
122 ::: incorrect
123
124 ```js
125 /*eslint no-sequences: ["error", { "allowInParentheses": false }]*/
126
127 foo = (doSomething(), val);
128
129 (0, eval)("doSomething();");
130
131 do {} while ((doSomething(), !!test));
132
133 for (; (doSomething(), !!test); );
134
135 if ((doSomething(), !!test));
136
137 switch ((val = foo(), val)) {}
138
139 while ((val = foo(), val < 42));
140
141 with ((doSomething(), val)) {}
142
143 const foo = (val) => ((console.log('bar'), val));
144 ```
145
146 :::
147
148 Examples of **correct** code for this rule with the `{ "allowInParentheses": false }` option:
149
150 ::: correct
151
152 ```js
153 /*eslint no-sequences: ["error", { "allowInParentheses": false }]*/
154
155 for (i = 0, j = 10; i < j; i++, j--);
156 ```
157
158 :::
159
160 ## When Not To Use It
161
162 Disable this rule if sequence expressions with the comma operator are acceptable.
163 Another case is where you might want to report all usages of the comma operator, even in a for loop. You can achieve this using rule `no-restricted-syntax`:
164
165 ```js
166 {
167 "rules": {
168 "no-restricted-syntax": ["error", "SequenceExpression"]
169 }
170 }
171 ```