1 | /**
|
2 | * @fileoverview Rule to flag on declaring variables already declared in the outer scope
|
3 | * @author Ilya Volodin
|
4 | */
|
5 |
|
6 | //------------------------------------------------------------------------------
|
7 | // Rule Definition
|
8 | //------------------------------------------------------------------------------
|
9 |
|
10 | module.exports = function(context) {
|
11 |
|
12 | ;
|
13 |
|
14 | function checkForShadows(node) {
|
15 | var scope = context.getScope();
|
16 |
|
17 | //remove variabes that were passed as arguments
|
18 | var args = node.params;
|
19 | //push 'arguments' into the array
|
20 | //args.push({name: "arguments" });
|
21 | var variables = scope.variables.filter(function(item) {
|
22 | return !args.some(function(variable) {
|
23 | return variable.name === item.name;
|
24 | });
|
25 | });
|
26 | //iterate through the array of variables and find duplicates with the upper scope
|
27 | var upper = scope.upper;
|
28 |
|
29 | function findDups(variables) {
|
30 | variables.forEach(function(variable) {
|
31 | if (upper.variables.some(function(scopeVar) {
|
32 | //filter out global variables that we add as part of ESLint
|
33 | if (scopeVar.identifiers.length > 0) {
|
34 | return scopeVar.name === variable.name;
|
35 | }
|
36 | return false;
|
37 | })) {
|
38 | context.report(variable.identifiers[0], "{{a}} is already declared in the upper scope.", {a: variable.name});
|
39 | }
|
40 | });
|
41 | }
|
42 |
|
43 | while(upper) {
|
44 | findDups(variables);
|
45 | upper = upper.upper;
|
46 | }
|
47 | }
|
48 |
|
49 | return {
|
50 |
|
51 | "FunctionDeclaration": checkForShadows,
|
52 | "FunctionExpression": checkForShadows
|
53 | };
|
54 |
|
55 | };
|