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