UNPKG

1.74 kBJavaScriptView Raw
1/**
2 * @fileoverview Rule to flag labels that are the same as an identifier
3 * @author Ian Christian Myers
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12var astUtils = require("../ast-utils");
13
14//------------------------------------------------------------------------------
15// Rule Definition
16//------------------------------------------------------------------------------
17
18module.exports = function(context) {
19
20 //--------------------------------------------------------------------------
21 // Helpers
22 //--------------------------------------------------------------------------
23
24 /**
25 * Check if the identifier is present inside current scope
26 * @param {object} scope current scope
27 * @param {string} name To evaluate
28 * @returns {boolean} True if its present
29 * @private
30 */
31 function findIdentifier(scope, name) {
32 return astUtils.getVariableByName(scope, name) !== null;
33 }
34
35 //--------------------------------------------------------------------------
36 // Public API
37 //--------------------------------------------------------------------------
38
39 return {
40
41 "LabeledStatement": function(node) {
42
43 // Fetch the innermost scope.
44 var scope = context.getScope();
45
46 // Recursively find the identifier walking up the scope, starting
47 // with the innermost scope.
48 if (findIdentifier(scope, node.label.name)) {
49 context.report(node, "Found identifier with same name as label.");
50 }
51 }
52
53 };
54
55};
56
57module.exports.schema = [];