c38f0ef6f4ea328dfcb1c7d7042a1ca6f9313ad0
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Disallow reassignment of function parameters.
3  * @author Nat Burns
4  * @copyright 2014 Nat Burns. All rights reserved.
5  */
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Rule Definition
10 //------------------------------------------------------------------------------
11
12 module.exports = function(context) {
13
14     //--------------------------------------------------------------------------
15     // Helpers
16     //--------------------------------------------------------------------------
17
18     /**
19      * Finds the declaration for a given variable by name, searching up the scope tree.
20      * @param {Scope} scope The scope in which to search.
21      * @param {String} name The name of the variable.
22      * @returns {Variable} The declaration information for the given variable, or null if no declaration was found.
23      */
24     function findDeclaration(scope, name) {
25         var variables = scope.variables;
26
27         for (var i = 0; i < variables.length; i++) {
28             if (variables[i].name === name) {
29                 return variables[i];
30             }
31         }
32
33         if (scope.upper) {
34             return findDeclaration(scope.upper, name);
35         } else {
36             return null;
37         }
38     }
39
40     /**
41      * Determines if a given variable is declared as a function parameter.
42      * @param {Variable} variable The variable declaration.
43      * @returns {boolean} True if the variable is a function parameter, false otherwise.
44      */
45     function isParameter(variable) {
46         var defs = variable.defs;
47
48         for (var i = 0; i < defs.length; i++) {
49             if (defs[i].type === "Parameter") {
50                 return true;
51             }
52         }
53
54         return false;
55     }
56
57     /**
58      * Checks whether a given node is an assignment to a function parameter.
59      * If so, a linting error will be reported.
60      * @param {ASTNode} node The node to check.
61      * @param {String} name The name of the variable being assigned to.
62      * @returns {void}
63      */
64     function checkParameter(node, name) {
65         var declaration = findDeclaration(context.getScope(), name);
66
67         if (declaration && isParameter(declaration)) {
68             context.report(node, "Assignment to function parameter '{{name}}'.", { name: name });
69         }
70     }
71
72     //--------------------------------------------------------------------------
73     // Public
74     //--------------------------------------------------------------------------
75
76     return {
77         "AssignmentExpression": function(node) {
78             checkParameter(node, node.left.name);
79         },
80
81         "UpdateExpression": function(node) {
82             checkParameter(node, node.argument.name);
83         }
84     };
85 };