]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/comma-dangle.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / comma-dangle.md
CommitLineData
eb39fafa
DC
1# require or disallow trailing commas (comma-dangle)
2
3Trailing 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
6var foo = {
7 bar: "baz",
8 qux: "quux",
9};
10```
11
12Trailing commas simplify adding and removing items to objects and arrays, since only the lines you are modifying must be touched.
13Another 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
15Less clear:
16
17```diff
18 var foo = {
19- bar: "baz",
20- qux: "quux"
21+ bar: "baz"
22 };
23```
24
25More clear:
26
27```diff
28 var foo = {
29 bar: "baz",
30- qux: "quux",
31 };
32```
33
34## Rule Details
35
36This rule enforces consistent use of trailing commas in object and array literals.
37
38## Options
39
40This 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
61You can also use an object option to configure this rule for each type of syntax.
62Each of the following options can be set to `"never"`, `"always"`, `"always-multiline"`, `"only-multiline"`, or `"ignore"`.
63The 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
74Examples of **incorrect** code for this rule with the default `"never"` option:
75
76```js
77/*eslint comma-dangle: ["error", "never"]*/
78
79var foo = {
80 bar: "baz",
81 qux: "quux",
82};
83
84var arr = [1,2,];
85
86foo({
87 bar: "baz",
88 qux: "quux",
89});
90```
91
92Examples of **correct** code for this rule with the default `"never"` option:
93
94```js
95/*eslint comma-dangle: ["error", "never"]*/
96
97var foo = {
98 bar: "baz",
99 qux: "quux"
100};
101
102var arr = [1,2];
103
104foo({
105 bar: "baz",
106 qux: "quux"
107});
108```
109
110### always
111
112Examples of **incorrect** code for this rule with the `"always"` option:
113
114```js
115/*eslint comma-dangle: ["error", "always"]*/
116
117var foo = {
118 bar: "baz",
119 qux: "quux"
120};
121
122var arr = [1,2];
123
124foo({
125 bar: "baz",
126 qux: "quux"
127});
128```
129
130Examples of **correct** code for this rule with the `"always"` option:
131
132```js
133/*eslint comma-dangle: ["error", "always"]*/
134
135var foo = {
136 bar: "baz",
137 qux: "quux",
138};
139
140var arr = [1,2,];
141
142foo({
143 bar: "baz",
144 qux: "quux",
145});
146```
147
148### always-multiline
149
150Examples of **incorrect** code for this rule with the `"always-multiline"` option:
151
152```js
153/*eslint comma-dangle: ["error", "always-multiline"]*/
154
155var foo = {
156 bar: "baz",
157 qux: "quux"
158};
159
160var foo = { bar: "baz", qux: "quux", };
161
162var arr = [1,2,];
163
164var arr = [1,
165 2,];
166
167var arr = [
168 1,
169 2
170];
171
172foo({
173 bar: "baz",
174 qux: "quux"
175});
176```
177
178Examples of **correct** code for this rule with the `"always-multiline"` option:
179
180```js
181/*eslint comma-dangle: ["error", "always-multiline"]*/
182
183var foo = {
184 bar: "baz",
185 qux: "quux",
186};
187
188var foo = {bar: "baz", qux: "quux"};
189var arr = [1,2];
190
191var arr = [1,
192 2];
193
194var arr = [
195 1,
196 2,
197];
198
199foo({
200 bar: "baz",
201 qux: "quux",
202});
203```
204
205### only-multiline
206
207Examples of **incorrect** code for this rule with the `"only-multiline"` option:
208
209```js
210/*eslint comma-dangle: ["error", "only-multiline"]*/
211
212var foo = { bar: "baz", qux: "quux", };
213
214var arr = [1,2,];
215
216var arr = [1,
217 2,];
218
219```
220
221Examples of **correct** code for this rule with the `"only-multiline"` option:
222
223```js
224/*eslint comma-dangle: ["error", "only-multiline"]*/
225
226var foo = {
227 bar: "baz",
228 qux: "quux",
229};
230
231var foo = {
232 bar: "baz",
233 qux: "quux"
234};
235
236var foo = {bar: "baz", qux: "quux"};
237var arr = [1,2];
238
239var arr = [1,
240 2];
241
242var arr = [
243 1,
244 2,
245];
246
247var arr = [
248 1,
249 2
250];
251
252foo({
253 bar: "baz",
254 qux: "quux",
255});
256
257foo({
258 bar: "baz",
259 qux: "quux"
260});
261```
262
263### functions
264
265Examples of **incorrect** code for this rule with the `{"functions": "never"}` option:
266
267```js
268/*eslint comma-dangle: ["error", {"functions": "never"}]*/
269
270function foo(a, b,) {
271}
272
273foo(a, b,);
274new foo(a, b,);
275```
276
277Examples of **correct** code for this rule with the `{"functions": "never"}` option:
278
279```js
280/*eslint comma-dangle: ["error", {"functions": "never"}]*/
281
282function foo(a, b) {
283}
284
285foo(a, b);
286new foo(a, b);
287```
288
289Examples of **incorrect** code for this rule with the `{"functions": "always"}` option:
290
291```js
292/*eslint comma-dangle: ["error", {"functions": "always"}]*/
293
294function foo(a, b) {
295}
296
297foo(a, b);
298new foo(a, b);
299```
300
301Examples of **correct** code for this rule with the `{"functions": "always"}` option:
302
303```js
304/*eslint comma-dangle: ["error", {"functions": "always"}]*/
305
306function foo(a, b,) {
307}
308
309foo(a, b,);
310new foo(a, b,);
311```
312
313## When Not To Use It
314
315You can turn this rule off if you are not concerned with dangling commas.