Skip to content
This repository was archived by the owner on Sep 10, 2023. It is now read-only.

Commit 69fef6b

Browse files
committed
fix: fix label for loop
1 parent 9139d2d commit 69fef6b

File tree

1 file changed

+17
-34
lines changed

1 file changed

+17
-34
lines changed

src/evaluate.ts

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ const visitors: EvaluateMap = {
180180
},
181181
LabeledStatement(path) {
182182
const label = path.node.label as types.Identifier;
183-
evaluate(
183+
return evaluate(
184184
path.createChild(path.node.body, path.scope, { labelName: label.name })
185185
);
186186
},
@@ -416,15 +416,6 @@ const visitors: EvaluateMap = {
416416
evaluate(path.createChild(node.init, forScope));
417417
}
418418

419-
const labelVarName: string = "@label-" + labelName;
420-
const labelBreakVarName: string = "@break-" + labelName;
421-
422-
// set the label for loop
423-
if (labelName) {
424-
// var label
425-
forScope.const(labelVarName, labelName);
426-
}
427-
428419
function update(): void {
429420
if (node.update) {
430421
evaluate(path.createChild(node.update, forScope));
@@ -441,44 +432,36 @@ const visitors: EvaluateMap = {
441432
const loopScope = forScope.fork("for_child");
442433
loopScope.isolated = false;
443434

444-
if (!test() || forScope.hasOwnBinding(labelBreakVarName)) {
435+
if (!test()) {
445436
break;
446437
}
447438

448-
const result = evaluate(
439+
const signal = evaluate(
449440
path.createChild(node.body, loopScope, { labelName: undefined })
450441
);
451442

452-
if (Signal.isBreak(result)) {
453-
if (result.value) {
454-
// Break specified loop
455-
// first, find the scope
456-
const labelScope = loopScope.locate("@label-" + result.value);
457-
458-
// if scope exist, set break mark
459-
if (labelScope && labelScope.origin) {
460-
labelScope.origin.var("@break-" + result.value, true);
461-
return new Signal("break", result.value);
462-
}
443+
if (Signal.isBreak(signal)) {
444+
if (!signal.value) {
445+
break;
463446
}
464-
465-
break;
466-
} else if (Signal.isContinue(result)) {
467-
if (result.value) {
468-
if (result.value === labelName) {
447+
if (signal.value === labelName) {
448+
break;
449+
}
450+
return new Signal("break", signal.value);
451+
} else if (Signal.isContinue(signal)) {
452+
if (signal.value) {
453+
if (signal.value === labelName) {
469454
update();
470455
continue;
471456
}
472-
return new Signal("continue", result.value);
457+
return new Signal("continue", signal.value);
473458
}
474459
continue;
475-
} else if (Signal.isReturn(result)) {
476-
return result;
460+
} else if (Signal.isReturn(signal)) {
461+
return signal;
477462
}
478463

479-
if (!forScope.hasOwnBinding(labelBreakVarName)) {
480-
update();
481-
}
464+
update();
482465
}
483466
},
484467
// @es2015 for of

0 commit comments

Comments
 (0)