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

Commit

Permalink
fix: continue SwitchStatement can not continue parent loop
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Apr 30, 2018
1 parent bbae392 commit ebd0dec
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,9 @@ const visitors: EvaluateMap = {
if (Signal.isBreak(result)) {
break;
} else if (Signal.isContinue(result)) {
continue;
// SwitchStatement can not use continue keyword
// but it can continue parent loop, like for, for-in, for-of, while
return result;
} else if (Signal.isReturn(result)) {
return result;
}
Expand Down
31 changes: 31 additions & 0 deletions test/ecma5/switch/SwitchStatement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,34 @@ module.exports = t;
t.deepEqual(func("axetroy"), "hi axetroy");
t.deepEqual(func("aa"), "hello world");
});

test("SwitchStatement with continue", t => {
const sandbox: any = vm.createContext({});

const func: any = vm.runInContext(
`
function t(type) {
const result = [];
let i = 0;
while (i < 5) {
i++;
switch (type + "") {
case "0":
continue;
}
result.push(i);
}
return result;
}
module.exports = t;
`,
sandbox
);

// t.deepEqual(func(1), [1, 2, 3, 4, 5]);
// t.deepEqual(func(2), [1, 2, 3, 4, 5]);

// the will loop will be continue
t.deepEqual(func(0), []);
});

0 comments on commit ebd0dec

Please sign in to comment.