noExcessiveLinesPerFunction
¥Summary
-
规则生效日期:
v2.0.0¥Rule available since:
v2.0.0 -
诊断类别:
lint/complexity/noExcessiveLinesPerFunction¥Diagnostic Category:
lint/complexity/noExcessiveLinesPerFunction -
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
¥Inspired from
max-lines-per-function
-
¥How to configure
{ "linter": { "rules": { "complexity": { "noExcessiveLinesPerFunction": "error" } } }}¥Description
限制函数中的代码行数。
¥Restrict the number of lines of code in a function.
此规则检查函数体中的行数,如果超过指定限制,则报告诊断信息。请记住,此规则仅计算函数体中的代码行数,而不是整个函数声明中的代码行数。有些人认为大型函数是一种代码异味。大型函数通常执行很多操作,可能会使代码难以理解。许多编码风格指南规定了函数可以包含的行数限制。此规则有助于强制执行该风格。
¥This rule checks the number of lines in a function body and reports a diagnostic if it exceeds a specified limit. Remember that this rule only counts the lines of code in the function body, not the entire function declaration. Some people consider large functions a code smell. Large functions tend to do a lot of things and can make it hard following what’s going on. Many coding style guides dictate a limit of the number of lines that a function can comprise of. This rule can help enforce that style.
¥Examples
¥Invalid
以下示例会在将 maxLines 限制设置为 3 时显示诊断信息,但默认值为 50。
¥The following example will show diagnostic when you set the maxLines limit to 3, however the default value is 50.
function foo () { const x = 0; const y = 1; const z = 2; return x + y + z;};¥Valid
function foo () { const x = 0; const y = 1;};¥Options
该规则支持以下选项:
¥The rule supports the following options:
{ "options": { "maxLines": 50, "skipBlankLines": false, "skipIifes": false }}maxLines
Section titled “maxLines”此选项设置函数体中允许的最大行数。如果函数体超出此限制,则会报告诊断信息。
¥This option sets the maximum number of lines allowed in a function body. If the function body exceeds this limit, a diagnostic will be reported.
默认:50
¥Default: 50
使用 maxLines: 2 时,以下函数将被视为无效:
¥When maxLines: 2, the following function will be considered invalid:
{ "linter": { "rules": { "complexity": { "noExcessiveLinesPerFunction": { "options": { "maxLines": 2 } } } } }}function example() { const a = 1; // 1 const b = 2; // 2 const c = 3; // 3};code-block.js:1:1 lint/complexity/noExcessiveLinesPerFunction ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This function has too many lines (3). Maximum allowed is 2.
> 1 │ function example() {
│ ^^^^^^^^^^^^^^^^^^^^
> 2 │ const a = 1; // 1
> 3 │ const b = 2; // 2
> 4 │ const c = 3; // 3
> 5 │ };
│ ^
6 │
ℹ Consider refactoring this function to split it into smaller functions.
skipBlankLines
Section titled “skipBlankLines”当此选项设置为 true 时,函数体中的空行不计入最大行数限制。这意味着只有包含实际代码或注释的行才会被计数。
¥When this options is set to true, blank lines in the function body are not counted towards the maximum line limit.
This means that only lines with actual code or comments will be counted.
默认:false
¥Default: false
当同时指定 maxLines: 2 和 skipBlankLines: true 时,以下函数也会被视为有效:
¥When maxLines: 2 and skipBlankLines: true, the following function will be considered valid:
{ "linter": { "rules": { "complexity": { "noExcessiveLinesPerFunction": { "options": { "maxLines": 2, "skipBlankLines": true } } } } }}function example() { const a = 1; // 1 // not counted const b = 2; // 2 // not counted};skipIifes
Section titled “skipIifes”当此选项设置为 true 时,不会检查立即调用函数表达式 (IIFE) 是否超过最大行数限制。
¥When this option is set to true, Immediately Invoked Function Expressions (IIFEs) are not checked for the maximum line limit.
默认:false
¥Default: false
当同时指定 maxLines: 2 和 skipIifes: true 时,即使以下立即执行函数表达式 (IIFE) 的主体有 3 行,它也会被视为有效:
¥When maxLines: 2 and skipIifes: true, the following IIFE will be considered valid even though its body has 3 lines:
{ "linter": { "rules": { "complexity": { "noExcessiveLinesPerFunction": { "options": { "maxLines": 2, "skipIifes": true } } } } }}(() => { const a = 1; // 1 const b = 2; // 2 const c = 3; // 3})();¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号