]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-multi-spaces.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / no-multi-spaces.md
1 ---
2 title: no-multi-spaces
3 layout: doc
4 rule_type: layout
5 related_rules:
6 - key-spacing
7 - space-infix-ops
8 - space-in-brackets
9 - space-in-parens
10 - space-after-keywords
11 - space-unary-ops
12 - space-return-throw-case
13 ---
14
15
16
17 Multiple spaces in a row that are not used for indentation are typically mistakes. For example:
18
19 ```js
20
21 if(foo === "bar") {}
22
23 ```
24
25 It's hard to tell, but there are two spaces between `foo` and `===`. Multiple spaces such as this are generally frowned upon in favor of single spaces:
26
27 ```js
28
29 if(foo === "bar") {}
30
31 ```
32
33 ## Rule Details
34
35 This rule aims to disallow multiple whitespace around logical expressions, conditional expressions, declarations, array elements, object properties, sequences and function parameters.
36
37 Examples of **incorrect** code for this rule:
38
39 ::: incorrect
40
41 ```js
42 /*eslint no-multi-spaces: "error"*/
43
44 var a = 1;
45
46 if(foo === "bar") {}
47
48 a << b
49
50 var arr = [1, 2];
51
52 a ? b: c
53 ```
54
55 :::
56
57 Examples of **correct** code for this rule:
58
59 ::: correct
60
61 ```js
62 /*eslint no-multi-spaces: "error"*/
63
64 var a = 1;
65
66 if(foo === "bar") {}
67
68 a << b
69
70 var arr = [1, 2];
71
72 a ? b: c
73 ```
74
75 :::
76
77 ## Options
78
79 This rule's configuration consists of an object with the following properties:
80
81 * `"ignoreEOLComments": true` (defaults to `false`) ignores multiple spaces before comments that occur at the end of lines
82 * `"exceptions": { "Property": true }` (`"Property"` is the only node specified by default) specifies nodes to ignore
83
84 ### ignoreEOLComments
85
86 Examples of **incorrect** code for this rule with the `{ "ignoreEOLComments": false }` (default) option:
87
88 ::: incorrect
89
90 ```js
91 /*eslint no-multi-spaces: ["error", { ignoreEOLComments: false }]*/
92
93 var x = 5; // comment
94 var x = 5; /* multiline
95 * comment
96 */
97 ```
98
99 :::
100
101 Examples of **correct** code for this rule with the `{ "ignoreEOLComments": false }` (default) option:
102
103 ::: correct
104
105 ```js
106 /*eslint no-multi-spaces: ["error", { ignoreEOLComments: false }]*/
107
108 var x = 5; // comment
109 var x = 5; /* multiline
110 * comment
111 */
112 ```
113
114 :::
115
116 Examples of **correct** code for this rule with the `{ "ignoreEOLComments": true }` option:
117
118 ::: correct
119
120 ```js
121 /*eslint no-multi-spaces: ["error", { ignoreEOLComments: true }]*/
122
123 var x = 5; // comment
124 var x = 5; // comment
125 var x = 5; /* multiline
126 * comment
127 */
128 var x = 5; /* multiline
129 * comment
130 */
131 ```
132
133 :::
134
135 ### exceptions
136
137 To avoid contradictions with other rules that require multiple spaces, this rule has an `exceptions` option to ignore certain nodes.
138
139 This option is an object that expects property names to be AST node types as defined by [ESTree](https://github.com/estree/estree). The easiest way to determine the node types for `exceptions` is to use [AST Explorer](https://astexplorer.net/) with the espree parser.
140
141 Only the `Property` node type is ignored by default, because for the [key-spacing](key-spacing) rule some alignment options require multiple spaces in properties of object literals.
142
143 Examples of **correct** code for the default `"exceptions": { "Property": true }` option:
144
145 ::: correct
146
147 ```js
148 /*eslint no-multi-spaces: "error"*/
149 /*eslint key-spacing: ["error", { align: "value" }]*/
150
151 var obj = {
152 first: "first",
153 second: "second"
154 };
155 ```
156
157 :::
158
159 Examples of **incorrect** code for the `"exceptions": { "Property": false }` option:
160
161 ::: incorrect
162
163 ```js
164 /*eslint no-multi-spaces: ["error", { exceptions: { "Property": false } }]*/
165 /*eslint key-spacing: ["error", { align: "value" }]*/
166
167 var obj = {
168 first: "first",
169 second: "second"
170 };
171 ```
172
173 :::
174
175 Examples of **correct** code for the `"exceptions": { "BinaryExpression": true }` option:
176
177 ::: correct
178
179 ```js
180 /*eslint no-multi-spaces: ["error", { exceptions: { "BinaryExpression": true } }]*/
181
182 var a = 1 * 2;
183 ```
184
185 :::
186
187 Examples of **correct** code for the `"exceptions": { "VariableDeclarator": true }` option:
188
189 ::: correct
190
191 ```js
192 /*eslint no-multi-spaces: ["error", { exceptions: { "VariableDeclarator": true } }]*/
193
194 var someVar = 'foo';
195 var someOtherVar = 'barBaz';
196 ```
197
198 :::
199
200 Examples of **correct** code for the `"exceptions": { "ImportDeclaration": true }` option:
201
202 ::: correct
203
204 ```js
205 /*eslint no-multi-spaces: ["error", { exceptions: { "ImportDeclaration": true } }]*/
206
207 import mod from 'mod';
208 import someOtherMod from 'some-other-mod';
209 ```
210
211 :::
212
213 ## When Not To Use It
214
215 If you don't want to check and disallow multiple spaces, then you should turn this rule off.