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.

prefer-promise-reject-errors.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * @fileoverview restrict values that can be used as Promise rejection reasons
  3. * @author Teddy Katz
  4. */
  5. "use strict";
  6. const astUtils = require("../util/ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "require using Error objects as Promise rejection reasons",
  15. category: "Best Practices",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/prefer-promise-reject-errors"
  18. },
  19. fixable: null,
  20. schema: [
  21. {
  22. type: "object",
  23. properties: {
  24. allowEmptyReject: { type: "boolean" }
  25. },
  26. additionalProperties: false
  27. }
  28. ]
  29. },
  30. create(context) {
  31. const ALLOW_EMPTY_REJECT = context.options.length && context.options[0].allowEmptyReject;
  32. //----------------------------------------------------------------------
  33. // Helpers
  34. //----------------------------------------------------------------------
  35. /**
  36. * Checks the argument of a reject() or Promise.reject() CallExpression, and reports it if it can't be an Error
  37. * @param {ASTNode} callExpression A CallExpression node which is used to reject a Promise
  38. * @returns {void}
  39. */
  40. function checkRejectCall(callExpression) {
  41. if (!callExpression.arguments.length && ALLOW_EMPTY_REJECT) {
  42. return;
  43. }
  44. if (
  45. !callExpression.arguments.length ||
  46. !astUtils.couldBeError(callExpression.arguments[0]) ||
  47. callExpression.arguments[0].type === "Identifier" && callExpression.arguments[0].name === "undefined"
  48. ) {
  49. context.report({
  50. node: callExpression,
  51. message: "Expected the Promise rejection reason to be an Error."
  52. });
  53. }
  54. }
  55. /**
  56. * Determines whether a function call is a Promise.reject() call
  57. * @param {ASTNode} node A CallExpression node
  58. * @returns {boolean} `true` if the call is a Promise.reject() call
  59. */
  60. function isPromiseRejectCall(node) {
  61. return node.callee.type === "MemberExpression" &&
  62. node.callee.object.type === "Identifier" && node.callee.object.name === "Promise" &&
  63. node.callee.property.type === "Identifier" && node.callee.property.name === "reject";
  64. }
  65. //----------------------------------------------------------------------
  66. // Public
  67. //----------------------------------------------------------------------
  68. return {
  69. // Check `Promise.reject(value)` calls.
  70. CallExpression(node) {
  71. if (isPromiseRejectCall(node)) {
  72. checkRejectCall(node);
  73. }
  74. },
  75. /*
  76. * Check for `new Promise((resolve, reject) => {})`, and check for reject() calls.
  77. * This function is run on "NewExpression:exit" instead of "NewExpression" to ensure that
  78. * the nodes in the expression already have the `parent` property.
  79. */
  80. "NewExpression:exit"(node) {
  81. if (
  82. node.callee.type === "Identifier" && node.callee.name === "Promise" &&
  83. node.arguments.length && astUtils.isFunction(node.arguments[0]) &&
  84. node.arguments[0].params.length > 1 && node.arguments[0].params[1].type === "Identifier"
  85. ) {
  86. context.getDeclaredVariables(node.arguments[0])
  87. /*
  88. * Find the first variable that matches the second parameter's name.
  89. * If the first parameter has the same name as the second parameter, then the variable will actually
  90. * be "declared" when the first parameter is evaluated, but then it will be immediately overwritten
  91. * by the second parameter. It's not possible for an expression with the variable to be evaluated before
  92. * the variable is overwritten, because functions with duplicate parameters cannot have destructuring or
  93. * default assignments in their parameter lists. Therefore, it's not necessary to explicitly account for
  94. * this case.
  95. */
  96. .find(variable => variable.name === node.arguments[0].params[1].name)
  97. // Get the references to that variable.
  98. .references
  99. // Only check the references that read the parameter's value.
  100. .filter(ref => ref.isRead())
  101. // Only check the references that are used as the callee in a function call, e.g. `reject(foo)`.
  102. .filter(ref => ref.identifier.parent.type === "CallExpression" && ref.identifier === ref.identifier.parent.callee)
  103. // Check the argument of the function call to determine whether it's an Error.
  104. .forEach(ref => checkRejectCall(ref.identifier.parent));
  105. }
  106. }
  107. };
  108. }
  109. };