]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-dupe-args.js
import 8.41.0 source
[pve-eslint.git] / eslint / lib / rules / no-dupe-args.js
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 /** @type {import('../shared/types').Rule} */
13 module.exports = {
14 meta: {
15 type: "problem",
16
17 docs: {
18 description: "Disallow duplicate arguments in `function` definitions",
19 recommended: true,
20 url: "https://eslint.org/docs/latest/rules/no-dupe-args"
21 },
22
23 schema: [],
24
25 messages: {
26 unexpected: "Duplicate param '{{name}}'."
27 }
28 },
29
30 create(context) {
31
32 const sourceCode = context.sourceCode;
33
34 //--------------------------------------------------------------------------
35 // Helpers
36 //--------------------------------------------------------------------------
37
38 /**
39 * Checks whether or not a given definition is a parameter's.
40 * @param {eslint-scope.DefEntry} def A definition to check.
41 * @returns {boolean} `true` if the definition is a parameter's.
42 */
43 function isParameter(def) {
44 return def.type === "Parameter";
45 }
46
47 /**
48 * Determines if a given node has duplicate parameters.
49 * @param {ASTNode} node The node to check.
50 * @returns {void}
51 * @private
52 */
53 function checkParams(node) {
54 const variables = sourceCode.getDeclaredVariables(node);
55
56 for (let i = 0; i < variables.length; ++i) {
57 const variable = variables[i];
58
59 // Checks and reports duplications.
60 const defs = variable.defs.filter(isParameter);
61
62 if (defs.length >= 2) {
63 context.report({
64 node,
65 messageId: "unexpected",
66 data: { name: variable.name }
67 });
68 }
69 }
70 }
71
72 //--------------------------------------------------------------------------
73 // Public API
74 //--------------------------------------------------------------------------
75
76 return {
77 FunctionDeclaration: checkParams,
78 FunctionExpression: checkParams
79 };
80
81 }
82 };