]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/max-params.js
import 8.23.1 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 /** @type {import('../shared/types').Rule} */
20 module.exports = {
21 meta: {
22 type: "suggestion",
23
24 docs: {
25 description: "Enforce a maximum number of parameters in function definitions",
26 recommended: false,
27 url: "https://eslint.org/docs/rules/max-params"
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) {
60 const sourceCode = context.getSourceCode();
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: {
87 name: upperCaseFirst(astUtils.getFunctionNameWithKind(node)),
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 };