space-before-blocks
在块之前强制执行一致的间距
此规则报告的一些问题可通过 --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
.
一致性是任何风格指南的重要组成部分。虽然将块的左大括号放在哪里是个人喜好,但它应该在整个项目中保持一致。风格不一致会分散读者看到代码重要部分的注意力。
¥Consistency is an important part of any style guide. While it is a personal preference where to put the opening brace of blocks, it should be consistent across a whole project. Having an inconsistent style distracts the reader from seeing the important parts of the code.
规则详情
¥Rule Details
此规则将强制块前间距的一致性。它仅适用于不在新行开始的块。
¥This rule will enforce consistency of spacing before blocks. It is only applied on blocks that don’t begin on a new line.
-
此规则忽略
=>
和块之间的间距。间距由arrow-spacing
规则处理。¥This rule ignores spacing which is between
=>
and a block. The spacing is handled by thearrow-spacing
rule. -
此规则忽略关键字和块之间的间距。间距由
keyword-spacing
规则处理。¥This rule ignores spacing which is between a keyword and a block. The spacing is handled by the
keyword-spacing
rule. -
此规则忽略开关盒的
:
和块之间的间距。间距由switch-colon-spacing
规则处理。¥This rule ignores spacing which is between
:
of a switch case and a block. The spacing is handled by theswitch-colon-spacing
rule.
选项
¥Options
这条规则有一个参数。如果是 "always"
,那么块必须总是有至少一个前面的空格。如果 "never"
,那么所有块都不应该有任何前面的空格。如果功能块、关键字块和类需要不同的间距,可以将可选的配置对象作为规则参数传递,以分别配置案例。如果配置对象中的任何值为 "off"
,则不会对这种类型的块强制执行任何样式。
¥This rule takes one argument. If it is "always"
then blocks must always have at least one preceding space. If "never"
then all blocks should never have any preceding space. If different spacing is desired for function
blocks, keyword blocks and classes, an optional configuration object can be passed as the rule argument to
configure the cases separately. If any value in the configuration object is "off"
, then neither style will be enforced for blocks of that kind.
(例如 { "functions": "never", "keywords": "always", "classes": "always" }
)
¥( e.g. { "functions": "never", "keywords": "always", "classes": "always" }
)
默认值为 "always"
。
¥The default is "always"
.
“always”
使用 “always” 选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the “always” option:
/*eslint space-before-blocks: "error"*/
if (a){
b();
}
function a(){}
for (;;){
b();
}
try {} catch(a){}
class Foo{
constructor(){}
}
使用 "always"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the "always"
option:
/*eslint space-before-blocks: "error"*/
if (a) {
b();
}
if (a) {
b();
} else{ /*no error. this is checked by `keyword-spacing` rule.*/
c();
}
class C {
static{} /*no error. this is checked by `keyword-spacing` rule.*/
}
function a() {}
for (;;) {
b();
}
try {} catch(a) {}
“never”
使用 "never"
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the "never"
option:
/*eslint space-before-blocks: ["error", "never"]*/
if (a) {
b();
}
function a() {}
for (;;) {
b();
}
try {} catch(a) {}
使用 "never"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the "never"
option:
/*eslint space-before-blocks: ["error", "never"]*/
if (a){
b();
}
function a(){}
for (;;){
b();
}
try{} catch(a){}
class Foo{
constructor(){}
}
配置 { "functions": "never", "keywords": "always", "classes": "never" }
时此规则的错误代码示例:
¥Examples of incorrect code for this rule when configured { "functions": "never", "keywords": "always", "classes": "never" }
:
/*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "always", "classes": "never" }]*/
function a() {}
try {} catch(a){}
class Foo{
constructor() {}
}
配置 { "functions": "never", "keywords": "always", "classes": "never" }
时此规则的正确代码示例:
¥Examples of correct code for this rule when configured { "functions": "never", "keywords": "always", "classes": "never" }
:
/*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "always", "classes": "never" }]*/
for (;;) {
// ...
}
describe(function(){
// ...
});
class Foo{
constructor(){}
}
配置 { "functions": "always", "keywords": "never", "classes": "never" }
时此规则的错误代码示例:
¥Examples of incorrect code for this rule when configured { "functions": "always", "keywords": "never", "classes": "never" }
:
/*eslint space-before-blocks: ["error", { "functions": "always", "keywords": "never", "classes": "never" }]*/
function a(){}
try {} catch(a) {}
class Foo {
constructor(){}
}
配置 { "functions": "always", "keywords": "never", "classes": "never" }
时此规则的正确代码示例:
¥Examples of correct code for this rule when configured { "functions": "always", "keywords": "never", "classes": "never" }
:
/*eslint space-before-blocks: ["error", { "functions": "always", "keywords": "never", "classes": "never" }]*/
if (a){
b();
}
var a = function() {}
class Foo{
constructor() {}
}
配置 { "functions": "never", "keywords": "never", "classes": "always" }
时此规则的错误代码示例:
¥Examples of incorrect code for this rule when configured { "functions": "never", "keywords": "never", "classes": "always" }
:
/*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "never", "classes": "always" }]*/
class Foo{
constructor(){}
}
配置 { "functions": "never", "keywords": "never", "classes": "always" }
时此规则的正确代码示例:
¥Examples of correct code for this rule when configured { "functions": "never", "keywords": "never", "classes": "always" }
:
/*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "never", "classes": "always" }]*/
class Foo {
constructor(){}
}
何时不使用
¥When Not To Use It
如果你不关心块前间距的一致性,你可以关闭此规则。
¥You can turn this rule off if you are not concerned with the consistency of spacing before blocks.
相关规则
版本
此规则是在 ESLint v0.9.0 中引入。