版本

max-lines

强制执行每个文件的最大行数

有些人认为大文件是代码异味。 大文件往往做很多事情,并且会使人难以理解发生了什么。 虽然对于文件中可接受的最大行数没有客观标准,但大多数人都会同意它不应该达到数千行。 建议通常在 100 到 500 行之间。

规则详情

此规则强制执行每个文件的最大行数,以帮助维护并降低复杂性。

请注意,如果文件以换行符结尾,则大多数编辑器会在末尾显示一个额外的空行。 此规则不计算该额外的行。

选项

此规则具有数字或对象选项

  • "max" (默认 300) 强制执行文件中的最大行数

  • "skipBlankLines": true 忽略完全由空格组成的行。

  • "skipComments": true 忽略仅包含注释的行

max

此规则的错误代码示例,最大值为 3

在 Playground 中打开
/*eslint max-lines: ["error", 3]*/
let a,
    b,
    c;
在 Playground 中打开
/*eslint max-lines: ["error", 3]*/

let a,
    b,c;
在 Playground 中打开
/*eslint max-lines: ["error", 3]*/
// a comment
let a,
    b,c;

此规则的正确代码示例,最大值为 3

在 Playground 中打开
/*eslint max-lines: ["error", 3]*/
let a,
    b, c;
在 Playground 中打开
/*eslint max-lines: ["error", 3]*/

let a, b, c;
在 Playground 中打开
/*eslint max-lines: ["error", 3]*/
// a comment
let a, b, c;

skipBlankLines

此规则的错误代码示例,带有 { "skipBlankLines": true } 选项

在 Playground 中打开
/*eslint max-lines: ["error", {"max": 3, "skipBlankLines": true}]*/

let a,
    b,
    c;

此规则的正确代码示例,带有 { "skipBlankLines": true } 选项

在 Playground 中打开
/*eslint max-lines: ["error", {"max": 3, "skipBlankLines": true}]*/

let a,
    b, c;

skipComments

此规则的错误代码示例,带有 { "skipComments": true } 选项

在 Playground 中打开
/*eslint max-lines: ["error", {"max": 2, "skipComments": true}]*/
// a comment
let a,
    b,
    c;

此规则的正确代码示例,带有 { "skipComments": true } 选项

在 Playground 中打开
/*eslint max-lines: ["error", {"max": 2, "skipComments": true}]*/
// a comment
let a,
    b, c;

何时不使用

如果您不关心文件中的行数,则可以关闭此规则。

兼容性

版本

此规则在 ESLint v2.12.0 中引入。

延伸阅读

资源

更改语言