2 * @fileoverview A class of the code path.
3 * @author Toru Nagashima
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const CodePathState = require("./code-path-state");
13 const IdGenerator = require("./id-generator");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
24 // eslint-disable-next-line jsdoc/require-description
26 * @param {string} id An identifier.
27 * @param {CodePath|null} upper The code path of the upper function scope.
28 * @param {Function} onLooped A callback function to notify looping.
30 constructor(id, upper, onLooped) {
33 * The identifier of this code path.
34 * Rules use it to store additional information of each rule.
40 * The code path of the upper function scope.
41 * @type {CodePath|null}
46 * The code paths of nested function scopes.
49 this.childCodePaths = [];
51 // Initializes internal state.
52 Object.defineProperty(
55 { value: new CodePathState(new IdGenerator(`${id}_`), onLooped) }
58 // Adds this into `childCodePaths` of `upper`.
60 upper.childCodePaths.push(this);
65 * Gets the state of a given code path.
66 * @param {CodePath} codePath A code path to get.
67 * @returns {CodePathState} The state of the code path.
69 static getState(codePath) {
70 return codePath.internal;
74 * The initial code path segment.
75 * @type {CodePathSegment}
77 get initialSegment() {
78 return this.internal.initialSegment;
82 * Final code path segments.
83 * This array is a mix of `returnedSegments` and `thrownSegments`.
84 * @type {CodePathSegment[]}
87 return this.internal.finalSegments;
91 * Final code path segments which is with `return` statements.
92 * This array contains the last path segment if it's reachable.
93 * Since the reachable last path returns `undefined`.
94 * @type {CodePathSegment[]}
96 get returnedSegments() {
97 return this.internal.returnedForkContext;
101 * Final code path segments which is with `throw` statements.
102 * @type {CodePathSegment[]}
104 get thrownSegments() {
105 return this.internal.thrownForkContext;
109 * Current code path segments.
110 * @type {CodePathSegment[]}
112 get currentSegments() {
113 return this.internal.currentSegments;
117 * Traverses all segments in this code path.
119 * codePath.traverseSegments(function(segment, controller) {
123 * This method enumerates segments in order from the head.
125 * The `controller` object has two methods.
127 * - `controller.skip()` - Skip the following segments in this branch.
128 * - `controller.break()` - Skip all following segments.
129 * @param {Object} [options] Omittable.
130 * @param {CodePathSegment} [options.first] The first segment to traverse.
131 * @param {CodePathSegment} [options.last] The last segment to traverse.
132 * @param {Function} callback A callback function.
135 traverseSegments(options, callback) {
137 let resolvedCallback;
139 if (typeof options === "function") {
140 resolvedCallback = options;
141 resolvedOptions = {};
143 resolvedOptions = options || {};
144 resolvedCallback = callback;
147 const startSegment = resolvedOptions.first || this.internal.initialSegment;
148 const lastSegment = resolvedOptions.last;
154 const visited = Object.create(null);
155 const stack = [[startSegment, 0]];
156 let skippedSegment = null;
160 if (stack.length <= 1) {
163 skippedSegment = stack[stack.length - 2][0];
172 * Checks a given previous segment has been visited.
173 * @param {CodePathSegment} prevSegment A previous segment to check.
174 * @returns {boolean} `true` if the segment has been visited.
176 function isVisited(prevSegment) {
178 visited[prevSegment.id] ||
179 segment.isLoopedPrevSegment(prevSegment)
183 while (stack.length > 0) {
184 item = stack[stack.length - 1];
190 // Skip if this segment has been visited already.
191 if (visited[segment.id]) {
196 // Skip if all previous segments have not been visited.
197 if (segment !== startSegment &&
198 segment.prevSegments.length > 0 &&
199 !segment.prevSegments.every(isVisited)
205 // Reset the flag of skipping if all branches have been skipped.
206 if (skippedSegment && segment.prevSegments.indexOf(skippedSegment) !== -1) {
207 skippedSegment = null;
209 visited[segment.id] = true;
211 // Call the callback when the first time.
212 if (!skippedSegment) {
213 resolvedCallback.call(this, segment, controller);
214 if (segment === lastSegment) {
224 end = segment.nextSegments.length - 1;
227 stack.push([segment.nextSegments[index], 0]);
228 } else if (index === end) {
229 item[0] = segment.nextSegments[index];
238 module.exports = CodePath;