]>
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
,
14 } = require("./utils/ast-utils");
16 //------------------------------------------------------------------------------
18 //------------------------------------------------------------------------------
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.
25 function isProhibitedIdentifier(name
) {
26 return /^(alert|confirm|prompt)$/u.test(name
);
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.
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]);
39 if (references
.length
=== 1) {
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.
51 function isShadowed(scope
, node
) {
52 const reference
= findReference(scope
, node
);
54 return reference
&& reference
.resolved
&& reference
.resolved
.defs
.length
> 0;
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.
63 function isGlobalThisReferenceOrGlobalWindow(scope
, node
) {
64 if (scope
.type
=== "global" && node
.type
=== "ThisExpression") {
67 if (node
.name
=== "window" || (node
.name
=== "globalThis" && getVariableByName(scope
, "globalThis"))) {
68 return !isShadowed(scope
, node
);
74 //------------------------------------------------------------------------------
76 //------------------------------------------------------------------------------
83 description
: "disallow the use of `alert`, `confirm`, and `prompt`",
84 category
: "Best Practices",
86 url
: "https://eslint.org/docs/rules/no-alert"
92 unexpected
: "Unexpected {{name}}."
98 CallExpression(node
) {
99 const callee
= node
.callee
,
100 currentScope
= context
.getScope();
103 if (callee
.type
=== "Identifier") {
104 const name
= callee
.name
;
106 if (!isShadowed(currentScope
, callee
) && isProhibitedIdentifier(callee
.name
)) {
109 messageId
: "unexpected",
114 } else if (callee
.type
=== "MemberExpression" && isGlobalThisReferenceOrGlobalWindow(currentScope
, callee
.object
)) {
115 const name
= getPropertyName(callee
);
117 if (isProhibitedIdentifier(name
)) {
120 messageId
: "unexpected",