da1b7bff4c282b0ab49bff38b10da81ec603a60d
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Rule to flag when the same variable is declared more then once.
3  * @author Ilya Volodin
4  */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Rule Definition
10 //------------------------------------------------------------------------------
11
12 module.exports = function(context) {
13
14     /**
15      * Find variables in a given scope and flag redeclared ones.
16      * @param {Scope} scope An escope scope object.
17      * @returns {void}
18      * @private
19      */
20     function findVariablesInScope(scope) {
21         scope.variables.forEach(function(variable) {
22
23             if (variable.identifiers && variable.identifiers.length > 1) {
24                 variable.identifiers.sort(function(a, b) {
25                     return a.range[1] - b.range[1];
26                 });
27
28                 for (var i = 1, l = variable.identifiers.length; i < l; i++) {
29                     context.report(variable.identifiers[i], "{{a}} is already defined", {a: variable.name});
30                 }
31             }
32         });
33
34     }
35
36     /**
37      * Find variables in a given node's associated scope.
38      * @param {ASTNode} node The node to check.
39      * @returns {void}
40      * @private
41      */
42     function findVariables(node) {
43         var scope = context.getScope();
44
45         findVariablesInScope(scope);
46
47         // globalReturn means one extra scope to check
48         if (node.type === "Program" && context.ecmaFeatures.globalReturn) {
49             findVariablesInScope(scope.childScopes[0]);
50         }
51     }
52
53     return {
54         "Program": findVariables,
55         "FunctionExpression": findVariables,
56         "FunctionDeclaration": findVariables
57     };
58 };