Skip to content

Commit 810a680

Browse files
committed
Fix missing final label.
Fixes #10876
1 parent 439fe79 commit 810a680

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/compiler/transformers/generators.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2629,7 +2629,7 @@ namespace ts {
26292629
* Flush the final label of the generator function body.
26302630
*/
26312631
function flushFinalLabel(operationIndex: number): void {
2632-
if (!lastOperationWasCompletion) {
2632+
if (isFinalLabelReachable(operationIndex)) {
26332633
tryEnterLabel(operationIndex);
26342634
withBlockStack = undefined;
26352635
writeReturn(/*expression*/ undefined, /*operationLocation*/ undefined);
@@ -2642,6 +2642,34 @@ namespace ts {
26422642
updateLabelExpressions();
26432643
}
26442644

2645+
/**
2646+
* Tests whether the final label of the generator function body
2647+
* is reachable by user code.
2648+
*/
2649+
function isFinalLabelReachable(operationIndex: number) {
2650+
// if the last operation was *not* a completion (return/throw) then
2651+
// the final label is reachable.
2652+
if (!lastOperationWasCompletion) {
2653+
return true;
2654+
}
2655+
2656+
// if there are no labels defined or referenced, then the final label is
2657+
// not reachable.
2658+
if (!labelOffsets || !labelExpressions) {
2659+
return false;
2660+
}
2661+
2662+
// if the label for this offset is referenced, then the final label
2663+
// is reachable.
2664+
for (let label = 0; label < labelOffsets.length; label++) {
2665+
if (labelOffsets[label] === operationIndex && labelExpressions[label]) {
2666+
return true;
2667+
}
2668+
}
2669+
2670+
return false;
2671+
}
2672+
26452673
/**
26462674
* Appends a case clause for the last label and sets the new label.
26472675
*

0 commit comments

Comments
 (0)