space-before-blocks
在块之前强制执行一致的间距
此规则报告的一些问题可通过 --fix 命令行 选项自动修复
This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.
一致性是任何风格指南的重要组成部分。虽然将代码块的开括号放在哪里是个人偏好,但在整个项目中应该保持一致。风格不一致会分散读者的注意力,使其无法看到代码的重要部分。
🌐 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规则处理。 - 此规则忽略关键字与代码块之间的空格。空格由
keyword-spacing规则处理。 - 此规则忽略位于 switch case 的
:和一个代码块之间的空格。空格由switch-colon-spacing规则处理。
选项
🌐 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" })
默认值是 "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 中引入。