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