padded-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.
一些风格指南要求代码块语句在开始和结束时都有空行。其目的是通过在代码块内容和周围代码之间进行视觉分隔来提高可读性。
🌐 Some style guides require block statements to start and end with blank lines. The goal is to improve readability by visually separating the block content and the surrounding code.
if (a) {
b();
}
既然保持一致的代码风格是好的,你应该要么总是写带间距的代码块,要么从不这样做。
🌐 Since it’s good to have a consistent code style, you should either always write padded blocks or never do it.
规则详情
🌐 Rule Details
此规则在块内强制执行一致的空行填充。
🌐 This rule enforces consistent empty line padding within blocks.
选项
🌐 Options
这个规则有两个选项,第一个可以是字符串选项或对象选项。第二个是对象选项,它可以允许例外。
🌐 This rule has two options, the first one can be a string option or an object option. The second one is an object option, it can allow exceptions.
第一个选项
🌐 First option
字符串选项:
🌐 String option:
"always"(默认)要求在块语句、函数体、类静态块、类以及switch语句的开头和结尾处有空行。"never"不允许在代码块语句、函数体、类静态块、类以及switch语句的开头和结尾出现空行。
对象选项:
🌐 Object option:
"blocks"要求或禁止在代码块语句、函数体和类静态块内使用空行"classes"要求或禁止类内部的填充"switches"要求或禁止在switch语句中使用填充
第二种选择
🌐 Second option
"allowSingleLineBlocks": true允许单行代码块
always
使用默认 "always" 选项时,该规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the default "always" option:
/*eslint padded-blocks: ["error", "always"]*/
if (a) {
b();
}
if (a) { b(); }
if (a)
{
b();
}
if (a) {
b();
}
if (a) {
// comment
b();
}
class C {
static {
a();
}
}
使用默认 "always" 选项时,该规则的正确代码示例:
🌐 Examples of correct code for this rule with the default "always" option:
/*eslint padded-blocks: ["error", "always"]*/
if (a) {
b();
}
if (a)
{
b();
}
if (a) {
// comment
b();
}
class C {
static {
a();
}
}
never
使用 "never" 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the "never" option:
/*eslint padded-blocks: ["error", "never"]*/
if (a) {
b();
}
if (a)
{
b();
}
if (a) {
b();
}
if (a) {
b();
}
class C {
static {
a();
}
}
使用 "never" 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "never" option:
/*eslint padded-blocks: ["error", "never"]*/
if (a) {
b();
}
if (a)
{
b();
}
class C {
static {
a();
}
}
blocks
使用 { "blocks": "always" } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "blocks": "always" } option:
/*eslint padded-blocks: ["error", { "blocks": "always" }]*/
if (a) {
b();
}
if (a) { b(); }
if (a)
{
b();
}
if (a) {
b();
}
if (a) {
b();
}
if (a) {
// comment
b();
}
class C {
static {
a();
}
}
使用 { "blocks": "always" } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "blocks": "always" } option:
/*eslint padded-blocks: ["error", { "blocks": "always" }]*/
if (a) {
b();
}
if (a)
{
b();
}
if (a) {
// comment
b();
}
class C {
static {
a();
}
}
class D {
static {
a();
}
}
使用 { "blocks": "never" } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "blocks": "never" } option:
/*eslint padded-blocks: ["error", { "blocks": "never" }]*/
if (a) {
b();
}
if (a)
{
b();
}
if (a) {
b();
}
if (a) {
b();
}
class C {
static {
a();
}
}
使用 { "blocks": "never" } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "blocks": "never" } option:
/*eslint padded-blocks: ["error", { "blocks": "never" }]*/
if (a) {
b();
}
if (a)
{
b();
}
class C {
static {
a();
}
}
class D {
static {
a();
}
}
classes
使用 { "classes": "always" } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "classes": "always" } option:
/*eslint padded-blocks: ["error", { "classes": "always" }]*/
class A {
constructor(){
}
}
使用 { "classes": "always" } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "classes": "always" } option:
/*eslint padded-blocks: ["error", { "classes": "always" }]*/
class A {
constructor(){
}
}
使用 { "classes": "never" } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "classes": "never" } option:
/*eslint padded-blocks: ["error", { "classes": "never" }]*/
class A {
constructor(){
}
}
使用 { "classes": "never" } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "classes": "never" } option:
/*eslint padded-blocks: ["error", { "classes": "never" }]*/
class A {
constructor(){
}
}
switches
使用 { "switches": "always" } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "switches": "always" } option:
/*eslint padded-blocks: ["error", { "switches": "always" }]*/
switch (a) {
case 0: foo();
}
使用 { "switches": "always" } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "switches": "always" } option:
/*eslint padded-blocks: ["error", { "switches": "always" }]*/
switch (a) {
case 0: foo();
}
if (a) {
b();
}
使用 { "switches": "never" } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "switches": "never" } option:
/*eslint padded-blocks: ["error", { "switches": "never" }]*/
switch (a) {
case 0: foo();
}
使用 { "switches": "never" } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "switches": "never" } option:
/*eslint padded-blocks: ["error", { "switches": "never" }]*/
switch (a) {
case 0: foo();
}
if (a) {
b();
}
always + allowSingleLineBlocks
使用 "always", {"allowSingleLineBlocks": true} 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the "always", {"allowSingleLineBlocks": true} options:
/*eslint padded-blocks: ["error", "always", { allowSingleLineBlocks: true }]*/
if (a) {
b();
}
if (a) {
b();
}
if (a) {
b();
}
使用 "always", {"allowSingleLineBlocks": true} 选项的此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "always", {"allowSingleLineBlocks": true} options:
/*eslint padded-blocks: ["error", "always", { allowSingleLineBlocks: true }]*/
if (a) { b(); }
if (a) {
b();
}
何时不使用
🌐 When Not To Use It
如果你不关心块内填充的一致性,你可以关闭此规则。
🌐 You can turn this rule off if you are not concerned with the consistency of padding within blocks.
相关规则
版本
此规则是在 ESLint v0.9.0 中引入。