版本

no-shadow-restricted-names

禁止标识符遮蔽受限名称

推荐

配置文件中使用来自@eslint/jsrecommended配置启用此规则

ES5 §15.1.1 全局对象的值属性(NaNInfinityundefined)以及严格模式限制标识符 evalarguments 被认为是 JavaScript 中的受限名称。将它们定义为其他含义可能会产生意想不到的后果,并使阅读代码的其他人感到困惑。例如,没有什么可以阻止你写

var undefined = "foo";

那么在同一作用域内使用的任何代码都将不会获得全局 undefined,而是具有非常不同含义的本地版本。

规则详情

此规则的错误代码示例

在 Playground 中打开
/*eslint no-shadow-restricted-names: "error"*/

function NaN(){}

!function(Infinity){};

var undefined = 5;

try {} catch(eval){}
在 Playground 中打开
/*eslint no-shadow-restricted-names: "error"*/

import NaN from "foo";

import { undefined } from "bar";

class Infinity {}

此规则的正确代码示例

在 Playground 中打开
/*eslint no-shadow-restricted-names: "error"*/

var Object;

function f(a, b){}

// Exception: `undefined` may be shadowed if the variable is never assigned a value.
var undefined;
在 Playground 中打开
/*eslint no-shadow-restricted-names: "error"*/

import { undefined as undef } from "bar";

版本

此规则在 ESLint v0.1.4 中引入。

延伸阅读

资源

更改语言