no-restricted-globals

禁止指定的全局变量

如果您想通过启用环境来允许一组全局变量,但仍想禁止其中一些变量,则禁止使用特定的全局变量会很有用。

例如,早期的 Internet Explorer 版本将当前 DOM 事件暴露为全局变量 event,但长期以来,使用此变量一直被认为是一种不好的做法。限制这一点将确保此变量不在浏览器代码中使用。

规则详情

此规则允许您指定不想在应用程序中使用的全局变量名称。

选项

此规则采用字符串列表,其中每个字符串都是要限制的全局字符串:

{
    "rules": {
        "no-restricted-globals": ["error", "event", "fdescribe"]
    }
}

Zd0HnIMOdv/+xIw6J0LrWKiftaVe99glff6KWHUHkBJNOCPqaVUaVOael0qn5AOV5s/ySD79cvu6Y0mKdVjNVmt4M5TXgGu/xNh1bjFLO2WoVfvJwvWPlg5Hkopb4q/H

{
    "rules": {
        "no-restricted-globals": [
            "error",
            {
                "name": "event",
                "message": "Use local parameter instead."
            },
            {
                "name": "fdescribe",
                "message": "Do not commit fdescribe. Use describe instead."
            }
        ]
    }
}

8iqUi9zFhIcmKMcZcDZ6Lwax2RLnsBilx5V5/XGVEggU51Xu57s4xmH/X+M0xM1k1S9VHNbAq+StBTjWOx9mSIYF2M93zE/qlhKRWNuT0mZK0pxCi9mQnIVHYQhfhTPh

/*global event, fdescribe*/
/*eslint no-restricted-globals: ["error", "event", "fdescribe"]*/

function onClick() {
    console.log(event);
}

fdescribe("foo", function() {
});

8iqUi9zFhIcmKMcZcDZ6L2dJY8dxnosqAHLbvOYcDq3kYfh+R1OOcyFDSUlIwxP1LgrYeLo1Xk9NAkH+4YmQ8CjVT03hRePW+SqixKDjOkM=

/*global event*/
/*eslint no-restricted-globals: ["error", "event"]*/

import event from "event-module";
/*global event*/
/*eslint no-restricted-globals: ["error", "event"]*/

var event = 1;

8iqUi9zFhIcmKMcZcDZ6L2dJY8dxnosqAHLbvOYcDq3kYfh+R1OOcyFDSUlIwxP1pL7yTdW0mVXLeG8obhaeNBbWcsvTFnWFJ0QPBDh/3c02uxjODoUeFYKBDeGLc6dTuEmPqQoV4smbtx4rO7OtNw==

/*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.
}