no-shadow-restricted-names
Disallow identifiers from shadowing restricted names
Using the recommended
config from @eslint/js
in a configuration file
enables this rule
ES2020 §18.1 Value Properties of the Global Object (globalThis
, NaN
, Infinity
, undefined
) as well as strict mode restricted identifiers eval
and arguments
are considered to be restricted names in JavaScript. Defining them to mean something else can have unintended consequences and confuse others reading the code. For example, there’s nothing preventing you from writing:
const undefined = "foo";
Then any code used within the same scope would not get the global undefined
, but rather the local version with a very different meaning.
Rule Details
Examples of incorrect code for this rule:
/*eslint no-shadow-restricted-names: "error"*/
function (){}
!function(){};
const = 5;
try {} catch(){}
/*eslint no-shadow-restricted-names: "error"*/
import from "foo";
import { } from "bar";
class {}
Examples of correct code for this rule:
/*eslint no-shadow-restricted-names: "error"*/
let Object;
function f(a, b){}
// Exception: `undefined` may be shadowed if the variable is never assigned a value.
let undefined;
/*eslint no-shadow-restricted-names: "error"*/
import { undefined as undef } from "bar";
Options
This rule has an object option:
"reportGlobalThis"
:true
(defaultfalse
) reports declarations ofglobalThis
.
reportGlobalThis
Examples of incorrect code for the { "reportGlobalThis": true }
option:
/*eslint no-shadow-restricted-names: ["error", { "reportGlobalThis": true }]*/
const = "foo";
/*eslint no-shadow-restricted-names: ["error", { "reportGlobalThis": true }]*/
function () {}
/*eslint no-shadow-restricted-names: ["error", { "reportGlobalThis": true }]*/
import { } from "bar";
/*eslint no-shadow-restricted-names: ["error", { "reportGlobalThis": true }]*/
class {}
Examples of correct code for the { "reportGlobalThis": true }
option:
/*eslint no-shadow-restricted-names: ["error", { "reportGlobalThis": true }]*/
const foo = globalThis;
function bar() {
return globalThis;
}
import { globalThis as baz } from "foo";
Related Rules
Version
This rule was introduced in ESLint v0.1.4.