Skip to content
← Back to rules

eslint/no-restricted-globals 限制

它的作用

指定在应用程序中不应使用的全局变量名称。

为什么这不好?

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

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

示例

如果我们有如下选项:

json
"no-restricted-globals": ["error", "event"]

以下模式被视为问题:

javascript
function onClick() {
  console.log(event); // 意外的全局变量 'event'。请改用局部参数。
}

使用方法

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 中添加。

参考资料