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.
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.
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.
23 ( e.g. `{ "functions": "never", "keywords": "always", "classes": "always" }` )
25 The default is `"always"`.
29 Examples of **incorrect** code for this rule with the "always" option:
32 /*eslint space-before-blocks: "error"*/
51 Examples of **correct** code for this rule with the `"always"` option:
54 /*eslint space-before-blocks: "error"*/
62 } else{ /*no error. this is checked by `keyword-spacing` rule.*/
67 static{} /*no error. this is checked by `keyword-spacing` rule.*/
81 Examples of **incorrect** code for this rule with the `"never"` option:
84 /*eslint space-before-blocks: ["error", "never"]*/
99 Examples of **correct** code for this rule with the `"never"` option:
102 /*eslint space-before-blocks: ["error", "never"]*/
121 Examples of **incorrect** code for this rule when configured `{ "functions": "never", "keywords": "always", "classes": "never" }`:
124 /*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "always", "classes": "never" }]*/
136 Examples of **correct** code for this rule when configured `{ "functions": "never", "keywords": "always", "classes": "never" }`:
139 /*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "always", "classes": "never" }]*/
155 Examples of **incorrect** code for this rule when configured `{ "functions": "always", "keywords": "never", "classes": "never" }`:
158 /*eslint space-before-blocks: ["error", { "functions": "always", "keywords": "never", "classes": "never" }]*/
170 Examples of **correct** code for this rule when configured `{ "functions": "always", "keywords": "never", "classes": "never" }`:
173 /*eslint space-before-blocks: ["error", { "functions": "always", "keywords": "never", "classes": "never" }]*/
180 var a = function() {}
187 Examples of **incorrect** code for this rule when configured `{ "functions": "never", "keywords": "never", "classes": "always" }`:
190 /*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "never", "classes": "always" }]*/
198 Examples of **correct** code for this rule when configured `{ "functions": "never", "keywords": "never", "classes": "always" }`:
201 /*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "never", "classes": "always" }]*/
209 ## When Not To Use It
211 You can turn this rule off if you are not concerned with the consistency of spacing before blocks.
215 * [keyword-spacing](keyword-spacing.md)
216 * [arrow-spacing](arrow-spacing.md)
217 * [switch-colon-spacing](switch-colon-spacing.md)
218 * [brace-style](brace-style.md)