no-restricted-globals
禁止指定的全局变量
如果你想允许一组全局变量,但仍想禁止其中一些变量,则禁止使用特定的全局变量会很有用。
¥Disallowing usage of specific global variables can be useful if you want to allow a set of global variables, but still want to disallow some of those.
例如,早期的 Internet Explorer 版本将当前 DOM 事件暴露为全局变量 event,但长期以来,使用此变量一直被认为是一种不好的做法。限制这一点将确保此变量不在浏览器代码中使用。
¥For instance, early Internet Explorer versions exposed the current DOM event as a global variable
event, but using this variable has been considered as a bad practice for a long time. Restricting
this will make sure this variable isn’t used in browser code.
规则详情
¥Rule Details
此规则允许你指定不想在应用中使用的全局变量名称。
¥This rule allows you to specify global variable names that you don’t want to use in your application.
选项
¥Options
此规则同时包含字符串和对象选项,用于指定要限制的全局变量。
¥This rule has both string and object options to specify the global variables to restrict.
使用字符串选项,你可以指定要限制为规则选项数组中值的全局变量名称:
¥Using the string option, you can specify the name of a global variable that you want to restrict as a value in the rule options array:
{
    "rules": {
        "no-restricted-globals": ["error", "event", "fdescribe"]
    }
}
或者,该规则还接受对象,其中指定了全局名称和可选的自定义消息:
¥Alternatively, the rule also accepts objects, where the global name and an optional custom message are specified:
{
    "rules": {
        "no-restricted-globals": [
            "error",
            {
                "name": "event",
                "message": "Use local parameter instead."
            },
            {
                "name": "fdescribe",
                "message": "Do not commit fdescribe. Use describe instead."
            }
        ]
    }
}
示例 "event", "fdescribe" 全局变量名称的错误代码示例:
¥Examples of incorrect code for sample "event", "fdescribe" global variable names:
/*global event, fdescribe*/
/*eslint no-restricted-globals: ["error", "event", "fdescribe"]*/
function onClick() {
    console.log(event);
}
fdescribe("foo", function() {
});
示例 "event" 全局变量名称的正确代码示例:
¥Examples of correct code for a sample "event" global variable name:
/*global event*/
/*eslint no-restricted-globals: ["error", "event"]*/
import event from "event-module";
/*global event*/
/*eslint no-restricted-globals: ["error", "event"]*/
const event = 1;
示例 "event" 全局变量名称的错误代码示例以及自定义错误消息:
¥Examples of incorrect code for a sample "event" global variable name, along with a custom error message:
/*global event*/
/* eslint no-restricted-globals: ["error", { name: "event", message: "Use local parameter instead." }] */
function onClick() {
    console.log(event);    // Unexpected global variable 'event'. Use local parameter instead.
}
globals
一个对象选项,其值是一个数组,其中包含要限制的全局变量的名称。
¥An object option whose value is an array containing the names of the globals you want to restrict.
"event" 和 "fdescribe" 全局变量名的错误代码示例:
¥Examples of incorrect code for "event" and "fdescribe" global variable names:
/*global event, fdescribe*/
/*eslint no-restricted-globals: ["error", { globals: ["event", "fdescribe"] }]*/
function onClick() {
    console.log(event);
}
fdescribe("foo", function() {
});
特定全局对象的自定义消息也可以在 globals 数组中使用 name 和 message 对象指定:
¥Custom messages for a particular global can also be specified in globals array using objects with name and message:
"event" 全局变量名的错误代码示例,以及自定义错误消息:
¥Examples of incorrect code for an "event" global variable name, along with a custom error message:
/*global event*/
/* eslint no-restricted-globals: ["error", { globals: [{ name: "event", message: "Use local parameter instead." }] }] */
function onClick() {
    console.log(event);
}
checkGlobalObject
一个布尔选项,用于检测通过全局对象访问的受限全局变量。默认为 false。
¥A boolean option that enables detection of restricted globals accessed via global objects. Default is false.
checkGlobalObject: true 选项的错误代码示例:
¥Examples of incorrect code for checkGlobalObject: true option:
/*global globalThis, self, window*/
/*eslint no-restricted-globals: ["error", { globals: ["Promise"], checkGlobalObject: true }]*/
globalThis.Promise
self.Promise
window.Promise
globalObjects
一个数组选项,用于指定在启用 checkGlobalObject 时要检查的其他全局对象名称。默认情况下,该规则会检查以下全局对象:globalThis、self 和 window。
¥An array option that specifies additional global object names to check when checkGlobalObject is enabled. By default, the rule checks these global objects: globalThis, self, and window.
globalObjects 选项的错误代码示例:
¥Examples of incorrect code for globalObjects option:
/*global globalThis, self, window, myGlobal*/
/*eslint no-restricted-globals: ["error", {
    globals: ["Promise"],
    checkGlobalObject: true,
    globalObjects: ["myGlobal"]
}]*/
globalThis.Promise
self.Promise
window.Promise
myGlobal.Promise;
此规则忽略 TypeScript 类型注释中使用的受限全局变量(例如类型引用、接口继承或类实现)。
¥Restricted globals used in TypeScript type annotations—such as type references, interface inheritance, or class implementations—are ignored by this rule.
“Promise”、“事件” 和 “Window” 全局变量名的正确 TypeScript 代码示例:
¥Examples of correct TypeScript code for “Promise”, “Event”, and “Window” global variable names:
/*eslint no-restricted-globals: ["error", "Promise", "Event", "Window"]*/
const fetchData: Promise<string> = fetchString();
interface CustomEvent extends Event {}
class CustomWindow implements Window {}
function handleClick(event: Event) {
  console.log(event);
}
相关规则
版本
此规则是在 ESLint v2.3.0 中引入。