1 # Require Or Disallow Space Before Blocks (space-before-blocks)
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.
10 This rule will enforce consistency of spacing before blocks. It is only applied on blocks that don’t begin on a new line.
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.
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.
22 ( e.g. `{ "functions": "never", "keywords": "always", "classes": "always" }` )
24 The default is `"always"`.
28 Examples of **incorrect** code for this rule with the "always" option:
31 /*eslint space-before-blocks: "error"*/
50 Examples of **correct** code for this rule with the `"always"` option:
53 /*eslint space-before-blocks: "error"*/
61 } else{ /*no error. this is checked by `keyword-spacing` rule.*/
77 Examples of **incorrect** code for this rule with the `"never"` option:
80 /*eslint space-before-blocks: ["error", "never"]*/
95 Examples of **correct** code for this rule with the `"never"` option:
98 /*eslint space-before-blocks: ["error", "never"]*/
117 Examples of **incorrect** code for this rule when configured `{ "functions": "never", "keywords": "always", "classes": "never" }`:
120 /*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "always", "classes": "never" }]*/
132 Examples of **correct** code for this rule when configured `{ "functions": "never", "keywords": "always", "classes": "never" }`:
135 /*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "always", "classes": "never" }]*/
151 Examples of **incorrect** code for this rule when configured `{ "functions": "always", "keywords": "never", "classes": "never" }`:
154 /*eslint space-before-blocks: ["error", { "functions": "always", "keywords": "never", "classes": "never" }]*/
166 Examples of **correct** code for this rule when configured `{ "functions": "always", "keywords": "never", "classes": "never" }`:
169 /*eslint space-before-blocks: ["error", { "functions": "always", "keywords": "never", "classes": "never" }]*/
176 var a = function() {}
183 Examples of **incorrect** code for this rule when configured `{ "functions": "never", "keywords": "never", "classes": "always" }`:
186 /*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "never", "classes": "always" }]*/
194 Examples of **correct** code for this rule when configured `{ "functions": "never", "keywords": "never", "classes": "always" }`:
197 /*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "never", "classes": "always" }]*/
205 ## When Not To Use It
207 You can turn this rule off if you are not concerned with the consistency of spacing before blocks.
211 * [keyword-spacing](keyword-spacing.md)
212 * [arrow-spacing](arrow-spacing.md)
213 * [brace-style](brace-style.md)