blob: 4906fa58f261f8db2691e18af0d56590d779be1f (
plain)
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
|
/**
* @fileoverview Rule to flag on declaring variables already declared in the outer scope
* @author Ilya Volodin
* @copyright 2013 Ilya Volodin. All rights reserved.
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
/**
* Checks if a variable is contained in the list of given scope variables.
* @param {Object} variable The variable to check.
* @param {Array} scopeVars The scope variables to look for.
* @returns {boolean} Whether or not the variable is contains in the list of scope variables.
*/
function isContainedInScopeVars(variable, scopeVars) {
return scopeVars.some(function (scopeVar) {
if (scopeVar.identifiers.length > 0) {
return variable.name === scopeVar.name;
}
return false;
});
}
/**
* Checks if the given variables are shadowed in the given scope.
* @param {Array} variables The variables to look for
* @param {Object} scope The scope to be checked.
* @returns {void}
*/
function checkShadowsInScope(variables, scope) {
variables.forEach(function (variable) {
if (isContainedInScopeVars(variable, scope.variables) &&
// "arguments" is a special case that has no identifiers (#1759)
variable.identifiers.length > 0 &&
// function names are exempt
variable.defs.length && variable.defs[0].type !== "FunctionName"
) {
context.report(variable.identifiers[0], "{{a}} is already declared in the upper scope.", {a: variable.name});
}
});
}
/**
* Checks the current context for shadowed variables.
* @returns {void}
*/
function checkForShadows() {
var scope = context.getScope(),
variables = scope.variables;
// iterate through the array of variables and find duplicates with the upper scope
var upper = scope.upper;
while (upper) {
checkShadowsInScope(variables, upper);
upper = upper.upper;
}
}
return {
"FunctionDeclaration": checkForShadows,
"FunctionExpression": checkForShadows,
"ArrowFunctionExpression": checkForShadows
};
};
|