Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.3KB

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