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
)强制文件中的最大行数¥
"max"
(default300
) enforces a maximum number of lines in a file -
"skipBlankLines": true
忽略纯粹由空格组成的行。¥
"skipBlankLines": true
ignore lines made up purely of whitespace. -
"skipComments": true
忽略仅包含注释的行¥
"skipComments": true
ignore lines containing just comments
max
此规则的错误代码示例(最大值为 3
):
¥Examples of incorrect code for this rule with a max value of 3
:
/*eslint max-lines: ["error", 3]*/
var a,
b,
c;
/*eslint max-lines: ["error", 3]*/
var a,
b,c;
/*eslint max-lines: ["error", 3]*/
// a comment
var a,
b,c;
此规则的正确代码示例(最大值为 3
):
¥Examples of correct code for this rule with a max value of 3
:
/*eslint max-lines: ["error", 3]*/
var a,
b, c;
/*eslint max-lines: ["error", 3]*/
var a, b, c;
/*eslint max-lines: ["error", 3]*/
// a comment
var 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}]*/
var 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}]*/
var 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
var 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
var 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 中引入。