2 * @fileoverview Rule to flag use of alert, confirm, prompt
3 * @author Nicholas C. Zakas
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
12 getStaticPropertyName: getPropertyName,
15 } = require("./utils/ast-utils");
17 //------------------------------------------------------------------------------
19 //------------------------------------------------------------------------------
22 * Checks if the given name is a prohibited identifier.
23 * @param {string} name The name to check
24 * @returns {boolean} Whether or not the name is prohibited.
26 function isProhibitedIdentifier(name) {
27 return /^(alert|confirm|prompt)$/u.test(name);
31 * Finds the eslint-scope reference in the given scope.
32 * @param {Object} scope The scope to search.
33 * @param {ASTNode} node The identifier node.
34 * @returns {Reference|null} Returns the found reference or null if none were found.
36 function findReference(scope, node) {
37 const references = scope.references.filter(reference => reference.identifier.range[0] === node.range[0] &&
38 reference.identifier.range[1] === node.range[1]);
40 if (references.length === 1) {
47 * Checks if the given identifier node is shadowed in the given scope.
48 * @param {Object} scope The current scope.
49 * @param {string} node The identifier node to check
50 * @returns {boolean} Whether or not the name is shadowed.
52 function isShadowed(scope, node) {
53 const reference = findReference(scope, node);
55 return reference && reference.resolved && reference.resolved.defs.length > 0;
59 * Checks if the given identifier node is a ThisExpression in the global scope or the global window property.
60 * @param {Object} scope The current scope.
61 * @param {string} node The identifier node to check
62 * @returns {boolean} Whether or not the node is a reference to the global object.
64 function isGlobalThisReferenceOrGlobalWindow(scope, node) {
65 if (scope.type === "global" && node.type === "ThisExpression") {
69 node.type === "Identifier" &&
71 node.name === "window" ||
72 (node.name === "globalThis" && getVariableByName(scope, "globalThis"))
75 return !isShadowed(scope, node);
81 //------------------------------------------------------------------------------
83 //------------------------------------------------------------------------------
90 description: "disallow the use of `alert`, `confirm`, and `prompt`",
91 category: "Best Practices",
93 url: "https://eslint.org/docs/rules/no-alert"
99 unexpected: "Unexpected {{name}}."
105 CallExpression(node) {
106 const callee = skipChainExpression(node.callee),
107 currentScope = context.getScope();
110 if (callee.type === "Identifier") {
111 const name = callee.name;
113 if (!isShadowed(currentScope, callee) && isProhibitedIdentifier(callee.name)) {
116 messageId: "unexpected",
121 } else if (callee.type === "MemberExpression" && isGlobalThisReferenceOrGlobalWindow(currentScope, callee.object)) {
122 const name = getPropertyName(callee);
124 if (isProhibitedIdentifier(name)) {
127 messageId: "unexpected",