]> git.proxmox.com Git - pve-eslint.git/blame_incremental - eslint/docs/src/rules/space-before-blocks.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / space-before-blocks.md
... / ...
CommitLineData
1---
2title: space-before-blocks
3rule_type: layout
4related_rules:
5- keyword-spacing
6- arrow-spacing
7- switch-colon-spacing
8- block-spacing
9- brace-style
10---
11
12
13
14Consistency is an important part of any style guide.
15While it is a personal preference where to put the opening brace of blocks,
16it should be consistent across a whole project.
17Having an inconsistent style distracts the reader from seeing the important parts of the code.
18
19## Rule Details
20
21This 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
29This rule takes one argument. If it is `"always"` then blocks must always have at least one preceding space. If `"never"`
30then all blocks should never have any preceding space. If different spacing is desired for function
31blocks, keyword blocks and classes, an optional configuration object can be passed as the rule argument to
32configure 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
36The default is `"always"`.
37
38### "always"
39
40Examples of **incorrect** code for this rule with the "always" option:
41
42::: incorrect
43
44```js
45/*eslint space-before-blocks: "error"*/
46
47if (a){
48 b();
49}
50
51function a(){}
52
53for (;;){
54 b();
55}
56
57try {} catch(a){}
58
59class Foo{
60 constructor(){}
61}
62```
63
64:::
65
66Examples of **correct** code for this rule with the `"always"` option:
67
68::: correct
69
70```js
71/*eslint space-before-blocks: "error"*/
72
73if (a) {
74 b();
75}
76
77if (a) {
78 b();
79} else{ /*no error. this is checked by `keyword-spacing` rule.*/
80 c();
81}
82
83class C {
84 static{} /*no error. this is checked by `keyword-spacing` rule.*/
85}
86
87function a() {}
88
89for (;;) {
90 b();
91}
92
93try {} catch(a) {}
94```
95
96:::
97
98### "never"
99
100Examples of **incorrect** code for this rule with the `"never"` option:
101
102::: incorrect
103
104```js
105/*eslint space-before-blocks: ["error", "never"]*/
106
107if (a) {
108 b();
109}
110
111function a() {}
112
113for (;;) {
114 b();
115}
116
117try {} catch(a) {}
118```
119
120:::
121
122Examples of **correct** code for this rule with the `"never"` option:
123
124::: correct
125
126```js
127/*eslint space-before-blocks: ["error", "never"]*/
128
129if (a){
130 b();
131}
132
133function a(){}
134
135for (;;){
136 b();
137}
138
139try{} catch(a){}
140
141class Foo{
142 constructor(){}
143}
144```
145
146:::
147
148Examples 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
156function a() {}
157
158try {} catch(a){}
159
160class Foo{
161 constructor() {}
162}
163```
164
165:::
166
167Examples 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
175for (;;) {
176 // ...
177}
178
179describe(function(){
180 // ...
181});
182
183class Foo{
184 constructor(){}
185}
186```
187
188:::
189
190Examples 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
198function a(){}
199
200try {} catch(a) {}
201
202class Foo {
203 constructor(){}
204}
205```
206
207:::
208
209Examples 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
217if (a){
218 b();
219}
220
221var a = function() {}
222
223class Foo{
224 constructor() {}
225}
226```
227
228:::
229
230Examples 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
238class Foo{
239 constructor(){}
240}
241```
242
243:::
244
245Examples 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
253class Foo {
254 constructor(){}
255}
256```
257
258:::
259
260## When Not To Use It
261
262You can turn this rule off if you are not concerned with the consistency of spacing before blocks.