Skip to content

Commit

Permalink
Handle conditional breaks in do-while loops with unconditional return (
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Oct 20, 2019
1 parent 06fb16a commit 24063a6
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 77 deletions.
14 changes: 3 additions & 11 deletions src/ast/nodes/DoWhileStatement.ts
@@ -1,8 +1,4 @@
import {
BROKEN_FLOW_BREAK_CONTINUE,
HasEffectsContext,
InclusionContext
} from '../ExecutionContext';
import { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import * as NodeType from './NodeType';
import { ExpressionNode, IncludeChildren, StatementBase, StatementNode } from './shared/Node';

Expand All @@ -22,9 +18,7 @@ export default class DoWhileStatement extends StatementBase {
if (this.body.hasEffects(context)) return true;
context.ignore.breaks = breaks;
context.ignore.continues = continues;
if (context.brokenFlow === BROKEN_FLOW_BREAK_CONTINUE) {
context.brokenFlow = brokenFlow;
}
context.brokenFlow = brokenFlow;
return false;
}

Expand All @@ -33,8 +27,6 @@ export default class DoWhileStatement extends StatementBase {
this.test.include(context, includeChildrenRecursively);
const { brokenFlow } = context;
this.body.include(context, includeChildrenRecursively);
if (context.brokenFlow === BROKEN_FLOW_BREAK_CONTINUE) {
context.brokenFlow = brokenFlow;
}
context.brokenFlow = brokenFlow;
}
}

This file was deleted.

This file was deleted.

This file was deleted.

5 changes: 5 additions & 0 deletions test/form/samples/break-control-flow/loop-errors/_expected.js
Expand Up @@ -17,8 +17,13 @@ try {
function doWhileLoop() {
console.log(hoisted);
do {
if (globalThis.unknown) {
break;
}
throw new Error();
} while (globalThis.unknown);
console.log('retained');
throw new Error();
do {
var hoisted;
} while (globalThis.unknown);
Expand Down
6 changes: 5 additions & 1 deletion test/form/samples/break-control-flow/loop-errors/main.js
Expand Up @@ -20,10 +20,14 @@ try {
function doWhileLoop() {
console.log(hoisted);
do {
if (globalThis.unknown) {
break;
}
throw new Error();
console.log('removed');
} while (globalThis.unknown);
console.log('removed');
console.log('retained');
throw new Error();
do {
var hoisted;
console.log('removed');
Expand Down
3 changes: 3 additions & 0 deletions test/function/samples/do-while-conditional-break/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'handles conditional breaks in do-while loops with unconditional return'
};
12 changes: 12 additions & 0 deletions test/function/samples/do-while-conditional-break/main.js
@@ -0,0 +1,12 @@
function test(condition) {
do {
if (condition) {
break;
}
return false;
} while (true);
return true;
}

assert.equal(test(false), false);
assert.equal(test(true), true);

0 comments on commit 24063a6

Please sign in to comment.