2 * @fileoverview Rule to flag use of alert, confirm, prompt
3 * @author Nicholas C. Zakas
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const getPropertyName = require("./utils/ast-utils").getStaticPropertyName;
13 //------------------------------------------------------------------------------
15 //------------------------------------------------------------------------------
18 * Checks if the given name is a prohibited identifier.
19 * @param {string} name The name to check
20 * @returns {boolean} Whether or not the name is prohibited.
22 function isProhibitedIdentifier(name) {
23 return /^(alert|confirm|prompt)$/u.test(name);
27 * Finds the eslint-scope reference in the given scope.
28 * @param {Object} scope The scope to search.
29 * @param {ASTNode} node The identifier node.
30 * @returns {Reference|null} Returns the found reference or null if none were found.
32 function findReference(scope, node) {
33 const references = scope.references.filter(reference => reference.identifier.range[0] === node.range[0] &&
34 reference.identifier.range[1] === node.range[1]);
36 if (references.length === 1) {
43 * Checks if the given identifier node is shadowed in the given scope.
44 * @param {Object} scope The current scope.
45 * @param {string} node The identifier node to check
46 * @returns {boolean} Whether or not the name is shadowed.
48 function isShadowed(scope, node) {
49 const reference = findReference(scope, node);
51 return reference && reference.resolved && reference.resolved.defs.length > 0;
55 * Checks if the given identifier node is a ThisExpression in the global scope or the global window property.
56 * @param {Object} scope The current scope.
57 * @param {string} node The identifier node to check
58 * @returns {boolean} Whether or not the node is a reference to the global object.
60 function isGlobalThisReferenceOrGlobalWindow(scope, node) {
61 if (scope.type === "global" && node.type === "ThisExpression") {
64 if (node.name === "window") {
65 return !isShadowed(scope, node);
71 //------------------------------------------------------------------------------
73 //------------------------------------------------------------------------------
80 description: "disallow the use of `alert`, `confirm`, and `prompt`",
81 category: "Best Practices",
83 url: "https://eslint.org/docs/rules/no-alert"
89 unexpected: "Unexpected {{name}}."
95 CallExpression(node) {
96 const callee = node.callee,
97 currentScope = context.getScope();
100 if (callee.type === "Identifier") {
101 const name = callee.name;
103 if (!isShadowed(currentScope, callee) && isProhibitedIdentifier(callee.name)) {
106 messageId: "unexpected",
111 } else if (callee.type === "MemberExpression" && isGlobalThisReferenceOrGlobalWindow(currentScope, callee.object)) {
112 const name = getPropertyName(callee);
114 if (isProhibitedIdentifier(name)) {
117 messageId: "unexpected",