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.

no-return-await.js 3.8KB

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