no-alert
禁止使用 alert
、confirm
和 prompt
JavaScript 的 alert
、confirm
和 prompt
函数被广泛认为是对 UI 元素的干扰,应该用更合适的自定义 UI 实现来替代。此外,alert
经常在调试代码时使用,这应该在部署到生产环境之前删除。
alert("here!");
规则详情
此规则旨在捕获应该删除的调试代码和应该替换为不太突兀的自定义 UI 的弹出 UI 元素。因此,当它遇到未被遮蔽的 alert
、prompt
和 confirm
函数调用时,它会发出警告。
此规则的错误代码示例
在 Playground 中打开
/*eslint no-alert: "error"*/
;
;
;
此规则的正确代码示例
在 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 中引入。