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