Skip to content

noRestrictedGlobals

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"style": {
"noRestrictedGlobals": "error"
}
}
}
}

¥Description

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

¥This rule allows you to specify global variable names that you don’t want to use in your application.

此规则始终禁止引用全局标识符 errorevent

¥References to the global identifiers error and event are always disallowed by this rule.

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

¥Disallowing usage of specific global variables can be useful if you want to allow a set of global variables by enabling an environment but still want to disallow some of those.

¥Examples

¥Invalid

console.log(event)
code-block.js:1:13 lint/style/noRestrictedGlobals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Do not use the global variable event.

> 1 │ console.log(event)
^^^^^
2 │

Use a local variable instead.

¥Valid

function f(event) {
console.log(event)
}

¥Options

使用选项指定要在源代码中限制的其他全局变量。

¥Use the options to specify additional globals that you want to restrict in your source code.

biome.json
{
"linter": {
"rules": {
"style": {
"noRestrictedGlobals": {
"options": {
"deniedGlobals": {
"$": "jQuery is not allowed. Use native DOM manipulation instead.",
"MooTools": "Do not use MooTools, use MeowTools instead."
}
}
}
}
}
}
}

在上面的示例中,如果尝试在不创建局部变量的情况下使用 $MooTools,规则将发出诊断信息。

¥In the example above, the rule will emit a diagnostics if tried to use $ or MooTools without creating a local variable.

¥Related links