Skip to content
← Back to rules

eslint/no-alert Restriction

作用

禁止使用 alertconfirmprompt

为什么不好?

JavaScript 的 alertconfirmprompt 函数被广泛认为是侵入性的 UI 元素,应该被更合适的自定义 UI 实现所取代。 此外,alert 常用于调试代码,在生产环境部署前应将其移除。

示例

此规则的 不正确 代码示例:

js
alert("这里!");

confirm("你确定吗?");

prompt("你叫什么名字?", "John Doe");

此规则的 正确 代码示例:

js
customAlert("发生了一些事情!");

customConfirm("你确定吗?");

customPrompt("你是谁?");

function foo() {
  var alert = myCustomLib.customAlert;
  alert();
}

如何使用

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

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

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

版本

此规则于 v0.9.3 中添加。

参考资料