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-return-await.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * @fileoverview Disallows unnecessary `return await`
  3. * @author Jordan Harband
  4. */
  5. "use strict";
  6. const astUtils = require("../util/ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. const message = "Redundant use of `await` on a return value.";
  11. module.exports = {
  12. meta: {
  13. type: "suggestion",
  14. docs: {
  15. description: "disallow unnecessary `return await`",
  16. category: "Best Practices",
  17. recommended: false,
  18. url: "https://eslint.org/docs/rules/no-return-await"
  19. },
  20. fixable: null,
  21. schema: [
  22. ]
  23. },
  24. create(context) {
  25. /**
  26. * Reports a found unnecessary `await` expression.
  27. * @param {ASTNode} node The node representing the `await` expression to report
  28. * @returns {void}
  29. */
  30. function reportUnnecessaryAwait(node) {
  31. context.report({
  32. node: context.getSourceCode().getFirstToken(node),
  33. loc: node.loc,
  34. message
  35. });
  36. }
  37. /**
  38. * Determines whether a thrown error from this node will be caught/handled within this function rather than immediately halting
  39. * this function. For example, a statement in a `try` block will always have an error handler. A statement in
  40. * a `catch` block will only have an error handler if there is also a `finally` block.
  41. * @param {ASTNode} node A node representing a location where an could be thrown
  42. * @returns {boolean} `true` if a thrown error will be caught/handled in this function
  43. */
  44. function hasErrorHandler(node) {
  45. let ancestor = node;
  46. while (!astUtils.isFunction(ancestor) && ancestor.type !== "Program") {
  47. if (ancestor.parent.type === "TryStatement" && (ancestor === ancestor.parent.block || ancestor === ancestor.parent.handler && ancestor.parent.finalizer)) {
  48. return true;
  49. }
  50. ancestor = ancestor.parent;
  51. }
  52. return false;
  53. }
  54. /**
  55. * Checks if a node is placed in tail call position. Once `return` arguments (or arrow function expressions) can be a complex expression,
  56. * an `await` expression could or could not be unnecessary by the definition of this rule. So we're looking for `await` expressions that are in tail position.
  57. * @param {ASTNode} node A node representing the `await` expression to check
  58. * @returns {boolean} The checking result
  59. */
  60. function isInTailCallPosition(node) {
  61. if (node.parent.type === "ArrowFunctionExpression") {
  62. return true;
  63. }
  64. if (node.parent.type === "ReturnStatement") {
  65. return !hasErrorHandler(node.parent);
  66. }
  67. if (node.parent.type === "ConditionalExpression" && (node === node.parent.consequent || node === node.parent.alternate)) {
  68. return isInTailCallPosition(node.parent);
  69. }
  70. if (node.parent.type === "LogicalExpression" && node === node.parent.right) {
  71. return isInTailCallPosition(node.parent);
  72. }
  73. if (node.parent.type === "SequenceExpression" && node === node.parent.expressions[node.parent.expressions.length - 1]) {
  74. return isInTailCallPosition(node.parent);
  75. }
  76. return false;
  77. }
  78. return {
  79. AwaitExpression(node) {
  80. if (isInTailCallPosition(node) && !hasErrorHandler(node)) {
  81. reportUnnecessaryAwait(node);
  82. }
  83. }
  84. };
  85. }
  86. };