Merge pull request #10883 from Microsoft/fix10876

Fix missing final label
This commit is contained in:
Ron Buckton
2016-09-13 10:10:57 -07:00
committed by GitHub
5 changed files with 103 additions and 1 deletions

View File

@@ -2629,7 +2629,7 @@ namespace ts {
* Flush the final label of the generator function body.
*/
function flushFinalLabel(operationIndex: number): void {
if (!lastOperationWasCompletion) {
if (isFinalLabelReachable(operationIndex)) {
tryEnterLabel(operationIndex);
withBlockStack = undefined;
writeReturn(/*expression*/ undefined, /*operationLocation*/ undefined);
@@ -2642,6 +2642,34 @@ namespace ts {
updateLabelExpressions();
}
/**
* Tests whether the final label of the generator function body
* is reachable by user code.
*/
function isFinalLabelReachable(operationIndex: number) {
// if the last operation was *not* a completion (return/throw) then
// the final label is reachable.
if (!lastOperationWasCompletion) {
return true;
}
// if there are no labels defined or referenced, then the final label is
// not reachable.
if (!labelOffsets || !labelExpressions) {
return false;
}
// if the label for this offset is referenced, then the final label
// is reachable.
for (let label = 0; label < labelOffsets.length; label++) {
if (labelOffsets[label] === operationIndex && labelExpressions[label]) {
return true;
}
}
return false;
}
/**
* Appends a case clause for the last label and sets the new label.
*