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