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