版本

no-alert

禁止使用 alertconfirmprompt

JavaScript 的 alertconfirmprompt 函数被广泛认为是对 UI 元素的干扰,应该用更合适的自定义 UI 实现来替代。此外,alert 经常在调试代码时使用,这应该在部署到生产环境之前删除。

alert("here!");

规则详情

此规则旨在捕获应该删除的调试代码和应该替换为不太突兀的自定义 UI 的弹出 UI 元素。因此,当它遇到未被遮蔽的 alertpromptconfirm 函数调用时,它会发出警告。

此规则的错误代码示例

在 Playground 中打开
/*eslint no-alert: "error"*/

alert("here!");

confirm("Are you sure?");

prompt("What's your name?", "John Doe");

此规则的正确代码示例

在 Playground 中打开
/*eslint no-alert: "error"*/

customAlert("Something happened!");

customConfirm("Are you sure?");

customPrompt("Who are you?");

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

版本

此规则在 ESLint v0.0.5 中引入。

资源

更改语言