UNPKG

2.48 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// Rule Definition
10//------------------------------------------------------------------------------
11
12module.exports = {
13 meta: {
14 docs: {
15 description: "enforce a maximum number of parameters in function definitions",
16 category: "Stylistic Issues",
17 recommended: false
18 },
19
20 schema: [
21 {
22 oneOf: [
23 {
24 type: "integer",
25 minimum: 0
26 },
27 {
28 type: "object",
29 properties: {
30 maximum: {
31 type: "integer",
32 minimum: 0
33 },
34 max: {
35 type: "integer",
36 minimum: 0
37 }
38 },
39 additionalProperties: false
40 }
41 ]
42 }
43 ]
44 },
45
46 create(context) {
47
48 const option = context.options[0];
49 let numParams = 3;
50
51 if (typeof option === "object" && option.hasOwnProperty("maximum") && typeof option.maximum === "number") {
52 numParams = option.maximum;
53 }
54 if (typeof option === "object" && option.hasOwnProperty("max") && typeof option.max === "number") {
55 numParams = option.max;
56 }
57 if (typeof option === "number") {
58 numParams = option;
59 }
60
61 /**
62 * Checks a function to see if it has too many parameters.
63 * @param {ASTNode} node The node to check.
64 * @returns {void}
65 * @private
66 */
67 function checkFunction(node) {
68 if (node.params.length > numParams) {
69 context.report(node, "This function has too many parameters ({{count}}). Maximum allowed is {{max}}.", {
70 count: node.params.length,
71 max: numParams
72 });
73 }
74 }
75
76 return {
77 FunctionDeclaration: checkFunction,
78 ArrowFunctionExpression: checkFunction,
79 FunctionExpression: checkFunction
80 };
81
82 }
83};