node/tools/eslint/lib/rules/no-label-var.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

64 lines
1.8 KiB
JavaScript

/**
* @fileoverview Rule to flag labels that are the same as an identifier
* @author Ian Christian Myers
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
//--------------------------------------------------------------------------
// Helpers
//--------------------------------------------------------------------------
function findIdentifier(scope, identifier) {
var found = false;
scope.variables.forEach(function(variable) {
if (variable.name === identifier) {
found = true;
}
});
scope.references.forEach(function(reference) {
if (reference.identifier.name === identifier) {
found = true;
}
});
// If we have not found the identifier in this scope, check the parent
// scope.
if (scope.upper && !found) {
return findIdentifier(scope.upper, identifier);
}
return found;
}
//--------------------------------------------------------------------------
// Public API
//--------------------------------------------------------------------------
return {
"LabeledStatement": function(node) {
// Fetch the innermost scope.
var scope = context.getScope();
// Recursively find the identifier walking up the scope, starting
// with the innermost scope.
if (findIdentifier(scope, node.label.name)) {
context.report(node, "Found identifier with same name as label.");
}
}
};
};
module.exports.schema = [];