]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Rule to flag on declaring variables already declared in the outer scope | |
3 | * @author Ilya Volodin | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const astUtils = require("./utils/ast-utils"); | |
13 | ||
8f9d1d4d DC |
14 | //------------------------------------------------------------------------------ |
15 | // Helpers | |
16 | //------------------------------------------------------------------------------ | |
17 | ||
18 | const FUNC_EXPR_NODE_TYPES = new Set(["ArrowFunctionExpression", "FunctionExpression"]); | |
19 | const CALL_EXPR_NODE_TYPE = new Set(["CallExpression"]); | |
20 | const FOR_IN_OF_TYPE = /^For(?:In|Of)Statement$/u; | |
21 | const SENTINEL_TYPE = /^(?:(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression|CatchClause|ImportDeclaration|ExportNamedDeclaration)$/u; | |
22 | ||
eb39fafa DC |
23 | //------------------------------------------------------------------------------ |
24 | // Rule Definition | |
25 | //------------------------------------------------------------------------------ | |
26 | ||
34eeec05 | 27 | /** @type {import('../shared/types').Rule} */ |
eb39fafa DC |
28 | module.exports = { |
29 | meta: { | |
30 | type: "suggestion", | |
31 | ||
32 | docs: { | |
8f9d1d4d | 33 | description: "Disallow variable declarations from shadowing variables declared in the outer scope", |
eb39fafa | 34 | recommended: false, |
f2a92ac6 | 35 | url: "https://eslint.org/docs/latest/rules/no-shadow" |
eb39fafa DC |
36 | }, |
37 | ||
38 | schema: [ | |
39 | { | |
40 | type: "object", | |
41 | properties: { | |
42 | builtinGlobals: { type: "boolean", default: false }, | |
43 | hoist: { enum: ["all", "functions", "never"], default: "functions" }, | |
44 | allow: { | |
45 | type: "array", | |
46 | items: { | |
47 | type: "string" | |
48 | } | |
8f9d1d4d DC |
49 | }, |
50 | ignoreOnInitialization: { type: "boolean", default: false } | |
eb39fafa DC |
51 | }, |
52 | additionalProperties: false | |
53 | } | |
54 | ], | |
55 | ||
56 | messages: { | |
5422a9cc TL |
57 | noShadow: "'{{name}}' is already declared in the upper scope on line {{shadowedLine}} column {{shadowedColumn}}.", |
58 | noShadowGlobal: "'{{name}}' is already a global variable." | |
eb39fafa DC |
59 | } |
60 | }, | |
61 | ||
62 | create(context) { | |
63 | ||
64 | const options = { | |
65 | builtinGlobals: context.options[0] && context.options[0].builtinGlobals, | |
66 | hoist: (context.options[0] && context.options[0].hoist) || "functions", | |
8f9d1d4d DC |
67 | allow: (context.options[0] && context.options[0].allow) || [], |
68 | ignoreOnInitialization: context.options[0] && context.options[0].ignoreOnInitialization | |
eb39fafa | 69 | }; |
f2a92ac6 | 70 | const sourceCode = context.sourceCode; |
eb39fafa | 71 | |
8f9d1d4d DC |
72 | /** |
73 | * Checks whether or not a given location is inside of the range of a given node. | |
74 | * @param {ASTNode} node An node to check. | |
75 | * @param {number} location A location to check. | |
76 | * @returns {boolean} `true` if the location is inside of the range of the node. | |
77 | */ | |
78 | function isInRange(node, location) { | |
79 | return node && node.range[0] <= location && location <= node.range[1]; | |
80 | } | |
81 | ||
82 | /** | |
83 | * Searches from the current node through its ancestry to find a matching node. | |
84 | * @param {ASTNode} node a node to get. | |
85 | * @param {(node: ASTNode) => boolean} match a callback that checks whether or not the node verifies its condition or not. | |
86 | * @returns {ASTNode|null} the matching node. | |
87 | */ | |
88 | function findSelfOrAncestor(node, match) { | |
89 | let currentNode = node; | |
90 | ||
91 | while (currentNode && !match(currentNode)) { | |
92 | currentNode = currentNode.parent; | |
93 | } | |
94 | return currentNode; | |
95 | } | |
96 | ||
97 | /** | |
98 | * Finds function's outer scope. | |
99 | * @param {Scope} scope Function's own scope. | |
100 | * @returns {Scope} Function's outer scope. | |
101 | */ | |
102 | function getOuterScope(scope) { | |
103 | const upper = scope.upper; | |
104 | ||
105 | if (upper.type === "function-expression-name") { | |
106 | return upper.upper; | |
107 | } | |
108 | return upper; | |
109 | } | |
110 | ||
111 | /** | |
112 | * Checks if a variable and a shadowedVariable have the same init pattern ancestor. | |
113 | * @param {Object} variable a variable to check. | |
114 | * @param {Object} shadowedVariable a shadowedVariable to check. | |
115 | * @returns {boolean} Whether or not the variable and the shadowedVariable have the same init pattern ancestor. | |
116 | */ | |
117 | function isInitPatternNode(variable, shadowedVariable) { | |
118 | const outerDef = shadowedVariable.defs[0]; | |
119 | ||
120 | if (!outerDef) { | |
121 | return false; | |
122 | } | |
123 | ||
124 | const { variableScope } = variable.scope; | |
125 | ||
126 | ||
127 | if (!(FUNC_EXPR_NODE_TYPES.has(variableScope.block.type) && getOuterScope(variableScope) === shadowedVariable.scope)) { | |
128 | return false; | |
129 | } | |
130 | ||
131 | const fun = variableScope.block; | |
132 | const { parent } = fun; | |
133 | ||
134 | const callExpression = findSelfOrAncestor( | |
135 | parent, | |
136 | node => CALL_EXPR_NODE_TYPE.has(node.type) | |
137 | ); | |
138 | ||
139 | if (!callExpression) { | |
140 | return false; | |
141 | } | |
142 | ||
143 | let node = outerDef.name; | |
144 | const location = callExpression.range[1]; | |
145 | ||
146 | while (node) { | |
147 | if (node.type === "VariableDeclarator") { | |
148 | if (isInRange(node.init, location)) { | |
149 | return true; | |
150 | } | |
151 | if (FOR_IN_OF_TYPE.test(node.parent.parent.type) && | |
152 | isInRange(node.parent.parent.right, location) | |
153 | ) { | |
154 | return true; | |
155 | } | |
156 | break; | |
157 | } else if (node.type === "AssignmentPattern") { | |
158 | if (isInRange(node.right, location)) { | |
159 | return true; | |
160 | } | |
161 | } else if (SENTINEL_TYPE.test(node.type)) { | |
162 | break; | |
163 | } | |
164 | ||
165 | node = node.parent; | |
166 | } | |
167 | ||
168 | return false; | |
169 | } | |
170 | ||
eb39fafa DC |
171 | /** |
172 | * Check if variable name is allowed. | |
609c276f | 173 | * @param {ASTNode} variable The variable to check. |
eb39fafa DC |
174 | * @returns {boolean} Whether or not the variable name is allowed. |
175 | */ | |
176 | function isAllowed(variable) { | |
8f9d1d4d | 177 | return options.allow.includes(variable.name); |
eb39fafa DC |
178 | } |
179 | ||
180 | /** | |
181 | * Checks if a variable of the class name in the class scope of ClassDeclaration. | |
182 | * | |
183 | * ClassDeclaration creates two variables of its name into its outer scope and its class scope. | |
184 | * So we should ignore the variable in the class scope. | |
185 | * @param {Object} variable The variable to check. | |
186 | * @returns {boolean} Whether or not the variable of the class name in the class scope of ClassDeclaration. | |
187 | */ | |
188 | function isDuplicatedClassNameVariable(variable) { | |
189 | const block = variable.scope.block; | |
190 | ||
191 | return block.type === "ClassDeclaration" && block.id === variable.identifiers[0]; | |
192 | } | |
193 | ||
194 | /** | |
195 | * Checks if a variable is inside the initializer of scopeVar. | |
196 | * | |
197 | * To avoid reporting at declarations such as `var a = function a() {};`. | |
198 | * But it should report `var a = function(a) {};` or `var a = function() { function a() {} };`. | |
199 | * @param {Object} variable The variable to check. | |
200 | * @param {Object} scopeVar The scope variable to look for. | |
201 | * @returns {boolean} Whether or not the variable is inside initializer of scopeVar. | |
202 | */ | |
203 | function isOnInitializer(variable, scopeVar) { | |
204 | const outerScope = scopeVar.scope; | |
205 | const outerDef = scopeVar.defs[0]; | |
206 | const outer = outerDef && outerDef.parent && outerDef.parent.range; | |
207 | const innerScope = variable.scope; | |
208 | const innerDef = variable.defs[0]; | |
209 | const inner = innerDef && innerDef.name.range; | |
210 | ||
211 | return ( | |
212 | outer && | |
8f9d1d4d DC |
213 | inner && |
214 | outer[0] < inner[0] && | |
215 | inner[1] < outer[1] && | |
216 | ((innerDef.type === "FunctionName" && innerDef.node.type === "FunctionExpression") || innerDef.node.type === "ClassExpression") && | |
217 | outerScope === innerScope.upper | |
eb39fafa DC |
218 | ); |
219 | } | |
220 | ||
221 | /** | |
222 | * Get a range of a variable's identifier node. | |
223 | * @param {Object} variable The variable to get. | |
224 | * @returns {Array|undefined} The range of the variable's identifier node. | |
225 | */ | |
226 | function getNameRange(variable) { | |
227 | const def = variable.defs[0]; | |
228 | ||
229 | return def && def.name.range; | |
230 | } | |
231 | ||
5422a9cc TL |
232 | /** |
233 | * Get declared line and column of a variable. | |
234 | * @param {eslint-scope.Variable} variable The variable to get. | |
235 | * @returns {Object} The declared line and column of the variable. | |
236 | */ | |
237 | function getDeclaredLocation(variable) { | |
238 | const identifier = variable.identifiers[0]; | |
239 | let obj; | |
240 | ||
241 | if (identifier) { | |
242 | obj = { | |
243 | global: false, | |
244 | line: identifier.loc.start.line, | |
245 | column: identifier.loc.start.column + 1 | |
246 | }; | |
247 | } else { | |
248 | obj = { | |
249 | global: true | |
250 | }; | |
251 | } | |
252 | return obj; | |
253 | } | |
254 | ||
eb39fafa DC |
255 | /** |
256 | * Checks if a variable is in TDZ of scopeVar. | |
257 | * @param {Object} variable The variable to check. | |
258 | * @param {Object} scopeVar The variable of TDZ. | |
259 | * @returns {boolean} Whether or not the variable is in TDZ of scopeVar. | |
260 | */ | |
261 | function isInTdz(variable, scopeVar) { | |
262 | const outerDef = scopeVar.defs[0]; | |
263 | const inner = getNameRange(variable); | |
264 | const outer = getNameRange(scopeVar); | |
265 | ||
266 | return ( | |
267 | inner && | |
8f9d1d4d DC |
268 | outer && |
269 | inner[1] < outer[0] && | |
eb39fafa | 270 | |
8f9d1d4d DC |
271 | // Excepts FunctionDeclaration if is {"hoist":"function"}. |
272 | (options.hoist !== "functions" || !outerDef || outerDef.node.type !== "FunctionDeclaration") | |
eb39fafa DC |
273 | ); |
274 | } | |
275 | ||
276 | /** | |
277 | * Checks the current context for shadowed variables. | |
278 | * @param {Scope} scope Fixme | |
279 | * @returns {void} | |
280 | */ | |
281 | function checkForShadows(scope) { | |
282 | const variables = scope.variables; | |
283 | ||
284 | for (let i = 0; i < variables.length; ++i) { | |
285 | const variable = variables[i]; | |
286 | ||
287 | // Skips "arguments" or variables of a class name in the class scope of ClassDeclaration. | |
288 | if (variable.identifiers.length === 0 || | |
8f9d1d4d DC |
289 | isDuplicatedClassNameVariable(variable) || |
290 | isAllowed(variable) | |
eb39fafa DC |
291 | ) { |
292 | continue; | |
293 | } | |
294 | ||
295 | // Gets shadowed variable. | |
296 | const shadowed = astUtils.getVariableByName(scope.upper, variable.name); | |
297 | ||
298 | if (shadowed && | |
8f9d1d4d DC |
299 | (shadowed.identifiers.length > 0 || (options.builtinGlobals && "writeable" in shadowed)) && |
300 | !isOnInitializer(variable, shadowed) && | |
301 | !(options.ignoreOnInitialization && isInitPatternNode(variable, shadowed)) && | |
302 | !(options.hoist !== "all" && isInTdz(variable, shadowed)) | |
eb39fafa | 303 | ) { |
5422a9cc TL |
304 | const location = getDeclaredLocation(shadowed); |
305 | const messageId = location.global ? "noShadowGlobal" : "noShadow"; | |
306 | const data = { name: variable.name }; | |
307 | ||
308 | if (!location.global) { | |
309 | data.shadowedLine = location.line; | |
310 | data.shadowedColumn = location.column; | |
311 | } | |
eb39fafa DC |
312 | context.report({ |
313 | node: variable.identifiers[0], | |
5422a9cc TL |
314 | messageId, |
315 | data | |
eb39fafa DC |
316 | }); |
317 | } | |
318 | } | |
319 | } | |
320 | ||
321 | return { | |
f2a92ac6 DC |
322 | "Program:exit"(node) { |
323 | const globalScope = sourceCode.getScope(node); | |
eb39fafa DC |
324 | const stack = globalScope.childScopes.slice(); |
325 | ||
326 | while (stack.length) { | |
327 | const scope = stack.pop(); | |
328 | ||
329 | stack.push(...scope.childScopes); | |
330 | checkForShadows(scope); | |
331 | } | |
332 | } | |
333 | }; | |
334 | ||
335 | } | |
336 | }; |