8 Keywords are syntax elements of JavaScript, such as `try` and `if`.
9 These keywords have special meaning to the language and so often appear in a different color in code editors.
10 As an important part of the language, style guides often refer to the spacing that should be used around keywords.
11 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:
21 Of course, you could also have a style guide that disallows spaces around keywords.
23 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).
27 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.
31 This rule has an object option:
33 * `"before": true` (default) requires at least one space before keywords
34 * `"before": false` disallows spaces before keywords
35 * `"after": true` (default) requires at least one space after keywords
36 * `"after": false` disallows spaces after keywords
37 * `"overrides"` allows overriding spacing style for specified keywords
41 Examples of **incorrect** code for this rule with the default `{ "before": true }` option:
46 /*eslint keyword-spacing: ["error", { "before": true }]*/
59 Examples of **correct** code for this rule with the default `{ "before": true }` option:
64 /*eslint keyword-spacing: ["error", { "before": true }]*/
75 // Avoid conflict with `array-bracket-spacing`
77 let b = [function() {}];
79 // Avoid conflict with `arrow-spacing`
80 let a = ()=> this.foo;
82 // Avoid conflict with `block-spacing`
85 // Avoid conflict with `comma-spacing`
86 let a = [100,this.foo, this.bar];
88 // Avoid conflict with `computed-property-spacing`
91 // Avoid conflict with `generator-star-spacing`
94 // Avoid conflict with `key-spacing`
99 // Avoid conflict with `object-curly-spacing`
100 let obj = {foo: this};
102 // Avoid conflict with `semi-spacing`
103 let a = this;function foo() {}
105 // Avoid conflict with `space-in-parens`
108 // Avoid conflict with `space-infix-ops`
109 if ("foo"in {foo: 0}) {}
110 if (10+this.foo<= this.bar) {}
112 // Avoid conflict with `jsx-curly-spacing`
113 let a = <A foo={this.foo} bar={function(){}} />
118 Examples of **incorrect** code for this rule with the `{ "before": false }` option:
123 /*eslint keyword-spacing: ["error", { "before": false }]*/
136 Examples of **correct** code for this rule with the `{ "before": false }` option:
141 /*eslint keyword-spacing: ["error", { "before": false }]*/
156 Examples of **incorrect** code for this rule with the default `{ "after": true }` option:
161 /*eslint keyword-spacing: ["error", { "after": true }]*/
174 Examples of **correct** code for this rule with the default `{ "after": true }` option:
179 /*eslint keyword-spacing: ["error", { "after": true }]*/
189 // Avoid conflict with `array-bracket-spacing`
192 // Avoid conflict with `arrow-spacing`
193 let a = ()=> this.foo;
195 // Avoid conflict with `comma-spacing`
196 let a = [100, this.foo, this.bar];
198 // Avoid conflict with `computed-property-spacing`
201 // Avoid conflict with `generator-star-spacing`
204 // Avoid conflict with `key-spacing`
209 // Avoid conflict with `func-call-spacing`
216 // Avoid conflict with `object-curly-spacing`
217 let obj = {foo: this};
219 // Avoid conflict with `semi-spacing`
220 let a = this;function foo() {}
222 // Avoid conflict with `space-before-function-paren`
225 // Avoid conflict with `space-infix-ops`
226 if ("foo"in{foo: 0}) {}
227 if (10+this.foo<= this.bar) {}
229 // Avoid conflict with `space-unary-ops`
234 // Avoid conflict with `yield-star-spacing`
239 // Avoid conflict with `jsx-curly-spacing`
240 let a = <A foo={this.foo} bar={function(){}} />
245 Examples of **incorrect** code for this rule with the `{ "after": false }` option:
250 /*eslint keyword-spacing: ["error", { "after": false }]*/
263 Examples of **correct** code for this rule with the `{ "after": false }` option:
268 /*eslint keyword-spacing: ["error", { "after": false }]*/
283 Examples of **correct** code for this rule with the `{ "overrides": { "if": { "after": false }, "for": { "after": false }, "while": { "after": false }, "static": { "after": false }, "as": { "after": false } } }` option:
288 /*eslint keyword-spacing: ["error", { "overrides": {
289 "if": { "after": false },
290 "for": { "after": false },
291 "while": { "after": false },
292 "static": { "after": false },
293 "as": { "after": false }
316 export { C as"my class" };
321 ## When Not To Use It
323 If you don't want to enforce consistency on keyword spacing, then it's safe to disable this rule.