-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
no-dupe-args.js
83 lines (67 loc) · 2.36 KB
/
no-dupe-args.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* @fileoverview Rule to flag duplicate arguments
* @author Jamund Ferguson
* @copyright 2015 Jamund Ferguson. All rights reserved.
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
//--------------------------------------------------------------------------
// Helpers
//--------------------------------------------------------------------------
/**
* Determines if a given node has duplicate parameters.
* @param {ASTNode} node The node to check.
* @returns {void}
* @private
*/
function checkParams(node) {
var params = {},
dups = {};
/**
* Marks a given param as either seen or duplicated.
* @param {string} name The name of the param to mark.
* @returns {void}
* @private
*/
function markParam(name) {
if (params.hasOwnProperty(name)) {
dups[name] = 1;
} else {
params[name] = 1;
}
}
// loop through and find each duplicate param
node.params.forEach(function(param) {
switch (param.type) {
case "Identifier":
markParam(param.name);
break;
case "ObjectPattern":
param.properties.forEach(function(property) {
markParam(property.key.name);
});
break;
case "ArrayPattern":
param.elements.forEach(function(element) {
markParam(element.name);
});
break;
// no default
}
});
// log an error for each duplicate (not 2 for each)
Object.keys(dups).forEach(function(currentParam) {
context.report(node, "Duplicate param '{{key}}'.", { key: currentParam });
});
}
//--------------------------------------------------------------------------
// Public API
//--------------------------------------------------------------------------
return {
"FunctionDeclaration": checkParams,
"FunctionExpression": checkParams
};
};