no-alert.js 3.9 KB

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