Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle conditional breaks in do-while loops with unconditional return #3180

Merged
merged 2 commits into from Oct 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);