]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
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 | ||
12 | const astUtils = require("./utils/ast-utils"); | |
13 | ||
14 | //------------------------------------------------------------------------------ | |
15 | // Rule Definition | |
16 | //------------------------------------------------------------------------------ | |
17 | ||
34eeec05 | 18 | /** @type {import('../shared/types').Rule} */ |
eb39fafa DC |
19 | module.exports = { |
20 | meta: { | |
21 | type: "suggestion", | |
22 | ||
23 | docs: { | |
8f9d1d4d | 24 | description: "Disallow labels that share a name with a variable", |
eb39fafa | 25 | recommended: false, |
f2a92ac6 | 26 | url: "https://eslint.org/docs/latest/rules/no-label-var" |
eb39fafa DC |
27 | }, |
28 | ||
29 | schema: [], | |
30 | ||
31 | messages: { | |
32 | identifierClashWithLabel: "Found identifier with same name as label." | |
33 | } | |
34 | }, | |
35 | ||
36 | create(context) { | |
f2a92ac6 | 37 | const sourceCode = context.sourceCode; |
eb39fafa DC |
38 | |
39 | //-------------------------------------------------------------------------- | |
40 | // Helpers | |
41 | //-------------------------------------------------------------------------- | |
42 | ||
43 | /** | |
44 | * Check if the identifier is present inside current scope | |
45 | * @param {Object} scope current scope | |
46 | * @param {string} name To evaluate | |
47 | * @returns {boolean} True if its present | |
48 | * @private | |
49 | */ | |
50 | function findIdentifier(scope, name) { | |
51 | return astUtils.getVariableByName(scope, name) !== null; | |
52 | } | |
53 | ||
54 | //-------------------------------------------------------------------------- | |
55 | // Public API | |
56 | //-------------------------------------------------------------------------- | |
57 | ||
58 | return { | |
59 | ||
60 | LabeledStatement(node) { | |
61 | ||
62 | // Fetch the innermost scope. | |
f2a92ac6 | 63 | const scope = sourceCode.getScope(node); |
eb39fafa DC |
64 | |
65 | /* | |
66 | * Recursively find the identifier walking up the scope, starting | |
67 | * with the innermost scope. | |
68 | */ | |
69 | if (findIdentifier(scope, node.label.name)) { | |
70 | context.report({ | |
71 | node, | |
72 | messageId: "identifierClashWithLabel" | |
73 | }); | |
74 | } | |
75 | } | |
76 | ||
77 | }; | |
78 | ||
79 | } | |
80 | }; |