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-loop-func.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * @fileoverview Rule to flag creation of function inside a loop
  3. * @author Ilya Volodin
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Helpers
  8. //------------------------------------------------------------------------------
  9. /**
  10. * Gets the containing loop node of a specified node.
  11. *
  12. * We don't need to check nested functions, so this ignores those.
  13. * `Scope.through` contains references of nested functions.
  14. *
  15. * @param {ASTNode} node - An AST node to get.
  16. * @returns {ASTNode|null} The containing loop node of the specified node, or
  17. * `null`.
  18. */
  19. function getContainingLoopNode(node) {
  20. for (let currentNode = node; currentNode.parent; currentNode = currentNode.parent) {
  21. const parent = currentNode.parent;
  22. switch (parent.type) {
  23. case "WhileStatement":
  24. case "DoWhileStatement":
  25. return parent;
  26. case "ForStatement":
  27. // `init` is outside of the loop.
  28. if (parent.init !== currentNode) {
  29. return parent;
  30. }
  31. break;
  32. case "ForInStatement":
  33. case "ForOfStatement":
  34. // `right` is outside of the loop.
  35. if (parent.right !== currentNode) {
  36. return parent;
  37. }
  38. break;
  39. case "ArrowFunctionExpression":
  40. case "FunctionExpression":
  41. case "FunctionDeclaration":
  42. // We don't need to check nested functions.
  43. return null;
  44. default:
  45. break;
  46. }
  47. }
  48. return null;
  49. }
  50. /**
  51. * Gets the containing loop node of a given node.
  52. * If the loop was nested, this returns the most outer loop.
  53. *
  54. * @param {ASTNode} node - A node to get. This is a loop node.
  55. * @param {ASTNode|null} excludedNode - A node that the result node should not
  56. * include.
  57. * @returns {ASTNode} The most outer loop node.
  58. */
  59. function getTopLoopNode(node, excludedNode) {
  60. const border = excludedNode ? excludedNode.range[1] : 0;
  61. let retv = node;
  62. let containingLoopNode = node;
  63. while (containingLoopNode && containingLoopNode.range[0] >= border) {
  64. retv = containingLoopNode;
  65. containingLoopNode = getContainingLoopNode(containingLoopNode);
  66. }
  67. return retv;
  68. }
  69. /**
  70. * Checks whether a given reference which refers to an upper scope's variable is
  71. * safe or not.
  72. *
  73. * @param {ASTNode} loopNode - A containing loop node.
  74. * @param {eslint-scope.Reference} reference - A reference to check.
  75. * @returns {boolean} `true` if the reference is safe or not.
  76. */
  77. function isSafe(loopNode, reference) {
  78. const variable = reference.resolved;
  79. const definition = variable && variable.defs[0];
  80. const declaration = definition && definition.parent;
  81. const kind = (declaration && declaration.type === "VariableDeclaration")
  82. ? declaration.kind
  83. : "";
  84. // Variables which are declared by `const` is safe.
  85. if (kind === "const") {
  86. return true;
  87. }
  88. /*
  89. * Variables which are declared by `let` in the loop is safe.
  90. * It's a different instance from the next loop step's.
  91. */
  92. if (kind === "let" &&
  93. declaration.range[0] > loopNode.range[0] &&
  94. declaration.range[1] < loopNode.range[1]
  95. ) {
  96. return true;
  97. }
  98. /*
  99. * WriteReferences which exist after this border are unsafe because those
  100. * can modify the variable.
  101. */
  102. const border = getTopLoopNode(
  103. loopNode,
  104. (kind === "let") ? declaration : null
  105. ).range[0];
  106. /**
  107. * Checks whether a given reference is safe or not.
  108. * The reference is every reference of the upper scope's variable we are
  109. * looking now.
  110. *
  111. * It's safeafe if the reference matches one of the following condition.
  112. * - is readonly.
  113. * - doesn't exist inside a local function and after the border.
  114. *
  115. * @param {eslint-scope.Reference} upperRef - A reference to check.
  116. * @returns {boolean} `true` if the reference is safe.
  117. */
  118. function isSafeReference(upperRef) {
  119. const id = upperRef.identifier;
  120. return (
  121. !upperRef.isWrite() ||
  122. variable.scope.variableScope === upperRef.from.variableScope &&
  123. id.range[0] < border
  124. );
  125. }
  126. return Boolean(variable) && variable.references.every(isSafeReference);
  127. }
  128. //------------------------------------------------------------------------------
  129. // Rule Definition
  130. //------------------------------------------------------------------------------
  131. module.exports = {
  132. meta: {
  133. type: "suggestion",
  134. docs: {
  135. description: "disallow `function` declarations and expressions inside loop statements",
  136. category: "Best Practices",
  137. recommended: false,
  138. url: "https://eslint.org/docs/rules/no-loop-func"
  139. },
  140. schema: []
  141. },
  142. create(context) {
  143. /**
  144. * Reports functions which match the following condition:
  145. *
  146. * - has a loop node in ancestors.
  147. * - has any references which refers to an unsafe variable.
  148. *
  149. * @param {ASTNode} node The AST node to check.
  150. * @returns {boolean} Whether or not the node is within a loop.
  151. */
  152. function checkForLoops(node) {
  153. const loopNode = getContainingLoopNode(node);
  154. if (!loopNode) {
  155. return;
  156. }
  157. const references = context.getScope().through;
  158. if (references.length > 0 &&
  159. !references.every(isSafe.bind(null, loopNode))
  160. ) {
  161. context.report({ node, message: "Don't make functions within a loop." });
  162. }
  163. }
  164. return {
  165. ArrowFunctionExpression: checkForLoops,
  166. FunctionExpression: checkForLoops,
  167. FunctionDeclaration: checkForLoops
  168. };
  169. }
  170. };