Index

no-multiple-empty-lines

不允许多个空行

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行 选项自动修复

Important

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.

Learn more

有些开发者更喜欢删除多个空行,而其他人则认为它有助于提高可读性。空白对于分隔代码的逻辑部分是有用的,但过多的空白会占用更多的屏幕空间。

🌐 Some developers prefer to have multiple blank lines removed, while others feel that it helps improve readability. Whitespace is useful for separating logical sections of code, but excess whitespace takes up more of the screen.

规则详情

🌐 Rule Details

此规则旨在减少阅读代码时所需的滚动量。当超过最大空行数时,它会发出警告。

🌐 This rule aims to reduce the scrolling required when reading through your code. It will warn when the maximum amount of empty lines has been exceeded.

选项

🌐 Options

此规则有一个对象选项:

🌐 This rule has an object option:

  • "max"(默认值:2)限制连续空行的最大数量。
  • "maxEOF" 强制限制文件末尾连续空行的最大数量。
  • "maxBOF" 强制在文件开头的连续空行数量不超过最大值。

max

使用默认 { "max": 2 } 选项时,该规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the default { "max": 2 } option:

在线运行
/*eslint no-multiple-empty-lines: "error"*/

var foo = 5;



var bar = 3;

使用默认 { "max": 2 } 选项时,该规则的正确代码示例:

🌐 Examples of correct code for this rule with the default { "max": 2 } option:

在线运行
/*eslint no-multiple-empty-lines: "error"*/

var foo = 5;


var bar = 3;

maxEOF

此规则使用 { max: 2, maxEOF: 0 } 选项的 错误 代码示例:

🌐 Examples of incorrect code for this rule with the { max: 2, maxEOF: 0 } options:

在线运行
/*eslint no-multiple-empty-lines: ["error", { "max": 2, "maxEOF": 0 }]*/⏎
⏎
var foo = 5;⏎
⏎
⏎
var bar = 3;⏎
⏎

使用 { max: 2, maxEOF: 0 } 选项的此规则的正确代码示例:

🌐 Examples of correct code for this rule with the { max: 2, maxEOF: 0 } options:

在线运行
/*eslint no-multiple-empty-lines: ["error", { "max": 2, "maxEOF": 0 }]*/⏎
⏎
var foo = 5;⏎
⏎
⏎
var bar = 3;

注意:虽然这可以确保在文件末尾没有空行,但如果文件以换行符结束,大多数编辑器仍会在文件末尾显示一个空行,如下所示。在最后一个 \n 之后,文件末尾实际上没有空行,尽管编辑器可能会显示一个额外的行。真正的额外行将由 \n\n 表示。

正确:

在线运行
/*eslint no-multiple-empty-lines: ["error", { "max": 2, "maxEOF": 0 }]*/⏎
⏎
var foo = 5;⏎
⏎
⏎
var bar = 3;

maxBOF

使用 { max: 2, maxBOF: 1 } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the { max: 2, maxBOF: 1 } options:

在线运行


/*eslint no-multiple-empty-lines: ["error", { "max": 2, "maxBOF": 1 }]*/


var foo = 5;


var bar = 3;

使用 { max: 2, maxBOF: 1 } 选项的此规则的正确代码示例:

🌐 Examples of correct code for this rule with the { max: 2, maxBOF: 1 } options:

在线运行
/*eslint no-multiple-empty-lines: ["error", { "max": 2, "maxBOF": 1}]*/

var foo = 5;


var bar = 3;
在线运行

/*eslint no-multiple-empty-lines: ["error", { "max": 2, "maxBOF": 1}]*/

var foo = 5;


var bar = 3;

何时不使用

🌐 When Not To Use It

如果你不关心多余的空行,请将其关闭。

🌐 If you do not care about extra blank lines, turn this off.

版本

此规则是在 ESLint v0.9.0 中引入。

资源