]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-alert.js
first commit
[pve-eslint.git] / eslint / lib / rules / no-alert.js
1 /**
2 * @fileoverview Rule to flag use of alert, confirm, prompt
3 * @author Nicholas C. Zakas
4 */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Requirements
9 //------------------------------------------------------------------------------
10
11 const {
12 getStaticPropertyName: getPropertyName,
13 getVariableByName
14 } = require("./utils/ast-utils");
15
16 //------------------------------------------------------------------------------
17 // Helpers
18 //------------------------------------------------------------------------------
19
20 /**
21 * Checks if the given name is a prohibited identifier.
22 * @param {string} name The name to check
23 * @returns {boolean} Whether or not the name is prohibited.
24 */
25 function isProhibitedIdentifier(name) {
26 return /^(alert|confirm|prompt)$/u.test(name);
27 }
28
29 /**
30 * Finds the eslint-scope reference in the given scope.
31 * @param {Object} scope The scope to search.
32 * @param {ASTNode} node The identifier node.
33 * @returns {Reference|null} Returns the found reference or null if none were found.
34 */
35 function findReference(scope, node) {
36 const references = scope.references.filter(reference => reference.identifier.range[0] === node.range[0] &&
37 reference.identifier.range[1] === node.range[1]);
38
39 if (references.length === 1) {
40 return references[0];
41 }
42 return null;
43 }
44
45 /**
46 * Checks if the given identifier node is shadowed in the given scope.
47 * @param {Object} scope The current scope.
48 * @param {string} node The identifier node to check
49 * @returns {boolean} Whether or not the name is shadowed.
50 */
51 function isShadowed(scope, node) {
52 const reference = findReference(scope, node);
53
54 return reference && reference.resolved && reference.resolved.defs.length > 0;
55 }
56
57 /**
58 * Checks if the given identifier node is a ThisExpression in the global scope or the global window property.
59 * @param {Object} scope The current scope.
60 * @param {string} node The identifier node to check
61 * @returns {boolean} Whether or not the node is a reference to the global object.
62 */
63 function isGlobalThisReferenceOrGlobalWindow(scope, node) {
64 if (scope.type === "global" && node.type === "ThisExpression") {
65 return true;
66 }
67 if (node.name === "window" || (node.name === "globalThis" && getVariableByName(scope, "globalThis"))) {
68 return !isShadowed(scope, node);
69 }
70
71 return false;
72 }
73
74 //------------------------------------------------------------------------------
75 // Rule Definition
76 //------------------------------------------------------------------------------
77
78 module.exports = {
79 meta: {
80 type: "suggestion",
81
82 docs: {
83 description: "disallow the use of `alert`, `confirm`, and `prompt`",
84 category: "Best Practices",
85 recommended: false,
86 url: "https://eslint.org/docs/rules/no-alert"
87 },
88
89 schema: [],
90
91 messages: {
92 unexpected: "Unexpected {{name}}."
93 }
94 },
95
96 create(context) {
97 return {
98 CallExpression(node) {
99 const callee = node.callee,
100 currentScope = context.getScope();
101
102 // without window.
103 if (callee.type === "Identifier") {
104 const name = callee.name;
105
106 if (!isShadowed(currentScope, callee) && isProhibitedIdentifier(callee.name)) {
107 context.report({
108 node,
109 messageId: "unexpected",
110 data: { name }
111 });
112 }
113
114 } else if (callee.type === "MemberExpression" && isGlobalThisReferenceOrGlobalWindow(currentScope, callee.object)) {
115 const name = getPropertyName(callee);
116
117 if (isProhibitedIdentifier(name)) {
118 context.report({
119 node,
120 messageId: "unexpected",
121 data: { name }
122 });
123 }
124 }
125 }
126 };
127
128 }
129 };