max-lines
强制每个文件的最大行数
有些人认为大文件是一种代码异味。大文件通常做很多事情,并且可能让人难以理解其内容。虽然没有被认为可接受的文件最大行数的客观标准,但大多数人会同意它不应该达到几千行。建议通常在100到500行之间。
🌐 Some people consider large files a code smell. Large files tend to do a lot of things and can make it hard following what’s going. While there is not an objective maximum number of lines considered acceptable in a file, most people would agree it should not be in the thousands. Recommendations usually range from 100 to 500 lines.
规则详情
🌐 Rule Details
此规则强制每个文件的最大行数,以帮助可维护性并降低复杂性。
🌐 This rule enforces a maximum number of lines per file, in order to aid in maintainability and reduce complexity.
请注意,如果文件以换行符结尾,大多数编辑器会在末尾显示一个额外的空行。此规则不计算该额外的空行。
🌐 Please note that most editors show an additional empty line at the end if the file ends with a line break. This rule does not count that extra line.
选项
🌐 Options
此规则有一个数字或对象选项:
🌐 This rule has a number or object option:
"max"(默认300)强制限制文件中的最大行数。"skipBlankLines": true忽略仅由空白字符组成的行。"skipComments": true忽略仅包含注释的行。
max
此规则在最大值为 3 时的错误代码示例:
🌐 Examples of incorrect code for this rule with a max value of 3:
/*eslint max-lines: ["error", 3]*/
let a,
b,
c;
/*eslint max-lines: ["error", 3]*/
let a,
b,c;
/*eslint max-lines: ["error", 3]*/
// a comment
let a,
b,c;
此规则的正确代码示例,最大值为 3:
🌐 Examples of correct code for this rule with a max value of 3:
/*eslint max-lines: ["error", 3]*/
let a,
b, c;
/*eslint max-lines: ["error", 3]*/
let a, b, c;
/*eslint max-lines: ["error", 3]*/
// a comment
let a, b, c;
skipBlankLines
使用 { "skipBlankLines": true } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "skipBlankLines": true } option:
/*eslint max-lines: ["error", {"max": 3, "skipBlankLines": true}]*/
let a,
b,
c;
使用 { "skipBlankLines": true } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "skipBlankLines": true } option:
/*eslint max-lines: ["error", {"max": 3, "skipBlankLines": true}]*/
let a,
b, c;
skipComments
使用 { "skipComments": true } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "skipComments": true } option:
/*eslint max-lines: ["error", {"max": 2, "skipComments": true}]*/
// a comment
let a,
b,
c;
使用 { "skipComments": true } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "skipComments": true } option:
/*eslint max-lines: ["error", {"max": 2, "skipComments": true}]*/
// a comment
let a,
b, c;
何时不使用
🌐 When Not To Use It
如果你不关心文件中的行数,可以关闭此规则。
🌐 You can turn this rule off if you are not concerned with the number of lines in your files.
兼容性
🌐 Compatibility
- JSCS: maximumNumberOfLines
相关规则
版本
此规则是在 ESLint v2.12.0 中引入。