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"
38 shadowingRestrictedName: "Shadowing of global property '{{name}}'."
45 const RESTRICTED = new Set(["undefined", "NaN", "Infinity", "arguments", "eval"]);
48 "VariableDeclaration, :function, CatchClause"(node) {
49 for (const variable of context.getDeclaredVariables(node)) {
50 if (variable.defs.length > 0 && RESTRICTED.has(variable.name) && !safelyShadowsUndefined(variable)) {
52 node: variable.defs[0].name,
53 messageId: "shadowingRestrictedName",