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

fix: handle conditional breaks in nested switch statement cases #4937

Merged
merged 5 commits into from Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions src/ast/ExecutionContext.ts
Expand Up @@ -21,6 +21,7 @@ interface ControlFlowContext {
}

export interface InclusionContext extends ControlFlowContext {
existedBroken: boolean;
includedCallArguments: Set<Entity>;
}

Expand All @@ -37,6 +38,7 @@ export interface HasEffectsContext extends ControlFlowContext {
export function createInclusionContext(): InclusionContext {
return {
brokenFlow: BROKEN_FLOW_NONE,
existedBroken: false,
includedCallArguments: new Set(),
includedLabels: new Set()
};
Expand Down
4 changes: 4 additions & 0 deletions src/ast/nodes/IfStatement.ts
Expand Up @@ -2,6 +2,7 @@ import type MagicString from 'magic-string';
import type { RenderOptions } from '../../utils/renderHelpers';
import type { DeoptimizableEntity } from '../DeoptimizableEntity';
import {
BROKEN_FLOW_BREAK_CONTINUE,
BROKEN_FLOW_NONE,
type HasEffectsContext,
type InclusionContext
Expand Down Expand Up @@ -173,10 +174,13 @@ export default class IfStatement extends StatementBase implements DeoptimizableE
this.consequent.include(context, false, { asSingleStatement: true });
// eslint-disable-next-line unicorn/consistent-destructuring
consequentBrokenFlow = context.brokenFlow;
context.existedBroken ||= consequentBrokenFlow === BROKEN_FLOW_BREAK_CONTINUE;
context.brokenFlow = brokenFlow;
}
if (this.alternate?.shouldBeIncluded(context)) {
this.alternate.include(context, false, { asSingleStatement: true });
// eslint-disable-next-line unicorn/consistent-destructuring
context.existedBroken ||= context.brokenFlow === BROKEN_FLOW_BREAK_CONTINUE;
context.brokenFlow =
// eslint-disable-next-line unicorn/consistent-destructuring
context.brokenFlow < consequentBrokenFlow ? context.brokenFlow : consequentBrokenFlow;
Expand Down
10 changes: 9 additions & 1 deletion src/ast/nodes/SwitchCase.ts
Expand Up @@ -5,7 +5,12 @@ import {
type RenderOptions,
renderStatementList
} from '../../utils/renderHelpers';
import type { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import {
BROKEN_FLOW_BREAK_CONTINUE,
BROKEN_FLOW_ERROR_RETURN_LABEL,
type HasEffectsContext,
type InclusionContext
} from '../ExecutionContext';
import type * as NodeType from './NodeType';
import {
type ExpressionNode,
Expand Down Expand Up @@ -36,6 +41,9 @@ export default class SwitchCase extends NodeBase {
if (includeChildrenRecursively || node.shouldBeIncluded(context))
node.include(context, includeChildrenRecursively);
}
if (context.existedBroken && context.brokenFlow === BROKEN_FLOW_ERROR_RETURN_LABEL) {
context.brokenFlow = BROKEN_FLOW_BREAK_CONTINUE;
}
}

render(code: MagicString, options: RenderOptions, nodeRenderOptions?: NodeRenderOptions): void {
Expand Down
3 changes: 3 additions & 0 deletions test/function/samples/switch-break/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'if switch does not always throw an error, retain the following break'
};
21 changes: 21 additions & 0 deletions test/function/samples/switch-break/main.js
@@ -0,0 +1,21 @@
function issue(obj) {
switch (obj.field1) {
case 'baz':
switch (obj.field2) {
case 'value': {
if (obj.field1) {
if (obj.field1) {
break;
}
}
throw new Error(`error 1`);
}
default:
throw new Error(`error 2`);
}
break; // retained
default:
throw new Error('error 3');
}
}
issue({ field1: 'baz', field2: 'value' });