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.

callback-return.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * @fileoverview Enforce return after a callback.
  3. * @author Jamund Ferguson
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "suggestion",
  12. docs: {
  13. description: "require `return` statements after callbacks",
  14. category: "Node.js and CommonJS",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/callback-return"
  17. },
  18. schema: [{
  19. type: "array",
  20. items: { type: "string" }
  21. }],
  22. messages: {
  23. missingReturn: "Expected return with your callback function."
  24. }
  25. },
  26. create(context) {
  27. const callbacks = context.options[0] || ["callback", "cb", "next"],
  28. sourceCode = context.getSourceCode();
  29. //--------------------------------------------------------------------------
  30. // Helpers
  31. //--------------------------------------------------------------------------
  32. /**
  33. * Find the closest parent matching a list of types.
  34. * @param {ASTNode} node The node whose parents we are searching
  35. * @param {Array} types The node types to match
  36. * @returns {ASTNode} The matched node or undefined.
  37. */
  38. function findClosestParentOfType(node, types) {
  39. if (!node.parent) {
  40. return null;
  41. }
  42. if (types.indexOf(node.parent.type) === -1) {
  43. return findClosestParentOfType(node.parent, types);
  44. }
  45. return node.parent;
  46. }
  47. /**
  48. * Check to see if a node contains only identifers
  49. * @param {ASTNode} node The node to check
  50. * @returns {boolean} Whether or not the node contains only identifers
  51. */
  52. function containsOnlyIdentifiers(node) {
  53. if (node.type === "Identifier") {
  54. return true;
  55. }
  56. if (node.type === "MemberExpression") {
  57. if (node.object.type === "Identifier") {
  58. return true;
  59. }
  60. if (node.object.type === "MemberExpression") {
  61. return containsOnlyIdentifiers(node.object);
  62. }
  63. }
  64. return false;
  65. }
  66. /**
  67. * Check to see if a CallExpression is in our callback list.
  68. * @param {ASTNode} node The node to check against our callback names list.
  69. * @returns {boolean} Whether or not this function matches our callback name.
  70. */
  71. function isCallback(node) {
  72. return containsOnlyIdentifiers(node.callee) && callbacks.indexOf(sourceCode.getText(node.callee)) > -1;
  73. }
  74. /**
  75. * Determines whether or not the callback is part of a callback expression.
  76. * @param {ASTNode} node The callback node
  77. * @param {ASTNode} parentNode The expression node
  78. * @returns {boolean} Whether or not this is part of a callback expression
  79. */
  80. function isCallbackExpression(node, parentNode) {
  81. // ensure the parent node exists and is an expression
  82. if (!parentNode || parentNode.type !== "ExpressionStatement") {
  83. return false;
  84. }
  85. // cb()
  86. if (parentNode.expression === node) {
  87. return true;
  88. }
  89. // special case for cb && cb() and similar
  90. if (parentNode.expression.type === "BinaryExpression" || parentNode.expression.type === "LogicalExpression") {
  91. if (parentNode.expression.right === node) {
  92. return true;
  93. }
  94. }
  95. return false;
  96. }
  97. //--------------------------------------------------------------------------
  98. // Public
  99. //--------------------------------------------------------------------------
  100. return {
  101. CallExpression(node) {
  102. // if we're not a callback we can return
  103. if (!isCallback(node)) {
  104. return;
  105. }
  106. // find the closest block, return or loop
  107. const closestBlock = findClosestParentOfType(node, ["BlockStatement", "ReturnStatement", "ArrowFunctionExpression"]) || {};
  108. // if our parent is a return we know we're ok
  109. if (closestBlock.type === "ReturnStatement") {
  110. return;
  111. }
  112. // arrow functions don't always have blocks and implicitly return
  113. if (closestBlock.type === "ArrowFunctionExpression") {
  114. return;
  115. }
  116. // block statements are part of functions and most if statements
  117. if (closestBlock.type === "BlockStatement") {
  118. // find the last item in the block
  119. const lastItem = closestBlock.body[closestBlock.body.length - 1];
  120. // if the callback is the last thing in a block that might be ok
  121. if (isCallbackExpression(node, lastItem)) {
  122. const parentType = closestBlock.parent.type;
  123. // but only if the block is part of a function
  124. if (parentType === "FunctionExpression" ||
  125. parentType === "FunctionDeclaration" ||
  126. parentType === "ArrowFunctionExpression"
  127. ) {
  128. return;
  129. }
  130. }
  131. // ending a block with a return is also ok
  132. if (lastItem.type === "ReturnStatement") {
  133. // but only if the callback is immediately before
  134. if (isCallbackExpression(node, closestBlock.body[closestBlock.body.length - 2])) {
  135. return;
  136. }
  137. }
  138. }
  139. // as long as you're the child of a function at this point you should be asked to return
  140. if (findClosestParentOfType(node, ["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"])) {
  141. context.report({ node, messageId: "missingReturn" });
  142. }
  143. }
  144. };
  145. }
  146. };