padded-blocks
一些风格指南要求块语句以空行开头和结尾。目的是通过视觉上分离块内容和周围代码来提高可读性。
if (a) {
b();
}由于保持一致的代码风格很重要,因此您应该始终编写带填充的块或从不编写带填充的块。
规则详细信息
此规则强制在块内一致的空行填充。
选项
此规则有两个选项,第一个选项可以是字符串选项或对象选项。第二个选项是对象选项,它可以允许例外。
第一个选项
字符串选项
"always"(默认)要求在块语句、函数体、类静态块、类和switch语句的开头和结尾处有空行。"never"不允许在块语句、函数体、类静态块、类和switch语句的开头和结尾处有空行。
对象选项
"blocks"要求或禁止在块语句、函数体和类静态块内填充"classes"要求或禁止在类内填充"switches"要求或禁止在switch语句内填充
第二个选项
"allowSingleLineBlocks": true允许单行块
始终
此规则使用默认的 "always" 选项时,不正确代码示例
/*eslint @stylistic/js/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" 选项时,正确代码示例
/*eslint @stylistic/js/padded-blocks: ["error", "always"]*/
if (a) {
b();
}
if (a)
{
b();
}
if (a) {
// comment
b();
}
class C {
static {
a();
}
}never
此规则使用 "never" 选项时,不正确代码示例
/*eslint @stylistic/js/padded-blocks: ["error", "never"]*/
if (a) {
b();
}
if (a)
{
b();
}
if (a) {
b();
}
if (a) {
b();
}
class C {
static {
a();
}
}此规则使用 "never" 选项时,正确代码示例
/*eslint @stylistic/js/padded-blocks: ["error", "never"]*/
if (a) {
b();
}
if (a)
{
b();
}
class C {
static {
a();
}
}blocks
此规则使用 { "blocks": "always" } 选项时,不正确代码示例
/*eslint @stylistic/js/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" } 选项时,正确代码示例
/*eslint @stylistic/js/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" } 选项时,不正确代码示例
/*eslint @stylistic/js/padded-blocks: ["error", { "blocks": "never" }]*/
if (a) {
b();
}
if (a)
{
b();
}
if (a) {
b();
}
if (a) {
b();
}
class C {
static {
a();
}
}此规则使用 { "blocks": "never" } 选项时,正确代码示例
/*eslint @stylistic/js/padded-blocks: ["error", { "blocks": "never" }]*/
if (a) {
b();
}
if (a)
{
b();
}
class C {
static {
a();
}
}
class D {
static {
a();
}
}classes
此规则使用 { "classes": "always" } 选项时,不正确代码示例
/*eslint @stylistic/js/padded-blocks: ["error", { "classes": "always" }]*/
class A {
constructor(){
}
}此规则使用 { "classes": "always" } 选项时,正确代码示例
/*eslint @stylistic/js/padded-blocks: ["error", { "classes": "always" }]*/
class A {
constructor(){
}
}此规则使用 { "classes": "never" } 选项时,不正确代码示例
/*eslint @stylistic/js/padded-blocks: ["error", { "classes": "never" }]*/
class A {
constructor(){
}
}此规则使用 { "classes": "never" } 选项时,正确代码示例
/*eslint @stylistic/js/padded-blocks: ["error", { "classes": "never" }]*/
class A {
constructor(){
}
}switches
此规则使用 { "switches": "always" } 选项时,不正确代码示例
/*eslint @stylistic/js/padded-blocks: ["error", { "switches": "always" }]*/
switch (a) {
case 0: foo();
}此规则使用 { "switches": "always" } 选项时,正确代码示例
/*eslint @stylistic/js/padded-blocks: ["error", { "switches": "always" }]*/
switch (a) {
case 0: foo();
}
if (a) {
b();
}此规则使用 { "switches": "never" } 选项时,不正确代码示例
/*eslint @stylistic/js/padded-blocks: ["error", { "switches": "never" }]*/
switch (a) {
case 0: foo();
}此规则使用 { "switches": "never" } 选项时,正确代码示例
/*eslint @stylistic/js/padded-blocks: ["error", { "switches": "never" }]*/
switch (a) {
case 0: foo();
}
if (a) {
b();
}always + allowSingleLineBlocks
此规则使用 "always", {"allowSingleLineBlocks": true} 选项时,不正确代码示例
/*eslint @stylistic/js/padded-blocks: ["error", "always", { allowSingleLineBlocks: true }]*/
if (a) {
b();
}
if (a) {
b();
}
if (a) {
b();
}此规则使用 "always", {"allowSingleLineBlocks": true} 选项时,正确代码示例
/*eslint @stylistic/js/padded-blocks: ["error", "always", { allowSingleLineBlocks: true }]*/
if (a) { b(); }
if (a) {
b();
}何时不使用它
如果您不关心块内填充的一致性,可以关闭此规则。