eslint/no-restricted-globals Restriction
它的作用
此规则允许你指定不想在应用中使用的全局变量名。
为什么这不好?
如果你想通过启用某个环境来允许一组全局变量,但仍然想禁止其中的一些,那么禁止使用特定的全局变量会很有用。
例如,早期 Internet Explorer 版本会将当前 DOM 事件暴露为全局变量 event,但长期以来,使用这个变量一直被认为是不好的做法。限制它可以确保在浏览器代码中不会使用这个变量。
示例
如果我们有如下选项:
json
"no-restricted-globals": ["error", "event"]以下模式被视为问题:
javascript
function onClick() {
console.log(event); // 意外的全局变量 'event'。请改用局部参数。
}配置
此规则接受一个具有以下属性的配置对象:
restrictedGlobals
type: Record<string, string>
default: {}
格式为 { "name": "event", "message": "请改用局部参数。" } 的对象,用于定义哪些全局变量 被限制使用。
如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"no-restricted-globals": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"no-restricted-globals": "error",
},
});bash
oxlint --deny no-restricted-globals版本
此规则在 v0.4.0 中添加。
