]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # space-before-keywords: enforce consistent spacing before keywords |
2 | ||
3 | (removed) This rule was **removed** in ESLint v2.0 and **replaced** by the [keyword-spacing](keyword-spacing.md) rule. | |
4 | ||
609c276f | 5 | (fixable) The `--fix` option on the [command line](../user-guide/command-line-interface#--fix) automatically fixed problems reported by this rule. |
eb39fafa DC |
6 | |
7 | Keywords are syntax elements of JavaScript, such as `function` and `if`. These identifiers have special meaning to the language and so often appear in a different color in code editors. As an important part of the language, style guides often refer to the spacing that should be used around keywords. For example, you might have a style guide that says keywords should be always be preceded by spaces, which would mean `if-else` statements must look like this: | |
8 | ||
9 | ```js | |
10 | if (foo) { | |
11 | // ... | |
12 | } else { | |
13 | // ... | |
14 | } | |
15 | ``` | |
16 | ||
17 | Of course, you could also have a style guide that disallows spaces before keywords. | |
18 | ||
19 | ## Rule Details | |
20 | ||
21 | This rule will enforce consistency of spacing before the keywords `if`, `else`, `for`, | |
22 | `while`, `do`, `switch`, `throw`, `try`, `catch`, `finally`, `with`, `break`, `continue`, | |
23 | `return`, `function`, `yield`, `class` and variable declarations (`let`, `const`, `var`) | |
24 | and label statements. | |
25 | ||
26 | This rule takes one argument: `"always"` or `"never"`. If `"always"` then the keywords | |
27 | must be preceded by at least one space. If `"never"` then no spaces will be allowed before | |
28 | the keywords `else`, `while` (do...while), `finally` and `catch`. The default value is `"always"`. | |
29 | ||
30 | This rule will allow keywords to be preceded by an opening curly brace (`{`). If you wish to alter | |
31 | this behavior, consider using the [block-spacing](block-spacing.md) rule. | |
32 | ||
33 | Examples of **incorrect** code for this rule with the default `"always"` option: | |
34 | ||
35 | ```js | |
36 | /*eslint space-before-keywords: ["error", "always"]*/ | |
37 | /*eslint-env es6*/ | |
38 | ||
39 | if (foo) { | |
40 | // ... | |
41 | }else {} | |
42 | ||
43 | const foo = 'bar';let baz = 'qux'; | |
44 | ||
45 | var foo =function bar () {} | |
46 | ||
47 | function bar() { | |
48 | if (foo) {return; } | |
49 | } | |
50 | ``` | |
51 | ||
52 | Examples of **correct** code for this rule with the default `"always"` option: | |
53 | ||
54 | ```js | |
55 | /*eslint space-before-keywords: ["error", "always"]*/ | |
56 | /*eslint-env es6*/ | |
57 | ||
58 | if (foo) { | |
59 | // ... | |
60 | } else {} | |
61 | ||
62 | (function() {})() | |
63 | ||
64 | <Foo onClick={function bar() {}} /> | |
65 | ||
66 | for (let foo of ['bar', 'baz', 'qux']) {} | |
67 | ``` | |
68 | ||
69 | Examples of **incorrect** code for this rule with the `"never"` option: | |
70 | ||
71 | ```js | |
72 | /*eslint space-before-keywords: ["error", "never"]*/ | |
73 | ||
74 | if (foo) { | |
75 | // ... | |
76 | } else {} | |
77 | ||
78 | do { | |
79 | ||
80 | } | |
81 | while (foo) | |
82 | ||
83 | try {} finally {} | |
84 | ||
85 | try {} catch(e) {} | |
86 | ``` | |
87 | ||
88 | Examples of **correct** code for this rule with the `"never"` option: | |
89 | ||
90 | ```js | |
91 | /*eslint space-before-keywords: ["error", "never"]*/ | |
92 | ||
93 | if (foo) { | |
94 | // ... | |
95 | }else {} | |
96 | ||
97 | do {}while (foo) | |
98 | ||
99 | try {}finally {} | |
100 | ||
101 | try{}catch(e) {} | |
102 | ``` | |
103 | ||
104 | ## When Not To Use It | |
105 | ||
106 | If you do not wish to enforce consistency on keyword spacing. | |
107 | ||
108 | ## Related Rules | |
109 | ||
110 | * [space-after-keywords](space-after-keywords.md) | |
111 | * [block-spacing](block-spacing.md) | |
112 | * [space-return-throw-case](space-return-throw-case.md) | |
113 | * [space-unary-ops](space-unary-ops.md) | |
114 | * [space-infix-ops](space-infix-ops.md) |