max-params
在函数定义中强制执行最大数量的参数
需要许多参数的函数可能难以阅读和编写,因为这需要记住每个参数是什么、它的类型以及它们应该出现的顺序。因此,许多程序员遵循一种约定,即限制函数可以接受的参数数量。
🌐 Functions that take numerous parameters can be difficult to read and write because it requires the memorization of what each parameter is, its type, and the order they should appear in. As a result, many coders adhere to a convention that caps the number of parameters a function can take.
function foo (bar, baz, qux, qxx) { // four parameters, may be too many
doSomething();
}
规则详情
🌐 Rule Details
此规则强制执行函数定义中允许的最大参数数量。
🌐 This rule enforces a maximum number of parameters allowed in function definitions.
选项
🌐 Options
此规则有一个数字或对象选项:
🌐 This rule has a number or object option:
"max"(默认3)强制限制函数定义中的最大参数数量"countThis"(默认"except-void",可选值"always"、"never"或"except-void")是否计算this声明(仅限 TypeScript)
已弃用: 对象属性 maximum 已被弃用;请改用对象属性 max。
已弃用: 对象属性 countVoidThis 已被弃用;请使用 countThis: "except-void" 替代 countVoidThis: false,并使用 countThis: "always" 替代 countVoidThis: true。
max
使用默认 { "max": 3 } 选项时,该规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the default { "max": 3 } option:
/*eslint max-params: ["error", 3]*/
function foo1 (bar, baz, qux, qxx) {
doSomething();
}
let foo2 = (bar, baz, qux, qxx) => {
doSomething();
};
使用默认 { "max": 3 } 选项时,该规则的正确代码示例:
🌐 Examples of correct code for this rule with the default { "max": 3 } option:
/*eslint max-params: ["error", 3]*/
function foo1 (bar, baz, qux) {
doSomething();
}
let foo2 = (bar, baz, qux) => {
doSomething();
};
countThis(仅限 TypeScript)
🌐 countThis (TypeScript only)
此规则有一个特定于 TypeScript 的选项 countThis,允许你计算 this 声明。
有效值为:
🌐 This rule has a TypeScript-specific option countThis that allows you to count a this declaration.
Valid values are:
"always":任何this类型的注解都将被视为一个参数"never":this永远不会被计算在内"except-void":this只有在其类型不是void时才会被计数。等同于已废弃的countVoidThis: false。
针对该规则的 正确 TypeScript 代码示例:
🌐 Examples of correct TypeScript code for this rule:
/*eslint max-params: ["error", { "max": 2, "countThis": "except-void" }]*/
function hasNoThis(this: void, first: string, second: string) {
// ...
}
/*eslint max-params: ["error", { "max": 2, "countThis": "never" }]*/
function hasThis(this: unknown[], first: string, second: string) {
// ...
}
针对该规则的 错误 TypeScript 代码示例:
🌐 Examples of incorrect TypeScript code for this rule:
/*eslint max-params: ["error", { "max": 2, "countThis": "always" }]*/
function hasNoThis(this: void, first: string, second: string) {
// ...
}
/*eslint max-params: ["error", { "max": 2, "countThis": "except-void" }]*/
function hasThis(this: unknown[], first: string, second: string) {
// ...
}
相关规则
版本
此规则是在 ESLint v0.0.9 中引入。