1 # enforce consistent spacing before and after keywords (keyword-spacing)
3 Keywords are syntax elements of JavaScript, such as `try` and `if`.
4 These keywords have special meaning to the language and so often appear in a different color in code editors.
5 As an important part of the language, style guides often refer to the spacing that should be used around keywords.
6 For example, you might have a style guide that says keywords should be always surrounded by spaces, which would mean `if-else` statements must look like this:
16 Of course, you could also have a style guide that disallows spaces around keywords.
18 However, if you want to enforce the style of spacing between the `function` keyword and the following opening parenthesis, please refer to [space-before-function-paren](space-before-function-paren.md).
22 This rule enforces consistent spacing around keywords and keyword-like tokens: `as` (in module declarations), `async` (of async functions), `await` (of await expressions), `break`, `case`, `catch`, `class`, `const`, `continue`, `debugger`, `default`, `delete`, `do`, `else`, `export`, `extends`, `finally`, `for`, `from` (in module declarations), `function`, `get` (of getters), `if`, `import`, `in` (in for-in statements), `let`, `new`, `of` (in for-of statements), `return`, `set` (of setters), `static`, `super`, `switch`, `this`, `throw`, `try`, `typeof`, `var`, `void`, `while`, `with`, and `yield`. This rule is designed carefully not to conflict with other spacing rules: it does not apply to spacing where other rules report problems.
26 This rule has an object option:
28 * `"before": true` (default) requires at least one space before keywords
29 * `"before": false` disallows spaces before keywords
30 * `"after": true` (default) requires at least one space after keywords
31 * `"after": false` disallows spaces after keywords
32 * `"overrides"` allows overriding spacing style for specified keywords
36 Examples of **incorrect** code for this rule with the default `{ "before": true }` option:
39 /*eslint keyword-spacing: ["error", { "before": true }]*/
50 Examples of **correct** code for this rule with the default `{ "before": true }` option:
53 /*eslint keyword-spacing: ["error", { "before": true }]*/
64 // Avoid conflict with `array-bracket-spacing`
66 let b = [function() {}];
68 // Avoid conflict with `arrow-spacing`
69 let a = ()=> this.foo;
71 // Avoid conflict with `block-spacing`
74 // Avoid conflict with `comma-spacing`
75 let a = [100,this.foo, this.bar];
77 // Avoid conflict with `computed-property-spacing`
80 // Avoid conflict with `generator-star-spacing`
83 // Avoid conflict with `key-spacing`
88 // Avoid conflict with `object-curly-spacing`
89 let obj = {foo: this};
91 // Avoid conflict with `semi-spacing`
92 let a = this;function foo() {}
94 // Avoid conflict with `space-in-parens`
97 // Avoid conflict with `space-infix-ops`
98 if ("foo"in {foo: 0}) {}
99 if (10+this.foo<= this.bar) {}
101 // Avoid conflict with `jsx-curly-spacing`
102 let a = <A foo={this.foo} bar={function(){}} />
105 Examples of **incorrect** code for this rule with the `{ "before": false }` option:
108 /*eslint keyword-spacing: ["error", { "before": false }]*/
119 Examples of **correct** code for this rule with the `{ "before": false }` option:
122 /*eslint keyword-spacing: ["error", { "before": false }]*/
135 Examples of **incorrect** code for this rule with the default `{ "after": true }` option:
138 /*eslint keyword-spacing: ["error", { "after": true }]*/
149 Examples of **correct** code for this rule with the default `{ "after": true }` option:
152 /*eslint keyword-spacing: ["error", { "after": true }]*/
162 // Avoid conflict with `array-bracket-spacing`
165 // Avoid conflict with `arrow-spacing`
166 let a = ()=> this.foo;
168 // Avoid conflict with `comma-spacing`
169 let a = [100, this.foo, this.bar];
171 // Avoid conflict with `computed-property-spacing`
174 // Avoid conflict with `generator-star-spacing`
177 // Avoid conflict with `key-spacing`
182 // Avoid conflict with `func-call-spacing`
189 // Avoid conflict with `object-curly-spacing`
190 let obj = {foo: this};
192 // Avoid conflict with `semi-spacing`
193 let a = this;function foo() {}
195 // Avoid conflict with `space-before-function-paren`
198 // Avoid conflict with `space-infix-ops`
199 if ("foo"in{foo: 0}) {}
200 if (10+this.foo<= this.bar) {}
202 // Avoid conflict with `space-unary-ops`
207 // Avoid conflict with `yield-star-spacing`
212 // Avoid conflict with `jsx-curly-spacing`
213 let a = <A foo={this.foo} bar={function(){}} />
216 Examples of **incorrect** code for this rule with the `{ "after": false }` option:
219 /*eslint keyword-spacing: ["error", { "after": false }]*/
230 Examples of **correct** code for this rule with the `{ "after": false }` option:
233 /*eslint keyword-spacing: ["error", { "after": false }]*/
246 Examples of **correct** code for this rule with the `{ "overrides": { "if": { "after": false }, "for": { "after": false }, "while": { "after": false }, "static": { "after": false } } }` option:
249 /*eslint keyword-spacing: ["error", { "overrides": {
250 "if": { "after": false },
251 "for": { "after": false },
252 "while": { "after": false },
253 "static": { "after": false }
277 ## When Not To Use It
279 If you don't want to enforce consistency on keyword spacing, then it's safe to disable this rule.