Skip to content

noAlert

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"suspicious": {
"noAlert": "error"
}
}
}
}

¥Description

禁止使用 alertconfirmprompt

¥Disallow the use of alert, confirm, and prompt.

JavaScript 的 alertconfirmprompt 函数被普遍认为作为 UI 元素过于突兀,应该被更合适的自定义 UI 实现所取代。此外,alert 通常用于调试代码,在部署到生产环境之前应该移除。

¥JavaScript’s alert, confirm, and prompt functions are widely considered to be obtrusive as UI elements and should be replaced by a more appropriate custom UI implementation. Furthermore, alert is often used while debugging code, which should be removed before deployment to production.

¥Examples

¥Invalid

alert("here!");
code-block.js:1:1 lint/suspicious/noAlert ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected alert

> 1 │ alert(“here!”);
^^^^^^^^^^^^^^
2 │

The alert function is considered to be obtrusive. Replace it with a custom UI implementation.

confirm("Are you sure?");
code-block.js:1:1 lint/suspicious/noAlert ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected confirm

> 1 │ confirm(“Are you sure?”);
^^^^^^^^^^^^^^^^^^^^^^^^
2 │

The confirm function is considered to be obtrusive. Replace it with a custom UI implementation.

prompt("What's your name?", "John Doe");
code-block.js:1:1 lint/suspicious/noAlert ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected prompt

> 1 │ prompt(“What’s your name?”, “John Doe”);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

The prompt function is considered to be obtrusive. Replace it with a custom UI implementation.

¥Valid

customAlert("Something happened!");
customConfirm("Are you sure?");
customPrompt("Who are you?");
function foo() {
const alert = myCustomLib.customAlert;
alert();
}

¥Related links