]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
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. | |
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 | ||
18 | This rule takes one argument. If it is `"always"` then blocks must always have at least one preceding space. If `"never"` | |
19 | then all blocks should never have any preceding space. If different spacing is desired for function | |
20 | blocks, keyword blocks and classes, an optional configuration object can be passed as the rule argument to | |
21 | configure 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 | ||
25 | The default is `"always"`. | |
26 | ||
27 | ### "always" | |
28 | ||
29 | Examples of **incorrect** code for this rule with the "always" option: | |
30 | ||
31 | ```js | |
32 | /*eslint space-before-blocks: "error"*/ | |
33 | ||
34 | if (a){ | |
35 | b(); | |
36 | } | |
37 | ||
38 | function a(){} | |
39 | ||
40 | for (;;){ | |
41 | b(); | |
42 | } | |
43 | ||
44 | try {} catch(a){} | |
45 | ||
46 | class Foo{ | |
47 | constructor(){} | |
48 | } | |
49 | ``` | |
50 | ||
51 | Examples of **correct** code for this rule with the `"always"` option: | |
52 | ||
53 | ```js | |
54 | /*eslint space-before-blocks: "error"*/ | |
55 | ||
56 | if (a) { | |
57 | b(); | |
58 | } | |
59 | ||
60 | if (a) { | |
61 | b(); | |
62 | } else{ /*no error. this is checked by `keyword-spacing` rule.*/ | |
63 | c(); | |
64 | } | |
65 | ||
609c276f TL |
66 | class C { |
67 | static{} /*no error. this is checked by `keyword-spacing` rule.*/ | |
68 | } | |
eb39fafa DC |
69 | |
70 | function a() {} | |
71 | ||
72 | for (;;) { | |
73 | b(); | |
74 | } | |
75 | ||
76 | try {} catch(a) {} | |
77 | ``` | |
78 | ||
79 | ### "never" | |
80 | ||
81 | Examples of **incorrect** code for this rule with the `"never"` option: | |
82 | ||
83 | ```js | |
84 | /*eslint space-before-blocks: ["error", "never"]*/ | |
85 | ||
86 | if (a) { | |
87 | b(); | |
88 | } | |
89 | ||
90 | function a() {} | |
91 | ||
92 | for (;;) { | |
93 | b(); | |
94 | } | |
95 | ||
96 | try {} catch(a) {} | |
97 | ``` | |
98 | ||
99 | Examples of **correct** code for this rule with the `"never"` option: | |
100 | ||
101 | ```js | |
102 | /*eslint space-before-blocks: ["error", "never"]*/ | |
103 | ||
104 | if (a){ | |
105 | b(); | |
106 | } | |
107 | ||
108 | function a(){} | |
109 | ||
110 | for (;;){ | |
111 | b(); | |
112 | } | |
113 | ||
114 | try{} catch(a){} | |
115 | ||
116 | class Foo{ | |
117 | constructor(){} | |
118 | } | |
119 | ``` | |
120 | ||
121 | Examples 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 | ||
127 | function a() {} | |
128 | ||
129 | try {} catch(a){} | |
130 | ||
131 | class Foo{ | |
132 | constructor() {} | |
133 | } | |
134 | ``` | |
135 | ||
136 | Examples 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 | ||
142 | for (;;) { | |
143 | // ... | |
144 | } | |
145 | ||
146 | describe(function(){ | |
147 | // ... | |
148 | }); | |
149 | ||
150 | class Foo{ | |
151 | constructor(){} | |
152 | } | |
153 | ``` | |
154 | ||
155 | Examples 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 | ||
161 | function a(){} | |
162 | ||
163 | try {} catch(a) {} | |
164 | ||
165 | class Foo { | |
166 | constructor(){} | |
167 | } | |
168 | ``` | |
169 | ||
170 | Examples 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 | ||
176 | if (a){ | |
177 | b(); | |
178 | } | |
179 | ||
180 | var a = function() {} | |
181 | ||
182 | class Foo{ | |
183 | constructor() {} | |
184 | } | |
185 | ``` | |
186 | ||
187 | Examples 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 | ||
193 | class Foo{ | |
194 | constructor(){} | |
195 | } | |
196 | ``` | |
197 | ||
198 | Examples 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 | ||
204 | class Foo { | |
205 | constructor(){} | |
206 | } | |
207 | ``` | |
208 | ||
209 | ## When Not To Use It | |
210 | ||
211 | You 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) |