]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-alert.js
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 //------------------------------------------------------------------------------
85 /** @type {import('../shared/types').Rule} */
91 description
: "Disallow the use of `alert`, `confirm`, and `prompt`",
93 url
: "https://eslint.org/docs/latest/rules/no-alert"
99 unexpected
: "Unexpected {{name}}."
104 const sourceCode
= context
.sourceCode
;
107 CallExpression(node
) {
108 const callee
= skipChainExpression(node
.callee
),
109 currentScope
= sourceCode
.getScope(node
);
112 if (callee
.type
=== "Identifier") {
113 const name
= callee
.name
;
115 if (!isShadowed(currentScope
, callee
) && isProhibitedIdentifier(callee
.name
)) {
118 messageId
: "unexpected",
123 } else if (callee
.type
=== "MemberExpression" && isGlobalThisReferenceOrGlobalWindow(currentScope
, callee
.object
)) {
124 const name
= getPropertyName(callee
);
126 if (isProhibitedIdentifier(name
)) {
129 messageId
: "unexpected",