]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/linter/code-path-analysis/code-path-state.js
2 * @fileoverview A class to manage state of generating a code path.
3 * @author Toru Nagashima
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const CodePathSegment
= require("./code-path-segment"),
13 ForkContext
= require("./fork-context");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
20 * Adds given segments into the `dest` array.
21 * If the `others` array does not includes the given segments, adds to the `all`
24 * This adds only reachable and used segments.
25 * @param {CodePathSegment[]} dest A destination array (`returnedSegments` or `thrownSegments`).
26 * @param {CodePathSegment[]} others Another destination array (`returnedSegments` or `thrownSegments`).
27 * @param {CodePathSegment[]} all The unified destination array (`finalSegments`).
28 * @param {CodePathSegment[]} segments Segments to add.
31 function addToReturnedOrThrown(dest
, others
, all
, segments
) {
32 for (let i
= 0; i
< segments
.length
; ++i
) {
33 const segment
= segments
[i
];
36 if (others
.indexOf(segment
) === -1) {
43 * Gets a loop-context for a `continue` statement.
44 * @param {CodePathState} state A state to get.
45 * @param {string} label The label of a `continue` statement.
46 * @returns {LoopContext} A loop-context for a `continue` statement.
48 function getContinueContext(state
, label
) {
50 return state
.loopContext
;
53 let context
= state
.loopContext
;
56 if (context
.label
=== label
) {
59 context
= context
.upper
;
62 /* istanbul ignore next: foolproof (syntax error) */
67 * Gets a context for a `break` statement.
68 * @param {CodePathState} state A state to get.
69 * @param {string} label The label of a `break` statement.
70 * @returns {LoopContext|SwitchContext} A context for a `break` statement.
72 function getBreakContext(state
, label
) {
73 let context
= state
.breakContext
;
76 if (label
? context
.label
=== label
: context
.breakable
) {
79 context
= context
.upper
;
82 /* istanbul ignore next: foolproof (syntax error) */
87 * Gets a context for a `return` statement.
88 * @param {CodePathState} state A state to get.
89 * @returns {TryContext|CodePathState} A context for a `return` statement.
91 function getReturnContext(state
) {
92 let context
= state
.tryContext
;
95 if (context
.hasFinalizer
&& context
.position
!== "finally") {
98 context
= context
.upper
;
105 * Gets a context for a `throw` statement.
106 * @param {CodePathState} state A state to get.
107 * @returns {TryContext|CodePathState} A context for a `throw` statement.
109 function getThrowContext(state
) {
110 let context
= state
.tryContext
;
113 if (context
.position
=== "try" ||
114 (context
.hasFinalizer
&& context
.position
=== "catch")
118 context
= context
.upper
;
125 * Removes a given element from a given array.
126 * @param {any[]} xs An array to remove the specific element.
127 * @param {any} x An element to be removed.
130 function remove(xs
, x
) {
131 xs
.splice(xs
.indexOf(x
), 1);
135 * Disconnect given segments.
137 * This is used in a process for switch statements.
138 * If there is the "default" chunk before other cases, the order is different
139 * between node's and running's.
140 * @param {CodePathSegment[]} prevSegments Forward segments to disconnect.
141 * @param {CodePathSegment[]} nextSegments Backward segments to disconnect.
144 function removeConnection(prevSegments
, nextSegments
) {
145 for (let i
= 0; i
< prevSegments
.length
; ++i
) {
146 const prevSegment
= prevSegments
[i
];
147 const nextSegment
= nextSegments
[i
];
149 remove(prevSegment
.nextSegments
, nextSegment
);
150 remove(prevSegment
.allNextSegments
, nextSegment
);
151 remove(nextSegment
.prevSegments
, prevSegment
);
152 remove(nextSegment
.allPrevSegments
, prevSegment
);
157 * Creates looping path.
158 * @param {CodePathState} state The instance.
159 * @param {CodePathSegment[]} unflattenedFromSegments Segments which are source.
160 * @param {CodePathSegment[]} unflattenedToSegments Segments which are destination.
163 function makeLooped(state
, unflattenedFromSegments
, unflattenedToSegments
) {
164 const fromSegments
= CodePathSegment
.flattenUnusedSegments(unflattenedFromSegments
);
165 const toSegments
= CodePathSegment
.flattenUnusedSegments(unflattenedToSegments
);
167 const end
= Math
.min(fromSegments
.length
, toSegments
.length
);
169 for (let i
= 0; i
< end
; ++i
) {
170 const fromSegment
= fromSegments
[i
];
171 const toSegment
= toSegments
[i
];
173 if (toSegment
.reachable
) {
174 fromSegment
.nextSegments
.push(toSegment
);
176 if (fromSegment
.reachable
) {
177 toSegment
.prevSegments
.push(fromSegment
);
179 fromSegment
.allNextSegments
.push(toSegment
);
180 toSegment
.allPrevSegments
.push(fromSegment
);
182 if (toSegment
.allPrevSegments
.length
>= 2) {
183 CodePathSegment
.markPrevSegmentAsLooped(toSegment
, fromSegment
);
186 state
.notifyLooped(fromSegment
, toSegment
);
191 * Finalizes segments of `test` chunk of a ForStatement.
193 * - Adds `false` paths to paths which are leaving from the loop.
194 * - Sets `true` paths to paths which go to the body.
195 * @param {LoopContext} context A loop context to modify.
196 * @param {ChoiceContext} choiceContext A choice context of this loop.
197 * @param {CodePathSegment[]} head The current head paths.
200 function finalizeTestSegmentsOfFor(context
, choiceContext
, head
) {
201 if (!choiceContext
.processed
) {
202 choiceContext
.trueForkContext
.add(head
);
203 choiceContext
.falseForkContext
.add(head
);
206 if (context
.test
!== true) {
207 context
.brokenForkContext
.addAll(choiceContext
.falseForkContext
);
209 context
.endOfTestSegments
= choiceContext
.trueForkContext
.makeNext(0, -1);
212 //------------------------------------------------------------------------------
214 //------------------------------------------------------------------------------
217 * A class which manages state to analyze code paths.
219 class CodePathState
{
221 // eslint-disable-next-line jsdoc/require-description
223 * @param {IdGenerator} idGenerator An id generator to generate id for code
225 * @param {Function} onLooped A callback function to notify looping.
227 constructor(idGenerator
, onLooped
) {
228 this.idGenerator
= idGenerator
;
229 this.notifyLooped
= onLooped
;
230 this.forkContext
= ForkContext
.newRoot(idGenerator
);
231 this.choiceContext
= null;
232 this.switchContext
= null;
233 this.tryContext
= null;
234 this.loopContext
= null;
235 this.breakContext
= null;
237 this.currentSegments
= [];
238 this.initialSegment
= this.forkContext
.head
[0];
240 // returnedSegments and thrownSegments push elements into finalSegments also.
241 const final
= this.finalSegments
= [];
242 const returned
= this.returnedForkContext
= [];
243 const thrown
= this.thrownForkContext
= [];
245 returned
.add
= addToReturnedOrThrown
.bind(null, returned
, thrown
, final
);
246 thrown
.add
= addToReturnedOrThrown
.bind(null, thrown
, returned
, final
);
251 * @type {CodePathSegment[]}
254 return this.forkContext
.head
;
258 * The parent forking context.
259 * This is used for the root of new forks.
260 * @type {ForkContext}
262 get parentForkContext() {
263 const current
= this.forkContext
;
265 return current
&& current
.upper
;
269 * Creates and stacks new forking context.
270 * @param {boolean} forkLeavingPath A flag which shows being in a
272 * @returns {ForkContext} The created context.
274 pushForkContext(forkLeavingPath
) {
275 this.forkContext
= ForkContext
.newEmpty(
280 return this.forkContext
;
284 * Pops and merges the last forking context.
285 * @returns {ForkContext} The last context.
288 const lastContext
= this.forkContext
;
290 this.forkContext
= lastContext
.upper
;
291 this.forkContext
.replaceHead(lastContext
.makeNext(0, -1));
297 * Creates a new path.
301 this.forkContext
.add(this.parentForkContext
.makeNext(-1, -1));
305 * Creates a bypass path.
306 * This is used for such as IfStatement which does not have "else" chunk.
310 this.forkContext
.add(this.parentForkContext
.head
);
313 //--------------------------------------------------------------------------
314 // ConditionalExpression, LogicalExpression, IfStatement
315 //--------------------------------------------------------------------------
318 * Creates a context for ConditionalExpression, LogicalExpression,
319 * IfStatement, WhileStatement, DoWhileStatement, or ForStatement.
321 * LogicalExpressions have cases that it goes different paths between the
322 * `true` case and the `false` case.
332 * In this case, `b` is evaluated always in the code path of the `else`
333 * block, but it's not so in the code path of the `if` block.
334 * So there are 3 paths.
339 * @param {string} kind A kind string.
340 * If the new context is LogicalExpression's, this is `"&&"` or `"||"`.
341 * If it's IfStatement's or ConditionalExpression's, this is `"test"`.
342 * Otherwise, this is `"loop"`.
343 * @param {boolean} isForkingAsResult A flag that shows that goes different
344 * paths between `true` and `false`.
347 pushChoiceContext(kind
, isForkingAsResult
) {
348 this.choiceContext
= {
349 upper
: this.choiceContext
,
352 trueForkContext
: ForkContext
.newEmpty(this.forkContext
),
353 falseForkContext
: ForkContext
.newEmpty(this.forkContext
),
359 * Pops the last choice context and finalizes it.
360 * @returns {ChoiceContext} The popped context.
363 const context
= this.choiceContext
;
365 this.choiceContext
= context
.upper
;
367 const forkContext
= this.forkContext
;
368 const headSegments
= forkContext
.head
;
370 switch (context
.kind
) {
375 * If any result were not transferred from child contexts,
376 * this sets the head segments to both cases.
377 * The head segments are the path of the right-hand operand.
379 if (!context
.processed
) {
380 context
.trueForkContext
.add(headSegments
);
381 context
.falseForkContext
.add(headSegments
);
385 * Transfers results to upper context if this context is in
388 if (context
.isForkingAsResult
) {
389 const parentContext
= this.choiceContext
;
391 parentContext
.trueForkContext
.addAll(context
.trueForkContext
);
392 parentContext
.falseForkContext
.addAll(context
.falseForkContext
);
393 parentContext
.processed
= true;
401 if (!context
.processed
) {
404 * The head segments are the path of the `if` block here.
405 * Updates the `true` path with the end of the `if` block.
407 context
.trueForkContext
.clear();
408 context
.trueForkContext
.add(headSegments
);
412 * The head segments are the path of the `else` block here.
413 * Updates the `false` path with the end of the `else`
416 context
.falseForkContext
.clear();
417 context
.falseForkContext
.add(headSegments
);
425 * Loops are addressed in popLoopContext().
426 * This is called from popLoopContext().
430 /* istanbul ignore next */
432 throw new Error("unreachable");
436 const prevForkContext
= context
.trueForkContext
;
438 prevForkContext
.addAll(context
.falseForkContext
);
439 forkContext
.replaceHead(prevForkContext
.makeNext(0, -1));
445 * Makes a code path segment of the right-hand operand of a logical
450 const context
= this.choiceContext
;
451 const forkContext
= this.forkContext
;
453 if (context
.processed
) {
456 * This got segments already from the child choice context.
457 * Creates the next path from own true/false fork context.
459 const prevForkContext
=
460 context
.kind
=== "&&" ? context
.trueForkContext
461 /* kind === "||" */ : context
.falseForkContext
;
463 forkContext
.replaceHead(prevForkContext
.makeNext(0, -1));
464 prevForkContext
.clear();
466 context
.processed
= false;
470 * This did not get segments from the child choice context.
471 * So addresses the head segments.
472 * The head segments are the path of the left-hand operand.
474 if (context
.kind
=== "&&") {
476 // The path does short-circuit if false.
477 context
.falseForkContext
.add(forkContext
.head
);
480 // The path does short-circuit if true.
481 context
.trueForkContext
.add(forkContext
.head
);
484 forkContext
.replaceHead(forkContext
.makeNext(-1, -1));
489 * Makes a code path segment of the `if` block.
493 const context
= this.choiceContext
;
494 const forkContext
= this.forkContext
;
497 * If any result were not transferred from child contexts,
498 * this sets the head segments to both cases.
499 * The head segments are the path of the test expression.
501 if (!context
.processed
) {
502 context
.trueForkContext
.add(forkContext
.head
);
503 context
.falseForkContext
.add(forkContext
.head
);
506 context
.processed
= false;
508 // Creates new path from the `true` case.
509 forkContext
.replaceHead(
510 context
.trueForkContext
.makeNext(0, -1)
515 * Makes a code path segment of the `else` block.
519 const context
= this.choiceContext
;
520 const forkContext
= this.forkContext
;
523 * The head segments are the path of the `if` block.
524 * Updates the `true` path with the end of the `if` block.
526 context
.trueForkContext
.clear();
527 context
.trueForkContext
.add(forkContext
.head
);
528 context
.processed
= true;
530 // Creates new path from the `false` case.
531 forkContext
.replaceHead(
532 context
.falseForkContext
.makeNext(0, -1)
536 //--------------------------------------------------------------------------
538 //--------------------------------------------------------------------------
541 * Creates a context object of SwitchStatement and stacks it.
542 * @param {boolean} hasCase `true` if the switch statement has one or more
544 * @param {string|null} label The label text.
547 pushSwitchContext(hasCase
, label
) {
548 this.switchContext
= {
549 upper
: this.switchContext
,
551 defaultSegments
: null,
552 defaultBodySegments
: null,
554 lastIsDefault
: false,
558 this.pushBreakContext(true, label
);
562 * Pops the last context of SwitchStatement and finalizes it.
564 * - Disposes all forking stack for `case` and `default`.
565 * - Creates the next code path segment from `context.brokenForkContext`.
566 * - If the last `SwitchCase` node is not a `default` part, creates a path
567 * to the `default` body.
571 const context
= this.switchContext
;
573 this.switchContext
= context
.upper
;
575 const forkContext
= this.forkContext
;
576 const brokenForkContext
= this.popBreakContext().brokenForkContext
;
578 if (context
.countForks
=== 0) {
581 * When there is only one `default` chunk and there is one or more
582 * `break` statements, even if forks are nothing, it needs to merge
585 if (!brokenForkContext
.empty
) {
586 brokenForkContext
.add(forkContext
.makeNext(-1, -1));
587 forkContext
.replaceHead(brokenForkContext
.makeNext(0, -1));
593 const lastSegments
= forkContext
.head
;
595 this.forkBypassPath();
596 const lastCaseSegments
= forkContext
.head
;
599 * `brokenForkContext` is used to make the next segment.
600 * It must add the last segment into `brokenForkContext`.
602 brokenForkContext
.add(lastSegments
);
605 * A path which is failed in all case test should be connected to path
606 * of `default` chunk.
608 if (!context
.lastIsDefault
) {
609 if (context
.defaultBodySegments
) {
612 * Remove a link from `default` label to its chunk.
615 removeConnection(context
.defaultSegments
, context
.defaultBodySegments
);
616 makeLooped(this, lastCaseSegments
, context
.defaultBodySegments
);
620 * It handles the last case body as broken if `default` chunk
623 brokenForkContext
.add(lastCaseSegments
);
627 // Pops the segment context stack until the entry segment.
628 for (let i
= 0; i
< context
.countForks
; ++i
) {
629 this.forkContext
= this.forkContext
.upper
;
633 * Creates a path from all brokenForkContext paths.
634 * This is a path after switch statement.
636 this.forkContext
.replaceHead(brokenForkContext
.makeNext(0, -1));
640 * Makes a code path segment for a `SwitchCase` node.
641 * @param {boolean} isEmpty `true` if the body is empty.
642 * @param {boolean} isDefault `true` if the body is the default case.
645 makeSwitchCaseBody(isEmpty
, isDefault
) {
646 const context
= this.switchContext
;
648 if (!context
.hasCase
) {
654 * The parent fork context has two segments.
655 * Those are from the current case and the body of the previous case.
657 const parentForkContext
= this.forkContext
;
658 const forkContext
= this.pushForkContext();
660 forkContext
.add(parentForkContext
.makeNext(0, -1));
663 * Save `default` chunk info.
664 * If the `default` label is not at the last, we must make a path from
665 * the last `case` to the `default` chunk.
668 context
.defaultSegments
= parentForkContext
.head
;
670 context
.foundDefault
= true;
672 context
.defaultBodySegments
= forkContext
.head
;
675 if (!isEmpty
&& context
.foundDefault
) {
676 context
.foundDefault
= false;
677 context
.defaultBodySegments
= forkContext
.head
;
681 context
.lastIsDefault
= isDefault
;
682 context
.countForks
+= 1;
685 //--------------------------------------------------------------------------
687 //--------------------------------------------------------------------------
690 * Creates a context object of TryStatement and stacks it.
691 * @param {boolean} hasFinalizer `true` if the try statement has a
695 pushTryContext(hasFinalizer
) {
697 upper
: this.tryContext
,
701 returnedForkContext
: hasFinalizer
702 ? ForkContext
.newEmpty(this.forkContext
)
705 thrownForkContext
: ForkContext
.newEmpty(this.forkContext
),
706 lastOfTryIsReachable
: false,
707 lastOfCatchIsReachable
: false
712 * Pops the last context of TryStatement and finalizes it.
716 const context
= this.tryContext
;
718 this.tryContext
= context
.upper
;
720 if (context
.position
=== "catch") {
722 // Merges two paths from the `try` block and `catch` block merely.
723 this.popForkContext();
728 * The following process is executed only when there is the `finally`
732 const returned
= context
.returnedForkContext
;
733 const thrown
= context
.thrownForkContext
;
735 if (returned
.empty
&& thrown
.empty
) {
739 // Separate head to normal paths and leaving paths.
740 const headSegments
= this.forkContext
.head
;
742 this.forkContext
= this.forkContext
.upper
;
743 const normalSegments
= headSegments
.slice(0, headSegments
.length
/ 2 | 0);
744 const leavingSegments
= headSegments
.slice(headSegments
.length
/ 2 | 0);
746 // Forwards the leaving path to upper contexts.
747 if (!returned
.empty
) {
748 getReturnContext(this).returnedForkContext
.add(leavingSegments
);
751 getThrowContext(this).thrownForkContext
.add(leavingSegments
);
754 // Sets the normal path as the next.
755 this.forkContext
.replaceHead(normalSegments
);
758 * If both paths of the `try` block and the `catch` block are
759 * unreachable, the next path becomes unreachable as well.
761 if (!context
.lastOfTryIsReachable
&& !context
.lastOfCatchIsReachable
) {
762 this.forkContext
.makeUnreachable();
767 * Makes a code path segment for a `catch` block.
771 const context
= this.tryContext
;
772 const forkContext
= this.forkContext
;
773 const thrown
= context
.thrownForkContext
;
776 context
.position
= "catch";
777 context
.thrownForkContext
= ForkContext
.newEmpty(forkContext
);
778 context
.lastOfTryIsReachable
= forkContext
.reachable
;
780 // Merge thrown paths.
781 thrown
.add(forkContext
.head
);
782 const thrownSegments
= thrown
.makeNext(0, -1);
784 // Fork to a bypass and the merged thrown path.
785 this.pushForkContext();
786 this.forkBypassPath();
787 this.forkContext
.add(thrownSegments
);
791 * Makes a code path segment for a `finally` block.
793 * In the `finally` block, parallel paths are created. The parallel paths
794 * are used as leaving-paths. The leaving-paths are paths from `return`
795 * statements and `throw` statements in a `try` block or a `catch` block.
799 const context
= this.tryContext
;
800 let forkContext
= this.forkContext
;
801 const returned
= context
.returnedForkContext
;
802 const thrown
= context
.thrownForkContext
;
803 const headOfLeavingSegments
= forkContext
.head
;
806 if (context
.position
=== "catch") {
808 // Merges two paths from the `try` block and `catch` block.
809 this.popForkContext();
810 forkContext
= this.forkContext
;
812 context
.lastOfCatchIsReachable
= forkContext
.reachable
;
814 context
.lastOfTryIsReachable
= forkContext
.reachable
;
816 context
.position
= "finally";
818 if (returned
.empty
&& thrown
.empty
) {
820 // This path does not leave.
825 * Create a parallel segment from merging returned and thrown.
826 * This segment will leave at the end of this finally block.
828 const segments
= forkContext
.makeNext(-1, -1);
830 for (let i
= 0; i
< forkContext
.count
; ++i
) {
831 const prevSegsOfLeavingSegment
= [headOfLeavingSegments
[i
]];
833 for (let j
= 0; j
< returned
.segmentsList
.length
; ++j
) {
834 prevSegsOfLeavingSegment
.push(returned
.segmentsList
[j
][i
]);
836 for (let j
= 0; j
< thrown
.segmentsList
.length
; ++j
) {
837 prevSegsOfLeavingSegment
.push(thrown
.segmentsList
[j
][i
]);
841 CodePathSegment
.newNext(
842 this.idGenerator
.next(),
843 prevSegsOfLeavingSegment
848 this.pushForkContext(true);
849 this.forkContext
.add(segments
);
853 * Makes a code path segment from the first throwable node to the `catch`
854 * block or the `finally` block.
857 makeFirstThrowablePathInTryBlock() {
858 const forkContext
= this.forkContext
;
860 if (!forkContext
.reachable
) {
864 const context
= getThrowContext(this);
866 if (context
=== this ||
867 context
.position
!== "try" ||
868 !context
.thrownForkContext
.empty
873 context
.thrownForkContext
.add(forkContext
.head
);
874 forkContext
.replaceHead(forkContext
.makeNext(-1, -1));
877 //--------------------------------------------------------------------------
879 //--------------------------------------------------------------------------
882 * Creates a context object of a loop statement and stacks it.
883 * @param {string} type The type of the node which was triggered. One of
884 * `WhileStatement`, `DoWhileStatement`, `ForStatement`, `ForInStatement`,
885 * and `ForStatement`.
886 * @param {string|null} label A label of the node which was triggered.
889 pushLoopContext(type
, label
) {
890 const forkContext
= this.forkContext
;
891 const breakContext
= this.pushBreakContext(true, label
);
894 case "WhileStatement":
895 this.pushChoiceContext("loop", false);
897 upper
: this.loopContext
,
901 continueDestSegments
: null,
902 brokenForkContext
: breakContext
.brokenForkContext
906 case "DoWhileStatement":
907 this.pushChoiceContext("loop", false);
909 upper
: this.loopContext
,
914 continueForkContext
: ForkContext
.newEmpty(forkContext
),
915 brokenForkContext
: breakContext
.brokenForkContext
920 this.pushChoiceContext("loop", false);
922 upper
: this.loopContext
,
926 endOfInitSegments
: null,
928 endOfTestSegments
: null,
929 updateSegments
: null,
930 endOfUpdateSegments
: null,
931 continueDestSegments
: null,
932 brokenForkContext
: breakContext
.brokenForkContext
936 case "ForInStatement":
937 case "ForOfStatement":
939 upper
: this.loopContext
,
944 endOfLeftSegments
: null,
945 continueDestSegments
: null,
946 brokenForkContext
: breakContext
.brokenForkContext
950 /* istanbul ignore next */
952 throw new Error(`unknown type: "${type}"`);
957 * Pops the last context of a loop statement and finalizes it.
961 const context
= this.loopContext
;
963 this.loopContext
= context
.upper
;
965 const forkContext
= this.forkContext
;
966 const brokenForkContext
= this.popBreakContext().brokenForkContext
;
968 // Creates a looped path.
969 switch (context
.type
) {
970 case "WhileStatement":
972 this.popChoiceContext();
976 context
.continueDestSegments
980 case "DoWhileStatement": {
981 const choiceContext
= this.popChoiceContext();
983 if (!choiceContext
.processed
) {
984 choiceContext
.trueForkContext
.add(forkContext
.head
);
985 choiceContext
.falseForkContext
.add(forkContext
.head
);
987 if (context
.test
!== true) {
988 brokenForkContext
.addAll(choiceContext
.falseForkContext
);
991 // `true` paths go to looping.
992 const segmentsList
= choiceContext
.trueForkContext
.segmentsList
;
994 for (let i
= 0; i
< segmentsList
.length
; ++i
) {
998 context
.entrySegments
1004 case "ForInStatement":
1005 case "ForOfStatement":
1006 brokenForkContext
.add(forkContext
.head
);
1010 context
.leftSegments
1014 /* istanbul ignore next */
1016 throw new Error("unreachable");
1020 if (brokenForkContext
.empty
) {
1021 forkContext
.replaceHead(forkContext
.makeUnreachable(-1, -1));
1023 forkContext
.replaceHead(brokenForkContext
.makeNext(0, -1));
1028 * Makes a code path segment for the test part of a WhileStatement.
1029 * @param {boolean|undefined} test The test value (only when constant).
1032 makeWhileTest(test
) {
1033 const context
= this.loopContext
;
1034 const forkContext
= this.forkContext
;
1035 const testSegments
= forkContext
.makeNext(0, -1);
1038 context
.test
= test
;
1039 context
.continueDestSegments
= testSegments
;
1040 forkContext
.replaceHead(testSegments
);
1044 * Makes a code path segment for the body part of a WhileStatement.
1048 const context
= this.loopContext
;
1049 const choiceContext
= this.choiceContext
;
1050 const forkContext
= this.forkContext
;
1052 if (!choiceContext
.processed
) {
1053 choiceContext
.trueForkContext
.add(forkContext
.head
);
1054 choiceContext
.falseForkContext
.add(forkContext
.head
);
1058 if (context
.test
!== true) {
1059 context
.brokenForkContext
.addAll(choiceContext
.falseForkContext
);
1061 forkContext
.replaceHead(choiceContext
.trueForkContext
.makeNext(0, -1));
1065 * Makes a code path segment for the body part of a DoWhileStatement.
1069 const context
= this.loopContext
;
1070 const forkContext
= this.forkContext
;
1071 const bodySegments
= forkContext
.makeNext(-1, -1);
1074 context
.entrySegments
= bodySegments
;
1075 forkContext
.replaceHead(bodySegments
);
1079 * Makes a code path segment for the test part of a DoWhileStatement.
1080 * @param {boolean|undefined} test The test value (only when constant).
1083 makeDoWhileTest(test
) {
1084 const context
= this.loopContext
;
1085 const forkContext
= this.forkContext
;
1087 context
.test
= test
;
1089 // Creates paths of `continue` statements.
1090 if (!context
.continueForkContext
.empty
) {
1091 context
.continueForkContext
.add(forkContext
.head
);
1092 const testSegments
= context
.continueForkContext
.makeNext(0, -1);
1094 forkContext
.replaceHead(testSegments
);
1099 * Makes a code path segment for the test part of a ForStatement.
1100 * @param {boolean|undefined} test The test value (only when constant).
1104 const context
= this.loopContext
;
1105 const forkContext
= this.forkContext
;
1106 const endOfInitSegments
= forkContext
.head
;
1107 const testSegments
= forkContext
.makeNext(-1, -1);
1110 context
.test
= test
;
1111 context
.endOfInitSegments
= endOfInitSegments
;
1112 context
.continueDestSegments
= context
.testSegments
= testSegments
;
1113 forkContext
.replaceHead(testSegments
);
1117 * Makes a code path segment for the update part of a ForStatement.
1121 const context
= this.loopContext
;
1122 const choiceContext
= this.choiceContext
;
1123 const forkContext
= this.forkContext
;
1125 // Make the next paths of the test.
1126 if (context
.testSegments
) {
1127 finalizeTestSegmentsOfFor(
1133 context
.endOfInitSegments
= forkContext
.head
;
1137 const updateSegments
= forkContext
.makeDisconnected(-1, -1);
1139 context
.continueDestSegments
= context
.updateSegments
= updateSegments
;
1140 forkContext
.replaceHead(updateSegments
);
1144 * Makes a code path segment for the body part of a ForStatement.
1148 const context
= this.loopContext
;
1149 const choiceContext
= this.choiceContext
;
1150 const forkContext
= this.forkContext
;
1153 if (context
.updateSegments
) {
1154 context
.endOfUpdateSegments
= forkContext
.head
;
1156 // `update` -> `test`
1157 if (context
.testSegments
) {
1160 context
.endOfUpdateSegments
,
1161 context
.testSegments
1164 } else if (context
.testSegments
) {
1165 finalizeTestSegmentsOfFor(
1171 context
.endOfInitSegments
= forkContext
.head
;
1174 let bodySegments
= context
.endOfTestSegments
;
1176 if (!bodySegments
) {
1179 * If there is not the `test` part, the `body` path comes from the
1180 * `init` part and the `update` part.
1182 const prevForkContext
= ForkContext
.newEmpty(forkContext
);
1184 prevForkContext
.add(context
.endOfInitSegments
);
1185 if (context
.endOfUpdateSegments
) {
1186 prevForkContext
.add(context
.endOfUpdateSegments
);
1189 bodySegments
= prevForkContext
.makeNext(0, -1);
1191 context
.continueDestSegments
= context
.continueDestSegments
|| bodySegments
;
1192 forkContext
.replaceHead(bodySegments
);
1196 * Makes a code path segment for the left part of a ForInStatement and a
1201 const context
= this.loopContext
;
1202 const forkContext
= this.forkContext
;
1203 const leftSegments
= forkContext
.makeDisconnected(-1, -1);
1206 context
.prevSegments
= forkContext
.head
;
1207 context
.leftSegments
= context
.continueDestSegments
= leftSegments
;
1208 forkContext
.replaceHead(leftSegments
);
1212 * Makes a code path segment for the right part of a ForInStatement and a
1216 makeForInOfRight() {
1217 const context
= this.loopContext
;
1218 const forkContext
= this.forkContext
;
1219 const temp
= ForkContext
.newEmpty(forkContext
);
1221 temp
.add(context
.prevSegments
);
1222 const rightSegments
= temp
.makeNext(-1, -1);
1225 context
.endOfLeftSegments
= forkContext
.head
;
1226 forkContext
.replaceHead(rightSegments
);
1230 * Makes a code path segment for the body part of a ForInStatement and a
1235 const context
= this.loopContext
;
1236 const forkContext
= this.forkContext
;
1237 const temp
= ForkContext
.newEmpty(forkContext
);
1239 temp
.add(context
.endOfLeftSegments
);
1240 const bodySegments
= temp
.makeNext(-1, -1);
1242 // Make a path: `right` -> `left`.
1243 makeLooped(this, forkContext
.head
, context
.leftSegments
);
1246 context
.brokenForkContext
.add(forkContext
.head
);
1247 forkContext
.replaceHead(bodySegments
);
1250 //--------------------------------------------------------------------------
1251 // Control Statements
1252 //--------------------------------------------------------------------------
1255 * Creates new context for BreakStatement.
1256 * @param {boolean} breakable The flag to indicate it can break by
1257 * an unlabeled BreakStatement.
1258 * @param {string|null} label The label of this context.
1259 * @returns {Object} The new context.
1261 pushBreakContext(breakable
, label
) {
1262 this.breakContext
= {
1263 upper
: this.breakContext
,
1266 brokenForkContext
: ForkContext
.newEmpty(this.forkContext
)
1268 return this.breakContext
;
1272 * Removes the top item of the break context stack.
1273 * @returns {Object} The removed context.
1276 const context
= this.breakContext
;
1277 const forkContext
= this.forkContext
;
1279 this.breakContext
= context
.upper
;
1281 // Process this context here for other than switches and loops.
1282 if (!context
.breakable
) {
1283 const brokenForkContext
= context
.brokenForkContext
;
1285 if (!brokenForkContext
.empty
) {
1286 brokenForkContext
.add(forkContext
.head
);
1287 forkContext
.replaceHead(brokenForkContext
.makeNext(0, -1));
1295 * Makes a path for a `break` statement.
1297 * It registers the head segment to a context of `break`.
1298 * It makes new unreachable segment, then it set the head with the segment.
1299 * @param {string} label A label of the break statement.
1303 const forkContext
= this.forkContext
;
1305 if (!forkContext
.reachable
) {
1309 const context
= getBreakContext(this, label
);
1311 /* istanbul ignore else: foolproof (syntax error) */
1313 context
.brokenForkContext
.add(forkContext
.head
);
1316 forkContext
.replaceHead(forkContext
.makeUnreachable(-1, -1));
1320 * Makes a path for a `continue` statement.
1322 * It makes a looping path.
1323 * It makes new unreachable segment, then it set the head with the segment.
1324 * @param {string} label A label of the continue statement.
1327 makeContinue(label
) {
1328 const forkContext
= this.forkContext
;
1330 if (!forkContext
.reachable
) {
1334 const context
= getContinueContext(this, label
);
1336 /* istanbul ignore else: foolproof (syntax error) */
1338 if (context
.continueDestSegments
) {
1339 makeLooped(this, forkContext
.head
, context
.continueDestSegments
);
1341 // If the context is a for-in/of loop, this effects a break also.
1342 if (context
.type
=== "ForInStatement" ||
1343 context
.type
=== "ForOfStatement"
1345 context
.brokenForkContext
.add(forkContext
.head
);
1348 context
.continueForkContext
.add(forkContext
.head
);
1351 forkContext
.replaceHead(forkContext
.makeUnreachable(-1, -1));
1355 * Makes a path for a `return` statement.
1357 * It registers the head segment to a context of `return`.
1358 * It makes new unreachable segment, then it set the head with the segment.
1362 const forkContext
= this.forkContext
;
1364 if (forkContext
.reachable
) {
1365 getReturnContext(this).returnedForkContext
.add(forkContext
.head
);
1366 forkContext
.replaceHead(forkContext
.makeUnreachable(-1, -1));
1371 * Makes a path for a `throw` statement.
1373 * It registers the head segment to a context of `throw`.
1374 * It makes new unreachable segment, then it set the head with the segment.
1378 const forkContext
= this.forkContext
;
1380 if (forkContext
.reachable
) {
1381 getThrowContext(this).thrownForkContext
.add(forkContext
.head
);
1382 forkContext
.replaceHead(forkContext
.makeUnreachable(-1, -1));
1387 * Makes the final path.
1391 const segments
= this.currentSegments
;
1393 if (segments
.length
> 0 && segments
[0].reachable
) {
1394 this.returnedForkContext
.add(segments
);
1399 module
.exports
= CodePathState
;