space-before-keywords
在关键字之前强制执行一致的间距。
🌐 Enforces consistent spacing before keywords.
(可修复)命令行上的 --fix 选项会自动修复此规则报告的问题。
关键字是 JavaScript 的语法元素,例如 function 和 if。这些标识符对语言有特殊的意义,因此在代码编辑器中通常会以不同的颜色显示。作为语言的重要组成部分,风格指南通常会提到关键字周围应使用的空格。例如,你可能有一个风格指南规定关键字前应始终有空格,这意味着 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
此规则将强制在关键字 if、else、for、while、do、switch、throw、try、catch、finally、with、break、continue、return、function、yield、class 以及变量声明(let、const、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",则不允许在关键字 else、while(do…while)、finally 和 catch 前有空格。默认值为 "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 中删除。