UNPKG

2.99 kBJavaScriptView Raw
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("../ast-utils");
15
16//------------------------------------------------------------------------------
17// Rule Definition
18//------------------------------------------------------------------------------
19
20module.exports = {
21 meta: {
22 docs: {
23 description: "enforce a maximum number of parameters in function definitions",
24 category: "Stylistic Issues",
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 },
54
55 create(context) {
56
57 const option = context.options[0];
58 let numParams = 3;
59
60 if (typeof option === "object" && option.hasOwnProperty("maximum") && typeof option.maximum === "number") {
61 numParams = option.maximum;
62 }
63 if (typeof option === "object" && option.hasOwnProperty("max") && typeof option.max === "number") {
64 numParams = option.max;
65 }
66 if (typeof option === "number") {
67 numParams = option;
68 }
69
70 /**
71 * Checks a function to see if it has too many parameters.
72 * @param {ASTNode} node The node to check.
73 * @returns {void}
74 * @private
75 */
76 function checkFunction(node) {
77 if (node.params.length > numParams) {
78 context.report({
79 node,
80 message: "{{name}} has too many parameters ({{count}}). Maximum allowed is {{max}}.",
81 data: {
82 name: lodash.upperFirst(astUtils.getFunctionNameWithKind(node)),
83 count: node.params.length,
84 max: numParams
85 }
86 });
87 }
88 }
89
90 return {
91 FunctionDeclaration: checkFunction,
92 ArrowFunctionExpression: checkFunction,
93 FunctionExpression: checkFunction
94 };
95
96 }
97};