]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/space-before-keywords.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / space-before-keywords.md
1 ---
2 title: space-before-keywords
3
4 related_rules:
5 - space-after-keywords
6 - block-spacing
7 - space-return-throw-case
8 - space-unary-ops
9 - space-infix-ops
10 ---
11
12 Enforces consistent spacing before keywords.
13
14 (removed) This rule was **removed** in ESLint v2.0 and **replaced** by the [keyword-spacing](keyword-spacing) rule.
15
16 (fixable) The `--fix` option on the [command line](../use/command-line-interface#--fix) automatically fixed problems reported by this rule.
17
18 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:
19
20 ```js
21 if (foo) {
22 // ...
23 } else {
24 // ...
25 }
26 ```
27
28 Of course, you could also have a style guide that disallows spaces before keywords.
29
30 ## Rule Details
31
32 This rule will enforce consistency of spacing before the keywords `if`, `else`, `for`,
33 `while`, `do`, `switch`, `throw`, `try`, `catch`, `finally`, `with`, `break`, `continue`,
34 `return`, `function`, `yield`, `class` and variable declarations (`let`, `const`, `var`)
35 and label statements.
36
37 This rule takes one argument: `"always"` or `"never"`. If `"always"` then the keywords
38 must be preceded by at least one space. If `"never"` then no spaces will be allowed before
39 the keywords `else`, `while` (do...while), `finally` and `catch`. The default value is `"always"`.
40
41 This rule will allow keywords to be preceded by an opening curly brace (`{`). If you wish to alter
42 this behavior, consider using the [block-spacing](block-spacing) rule.
43
44 Examples of **incorrect** code for this rule with the default `"always"` option:
45
46 ::: incorrect
47
48 ```js
49 /*eslint space-before-keywords: ["error", "always"]*/
50 /*eslint-env es6*/
51
52 if (foo) {
53 // ...
54 }else {}
55
56 const foo = 'bar';let baz = 'qux';
57
58 var foo =function bar () {}
59
60 function bar() {
61 if (foo) {return; }
62 }
63 ```
64
65 :::
66
67 Examples of **correct** code for this rule with the default `"always"` option:
68
69 ::: correct
70
71 ```js
72 /*eslint space-before-keywords: ["error", "always"]*/
73 /*eslint-env es6*/
74
75 if (foo) {
76 // ...
77 } else {}
78
79 (function() {})()
80
81 <Foo onClick={function bar() {}} />
82
83 for (let foo of ['bar', 'baz', 'qux']) {}
84 ```
85
86 :::
87
88 Examples of **incorrect** code for this rule with the `"never"` option:
89
90 ::: incorrect
91
92 ```js
93 /*eslint space-before-keywords: ["error", "never"]*/
94
95 if (foo) {
96 // ...
97 } else {}
98
99 do {
100
101 }
102 while (foo)
103
104 try {} finally {}
105
106 try {} catch(e) {}
107 ```
108
109 :::
110
111 Examples of **correct** code for this rule with the `"never"` option:
112
113 ::: correct
114
115 ```js
116 /*eslint space-before-keywords: ["error", "never"]*/
117
118 if (foo) {
119 // ...
120 }else {}
121
122 do {}while (foo)
123
124 try {}finally {}
125
126 try{}catch(e) {}
127 ```
128
129 :::
130
131 ## When Not To Use It
132
133 If you do not wish to enforce consistency on keyword spacing.