2 * @fileoverview Disallow reassignment of function parameters.
4 * @copyright 2014 Nat Burns. All rights reserved.
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 module.exports = function(context) {
14 //--------------------------------------------------------------------------
16 //--------------------------------------------------------------------------
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.
24 function findDeclaration(scope, name) {
25 var variables = scope.variables;
27 for (var i = 0; i < variables.length; i++) {
28 if (variables[i].name === name) {
34 return findDeclaration(scope.upper, name);
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.
45 function isParameter(variable) {
46 var defs = variable.defs;
48 for (var i = 0; i < defs.length; i++) {
49 if (defs[i].type === "Parameter") {
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.
64 function checkParameter(node, name) {
65 var declaration = findDeclaration(context.getScope(), name);
67 if (declaration && isParameter(declaration)) {
68 context.report(node, "Assignment to function parameter '{{name}}'.", { name: name });
72 //--------------------------------------------------------------------------
74 //--------------------------------------------------------------------------
77 "AssignmentExpression": function(node) {
78 checkParameter(node, node.left.name);
81 "UpdateExpression": function(node) {
82 checkParameter(node, node.argument.name);