Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-alert.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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("../util/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. type: "suggestion",
  66. docs: {
  67. description: "disallow the use of `alert`, `confirm`, and `prompt`",
  68. category: "Best Practices",
  69. recommended: false,
  70. url: "https://eslint.org/docs/rules/no-alert"
  71. },
  72. schema: [],
  73. messages: {
  74. unexpected: "Unexpected {{name}}."
  75. }
  76. },
  77. create(context) {
  78. return {
  79. CallExpression(node) {
  80. const callee = node.callee,
  81. currentScope = context.getScope();
  82. // without window.
  83. if (callee.type === "Identifier") {
  84. const name = callee.name;
  85. if (!isShadowed(currentScope, callee) && isProhibitedIdentifier(callee.name)) {
  86. context.report({
  87. node,
  88. messageId: "unexpected",
  89. data: { name }
  90. });
  91. }
  92. } else if (callee.type === "MemberExpression" && isGlobalThisReferenceOrGlobalWindow(currentScope, callee.object)) {
  93. const name = getPropertyName(callee);
  94. if (isProhibitedIdentifier(name)) {
  95. context.report({
  96. node,
  97. messageId: "unexpected",
  98. data: { name }
  99. });
  100. }
  101. }
  102. }
  103. };
  104. }
  105. };