]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/linter/code-path-analysis/code-path-state.js
9e760601a0f744202ee19c4ff2d3b3f4094a7555
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
);
204 choiceContext
.qqForkContext
.add(head
);
207 if (context
.test
!== true) {
208 context
.brokenForkContext
.addAll(choiceContext
.falseForkContext
);
210 context
.endOfTestSegments
= choiceContext
.trueForkContext
.makeNext(0, -1);
213 //------------------------------------------------------------------------------
215 //------------------------------------------------------------------------------
218 * A class which manages state to analyze code paths.
220 class CodePathState
{
222 // eslint-disable-next-line jsdoc/require-description
224 * @param {IdGenerator} idGenerator An id generator to generate id for code
226 * @param {Function} onLooped A callback function to notify looping.
228 constructor(idGenerator
, onLooped
) {
229 this.idGenerator
= idGenerator
;
230 this.notifyLooped
= onLooped
;
231 this.forkContext
= ForkContext
.newRoot(idGenerator
);
232 this.choiceContext
= null;
233 this.switchContext
= null;
234 this.tryContext
= null;
235 this.loopContext
= null;
236 this.breakContext
= null;
238 this.currentSegments
= [];
239 this.initialSegment
= this.forkContext
.head
[0];
241 // returnedSegments and thrownSegments push elements into finalSegments also.
242 const final
= this.finalSegments
= [];
243 const returned
= this.returnedForkContext
= [];
244 const thrown
= this.thrownForkContext
= [];
246 returned
.add
= addToReturnedOrThrown
.bind(null, returned
, thrown
, final
);
247 thrown
.add
= addToReturnedOrThrown
.bind(null, thrown
, returned
, final
);
252 * @type {CodePathSegment[]}
255 return this.forkContext
.head
;
259 * The parent forking context.
260 * This is used for the root of new forks.
261 * @type {ForkContext}
263 get parentForkContext() {
264 const current
= this.forkContext
;
266 return current
&& current
.upper
;
270 * Creates and stacks new forking context.
271 * @param {boolean} forkLeavingPath A flag which shows being in a
273 * @returns {ForkContext} The created context.
275 pushForkContext(forkLeavingPath
) {
276 this.forkContext
= ForkContext
.newEmpty(
281 return this.forkContext
;
285 * Pops and merges the last forking context.
286 * @returns {ForkContext} The last context.
289 const lastContext
= this.forkContext
;
291 this.forkContext
= lastContext
.upper
;
292 this.forkContext
.replaceHead(lastContext
.makeNext(0, -1));
298 * Creates a new path.
302 this.forkContext
.add(this.parentForkContext
.makeNext(-1, -1));
306 * Creates a bypass path.
307 * This is used for such as IfStatement which does not have "else" chunk.
311 this.forkContext
.add(this.parentForkContext
.head
);
314 //--------------------------------------------------------------------------
315 // ConditionalExpression, LogicalExpression, IfStatement
316 //--------------------------------------------------------------------------
319 * Creates a context for ConditionalExpression, LogicalExpression,
320 * IfStatement, WhileStatement, DoWhileStatement, or ForStatement.
322 * LogicalExpressions have cases that it goes different paths between the
323 * `true` case and the `false` case.
333 * In this case, `b` is evaluated always in the code path of the `else`
334 * block, but it's not so in the code path of the `if` block.
335 * So there are 3 paths.
340 * @param {string} kind A kind string.
341 * If the new context is LogicalExpression's, this is `"&&"` or `"||"`.
342 * If it's IfStatement's or ConditionalExpression's, this is `"test"`.
343 * Otherwise, this is `"loop"`.
344 * @param {boolean} isForkingAsResult A flag that shows that goes different
345 * paths between `true` and `false`.
348 pushChoiceContext(kind
, isForkingAsResult
) {
349 this.choiceContext
= {
350 upper
: this.choiceContext
,
353 trueForkContext
: ForkContext
.newEmpty(this.forkContext
),
354 falseForkContext
: ForkContext
.newEmpty(this.forkContext
),
355 qqForkContext
: ForkContext
.newEmpty(this.forkContext
),
361 * Pops the last choice context and finalizes it.
362 * @returns {ChoiceContext} The popped context.
365 const context
= this.choiceContext
;
367 this.choiceContext
= context
.upper
;
369 const forkContext
= this.forkContext
;
370 const headSegments
= forkContext
.head
;
372 switch (context
.kind
) {
378 * If any result were not transferred from child contexts,
379 * this sets the head segments to both cases.
380 * The head segments are the path of the right-hand operand.
382 if (!context
.processed
) {
383 context
.trueForkContext
.add(headSegments
);
384 context
.falseForkContext
.add(headSegments
);
385 context
.qqForkContext
.add(headSegments
);
389 * Transfers results to upper context if this context is in
392 if (context
.isForkingAsResult
) {
393 const parentContext
= this.choiceContext
;
395 parentContext
.trueForkContext
.addAll(context
.trueForkContext
);
396 parentContext
.falseForkContext
.addAll(context
.falseForkContext
);
397 parentContext
.qqForkContext
.addAll(context
.qqForkContext
);
398 parentContext
.processed
= true;
406 if (!context
.processed
) {
409 * The head segments are the path of the `if` block here.
410 * Updates the `true` path with the end of the `if` block.
412 context
.trueForkContext
.clear();
413 context
.trueForkContext
.add(headSegments
);
417 * The head segments are the path of the `else` block here.
418 * Updates the `false` path with the end of the `else`
421 context
.falseForkContext
.clear();
422 context
.falseForkContext
.add(headSegments
);
430 * Loops are addressed in popLoopContext().
431 * This is called from popLoopContext().
435 /* istanbul ignore next */
437 throw new Error("unreachable");
441 const prevForkContext
= context
.trueForkContext
;
443 prevForkContext
.addAll(context
.falseForkContext
);
444 forkContext
.replaceHead(prevForkContext
.makeNext(0, -1));
450 * Makes a code path segment of the right-hand operand of a logical
455 const context
= this.choiceContext
;
456 const forkContext
= this.forkContext
;
458 if (context
.processed
) {
461 * This got segments already from the child choice context.
462 * Creates the next path from own true/false fork context.
466 switch (context
.kind
) {
467 case "&&": // if true then go to the right-hand side.
468 prevForkContext
= context
.trueForkContext
;
470 case "||": // if false then go to the right-hand side.
471 prevForkContext
= context
.falseForkContext
;
473 case "??": // Both true/false can short-circuit, so needs the third path to go to the right-hand side. That's qqForkContext.
474 prevForkContext
= context
.qqForkContext
;
477 throw new Error("unreachable");
480 forkContext
.replaceHead(prevForkContext
.makeNext(0, -1));
481 prevForkContext
.clear();
482 context
.processed
= false;
486 * This did not get segments from the child choice context.
487 * So addresses the head segments.
488 * The head segments are the path of the left-hand operand.
490 switch (context
.kind
) {
491 case "&&": // the false path can short-circuit.
492 context
.falseForkContext
.add(forkContext
.head
);
494 case "||": // the true path can short-circuit.
495 context
.trueForkContext
.add(forkContext
.head
);
497 case "??": // both can short-circuit.
498 context
.trueForkContext
.add(forkContext
.head
);
499 context
.falseForkContext
.add(forkContext
.head
);
502 throw new Error("unreachable");
505 forkContext
.replaceHead(forkContext
.makeNext(-1, -1));
510 * Makes a code path segment of the `if` block.
514 const context
= this.choiceContext
;
515 const forkContext
= this.forkContext
;
518 * If any result were not transferred from child contexts,
519 * this sets the head segments to both cases.
520 * The head segments are the path of the test expression.
522 if (!context
.processed
) {
523 context
.trueForkContext
.add(forkContext
.head
);
524 context
.falseForkContext
.add(forkContext
.head
);
525 context
.qqForkContext
.add(forkContext
.head
);
528 context
.processed
= false;
530 // Creates new path from the `true` case.
531 forkContext
.replaceHead(
532 context
.trueForkContext
.makeNext(0, -1)
537 * Makes a code path segment of the `else` block.
541 const context
= this.choiceContext
;
542 const forkContext
= this.forkContext
;
545 * The head segments are the path of the `if` block.
546 * Updates the `true` path with the end of the `if` block.
548 context
.trueForkContext
.clear();
549 context
.trueForkContext
.add(forkContext
.head
);
550 context
.processed
= true;
552 // Creates new path from the `false` case.
553 forkContext
.replaceHead(
554 context
.falseForkContext
.makeNext(0, -1)
558 //--------------------------------------------------------------------------
560 //--------------------------------------------------------------------------
563 * Creates a context object of SwitchStatement and stacks it.
564 * @param {boolean} hasCase `true` if the switch statement has one or more
566 * @param {string|null} label The label text.
569 pushSwitchContext(hasCase
, label
) {
570 this.switchContext
= {
571 upper
: this.switchContext
,
573 defaultSegments
: null,
574 defaultBodySegments
: null,
576 lastIsDefault
: false,
580 this.pushBreakContext(true, label
);
584 * Pops the last context of SwitchStatement and finalizes it.
586 * - Disposes all forking stack for `case` and `default`.
587 * - Creates the next code path segment from `context.brokenForkContext`.
588 * - If the last `SwitchCase` node is not a `default` part, creates a path
589 * to the `default` body.
593 const context
= this.switchContext
;
595 this.switchContext
= context
.upper
;
597 const forkContext
= this.forkContext
;
598 const brokenForkContext
= this.popBreakContext().brokenForkContext
;
600 if (context
.countForks
=== 0) {
603 * When there is only one `default` chunk and there is one or more
604 * `break` statements, even if forks are nothing, it needs to merge
607 if (!brokenForkContext
.empty
) {
608 brokenForkContext
.add(forkContext
.makeNext(-1, -1));
609 forkContext
.replaceHead(brokenForkContext
.makeNext(0, -1));
615 const lastSegments
= forkContext
.head
;
617 this.forkBypassPath();
618 const lastCaseSegments
= forkContext
.head
;
621 * `brokenForkContext` is used to make the next segment.
622 * It must add the last segment into `brokenForkContext`.
624 brokenForkContext
.add(lastSegments
);
627 * A path which is failed in all case test should be connected to path
628 * of `default` chunk.
630 if (!context
.lastIsDefault
) {
631 if (context
.defaultBodySegments
) {
634 * Remove a link from `default` label to its chunk.
637 removeConnection(context
.defaultSegments
, context
.defaultBodySegments
);
638 makeLooped(this, lastCaseSegments
, context
.defaultBodySegments
);
642 * It handles the last case body as broken if `default` chunk
645 brokenForkContext
.add(lastCaseSegments
);
649 // Pops the segment context stack until the entry segment.
650 for (let i
= 0; i
< context
.countForks
; ++i
) {
651 this.forkContext
= this.forkContext
.upper
;
655 * Creates a path from all brokenForkContext paths.
656 * This is a path after switch statement.
658 this.forkContext
.replaceHead(brokenForkContext
.makeNext(0, -1));
662 * Makes a code path segment for a `SwitchCase` node.
663 * @param {boolean} isEmpty `true` if the body is empty.
664 * @param {boolean} isDefault `true` if the body is the default case.
667 makeSwitchCaseBody(isEmpty
, isDefault
) {
668 const context
= this.switchContext
;
670 if (!context
.hasCase
) {
676 * The parent fork context has two segments.
677 * Those are from the current case and the body of the previous case.
679 const parentForkContext
= this.forkContext
;
680 const forkContext
= this.pushForkContext();
682 forkContext
.add(parentForkContext
.makeNext(0, -1));
685 * Save `default` chunk info.
686 * If the `default` label is not at the last, we must make a path from
687 * the last `case` to the `default` chunk.
690 context
.defaultSegments
= parentForkContext
.head
;
692 context
.foundDefault
= true;
694 context
.defaultBodySegments
= forkContext
.head
;
697 if (!isEmpty
&& context
.foundDefault
) {
698 context
.foundDefault
= false;
699 context
.defaultBodySegments
= forkContext
.head
;
703 context
.lastIsDefault
= isDefault
;
704 context
.countForks
+= 1;
707 //--------------------------------------------------------------------------
709 //--------------------------------------------------------------------------
712 * Creates a context object of TryStatement and stacks it.
713 * @param {boolean} hasFinalizer `true` if the try statement has a
717 pushTryContext(hasFinalizer
) {
719 upper
: this.tryContext
,
723 returnedForkContext
: hasFinalizer
724 ? ForkContext
.newEmpty(this.forkContext
)
727 thrownForkContext
: ForkContext
.newEmpty(this.forkContext
),
728 lastOfTryIsReachable
: false,
729 lastOfCatchIsReachable
: false
734 * Pops the last context of TryStatement and finalizes it.
738 const context
= this.tryContext
;
740 this.tryContext
= context
.upper
;
742 if (context
.position
=== "catch") {
744 // Merges two paths from the `try` block and `catch` block merely.
745 this.popForkContext();
750 * The following process is executed only when there is the `finally`
754 const returned
= context
.returnedForkContext
;
755 const thrown
= context
.thrownForkContext
;
757 if (returned
.empty
&& thrown
.empty
) {
761 // Separate head to normal paths and leaving paths.
762 const headSegments
= this.forkContext
.head
;
764 this.forkContext
= this.forkContext
.upper
;
765 const normalSegments
= headSegments
.slice(0, headSegments
.length
/ 2 | 0);
766 const leavingSegments
= headSegments
.slice(headSegments
.length
/ 2 | 0);
768 // Forwards the leaving path to upper contexts.
769 if (!returned
.empty
) {
770 getReturnContext(this).returnedForkContext
.add(leavingSegments
);
773 getThrowContext(this).thrownForkContext
.add(leavingSegments
);
776 // Sets the normal path as the next.
777 this.forkContext
.replaceHead(normalSegments
);
780 * If both paths of the `try` block and the `catch` block are
781 * unreachable, the next path becomes unreachable as well.
783 if (!context
.lastOfTryIsReachable
&& !context
.lastOfCatchIsReachable
) {
784 this.forkContext
.makeUnreachable();
789 * Makes a code path segment for a `catch` block.
793 const context
= this.tryContext
;
794 const forkContext
= this.forkContext
;
795 const thrown
= context
.thrownForkContext
;
798 context
.position
= "catch";
799 context
.thrownForkContext
= ForkContext
.newEmpty(forkContext
);
800 context
.lastOfTryIsReachable
= forkContext
.reachable
;
802 // Merge thrown paths.
803 thrown
.add(forkContext
.head
);
804 const thrownSegments
= thrown
.makeNext(0, -1);
806 // Fork to a bypass and the merged thrown path.
807 this.pushForkContext();
808 this.forkBypassPath();
809 this.forkContext
.add(thrownSegments
);
813 * Makes a code path segment for a `finally` block.
815 * In the `finally` block, parallel paths are created. The parallel paths
816 * are used as leaving-paths. The leaving-paths are paths from `return`
817 * statements and `throw` statements in a `try` block or a `catch` block.
821 const context
= this.tryContext
;
822 let forkContext
= this.forkContext
;
823 const returned
= context
.returnedForkContext
;
824 const thrown
= context
.thrownForkContext
;
825 const headOfLeavingSegments
= forkContext
.head
;
828 if (context
.position
=== "catch") {
830 // Merges two paths from the `try` block and `catch` block.
831 this.popForkContext();
832 forkContext
= this.forkContext
;
834 context
.lastOfCatchIsReachable
= forkContext
.reachable
;
836 context
.lastOfTryIsReachable
= forkContext
.reachable
;
838 context
.position
= "finally";
840 if (returned
.empty
&& thrown
.empty
) {
842 // This path does not leave.
847 * Create a parallel segment from merging returned and thrown.
848 * This segment will leave at the end of this finally block.
850 const segments
= forkContext
.makeNext(-1, -1);
852 for (let i
= 0; i
< forkContext
.count
; ++i
) {
853 const prevSegsOfLeavingSegment
= [headOfLeavingSegments
[i
]];
855 for (let j
= 0; j
< returned
.segmentsList
.length
; ++j
) {
856 prevSegsOfLeavingSegment
.push(returned
.segmentsList
[j
][i
]);
858 for (let j
= 0; j
< thrown
.segmentsList
.length
; ++j
) {
859 prevSegsOfLeavingSegment
.push(thrown
.segmentsList
[j
][i
]);
863 CodePathSegment
.newNext(
864 this.idGenerator
.next(),
865 prevSegsOfLeavingSegment
870 this.pushForkContext(true);
871 this.forkContext
.add(segments
);
875 * Makes a code path segment from the first throwable node to the `catch`
876 * block or the `finally` block.
879 makeFirstThrowablePathInTryBlock() {
880 const forkContext
= this.forkContext
;
882 if (!forkContext
.reachable
) {
886 const context
= getThrowContext(this);
888 if (context
=== this ||
889 context
.position
!== "try" ||
890 !context
.thrownForkContext
.empty
895 context
.thrownForkContext
.add(forkContext
.head
);
896 forkContext
.replaceHead(forkContext
.makeNext(-1, -1));
899 //--------------------------------------------------------------------------
901 //--------------------------------------------------------------------------
904 * Creates a context object of a loop statement and stacks it.
905 * @param {string} type The type of the node which was triggered. One of
906 * `WhileStatement`, `DoWhileStatement`, `ForStatement`, `ForInStatement`,
907 * and `ForStatement`.
908 * @param {string|null} label A label of the node which was triggered.
911 pushLoopContext(type
, label
) {
912 const forkContext
= this.forkContext
;
913 const breakContext
= this.pushBreakContext(true, label
);
916 case "WhileStatement":
917 this.pushChoiceContext("loop", false);
919 upper
: this.loopContext
,
923 continueDestSegments
: null,
924 brokenForkContext
: breakContext
.brokenForkContext
928 case "DoWhileStatement":
929 this.pushChoiceContext("loop", false);
931 upper
: this.loopContext
,
936 continueForkContext
: ForkContext
.newEmpty(forkContext
),
937 brokenForkContext
: breakContext
.brokenForkContext
942 this.pushChoiceContext("loop", false);
944 upper
: this.loopContext
,
948 endOfInitSegments
: null,
950 endOfTestSegments
: null,
951 updateSegments
: null,
952 endOfUpdateSegments
: null,
953 continueDestSegments
: null,
954 brokenForkContext
: breakContext
.brokenForkContext
958 case "ForInStatement":
959 case "ForOfStatement":
961 upper
: this.loopContext
,
966 endOfLeftSegments
: null,
967 continueDestSegments
: null,
968 brokenForkContext
: breakContext
.brokenForkContext
972 /* istanbul ignore next */
974 throw new Error(`unknown type: "${type}"`);
979 * Pops the last context of a loop statement and finalizes it.
983 const context
= this.loopContext
;
985 this.loopContext
= context
.upper
;
987 const forkContext
= this.forkContext
;
988 const brokenForkContext
= this.popBreakContext().brokenForkContext
;
990 // Creates a looped path.
991 switch (context
.type
) {
992 case "WhileStatement":
994 this.popChoiceContext();
998 context
.continueDestSegments
1002 case "DoWhileStatement": {
1003 const choiceContext
= this.popChoiceContext();
1005 if (!choiceContext
.processed
) {
1006 choiceContext
.trueForkContext
.add(forkContext
.head
);
1007 choiceContext
.falseForkContext
.add(forkContext
.head
);
1009 if (context
.test
!== true) {
1010 brokenForkContext
.addAll(choiceContext
.falseForkContext
);
1013 // `true` paths go to looping.
1014 const segmentsList
= choiceContext
.trueForkContext
.segmentsList
;
1016 for (let i
= 0; i
< segmentsList
.length
; ++i
) {
1020 context
.entrySegments
1026 case "ForInStatement":
1027 case "ForOfStatement":
1028 brokenForkContext
.add(forkContext
.head
);
1032 context
.leftSegments
1036 /* istanbul ignore next */
1038 throw new Error("unreachable");
1042 if (brokenForkContext
.empty
) {
1043 forkContext
.replaceHead(forkContext
.makeUnreachable(-1, -1));
1045 forkContext
.replaceHead(brokenForkContext
.makeNext(0, -1));
1050 * Makes a code path segment for the test part of a WhileStatement.
1051 * @param {boolean|undefined} test The test value (only when constant).
1054 makeWhileTest(test
) {
1055 const context
= this.loopContext
;
1056 const forkContext
= this.forkContext
;
1057 const testSegments
= forkContext
.makeNext(0, -1);
1060 context
.test
= test
;
1061 context
.continueDestSegments
= testSegments
;
1062 forkContext
.replaceHead(testSegments
);
1066 * Makes a code path segment for the body part of a WhileStatement.
1070 const context
= this.loopContext
;
1071 const choiceContext
= this.choiceContext
;
1072 const forkContext
= this.forkContext
;
1074 if (!choiceContext
.processed
) {
1075 choiceContext
.trueForkContext
.add(forkContext
.head
);
1076 choiceContext
.falseForkContext
.add(forkContext
.head
);
1080 if (context
.test
!== true) {
1081 context
.brokenForkContext
.addAll(choiceContext
.falseForkContext
);
1083 forkContext
.replaceHead(choiceContext
.trueForkContext
.makeNext(0, -1));
1087 * Makes a code path segment for the body part of a DoWhileStatement.
1091 const context
= this.loopContext
;
1092 const forkContext
= this.forkContext
;
1093 const bodySegments
= forkContext
.makeNext(-1, -1);
1096 context
.entrySegments
= bodySegments
;
1097 forkContext
.replaceHead(bodySegments
);
1101 * Makes a code path segment for the test part of a DoWhileStatement.
1102 * @param {boolean|undefined} test The test value (only when constant).
1105 makeDoWhileTest(test
) {
1106 const context
= this.loopContext
;
1107 const forkContext
= this.forkContext
;
1109 context
.test
= test
;
1111 // Creates paths of `continue` statements.
1112 if (!context
.continueForkContext
.empty
) {
1113 context
.continueForkContext
.add(forkContext
.head
);
1114 const testSegments
= context
.continueForkContext
.makeNext(0, -1);
1116 forkContext
.replaceHead(testSegments
);
1121 * Makes a code path segment for the test part of a ForStatement.
1122 * @param {boolean|undefined} test The test value (only when constant).
1126 const context
= this.loopContext
;
1127 const forkContext
= this.forkContext
;
1128 const endOfInitSegments
= forkContext
.head
;
1129 const testSegments
= forkContext
.makeNext(-1, -1);
1132 context
.test
= test
;
1133 context
.endOfInitSegments
= endOfInitSegments
;
1134 context
.continueDestSegments
= context
.testSegments
= testSegments
;
1135 forkContext
.replaceHead(testSegments
);
1139 * Makes a code path segment for the update part of a ForStatement.
1143 const context
= this.loopContext
;
1144 const choiceContext
= this.choiceContext
;
1145 const forkContext
= this.forkContext
;
1147 // Make the next paths of the test.
1148 if (context
.testSegments
) {
1149 finalizeTestSegmentsOfFor(
1155 context
.endOfInitSegments
= forkContext
.head
;
1159 const updateSegments
= forkContext
.makeDisconnected(-1, -1);
1161 context
.continueDestSegments
= context
.updateSegments
= updateSegments
;
1162 forkContext
.replaceHead(updateSegments
);
1166 * Makes a code path segment for the body part of a ForStatement.
1170 const context
= this.loopContext
;
1171 const choiceContext
= this.choiceContext
;
1172 const forkContext
= this.forkContext
;
1175 if (context
.updateSegments
) {
1176 context
.endOfUpdateSegments
= forkContext
.head
;
1178 // `update` -> `test`
1179 if (context
.testSegments
) {
1182 context
.endOfUpdateSegments
,
1183 context
.testSegments
1186 } else if (context
.testSegments
) {
1187 finalizeTestSegmentsOfFor(
1193 context
.endOfInitSegments
= forkContext
.head
;
1196 let bodySegments
= context
.endOfTestSegments
;
1198 if (!bodySegments
) {
1201 * If there is not the `test` part, the `body` path comes from the
1202 * `init` part and the `update` part.
1204 const prevForkContext
= ForkContext
.newEmpty(forkContext
);
1206 prevForkContext
.add(context
.endOfInitSegments
);
1207 if (context
.endOfUpdateSegments
) {
1208 prevForkContext
.add(context
.endOfUpdateSegments
);
1211 bodySegments
= prevForkContext
.makeNext(0, -1);
1213 context
.continueDestSegments
= context
.continueDestSegments
|| bodySegments
;
1214 forkContext
.replaceHead(bodySegments
);
1218 * Makes a code path segment for the left part of a ForInStatement and a
1223 const context
= this.loopContext
;
1224 const forkContext
= this.forkContext
;
1225 const leftSegments
= forkContext
.makeDisconnected(-1, -1);
1228 context
.prevSegments
= forkContext
.head
;
1229 context
.leftSegments
= context
.continueDestSegments
= leftSegments
;
1230 forkContext
.replaceHead(leftSegments
);
1234 * Makes a code path segment for the right part of a ForInStatement and a
1238 makeForInOfRight() {
1239 const context
= this.loopContext
;
1240 const forkContext
= this.forkContext
;
1241 const temp
= ForkContext
.newEmpty(forkContext
);
1243 temp
.add(context
.prevSegments
);
1244 const rightSegments
= temp
.makeNext(-1, -1);
1247 context
.endOfLeftSegments
= forkContext
.head
;
1248 forkContext
.replaceHead(rightSegments
);
1252 * Makes a code path segment for the body part of a ForInStatement and a
1257 const context
= this.loopContext
;
1258 const forkContext
= this.forkContext
;
1259 const temp
= ForkContext
.newEmpty(forkContext
);
1261 temp
.add(context
.endOfLeftSegments
);
1262 const bodySegments
= temp
.makeNext(-1, -1);
1264 // Make a path: `right` -> `left`.
1265 makeLooped(this, forkContext
.head
, context
.leftSegments
);
1268 context
.brokenForkContext
.add(forkContext
.head
);
1269 forkContext
.replaceHead(bodySegments
);
1272 //--------------------------------------------------------------------------
1273 // Control Statements
1274 //--------------------------------------------------------------------------
1277 * Creates new context for BreakStatement.
1278 * @param {boolean} breakable The flag to indicate it can break by
1279 * an unlabeled BreakStatement.
1280 * @param {string|null} label The label of this context.
1281 * @returns {Object} The new context.
1283 pushBreakContext(breakable
, label
) {
1284 this.breakContext
= {
1285 upper
: this.breakContext
,
1288 brokenForkContext
: ForkContext
.newEmpty(this.forkContext
)
1290 return this.breakContext
;
1294 * Removes the top item of the break context stack.
1295 * @returns {Object} The removed context.
1298 const context
= this.breakContext
;
1299 const forkContext
= this.forkContext
;
1301 this.breakContext
= context
.upper
;
1303 // Process this context here for other than switches and loops.
1304 if (!context
.breakable
) {
1305 const brokenForkContext
= context
.brokenForkContext
;
1307 if (!brokenForkContext
.empty
) {
1308 brokenForkContext
.add(forkContext
.head
);
1309 forkContext
.replaceHead(brokenForkContext
.makeNext(0, -1));
1317 * Makes a path for a `break` statement.
1319 * It registers the head segment to a context of `break`.
1320 * It makes new unreachable segment, then it set the head with the segment.
1321 * @param {string} label A label of the break statement.
1325 const forkContext
= this.forkContext
;
1327 if (!forkContext
.reachable
) {
1331 const context
= getBreakContext(this, label
);
1333 /* istanbul ignore else: foolproof (syntax error) */
1335 context
.brokenForkContext
.add(forkContext
.head
);
1338 forkContext
.replaceHead(forkContext
.makeUnreachable(-1, -1));
1342 * Makes a path for a `continue` statement.
1344 * It makes a looping path.
1345 * It makes new unreachable segment, then it set the head with the segment.
1346 * @param {string} label A label of the continue statement.
1349 makeContinue(label
) {
1350 const forkContext
= this.forkContext
;
1352 if (!forkContext
.reachable
) {
1356 const context
= getContinueContext(this, label
);
1358 /* istanbul ignore else: foolproof (syntax error) */
1360 if (context
.continueDestSegments
) {
1361 makeLooped(this, forkContext
.head
, context
.continueDestSegments
);
1363 // If the context is a for-in/of loop, this effects a break also.
1364 if (context
.type
=== "ForInStatement" ||
1365 context
.type
=== "ForOfStatement"
1367 context
.brokenForkContext
.add(forkContext
.head
);
1370 context
.continueForkContext
.add(forkContext
.head
);
1373 forkContext
.replaceHead(forkContext
.makeUnreachable(-1, -1));
1377 * Makes a path for a `return` statement.
1379 * It registers the head segment to a context of `return`.
1380 * It makes new unreachable segment, then it set the head with the segment.
1384 const forkContext
= this.forkContext
;
1386 if (forkContext
.reachable
) {
1387 getReturnContext(this).returnedForkContext
.add(forkContext
.head
);
1388 forkContext
.replaceHead(forkContext
.makeUnreachable(-1, -1));
1393 * Makes a path for a `throw` statement.
1395 * It registers the head segment to a context of `throw`.
1396 * It makes new unreachable segment, then it set the head with the segment.
1400 const forkContext
= this.forkContext
;
1402 if (forkContext
.reachable
) {
1403 getThrowContext(this).thrownForkContext
.add(forkContext
.head
);
1404 forkContext
.replaceHead(forkContext
.makeUnreachable(-1, -1));
1409 * Makes the final path.
1413 const segments
= this.currentSegments
;
1415 if (segments
.length
> 0 && segments
[0].reachable
) {
1416 this.returnedForkContext
.add(segments
);
1421 module
.exports
= CodePathState
;