UNPKG

2.05 kBJavaScriptView Raw
1/**
2 * @fileoverview Disallow shadowing of NaN, undefined, and Infinity (ES5 section 15.1.1)
3 * @author Michael Ficarra
4 */
5"use strict";
6
7/**
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
10 * as the global).
11 * @param {eslintScope.Variable} variable The variable to check
12 * @returns {boolean} true if this variable safely shadows `undefined`
13 */
14function 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);
18}
19
20//------------------------------------------------------------------------------
21// Rule Definition
22//------------------------------------------------------------------------------
23
24module.exports = {
25 meta: {
26 type: "suggestion",
27
28 docs: {
29 description: "disallow identifiers from shadowing restricted names",
30 category: "Variables",
31 recommended: true,
32 url: "https://eslint.org/docs/rules/no-shadow-restricted-names"
33 },
34
35 schema: []
36 },
37
38 create(context) {
39
40
41 const RESTRICTED = new Set(["undefined", "NaN", "Infinity", "arguments", "eval"]);
42
43 return {
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)) {
47 context.report({
48 node: variable.defs[0].name,
49 message: "Shadowing of global property '{{idName}}'.",
50 data: {
51 idName: variable.name
52 }
53 });
54 }
55 }
56 }
57 };
58
59 }
60};