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.

callback-return.js 6.4KB

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