]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/comma-spacing.md
import 7.12.1 upstream release
[pve-eslint.git] / eslint / docs / rules / comma-spacing.md
1 # Enforces spacing around commas (comma-spacing)
2
3 Spacing around commas improves readability of a list of items. Although most of the style guidelines for languages prescribe adding a space after a comma and not before it, it is subjective to the preferences of a project.
4
5 ```js
6 var foo = 1, bar = 2;
7 var foo = 1 ,bar = 2;
8 ```
9
10 ## Rule Details
11
12 This rule enforces consistent spacing before and after commas in variable declarations, array literals, object literals, function parameters, and sequences.
13
14 This rule does not apply in an `ArrayExpression` or `ArrayPattern` in either of the following cases:
15
16 * adjacent null elements
17 * an initial null element, to avoid conflicts with the [`array-bracket-spacing`](array-bracket-spacing.md) rule
18
19 ## Options
20
21 This rule has an object option:
22
23 * `"before": false` (default) disallows spaces before commas
24 * `"before": true` requires one or more spaces before commas
25 * `"after": true` (default) requires one or more spaces after commas
26 * `"after": false` disallows spaces after commas
27
28 ### after
29
30 Examples of **incorrect** code for this rule with the default `{ "before": false, "after": true }` options:
31
32 ```js
33 /*eslint comma-spacing: ["error", { "before": false, "after": true }]*/
34
35 var foo = 1 ,bar = 2;
36 var arr = [1 , 2];
37 var obj = {"foo": "bar" ,"baz": "qur"};
38 foo(a ,b);
39 new Foo(a ,b);
40 function foo(a ,b){}
41 a ,b
42 ```
43
44 Examples of **correct** code for this rule with the default `{ "before": false, "after": true }` options:
45
46 ```js
47 /*eslint comma-spacing: ["error", { "before": false, "after": true }]*/
48
49 var foo = 1, bar = 2
50 , baz = 3;
51 var arr = [1, 2];
52 var arr = [1,, 3]
53 var obj = {"foo": "bar", "baz": "qur"};
54 foo(a, b);
55 new Foo(a, b);
56 function foo(a, b){}
57 a, b
58 ```
59
60 Example of **correct** code for this rule with initial null element for the default `{ "before": false, "after": true }` options:
61
62 ```js
63 /*eslint comma-spacing: ["error", { "before": false, "after": true }]*/
64 /*eslint array-bracket-spacing: ["error", "always"]*/
65
66 var arr = [ , 2, 3 ]
67 ```
68
69 ### before
70
71 Examples of **incorrect** code for this rule with the `{ "before": true, "after": false }` options:
72
73 ```js
74 /*eslint comma-spacing: ["error", { "before": true, "after": false }]*/
75
76 var foo = 1, bar = 2;
77 var arr = [1 , 2];
78 var obj = {"foo": "bar", "baz": "qur"};
79 new Foo(a,b);
80 function foo(a,b){}
81 a, b
82 ```
83
84 Examples of **correct** code for this rule with the `{ "before": true, "after": false }` options:
85
86 ```js
87 /*eslint comma-spacing: ["error", { "before": true, "after": false }]*/
88
89 var foo = 1 ,bar = 2 ,
90 baz = true;
91 var arr = [1 ,2];
92 var arr = [1 ,,3]
93 var obj = {"foo": "bar" ,"baz": "qur"};
94 foo(a ,b);
95 new Foo(a ,b);
96 function foo(a ,b){}
97 a ,b
98 ```
99
100 Examples of **correct** code for this rule with initial null element for the `{ "before": true, "after": false }` options:
101
102 ```js
103 /*eslint comma-spacing: ["error", { "before": true, "after": false }]*/
104 /*eslint array-bracket-spacing: ["error", "never"]*/
105
106 var arr = [,2 ,3]
107 ```
108
109 ## When Not To Use It
110
111 If your project will not be following a consistent comma-spacing pattern, turn this rule off.
112
113
114 ## Further Reading
115
116 * [JavaScript](http://javascript.crockford.com/code.html)
117 * [Dojo Style Guide](https://dojotoolkit.org/reference-guide/1.9/developer/styleguide.html)
118
119
120 ## Related Rules
121
122 * [array-bracket-spacing](array-bracket-spacing.md)
123 * [comma-style](comma-style.md)
124 * [space-in-brackets](space-in-brackets.md) (deprecated)
125 * [space-in-parens](space-in-parens.md)
126 * [space-infix-ops](space-infix-ops.md)
127 * [space-after-keywords](space-after-keywords.md)
128 * [space-unary-ops](space-unary-ops.md)
129 * [space-return-throw-case](space-return-throw-case.md)