code-path.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * @fileoverview A class of the code path.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const CodePathState = require("./code-path-state");
  10. const IdGenerator = require("./id-generator");
  11. //------------------------------------------------------------------------------
  12. // Public Interface
  13. //------------------------------------------------------------------------------
  14. /**
  15. * A code path.
  16. */
  17. class CodePath {
  18. // eslint-disable-next-line jsdoc/require-description
  19. /**
  20. * @param {string} id An identifier.
  21. * @param {CodePath|null} upper The code path of the upper function scope.
  22. * @param {Function} onLooped A callback function to notify looping.
  23. */
  24. constructor(id, upper, onLooped) {
  25. /**
  26. * The identifier of this code path.
  27. * Rules use it to store additional information of each rule.
  28. * @type {string}
  29. */
  30. this.id = id;
  31. /**
  32. * The code path of the upper function scope.
  33. * @type {CodePath|null}
  34. */
  35. this.upper = upper;
  36. /**
  37. * The code paths of nested function scopes.
  38. * @type {CodePath[]}
  39. */
  40. this.childCodePaths = [];
  41. // Initializes internal state.
  42. Object.defineProperty(
  43. this,
  44. "internal",
  45. { value: new CodePathState(new IdGenerator(`${id}_`), onLooped) }
  46. );
  47. // Adds this into `childCodePaths` of `upper`.
  48. if (upper) {
  49. upper.childCodePaths.push(this);
  50. }
  51. }
  52. /**
  53. * Gets the state of a given code path.
  54. * @param {CodePath} codePath A code path to get.
  55. * @returns {CodePathState} The state of the code path.
  56. */
  57. static getState(codePath) {
  58. return codePath.internal;
  59. }
  60. /**
  61. * The initial code path segment.
  62. * @type {CodePathSegment}
  63. */
  64. get initialSegment() {
  65. return this.internal.initialSegment;
  66. }
  67. /**
  68. * Final code path segments.
  69. * This array is a mix of `returnedSegments` and `thrownSegments`.
  70. * @type {CodePathSegment[]}
  71. */
  72. get finalSegments() {
  73. return this.internal.finalSegments;
  74. }
  75. /**
  76. * Final code path segments which is with `return` statements.
  77. * This array contains the last path segment if it's reachable.
  78. * Since the reachable last path returns `undefined`.
  79. * @type {CodePathSegment[]}
  80. */
  81. get returnedSegments() {
  82. return this.internal.returnedForkContext;
  83. }
  84. /**
  85. * Final code path segments which is with `throw` statements.
  86. * @type {CodePathSegment[]}
  87. */
  88. get thrownSegments() {
  89. return this.internal.thrownForkContext;
  90. }
  91. /**
  92. * Current code path segments.
  93. * @type {CodePathSegment[]}
  94. */
  95. get currentSegments() {
  96. return this.internal.currentSegments;
  97. }
  98. /**
  99. * Traverses all segments in this code path.
  100. *
  101. * codePath.traverseSegments(function(segment, controller) {
  102. * // do something.
  103. * });
  104. *
  105. * This method enumerates segments in order from the head.
  106. *
  107. * The `controller` object has two methods.
  108. *
  109. * - `controller.skip()` - Skip the following segments in this branch.
  110. * - `controller.break()` - Skip all following segments.
  111. * @param {Object} [options] Omittable.
  112. * @param {CodePathSegment} [options.first] The first segment to traverse.
  113. * @param {CodePathSegment} [options.last] The last segment to traverse.
  114. * @param {Function} callback A callback function.
  115. * @returns {void}
  116. */
  117. traverseSegments(options, callback) {
  118. let resolvedOptions;
  119. let resolvedCallback;
  120. if (typeof options === "function") {
  121. resolvedCallback = options;
  122. resolvedOptions = {};
  123. } else {
  124. resolvedOptions = options || {};
  125. resolvedCallback = callback;
  126. }
  127. const startSegment = resolvedOptions.first || this.internal.initialSegment;
  128. const lastSegment = resolvedOptions.last;
  129. let item = null;
  130. let index = 0;
  131. let end = 0;
  132. let segment = null;
  133. const visited = Object.create(null);
  134. const stack = [[startSegment, 0]];
  135. let skippedSegment = null;
  136. let broken = false;
  137. const controller = {
  138. skip() {
  139. if (stack.length <= 1) {
  140. broken = true;
  141. } else {
  142. skippedSegment = stack[stack.length - 2][0];
  143. }
  144. },
  145. break() {
  146. broken = true;
  147. }
  148. };
  149. /**
  150. * Checks a given previous segment has been visited.
  151. * @param {CodePathSegment} prevSegment A previous segment to check.
  152. * @returns {boolean} `true` if the segment has been visited.
  153. */
  154. function isVisited(prevSegment) {
  155. return (
  156. visited[prevSegment.id] ||
  157. segment.isLoopedPrevSegment(prevSegment)
  158. );
  159. }
  160. while (stack.length > 0) {
  161. item = stack[stack.length - 1];
  162. segment = item[0];
  163. index = item[1];
  164. if (index === 0) {
  165. // Skip if this segment has been visited already.
  166. if (visited[segment.id]) {
  167. stack.pop();
  168. continue;
  169. }
  170. // Skip if all previous segments have not been visited.
  171. if (segment !== startSegment &&
  172. segment.prevSegments.length > 0 &&
  173. !segment.prevSegments.every(isVisited)
  174. ) {
  175. stack.pop();
  176. continue;
  177. }
  178. // Reset the flag of skipping if all branches have been skipped.
  179. if (skippedSegment && segment.prevSegments.indexOf(skippedSegment) !== -1) {
  180. skippedSegment = null;
  181. }
  182. visited[segment.id] = true;
  183. // Call the callback when the first time.
  184. if (!skippedSegment) {
  185. resolvedCallback.call(this, segment, controller);
  186. if (segment === lastSegment) {
  187. controller.skip();
  188. }
  189. if (broken) {
  190. break;
  191. }
  192. }
  193. }
  194. // Update the stack.
  195. end = segment.nextSegments.length - 1;
  196. if (index < end) {
  197. item[1] += 1;
  198. stack.push([segment.nextSegments[index], 0]);
  199. } else if (index === end) {
  200. item[0] = segment.nextSegments[index];
  201. item[1] = 0;
  202. } else {
  203. stack.pop();
  204. }
  205. }
  206. }
  207. }
  208. module.exports = CodePath;