UNPKG

2.35 kBJavaScriptView Raw
1/**
2 * @fileoverview Rule to flag duplicate arguments
3 * @author Jamund Ferguson
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Rule Definition
10//------------------------------------------------------------------------------
11
12module.exports = {
13 meta: {
14 type: "problem",
15
16 docs: {
17 description: "disallow duplicate arguments in `function` definitions",
18 category: "Possible Errors",
19 recommended: true,
20 url: "https://eslint.org/docs/rules/no-dupe-args"
21 },
22
23 schema: [],
24
25 messages: {
26 unexpected: "Duplicate param '{{name}}'."
27 }
28 },
29
30 create(context) {
31
32 //--------------------------------------------------------------------------
33 // Helpers
34 //--------------------------------------------------------------------------
35
36 /**
37 * Checks whether or not a given definition is a parameter's.
38 * @param {eslint-scope.DefEntry} def A definition to check.
39 * @returns {boolean} `true` if the definition is a parameter's.
40 */
41 function isParameter(def) {
42 return def.type === "Parameter";
43 }
44
45 /**
46 * Determines if a given node has duplicate parameters.
47 * @param {ASTNode} node The node to check.
48 * @returns {void}
49 * @private
50 */
51 function checkParams(node) {
52 const variables = context.getDeclaredVariables(node);
53
54 for (let i = 0; i < variables.length; ++i) {
55 const variable = variables[i];
56
57 // Checks and reports duplications.
58 const defs = variable.defs.filter(isParameter);
59
60 if (defs.length >= 2) {
61 context.report({
62 node,
63 messageId: "unexpected",
64 data: { name: variable.name }
65 });
66 }
67 }
68 }
69
70 //--------------------------------------------------------------------------
71 // Public API
72 //--------------------------------------------------------------------------
73
74 return {
75 FunctionDeclaration: checkParams,
76 FunctionExpression: checkParams
77 };
78
79 }
80};