Skip to content
← Back to rules

eslint/no-implicit-globals Restriction

它的作用

禁止在全局作用域中声明、全局变量泄漏,以及 对只读全局变量的写入或重新声明。

为什么这很糟糕?

浏览器脚本共享同一个全局作用域。顶层的 varfunction 声明,以及在宽松模式下对未声明变量的赋值, 都会创建可能与其他脚本冲突的全局变量。

示例

以下是此规则的错误代码示例:

js
var foo = 1;
function bar() {}
baz = 1;

以下是此规则的正确代码示例:

js
window.foo = 1;
(function () {
  var bar = 1;
})();

配置

lexicalBindings

type: boolean

default: false

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "no-implicit-globals": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "no-implicit-globals": "error",
  },
});
bash
oxlint --deny no-implicit-globals

版本

此规则是在 v1.65.0 中添加的。

参考资料