2 * @fileoverview Rule to flag labels that are the same as an identifier
3 * @author Ian Christian Myers
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils = require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
23 description: "disallow labels that share a name with a variable",
24 category: "Variables",
26 url: "https://eslint.org/docs/rules/no-label-var"
34 //--------------------------------------------------------------------------
36 //--------------------------------------------------------------------------
39 * Check if the identifier is present inside current scope
40 * @param {Object} scope current scope
41 * @param {string} name To evaluate
42 * @returns {boolean} True if its present
45 function findIdentifier(scope, name) {
46 return astUtils.getVariableByName(scope, name) !== null;
49 //--------------------------------------------------------------------------
51 //--------------------------------------------------------------------------
55 LabeledStatement(node) {
57 // Fetch the innermost scope.
58 const scope = context.getScope();
61 * Recursively find the identifier walking up the scope, starting
62 * with the innermost scope.
64 if (findIdentifier(scope, node.label.name)) {
65 context.report({ node, message: "Found identifier with same name as label." });