]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/space-before-keywords.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / space-before-keywords.md
CommitLineData
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
7Keywords 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
10if (foo) {
11 // ...
12} else {
13 // ...
14}
15```
16
17Of course, you could also have a style guide that disallows spaces before keywords.
18
19## Rule Details
20
21This 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`)
24and label statements.
25
26This rule takes one argument: `"always"` or `"never"`. If `"always"` then the keywords
27must be preceded by at least one space. If `"never"` then no spaces will be allowed before
28the keywords `else`, `while` (do...while), `finally` and `catch`. The default value is `"always"`.
29
30This rule will allow keywords to be preceded by an opening curly brace (`{`). If you wish to alter
31this behavior, consider using the [block-spacing](block-spacing.md) rule.
32
33Examples 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
39if (foo) {
40 // ...
41}else {}
42
43const foo = 'bar';let baz = 'qux';
44
45var foo =function bar () {}
46
47function bar() {
48 if (foo) {return; }
49}
50```
51
52Examples 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
58if (foo) {
59 // ...
60} else {}
61
62(function() {})()
63
64<Foo onClick={function bar() {}} />
65
66for (let foo of ['bar', 'baz', 'qux']) {}
67```
68
69Examples of **incorrect** code for this rule with the `"never"` option:
70
71```js
72/*eslint space-before-keywords: ["error", "never"]*/
73
74if (foo) {
75 // ...
76} else {}
77
78do {
79
80}
81while (foo)
82
83try {} finally {}
84
85try {} catch(e) {}
86```
87
88Examples of **correct** code for this rule with the `"never"` option:
89
90```js
91/*eslint space-before-keywords: ["error", "never"]*/
92
93if (foo) {
94 // ...
95}else {}
96
97do {}while (foo)
98
99try {}finally {}
100
101try{}catch(e) {}
102```
103
104## When Not To Use It
105
106If 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)