space-before-keywords

在关键字之前强制执行一致的间距。

¥Enforces consistent spacing before keywords.

(可修复)命令行 上的 --fix 选项自动修复了此规则报告的问题。

¥(fixable) The --fix option on the command line automatically fixed problems reported by this rule.

关键字是 JavaScript 的语法元素,例如 functionif。这些标识符对语言具有特殊意义,因此在代码编辑器中经常以不同的颜色出现。作为语言的重要组成部分,风格指南通常指的是应该在关键字周围使用的间距。例如,你可能有一个风格指南说关键字应该始终以空格开头,这意味着 if-else 语句必须如下所示:

¥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:

if (foo) {
    // ...
} else {
    // ...
}

当然,你也可以有一个不允许关键字前有空格的风格指南。

¥Of course, you could also have a style guide that disallows spaces before keywords.

规则详情

¥Rule Details

此规则将强制关键字 ifelseforwhiledoswitchthrowtrycatchfinallywithbreakcontinuereturnfunctionyieldclass 和变量声明(letconst)之前的间距保持一致, var) 和标签声明。

¥This rule will enforce consistency of spacing before the keywords if, else, for, while, do, switch, throw, try, catch, finally, with, break, continue, return, function, yield, class and variable declarations (let, const, var) and label statements.

该规则采用一个参数:"always""never"。如果 "always" 则关键字前面必须至少有一个空格。如果是 "never",则在关键字 elsewhile(do…while)、finallycatch 之前不允许有空格。默认值为 "always"

¥This rule takes one argument: "always" or "never". If "always" then the keywords must be preceded by at least one space. If "never" then no spaces will be allowed before the keywords else, while (do…while), finally and catch. The default value is "always".

此规则将允许关键字前面有一个左大括号 ({)。如果你希望更改此行为,请考虑使用 block-spacing 规则。

¥This rule will allow keywords to be preceded by an opening curly brace ({). If you wish to alter this behavior, consider using the block-spacing rule.

使用默认 "always" 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the default "always" option:

/*eslint space-before-keywords: ["error", "always"]*/

if (foo) {
    // ...
}else {}

const foo = 'bar';let baz = 'qux';

var qux =function bar () {}

function bar() {
    if (foo) {return; }
}

使用默认 "always" 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the default "always" option:

/*eslint space-before-keywords: ["error", "always"]*/

if (foo) {
    // ...
} else {}

(function() {})();

<Foo onClick={function bar() {}} />

for (let foo of ['bar', 'baz', 'qux']) {}

使用 "never" 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "never" option:

/*eslint space-before-keywords: ["error", "never"]*/

if (foo) {
    // ...
} else {}

do {

}
while (foo)

try {} finally {}

try {} catch(e) {}

使用 "never" 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the "never" option:

/*eslint space-before-keywords: ["error", "never"]*/

if (foo) {
    // ...
}else {}

do {}while (foo)

try {}finally {}

try{}catch(e) {}

何时不使用

¥When Not To Use It

如果你不希望强制关键字间距的一致性。

¥If you do not wish to enforce consistency on keyword spacing.

版本

此规则是在 ESLint v1.4.0 中引入,并在 v2.0.0-beta.3 中删除。