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

63 lines
1.7 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.");
}
}
};
};