1 | /**
|
2 | * @fileoverview Rule to flag on declaring variables already declared in the outer scope
|
3 | * @author Ilya Volodin
|
4 | */
|
5 |
|
6 | ;
|
7 |
|
8 | //------------------------------------------------------------------------------
|
9 | // Requirements
|
10 | //------------------------------------------------------------------------------
|
11 |
|
12 | const astUtils = require("./utils/ast-utils");
|
13 |
|
14 | //------------------------------------------------------------------------------
|
15 | // Rule Definition
|
16 | //------------------------------------------------------------------------------
|
17 |
|
18 | module.exports = {
|
19 | meta: {
|
20 | type: "suggestion",
|
21 |
|
22 | docs: {
|
23 | description: "disallow variable declarations from shadowing variables declared in the outer scope",
|
24 | category: "Variables",
|
25 | recommended: false,
|
26 | url: "https://eslint.org/docs/rules/no-shadow"
|
27 | },
|
28 |
|
29 | schema: [
|
30 | {
|
31 | type: "object",
|
32 | properties: {
|
33 | builtinGlobals: { type: "boolean", default: false },
|
34 | hoist: { enum: ["all", "functions", "never"], default: "functions" },
|
35 | allow: {
|
36 | type: "array",
|
37 | items: {
|
38 | type: "string"
|
39 | }
|
40 | }
|
41 | },
|
42 | additionalProperties: false
|
43 | }
|
44 | ]
|
45 | },
|
46 |
|
47 | create(context) {
|
48 |
|
49 | const options = {
|
50 | builtinGlobals: context.options[0] && context.options[0].builtinGlobals,
|
51 | hoist: (context.options[0] && context.options[0].hoist) || "functions",
|
52 | allow: (context.options[0] && context.options[0].allow) || []
|
53 | };
|
54 |
|
55 | /**
|
56 | * Check if variable name is allowed.
|
57 | *
|
58 | * @param {ASTNode} variable The variable to check.
|
59 | * @returns {boolean} Whether or not the variable name is allowed.
|
60 | */
|
61 | function isAllowed(variable) {
|
62 | return options.allow.indexOf(variable.name) !== -1;
|
63 | }
|
64 |
|
65 | /**
|
66 | * Checks if a variable of the class name in the class scope of ClassDeclaration.
|
67 | *
|
68 | * ClassDeclaration creates two variables of its name into its outer scope and its class scope.
|
69 | * So we should ignore the variable in the class scope.
|
70 | *
|
71 | * @param {Object} variable The variable to check.
|
72 | * @returns {boolean} Whether or not the variable of the class name in the class scope of ClassDeclaration.
|
73 | */
|
74 | function isDuplicatedClassNameVariable(variable) {
|
75 | const block = variable.scope.block;
|
76 |
|
77 | return block.type === "ClassDeclaration" && block.id === variable.identifiers[0];
|
78 | }
|
79 |
|
80 | /**
|
81 | * Checks if a variable is inside the initializer of scopeVar.
|
82 | *
|
83 | * To avoid reporting at declarations such as `var a = function a() {};`.
|
84 | * But it should report `var a = function(a) {};` or `var a = function() { function a() {} };`.
|
85 | *
|
86 | * @param {Object} variable The variable to check.
|
87 | * @param {Object} scopeVar The scope variable to look for.
|
88 | * @returns {boolean} Whether or not the variable is inside initializer of scopeVar.
|
89 | */
|
90 | function isOnInitializer(variable, scopeVar) {
|
91 | const outerScope = scopeVar.scope;
|
92 | const outerDef = scopeVar.defs[0];
|
93 | const outer = outerDef && outerDef.parent && outerDef.parent.range;
|
94 | const innerScope = variable.scope;
|
95 | const innerDef = variable.defs[0];
|
96 | const inner = innerDef && innerDef.name.range;
|
97 |
|
98 | return (
|
99 | outer &&
|
100 | inner &&
|
101 | outer[0] < inner[0] &&
|
102 | inner[1] < outer[1] &&
|
103 | ((innerDef.type === "FunctionName" && innerDef.node.type === "FunctionExpression") || innerDef.node.type === "ClassExpression") &&
|
104 | outerScope === innerScope.upper
|
105 | );
|
106 | }
|
107 |
|
108 | /**
|
109 | * Get a range of a variable's identifier node.
|
110 | * @param {Object} variable The variable to get.
|
111 | * @returns {Array|undefined} The range of the variable's identifier node.
|
112 | */
|
113 | function getNameRange(variable) {
|
114 | const def = variable.defs[0];
|
115 |
|
116 | return def && def.name.range;
|
117 | }
|
118 |
|
119 | /**
|
120 | * Checks if a variable is in TDZ of scopeVar.
|
121 | * @param {Object} variable The variable to check.
|
122 | * @param {Object} scopeVar The variable of TDZ.
|
123 | * @returns {boolean} Whether or not the variable is in TDZ of scopeVar.
|
124 | */
|
125 | function isInTdz(variable, scopeVar) {
|
126 | const outerDef = scopeVar.defs[0];
|
127 | const inner = getNameRange(variable);
|
128 | const outer = getNameRange(scopeVar);
|
129 |
|
130 | return (
|
131 | inner &&
|
132 | outer &&
|
133 | inner[1] < outer[0] &&
|
134 |
|
135 | // Excepts FunctionDeclaration if is {"hoist":"function"}.
|
136 | (options.hoist !== "functions" || !outerDef || outerDef.node.type !== "FunctionDeclaration")
|
137 | );
|
138 | }
|
139 |
|
140 | /**
|
141 | * Checks the current context for shadowed variables.
|
142 | * @param {Scope} scope - Fixme
|
143 | * @returns {void}
|
144 | */
|
145 | function checkForShadows(scope) {
|
146 | const variables = scope.variables;
|
147 |
|
148 | for (let i = 0; i < variables.length; ++i) {
|
149 | const variable = variables[i];
|
150 |
|
151 | // Skips "arguments" or variables of a class name in the class scope of ClassDeclaration.
|
152 | if (variable.identifiers.length === 0 ||
|
153 | isDuplicatedClassNameVariable(variable) ||
|
154 | isAllowed(variable)
|
155 | ) {
|
156 | continue;
|
157 | }
|
158 |
|
159 | // Gets shadowed variable.
|
160 | const shadowed = astUtils.getVariableByName(scope.upper, variable.name);
|
161 |
|
162 | if (shadowed &&
|
163 | (shadowed.identifiers.length > 0 || (options.builtinGlobals && "writeable" in shadowed)) &&
|
164 | !isOnInitializer(variable, shadowed) &&
|
165 | !(options.hoist !== "all" && isInTdz(variable, shadowed))
|
166 | ) {
|
167 | context.report({
|
168 | node: variable.identifiers[0],
|
169 | message: "'{{name}}' is already declared in the upper scope.",
|
170 | data: variable
|
171 | });
|
172 | }
|
173 | }
|
174 | }
|
175 |
|
176 | return {
|
177 | "Program:exit"() {
|
178 | const globalScope = context.getScope();
|
179 | const stack = globalScope.childScopes.slice();
|
180 |
|
181 | while (stack.length) {
|
182 | const scope = stack.pop();
|
183 |
|
184 | stack.push(...scope.childScopes);
|
185 | checkForShadows(scope);
|
186 | }
|
187 | }
|
188 | };
|
189 |
|
190 | }
|
191 | };
|