no-multiple-empty-lines

不允许多个空行

🔧 Fixable

此规则报告的一些问题可通过 --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.

一些开发者更喜欢删除多个空行,而另一些开发者则认为这有助于提高可读性。空格对于分隔代码的逻辑部分很有用,但多余的空格会占用更多的屏幕。

¥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)强制连续空行的最大数量。

    ¥"max" (default: 2) enforces a maximum number of consecutive empty lines.

  • "maxEOF" 强制文件末尾连续空行的最大数量。

    ¥"maxEOF" enforces a maximum number of consecutive empty lines at the end of files.

  • "maxBOF" 强制文件开头连续空行的最大数量。

    ¥"maxBOF" enforces a maximum number of consecutive empty lines at the beginning of files.

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;

注意:尽管这确保了 EOF 处的空行为零,但如果文件以换行符结尾,大多数编辑器仍会在末尾显示一个空行,如下图所示。在最后一个 \n 之后的文件末尾没有空行,尽管编辑器可能会显示一个额外的行。真正的附加线将由 \n\n 表示。

¥Note: Although this ensures zero empty lines at the EOF, most editors will still show one empty line at the end if the file ends with a line break, as illustrated below. There is no empty line at the end of a file after the last \n, although editors may show an additional line. A true additional line would be represented by \n\n.

正确的:

¥Correct:

在线运行
/*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 中引入。

资源

ESLint 中文网
粤ICP备13048890号