2 * @fileoverview Rule to flag on declaring variables already declared in the outer scope
4 * @copyright 2013 Ilya Volodin. All rights reserved.
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
13 module.exports = function(context) {
16 * Checks if a variable is contained in the list of given scope variables.
17 * @param {Object} variable The variable to check.
18 * @param {Array} scopeVars The scope variables to look for.
19 * @returns {boolean} Whether or not the variable is contains in the list of scope variables.
21 function isContainedInScopeVars(variable, scopeVars) {
22 return scopeVars.some(function (scopeVar) {
23 if (scopeVar.identifiers.length > 0) {
24 return variable.name === scopeVar.name;
31 * Checks if the given variables are shadowed in the given scope.
32 * @param {Array} variables The variables to look for
33 * @param {Object} scope The scope to be checked.
36 function checkShadowsInScope(variables, scope) {
37 variables.forEach(function (variable) {
38 if (isContainedInScopeVars(variable, scope.variables) &&
39 // "arguments" is a special case that has no identifiers (#1759)
40 variable.identifiers.length > 0
43 context.report(variable.identifiers[0], "{{a}} is already declared in the upper scope.", {a: variable.name});
49 * Checks the current context for shadowed variables.
52 function checkForShadows() {
53 var scope = context.getScope(),
54 variables = scope.variables;
56 // iterate through the array of variables and find duplicates with the upper scope
57 var upper = scope.upper;
59 checkShadowsInScope(variables, upper);
65 "FunctionDeclaration": checkForShadows,
66 "FunctionExpression": checkForShadows