no-alert.js 4.2 KB

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