prefer-promise-reject-errors.js 5.4 KB

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