You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

62 lines
1.7 KiB

/**
* @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.");
}
}
};
};