]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # require or disallow trailing commas (comma-dangle) |
2 | ||
3 | Trailing commas in object literals are valid according to the ECMAScript 5 (and ECMAScript 3!) spec. However, IE8 (when not in IE8 document mode) and below will throw an error when it encounters trailing commas in JavaScript. | |
4 | ||
5 | ```js | |
6 | var foo = { | |
7 | bar: "baz", | |
8 | qux: "quux", | |
9 | }; | |
10 | ``` | |
11 | ||
12 | Trailing commas simplify adding and removing items to objects and arrays, since only the lines you are modifying must be touched. | |
13 | Another argument in favor of trailing commas is that it improves the clarity of diffs when an item is added or removed from an object or array: | |
14 | ||
15 | Less clear: | |
16 | ||
17 | ```diff | |
18 | var foo = { | |
19 | - bar: "baz", | |
20 | - qux: "quux" | |
21 | + bar: "baz" | |
22 | }; | |
23 | ``` | |
24 | ||
25 | More clear: | |
26 | ||
27 | ```diff | |
28 | var foo = { | |
29 | bar: "baz", | |
30 | - qux: "quux", | |
31 | }; | |
32 | ``` | |
33 | ||
34 | ## Rule Details | |
35 | ||
36 | This rule enforces consistent use of trailing commas in object and array literals. | |
37 | ||
38 | ## Options | |
39 | ||
40 | This rule has a string option or an object option: | |
41 | ||
42 | ```json | |
43 | { | |
44 | "comma-dangle": ["error", "never"], | |
45 | // or | |
46 | "comma-dangle": ["error", { | |
47 | "arrays": "never", | |
48 | "objects": "never", | |
49 | "imports": "never", | |
50 | "exports": "never", | |
51 | "functions": "never" | |
52 | }] | |
53 | } | |
54 | ``` | |
55 | ||
56 | * `"never"` (default) disallows trailing commas | |
57 | * `"always"` requires trailing commas | |
58 | * `"always-multiline"` requires trailing commas when the last element or property is in a *different* line than the closing `]` or `}` and disallows trailing commas when the last element or property is on the *same* line as the closing `]` or `}` | |
59 | * `"only-multiline"` allows (but does not require) trailing commas when the last element or property is in a *different* line than the closing `]` or `}` and disallows trailing commas when the last element or property is on the *same* line as the closing `]` or `}` | |
60 | ||
61 | You can also use an object option to configure this rule for each type of syntax. | |
62 | Each of the following options can be set to `"never"`, `"always"`, `"always-multiline"`, `"only-multiline"`, or `"ignore"`. | |
63 | The default for each option is `"never"` unless otherwise specified. | |
64 | ||
65 | * `arrays` is for array literals and array patterns of destructuring. (e.g. `let [a,] = [1,];`) | |
66 | * `objects` is for object literals and object patterns of destructuring. (e.g. `let {a,} = {a: 1};`) | |
67 | * `imports` is for import declarations of ES Modules. (e.g. `import {a,} from "foo";`) | |
68 | * `exports` is for export declarations of ES Modules. (e.g. `export {a,};`) | |
69 | * `functions` is for function declarations and function calls. (e.g. `(function(a,){ })(b,);`) | |
70 | * `functions` should only be enabled when linting ECMAScript 2017 or higher. | |
71 | ||
72 | ### never | |
73 | ||
74 | Examples of **incorrect** code for this rule with the default `"never"` option: | |
75 | ||
76 | ```js | |
77 | /*eslint comma-dangle: ["error", "never"]*/ | |
78 | ||
79 | var foo = { | |
80 | bar: "baz", | |
81 | qux: "quux", | |
82 | }; | |
83 | ||
84 | var arr = [1,2,]; | |
85 | ||
86 | foo({ | |
87 | bar: "baz", | |
88 | qux: "quux", | |
89 | }); | |
90 | ``` | |
91 | ||
92 | Examples of **correct** code for this rule with the default `"never"` option: | |
93 | ||
94 | ```js | |
95 | /*eslint comma-dangle: ["error", "never"]*/ | |
96 | ||
97 | var foo = { | |
98 | bar: "baz", | |
99 | qux: "quux" | |
100 | }; | |
101 | ||
102 | var arr = [1,2]; | |
103 | ||
104 | foo({ | |
105 | bar: "baz", | |
106 | qux: "quux" | |
107 | }); | |
108 | ``` | |
109 | ||
110 | ### always | |
111 | ||
112 | Examples of **incorrect** code for this rule with the `"always"` option: | |
113 | ||
114 | ```js | |
115 | /*eslint comma-dangle: ["error", "always"]*/ | |
116 | ||
117 | var foo = { | |
118 | bar: "baz", | |
119 | qux: "quux" | |
120 | }; | |
121 | ||
122 | var arr = [1,2]; | |
123 | ||
124 | foo({ | |
125 | bar: "baz", | |
126 | qux: "quux" | |
127 | }); | |
128 | ``` | |
129 | ||
130 | Examples of **correct** code for this rule with the `"always"` option: | |
131 | ||
132 | ```js | |
133 | /*eslint comma-dangle: ["error", "always"]*/ | |
134 | ||
135 | var foo = { | |
136 | bar: "baz", | |
137 | qux: "quux", | |
138 | }; | |
139 | ||
140 | var arr = [1,2,]; | |
141 | ||
142 | foo({ | |
143 | bar: "baz", | |
144 | qux: "quux", | |
145 | }); | |
146 | ``` | |
147 | ||
148 | ### always-multiline | |
149 | ||
150 | Examples of **incorrect** code for this rule with the `"always-multiline"` option: | |
151 | ||
152 | ```js | |
153 | /*eslint comma-dangle: ["error", "always-multiline"]*/ | |
154 | ||
155 | var foo = { | |
156 | bar: "baz", | |
157 | qux: "quux" | |
158 | }; | |
159 | ||
160 | var foo = { bar: "baz", qux: "quux", }; | |
161 | ||
162 | var arr = [1,2,]; | |
163 | ||
164 | var arr = [1, | |
165 | 2,]; | |
166 | ||
167 | var arr = [ | |
168 | 1, | |
169 | 2 | |
170 | ]; | |
171 | ||
172 | foo({ | |
173 | bar: "baz", | |
174 | qux: "quux" | |
175 | }); | |
176 | ``` | |
177 | ||
178 | Examples of **correct** code for this rule with the `"always-multiline"` option: | |
179 | ||
180 | ```js | |
181 | /*eslint comma-dangle: ["error", "always-multiline"]*/ | |
182 | ||
183 | var foo = { | |
184 | bar: "baz", | |
185 | qux: "quux", | |
186 | }; | |
187 | ||
188 | var foo = {bar: "baz", qux: "quux"}; | |
189 | var arr = [1,2]; | |
190 | ||
191 | var arr = [1, | |
192 | 2]; | |
193 | ||
194 | var arr = [ | |
195 | 1, | |
196 | 2, | |
197 | ]; | |
198 | ||
199 | foo({ | |
200 | bar: "baz", | |
201 | qux: "quux", | |
202 | }); | |
203 | ``` | |
204 | ||
205 | ### only-multiline | |
206 | ||
207 | Examples of **incorrect** code for this rule with the `"only-multiline"` option: | |
208 | ||
209 | ```js | |
210 | /*eslint comma-dangle: ["error", "only-multiline"]*/ | |
211 | ||
212 | var foo = { bar: "baz", qux: "quux", }; | |
213 | ||
214 | var arr = [1,2,]; | |
215 | ||
216 | var arr = [1, | |
217 | 2,]; | |
218 | ||
219 | ``` | |
220 | ||
221 | Examples of **correct** code for this rule with the `"only-multiline"` option: | |
222 | ||
223 | ```js | |
224 | /*eslint comma-dangle: ["error", "only-multiline"]*/ | |
225 | ||
226 | var foo = { | |
227 | bar: "baz", | |
228 | qux: "quux", | |
229 | }; | |
230 | ||
231 | var foo = { | |
232 | bar: "baz", | |
233 | qux: "quux" | |
234 | }; | |
235 | ||
236 | var foo = {bar: "baz", qux: "quux"}; | |
237 | var arr = [1,2]; | |
238 | ||
239 | var arr = [1, | |
240 | 2]; | |
241 | ||
242 | var arr = [ | |
243 | 1, | |
244 | 2, | |
245 | ]; | |
246 | ||
247 | var arr = [ | |
248 | 1, | |
249 | 2 | |
250 | ]; | |
251 | ||
252 | foo({ | |
253 | bar: "baz", | |
254 | qux: "quux", | |
255 | }); | |
256 | ||
257 | foo({ | |
258 | bar: "baz", | |
259 | qux: "quux" | |
260 | }); | |
261 | ``` | |
262 | ||
263 | ### functions | |
264 | ||
265 | Examples of **incorrect** code for this rule with the `{"functions": "never"}` option: | |
266 | ||
267 | ```js | |
268 | /*eslint comma-dangle: ["error", {"functions": "never"}]*/ | |
269 | ||
270 | function foo(a, b,) { | |
271 | } | |
272 | ||
273 | foo(a, b,); | |
274 | new foo(a, b,); | |
275 | ``` | |
276 | ||
277 | Examples of **correct** code for this rule with the `{"functions": "never"}` option: | |
278 | ||
279 | ```js | |
280 | /*eslint comma-dangle: ["error", {"functions": "never"}]*/ | |
281 | ||
282 | function foo(a, b) { | |
283 | } | |
284 | ||
285 | foo(a, b); | |
286 | new foo(a, b); | |
287 | ``` | |
288 | ||
289 | Examples of **incorrect** code for this rule with the `{"functions": "always"}` option: | |
290 | ||
291 | ```js | |
292 | /*eslint comma-dangle: ["error", {"functions": "always"}]*/ | |
293 | ||
294 | function foo(a, b) { | |
295 | } | |
296 | ||
297 | foo(a, b); | |
298 | new foo(a, b); | |
299 | ``` | |
300 | ||
301 | Examples of **correct** code for this rule with the `{"functions": "always"}` option: | |
302 | ||
303 | ```js | |
304 | /*eslint comma-dangle: ["error", {"functions": "always"}]*/ | |
305 | ||
306 | function foo(a, b,) { | |
307 | } | |
308 | ||
309 | foo(a, b,); | |
310 | new foo(a, b,); | |
311 | ``` | |
312 | ||
313 | ## When Not To Use It | |
314 | ||
315 | You can turn this rule off if you are not concerned with dangling commas. |