UNPKG

4.01 kBJavaScriptView Raw
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
11const getPropertyName = require("../ast-utils").getStaticPropertyName;
12
13//------------------------------------------------------------------------------
14// Helpers
15//------------------------------------------------------------------------------
16
17/**
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.
21 */
22function isProhibitedIdentifier(name) {
23 return /^(alert|confirm|prompt)$/.test(name);
24}
25
26/**
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.
31 */
32function 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]);
35
36 if (references.length === 1) {
37 return references[0];
38 }
39 return null;
40}
41
42/**
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.
47 */
48function isShadowed(scope, node) {
49 const reference = findReference(scope, node);
50
51 return reference && reference.resolved && reference.resolved.defs.length > 0;
52}
53
54/**
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.
59 */
60function isGlobalThisReferenceOrGlobalWindow(scope, node) {
61 if (scope.type === "global" && node.type === "ThisExpression") {
62 return true;
63 }
64 if (node.name === "window") {
65 return !isShadowed(scope, node);
66 }
67
68 return false;
69}
70
71//------------------------------------------------------------------------------
72// Rule Definition
73//------------------------------------------------------------------------------
74
75module.exports = {
76 meta: {
77 docs: {
78 description: "disallow the use of `alert`, `confirm`, and `prompt`",
79 category: "Best Practices",
80 recommended: false,
81 url: "https://eslint.org/docs/rules/no-alert"
82 },
83
84 schema: [],
85
86 messages: {
87 unexpected: "Unexpected {{name}}."
88 }
89 },
90
91 create(context) {
92 return {
93 CallExpression(node) {
94 const callee = node.callee,
95 currentScope = context.getScope();
96
97 // without window.
98 if (callee.type === "Identifier") {
99 const name = callee.name;
100
101 if (!isShadowed(currentScope, callee) && isProhibitedIdentifier(callee.name)) {
102 context.report({
103 node,
104 messageId: "unexpected",
105 data: { name }
106 });
107 }
108
109 } else if (callee.type === "MemberExpression" && isGlobalThisReferenceOrGlobalWindow(currentScope, callee.object)) {
110 const name = getPropertyName(callee);
111
112 if (isProhibitedIdentifier(name)) {
113 context.report({
114 node,
115 messageId: "unexpected",
116 data: { name }
117 });
118 }
119 }
120
121 }
122 };
123
124 }
125};