]> git.proxmox.com Git - pve-eslint.git/blame - 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
8f9d1d4d
DC
1---
2title: space-before-blocks
8f9d1d4d
DC
3rule_type: layout
4related_rules:
5- keyword-spacing
6- arrow-spacing
7- switch-colon-spacing
8- block-spacing
9- brace-style
10---
11
12
eb39fafa
DC
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.
609c276f 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.
eb39fafa
DC
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
8f9d1d4d
DC
42::: incorrect
43
eb39fafa
DC
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
8f9d1d4d
DC
64:::
65
eb39fafa
DC
66Examples of **correct** code for this rule with the `"always"` option:
67
8f9d1d4d
DC
68::: correct
69
eb39fafa
DC
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
609c276f
TL
83class C {
84 static{} /*no error. this is checked by `keyword-spacing` rule.*/
85}
eb39fafa
DC
86
87function a() {}
88
89for (;;) {
90 b();
91}
92
93try {} catch(a) {}
94```
95
8f9d1d4d
DC
96:::
97
eb39fafa
DC
98### "never"
99
100Examples of **incorrect** code for this rule with the `"never"` option:
101
8f9d1d4d
DC
102::: incorrect
103
eb39fafa
DC
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
8f9d1d4d
DC
120:::
121
eb39fafa
DC
122Examples of **correct** code for this rule with the `"never"` option:
123
8f9d1d4d
DC
124::: correct
125
eb39fafa
DC
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
8f9d1d4d
DC
146:::
147
eb39fafa
DC
148Examples of **incorrect** code for this rule when configured `{ "functions": "never", "keywords": "always", "classes": "never" }`:
149
8f9d1d4d
DC
150::: incorrect
151
eb39fafa
DC
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
8f9d1d4d
DC
165:::
166
eb39fafa
DC
167Examples of **correct** code for this rule when configured `{ "functions": "never", "keywords": "always", "classes": "never" }`:
168
8f9d1d4d
DC
169::: correct
170
eb39fafa
DC
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
8f9d1d4d
DC
188:::
189
eb39fafa
DC
190Examples of **incorrect** code for this rule when configured `{ "functions": "always", "keywords": "never", "classes": "never" }`:
191
8f9d1d4d
DC
192::: incorrect
193
eb39fafa
DC
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
8f9d1d4d
DC
207:::
208
eb39fafa
DC
209Examples of **correct** code for this rule when configured `{ "functions": "always", "keywords": "never", "classes": "never" }`:
210
8f9d1d4d
DC
211::: correct
212
eb39fafa
DC
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
8f9d1d4d
DC
228:::
229
eb39fafa
DC
230Examples of **incorrect** code for this rule when configured `{ "functions": "never", "keywords": "never", "classes": "always" }`:
231
8f9d1d4d
DC
232::: incorrect
233
eb39fafa
DC
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
8f9d1d4d
DC
243:::
244
eb39fafa
DC
245Examples of **correct** code for this rule when configured `{ "functions": "never", "keywords": "never", "classes": "always" }`:
246
8f9d1d4d
DC
247::: correct
248
eb39fafa
DC
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
8f9d1d4d
DC
258:::
259
eb39fafa
DC
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.