node/tools/eslint/lib/rules/no-redeclare.js
Roman Reiss d91e10b3bd tools: update eslint to 0.24.0
PR-URL: https://github.com/nodejs/io.js/pull/2072
Reviewed-By: Yosuke Furukawa <yosuke.furukawa@gmail.com>
Reviewed-by: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Alex Kocharin <alex@kocharin.ru>
2015-06-29 19:02:17 +02:00

68 lines
2 KiB
JavaScript

/**
* @fileoverview Rule to flag when the same variable is declared more then once.
* @author Ilya Volodin
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
/**
* Find variables in a given scope and flag redeclared ones.
* @param {Scope} scope An escope scope object.
* @returns {void}
* @private
*/
function findVariablesInScope(scope) {
scope.variables.forEach(function(variable) {
if (variable.identifiers && variable.identifiers.length > 1) {
variable.identifiers.sort(function(a, b) {
return a.range[1] - b.range[1];
});
for (var i = 1, l = variable.identifiers.length; i < l; i++) {
context.report(variable.identifiers[i], "{{a}} is already defined", {a: variable.name});
}
}
});
}
/**
* Find variables in a given node's associated scope.
* @param {ASTNode} node The node to check.
* @returns {void}
* @private
*/
function findVariables(node) {
var scope = context.getScope();
findVariablesInScope(scope);
// globalReturn means one extra scope to check
if (node.type === "Program" && context.ecmaFeatures.globalReturn) {
findVariablesInScope(scope.childScopes[0]);
}
}
if (context.ecmaFeatures.blockBindings) {
return {
"Program": findVariables,
"BlockStatement": findVariables,
"SwitchStatement": findVariables
};
} else {
return {
"Program": findVariables,
"FunctionDeclaration": findVariables,
"FunctionExpression": findVariables,
"ArrowFunctionExpression": findVariables
};
}
};
module.exports.schema = [];