]>
Commit | Line | Data |
---|---|---|
1 | --- | |
2 | title: space-before-blocks | |
3 | rule_type: layout | |
4 | related_rules: | |
5 | - keyword-spacing | |
6 | - arrow-spacing | |
7 | - switch-colon-spacing | |
8 | - block-spacing | |
9 | - brace-style | |
10 | --- | |
11 | ||
12 | ||
13 | ||
14 | Consistency is an important part of any style guide. | |
15 | While it is a personal preference where to put the opening brace of blocks, | |
16 | it should be consistent across a whole project. | |
17 | Having an inconsistent style distracts the reader from seeing the important parts of the code. | |
18 | ||
19 | ## Rule Details | |
20 | ||
21 | This rule will enforce consistency of spacing before blocks. It is only applied on blocks that don’t begin on a new line. | |
22 | ||
23 | * This rule ignores spacing which is between `=>` and a block. The spacing is handled by the `arrow-spacing` rule. | |
24 | * This rule ignores spacing which is between a keyword and a block. The spacing is handled by the `keyword-spacing` rule. | |
25 | * This rule ignores spacing which is between `:` of a switch case and a block. The spacing is handled by the `switch-colon-spacing` rule. | |
26 | ||
27 | ## Options | |
28 | ||
29 | This rule takes one argument. If it is `"always"` then blocks must always have at least one preceding space. If `"never"` | |
30 | then all blocks should never have any preceding space. If different spacing is desired for function | |
31 | blocks, keyword blocks and classes, an optional configuration object can be passed as the rule argument to | |
32 | configure the cases separately. If any value in the configuration object is `"off"`, then neither style will be enforced for blocks of that kind. | |
33 | ||
34 | ( e.g. `{ "functions": "never", "keywords": "always", "classes": "always" }` ) | |
35 | ||
36 | The default is `"always"`. | |
37 | ||
38 | ### "always" | |
39 | ||
40 | Examples of **incorrect** code for this rule with the "always" option: | |
41 | ||
42 | ::: incorrect | |
43 | ||
44 | ```js | |
45 | /*eslint space-before-blocks: "error"*/ | |
46 | ||
47 | if (a){ | |
48 | b(); | |
49 | } | |
50 | ||
51 | function a(){} | |
52 | ||
53 | for (;;){ | |
54 | b(); | |
55 | } | |
56 | ||
57 | try {} catch(a){} | |
58 | ||
59 | class Foo{ | |
60 | constructor(){} | |
61 | } | |
62 | ``` | |
63 | ||
64 | ::: | |
65 | ||
66 | Examples of **correct** code for this rule with the `"always"` option: | |
67 | ||
68 | ::: correct | |
69 | ||
70 | ```js | |
71 | /*eslint space-before-blocks: "error"*/ | |
72 | ||
73 | if (a) { | |
74 | b(); | |
75 | } | |
76 | ||
77 | if (a) { | |
78 | b(); | |
79 | } else{ /*no error. this is checked by `keyword-spacing` rule.*/ | |
80 | c(); | |
81 | } | |
82 | ||
83 | class C { | |
84 | static{} /*no error. this is checked by `keyword-spacing` rule.*/ | |
85 | } | |
86 | ||
87 | function a() {} | |
88 | ||
89 | for (;;) { | |
90 | b(); | |
91 | } | |
92 | ||
93 | try {} catch(a) {} | |
94 | ``` | |
95 | ||
96 | ::: | |
97 | ||
98 | ### "never" | |
99 | ||
100 | Examples of **incorrect** code for this rule with the `"never"` option: | |
101 | ||
102 | ::: incorrect | |
103 | ||
104 | ```js | |
105 | /*eslint space-before-blocks: ["error", "never"]*/ | |
106 | ||
107 | if (a) { | |
108 | b(); | |
109 | } | |
110 | ||
111 | function a() {} | |
112 | ||
113 | for (;;) { | |
114 | b(); | |
115 | } | |
116 | ||
117 | try {} catch(a) {} | |
118 | ``` | |
119 | ||
120 | ::: | |
121 | ||
122 | Examples of **correct** code for this rule with the `"never"` option: | |
123 | ||
124 | ::: correct | |
125 | ||
126 | ```js | |
127 | /*eslint space-before-blocks: ["error", "never"]*/ | |
128 | ||
129 | if (a){ | |
130 | b(); | |
131 | } | |
132 | ||
133 | function a(){} | |
134 | ||
135 | for (;;){ | |
136 | b(); | |
137 | } | |
138 | ||
139 | try{} catch(a){} | |
140 | ||
141 | class Foo{ | |
142 | constructor(){} | |
143 | } | |
144 | ``` | |
145 | ||
146 | ::: | |
147 | ||
148 | Examples of **incorrect** code for this rule when configured `{ "functions": "never", "keywords": "always", "classes": "never" }`: | |
149 | ||
150 | ::: incorrect | |
151 | ||
152 | ```js | |
153 | /*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "always", "classes": "never" }]*/ | |
154 | /*eslint-env es6*/ | |
155 | ||
156 | function a() {} | |
157 | ||
158 | try {} catch(a){} | |
159 | ||
160 | class Foo{ | |
161 | constructor() {} | |
162 | } | |
163 | ``` | |
164 | ||
165 | ::: | |
166 | ||
167 | Examples of **correct** code for this rule when configured `{ "functions": "never", "keywords": "always", "classes": "never" }`: | |
168 | ||
169 | ::: correct | |
170 | ||
171 | ```js | |
172 | /*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "always", "classes": "never" }]*/ | |
173 | /*eslint-env es6*/ | |
174 | ||
175 | for (;;) { | |
176 | // ... | |
177 | } | |
178 | ||
179 | describe(function(){ | |
180 | // ... | |
181 | }); | |
182 | ||
183 | class Foo{ | |
184 | constructor(){} | |
185 | } | |
186 | ``` | |
187 | ||
188 | ::: | |
189 | ||
190 | Examples of **incorrect** code for this rule when configured `{ "functions": "always", "keywords": "never", "classes": "never" }`: | |
191 | ||
192 | ::: incorrect | |
193 | ||
194 | ```js | |
195 | /*eslint space-before-blocks: ["error", { "functions": "always", "keywords": "never", "classes": "never" }]*/ | |
196 | /*eslint-env es6*/ | |
197 | ||
198 | function a(){} | |
199 | ||
200 | try {} catch(a) {} | |
201 | ||
202 | class Foo { | |
203 | constructor(){} | |
204 | } | |
205 | ``` | |
206 | ||
207 | ::: | |
208 | ||
209 | Examples of **correct** code for this rule when configured `{ "functions": "always", "keywords": "never", "classes": "never" }`: | |
210 | ||
211 | ::: correct | |
212 | ||
213 | ```js | |
214 | /*eslint space-before-blocks: ["error", { "functions": "always", "keywords": "never", "classes": "never" }]*/ | |
215 | /*eslint-env es6*/ | |
216 | ||
217 | if (a){ | |
218 | b(); | |
219 | } | |
220 | ||
221 | var a = function() {} | |
222 | ||
223 | class Foo{ | |
224 | constructor() {} | |
225 | } | |
226 | ``` | |
227 | ||
228 | ::: | |
229 | ||
230 | Examples of **incorrect** code for this rule when configured `{ "functions": "never", "keywords": "never", "classes": "always" }`: | |
231 | ||
232 | ::: incorrect | |
233 | ||
234 | ```js | |
235 | /*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "never", "classes": "always" }]*/ | |
236 | /*eslint-env es6*/ | |
237 | ||
238 | class Foo{ | |
239 | constructor(){} | |
240 | } | |
241 | ``` | |
242 | ||
243 | ::: | |
244 | ||
245 | Examples of **correct** code for this rule when configured `{ "functions": "never", "keywords": "never", "classes": "always" }`: | |
246 | ||
247 | ::: correct | |
248 | ||
249 | ```js | |
250 | /*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "never", "classes": "always" }]*/ | |
251 | /*eslint-env es6*/ | |
252 | ||
253 | class Foo { | |
254 | constructor(){} | |
255 | } | |
256 | ``` | |
257 | ||
258 | ::: | |
259 | ||
260 | ## When Not To Use It | |
261 | ||
262 | You can turn this rule off if you are not concerned with the consistency of spacing before blocks. |