noAlert
Summary
Section titled “Summary”- Diagnostic Category:
lint/suspicious/noAlert
- This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Same as
no-alert
- Same as
Description
Section titled “Description”Disallow the use of alert
, confirm
, and prompt
.
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
Section titled “Examples”Invalid
Section titled “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.
customAlert("Something happened!");
customConfirm("Are you sure?");
customPrompt("Who are you?");
function foo() { const alert = myCustomLib.customAlert; alert();}
How to configure
Section titled “How to configure”{ "linter": { "rules": { "suspicious": { "noAlert": "error" } } }}