keyword-spacing
在关键字前后强制使用一致的间距
此规则报告的一些问题可通过 --fix
命令行选项自动修复
此规则在 ESLint v8.53.0 中已弃用。请在 @stylistic/eslint-plugin-js
中使用 相应的规则。
¥This rule was deprecated in ESLint v8.53.0. Please use the corresponding rule in @stylistic/eslint-plugin-js
.
关键字是 JavaScript 的语法元素,例如 try
和 if
。这些关键字对语言具有特殊意义,因此在代码编辑器中经常以不同的颜色出现。作为语言的重要组成部分,风格指南通常指的是应该在关键字周围使用的间距。例如,你可能有一个风格指南,它说关键字应该始终被空格包围,这意味着 if-else
语句必须如下所示:
¥Keywords are syntax elements of JavaScript, such as try
and if
.
These keywords 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 surrounded by spaces, which would mean if-else
statements must look like this:
if (foo) {
// ...
} else {
// ...
}
当然,你也可以有一个不允许关键字周围有空格的风格指南。
¥Of course, you could also have a style guide that disallows spaces around keywords.
但是,如果要强制 function
关键字和后面的左括号之间的空格样式,请参考 space-before-function-paren。
¥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.
规则详情
¥Rule Details
此规则在关键字和类似关键字的标记周围强制执行一致的间距:as
(在模块声明中)、async
(异步函数)、await
(等待表达式)、break
、case
、catch
、class
、const
、continue
、debugger
、default
、delete
、do
、else
、export
、extends
、finally
、for
,from
(在模块声明中),function
,get
(getters),if
,import
,in
(for-in 语句),let
,new
,of
(for-of 语句),return
,set
(setters), static
、super
、switch
、this
、throw
、try
、typeof
、var
、void
、while
、with
和 yield
。此规则经过精心设计,不会与其他间距规则冲突:它不适用于其他规则报告问题的间距。
¥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.
选项
¥Options
此规则有一个对象选项:
¥This rule has an object option:
-
"before": true
(默认)关键字前至少需要一个空格¥
"before": true
(default) requires at least one space before keywords -
"before": false
关键字前不允许有空格¥
"before": false
disallows spaces before keywords -
"after": true
(默认)关键字后至少需要一个空格¥
"after": true
(default) requires at least one space after keywords -
"after": false
关键字后不允许有空格¥
"after": false
disallows spaces after keywords -
"overrides"
允许覆盖指定关键字的间距样式¥
"overrides"
allows overriding spacing style for specified keywords
before
使用默认 { "before": true }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the default { "before": true }
option:
/*eslint keyword-spacing: ["error", { "before": true }]*/
if (foo) {
//...
}else if (bar) {
//...
}else {
//...
}
使用默认 { "before": true }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the default { "before": true }
option:
/*eslint keyword-spacing: ["error", { "before": true }]*/
if (foo) {
//...
} else if (bar) {
//...
} else {
//...
}
// Avoid conflict with `array-bracket-spacing`
let a = [this];
let b = [function() {}];
// Avoid conflict with `arrow-spacing`
let c = ()=> this.foo;
// Avoid conflict with `block-spacing`
{function foo() {}}
// Avoid conflict with `comma-spacing`
let d = [100,this.foo, this.bar];
// Avoid conflict with `computed-property-spacing`
obj[this.foo] = 0;
// Avoid conflict with `generator-star-spacing`
function *bar() {}
// Avoid conflict with `key-spacing`
let obj1 = {
foo:function() {}
};
// Avoid conflict with `object-curly-spacing`
let obj2 = {foo: this};
// Avoid conflict with `semi-spacing`
let e = this;function foo() {}
// Avoid conflict with `space-in-parens`
(function () {})();
// Avoid conflict with `space-infix-ops`
if ("foo"in {foo: 0}) {}
if (10+this.foo<= this.bar) {}
// Avoid conflict with `jsx-curly-spacing`
let f = <A foo={this.foo} bar={function(){}} />
使用 { "before": false }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the { "before": false }
option:
/*eslint keyword-spacing: ["error", { "before": false }]*/
if (foo) {
//...
} else if (bar) {
//...
} else {
//...
}
使用 { "before": false }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "before": false }
option:
/*eslint keyword-spacing: ["error", { "before": false }]*/
if (foo) {
//...
}else if (bar) {
//...
}else {
//...
}
after
使用默认 { "after": true }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the default { "after": true }
option:
/*eslint keyword-spacing: ["error", { "after": true }]*/
if(foo) {
//...
} else if(bar) {
//...
} else{
//...
}
使用默认 { "after": true }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the default { "after": true }
option:
/*eslint keyword-spacing: ["error", { "after": true }]*/
if (foo) {
//...
} else if (bar) {
//...
} else {
//...
}
// Avoid conflict with `array-bracket-spacing`
let a = [this];
// Avoid conflict with `arrow-spacing`
let b = ()=> this.foo;
// Avoid conflict with `comma-spacing`
let c = [100, this.foo, this.bar];
// Avoid conflict with `computed-property-spacing`
obj[this.foo] = 0;
// Avoid conflict with `generator-star-spacing`
function* foo() {}
// Avoid conflict with `key-spacing`
let obj1 = {
foo:function() {}
};
// Avoid conflict with `func-call-spacing`
class A extends B {
constructor() {
super();
}
}
// Avoid conflict with `object-curly-spacing`
let obj2 = {foo: this};
// Avoid conflict with `semi-spacing`
let d = this;function bar() {}
// Avoid conflict with `space-before-function-paren`
(function() {})();
// Avoid conflict with `space-infix-ops`
if ("foo"in{foo: 0}) {}
if (10+this.foo<= this.bar) {}
// Avoid conflict with `space-unary-ops`
function* baz(a) {
return yield+a;
}
// Avoid conflict with `yield-star-spacing`
function* qux(a) {
return yield* a;
}
// Avoid conflict with `jsx-curly-spacing`
let e = <A foo={this.foo} bar={function(){}} />
使用 { "after": false }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the { "after": false }
option:
/*eslint keyword-spacing: ["error", { "after": false }]*/
if (foo) {
//...
} else if (bar) {
//...
} else {
//...
}
使用 { "after": false }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "after": false }
option:
/*eslint keyword-spacing: ["error", { "after": false }]*/
if(foo) {
//...
} else if(bar) {
//...
} else{
//...
}
overrides
使用 { "overrides": { "if": { "after": false }, "for": { "after": false }, "while": { "after": false }, "static": { "after": false }, "as": { "after": false } } }
选项的此规则的正确代码示例:
¥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:
/*eslint keyword-spacing: ["error", { "overrides": {
"if": { "after": false },
"for": { "after": false },
"while": { "after": false },
"static": { "after": false },
"as": { "after": false }
} }]*/
if(foo) {
//...
} else if(bar) {
//...
} else {
//...
}
for(;;);
while(true) {
//...
}
class C {
static{
//...
}
}
export { C as"my class" };
何时不使用
¥When Not To Use It
如果你不想强制关键字间距的一致性,那么禁用此规则是安全的。
¥If you don’t want to enforce consistency on keyword spacing, then it’s safe to disable this rule.
版本
此规则是在 ESLint v2.0.0-beta.1 中引入。