node/tools/eslint/lib/rules/no-param-reassign.js
Yosuke Furukawa f9dd34d301 tools: replace closure-linter with eslint
PR-URL: https://github.com/iojs/io.js/pull/1539
Fixes: https://github.com/iojs/io.js/issues/1253
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
Reviewed-By: Johan Bergström <bugs@bergstroem.nu>
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
2015-05-09 12:09:52 +09:00

86 lines
2.7 KiB
JavaScript

/**
* @fileoverview Disallow reassignment of function parameters.
* @author Nat Burns
* @copyright 2014 Nat Burns. All rights reserved.
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
//--------------------------------------------------------------------------
// Helpers
//--------------------------------------------------------------------------
/**
* Finds the declaration for a given variable by name, searching up the scope tree.
* @param {Scope} scope The scope in which to search.
* @param {String} name The name of the variable.
* @returns {Variable} The declaration information for the given variable, or null if no declaration was found.
*/
function findDeclaration(scope, name) {
var variables = scope.variables;
for (var i = 0; i < variables.length; i++) {
if (variables[i].name === name) {
return variables[i];
}
}
if (scope.upper) {
return findDeclaration(scope.upper, name);
} else {
return null;
}
}
/**
* Determines if a given variable is declared as a function parameter.
* @param {Variable} variable The variable declaration.
* @returns {boolean} True if the variable is a function parameter, false otherwise.
*/
function isParameter(variable) {
var defs = variable.defs;
for (var i = 0; i < defs.length; i++) {
if (defs[i].type === "Parameter") {
return true;
}
}
return false;
}
/**
* Checks whether a given node is an assignment to a function parameter.
* If so, a linting error will be reported.
* @param {ASTNode} node The node to check.
* @param {String} name The name of the variable being assigned to.
* @returns {void}
*/
function checkParameter(node, name) {
var declaration = findDeclaration(context.getScope(), name);
if (declaration && isParameter(declaration)) {
context.report(node, "Assignment to function parameter '{{name}}'.", { name: name });
}
}
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
return {
"AssignmentExpression": function(node) {
checkParameter(node, node.left.name);
},
"UpdateExpression": function(node) {
checkParameter(node, node.argument.name);
}
};
};