2 * @fileoverview Rule to flag labels that are the same as an identifier
3 * @author Ian Christian Myers
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 module.exports = function(context) {
14 //--------------------------------------------------------------------------
16 //--------------------------------------------------------------------------
18 function findIdentifier(scope, identifier) {
21 scope.variables.forEach(function(variable) {
22 if (variable.name === identifier) {
27 scope.references.forEach(function(reference) {
28 if (reference.identifier.name === identifier) {
33 // If we have not found the identifier in this scope, check the parent
35 if (scope.upper && !found) {
36 return findIdentifier(scope.upper, identifier);
42 //--------------------------------------------------------------------------
44 //--------------------------------------------------------------------------
48 "LabeledStatement": function(node) {
50 // Fetch the innermost scope.
51 var scope = context.getScope();
53 // Recursively find the identifier walking up the scope, starting
54 // with the innermost scope.
55 if (findIdentifier(scope, node.label.name)) {
56 context.report(node, "Found identifier with same name as label.");