2 * @fileoverview Disallow shadowing of NaN, undefined, and Infinity (ES5 section 15.1.1)
3 * @author Michael Ficarra
8 * Determines if a variable safely shadows undefined.
9 * This is the case when a variable named `undefined` is never assigned to a value (i.e. it always shares the same value
11 * @param {eslintScope.Variable} variable The variable to check
12 * @returns {boolean} true if this variable safely shadows `undefined`
14 function safelyShadowsUndefined(variable) {
15 return variable.name === "undefined" &&
16 variable.references.every(ref => !ref.isWrite()) &&
17 variable.defs.every(def => def.node.type === "VariableDeclarator" && def.node.init === null);
20 //------------------------------------------------------------------------------
22 //------------------------------------------------------------------------------
29 description: "disallow identifiers from shadowing restricted names",
30 category: "Variables",
32 url: "https://eslint.org/docs/rules/no-shadow-restricted-names"
41 const RESTRICTED = new Set(["undefined", "NaN", "Infinity", "arguments", "eval"]);
44 "VariableDeclaration, :function, CatchClause"(node) {
45 for (const variable of context.getDeclaredVariables(node)) {
46 if (variable.defs.length > 0 && RESTRICTED.has(variable.name) && !safelyShadowsUndefined(variable)) {
48 node: variable.defs[0].name,
49 message: "Shadowing of global property '{{idName}}'.",