]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Rule to flag when a function has too many parameters | |
3 | * @author Ilya Volodin | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
eb39fafa | 12 | const astUtils = require("./utils/ast-utils"); |
5422a9cc | 13 | const { upperCaseFirst } = require("../shared/string-utils"); |
eb39fafa DC |
14 | |
15 | //------------------------------------------------------------------------------ | |
16 | // Rule Definition | |
17 | //------------------------------------------------------------------------------ | |
18 | ||
34eeec05 | 19 | /** @type {import('../shared/types').Rule} */ |
eb39fafa DC |
20 | module.exports = { |
21 | meta: { | |
22 | type: "suggestion", | |
23 | ||
24 | docs: { | |
8f9d1d4d | 25 | description: "Enforce a maximum number of parameters in function definitions", |
eb39fafa | 26 | recommended: false, |
f2a92ac6 | 27 | url: "https://eslint.org/docs/latest/rules/max-params" |
eb39fafa DC |
28 | }, |
29 | ||
30 | schema: [ | |
31 | { | |
32 | oneOf: [ | |
33 | { | |
34 | type: "integer", | |
35 | minimum: 0 | |
36 | }, | |
37 | { | |
38 | type: "object", | |
39 | properties: { | |
40 | maximum: { | |
41 | type: "integer", | |
42 | minimum: 0 | |
43 | }, | |
44 | max: { | |
45 | type: "integer", | |
46 | minimum: 0 | |
47 | } | |
48 | }, | |
49 | additionalProperties: false | |
50 | } | |
51 | ] | |
52 | } | |
53 | ], | |
54 | messages: { | |
55 | exceed: "{{name}} has too many parameters ({{count}}). Maximum allowed is {{max}}." | |
56 | } | |
57 | }, | |
58 | ||
59 | create(context) { | |
f2a92ac6 | 60 | const sourceCode = context.sourceCode; |
eb39fafa DC |
61 | const option = context.options[0]; |
62 | let numParams = 3; | |
63 | ||
64 | if ( | |
65 | typeof option === "object" && | |
66 | (Object.prototype.hasOwnProperty.call(option, "maximum") || Object.prototype.hasOwnProperty.call(option, "max")) | |
67 | ) { | |
68 | numParams = option.maximum || option.max; | |
69 | } | |
70 | if (typeof option === "number") { | |
71 | numParams = option; | |
72 | } | |
73 | ||
74 | /** | |
75 | * Checks a function to see if it has too many parameters. | |
76 | * @param {ASTNode} node The node to check. | |
77 | * @returns {void} | |
78 | * @private | |
79 | */ | |
80 | function checkFunction(node) { | |
81 | if (node.params.length > numParams) { | |
82 | context.report({ | |
83 | loc: astUtils.getFunctionHeadLoc(node, sourceCode), | |
84 | node, | |
85 | messageId: "exceed", | |
86 | data: { | |
5422a9cc | 87 | name: upperCaseFirst(astUtils.getFunctionNameWithKind(node)), |
eb39fafa DC |
88 | count: node.params.length, |
89 | max: numParams | |
90 | } | |
91 | }); | |
92 | } | |
93 | } | |
94 | ||
95 | return { | |
96 | FunctionDeclaration: checkFunction, | |
97 | ArrowFunctionExpression: checkFunction, | |
98 | FunctionExpression: checkFunction | |
99 | }; | |
100 | ||
101 | } | |
102 | }; |