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

Do not entirely move else branch if another else branch might accidentally be referenced. #3725

Merged
merged 2 commits into from
Aug 15, 2020
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
17 changes: 17 additions & 0 deletions src/ast/nodes/IfStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DeoptimizableEntity } from '../DeoptimizableEntity';
import { BROKEN_FLOW_NONE, HasEffectsContext, InclusionContext } from '../ExecutionContext';
import { EMPTY_PATH, SHARED_RECURSION_TRACKER } from '../utils/PathTracker';
import { LiteralValueOrUnknown, UnknownValue } from '../values';
import BlockStatement from './BlockStatement';
import * as NodeType from './NodeType';
import { ExpressionNode, IncludeChildren, StatementBase, StatementNode } from './shared/Node';

Expand Down Expand Up @@ -86,6 +87,8 @@ export default class IfStatement extends StatementBase implements DeoptimizableE
code.prependLeft(this.alternate.start, ' ');
}
this.alternate.render(code, options);
} else if (this.shouldKeepAlternateBranch()) {
code.overwrite(this.alternate.start, this.alternate.end, ';');
} else {
code.remove(this.consequent.end, this.alternate.end);
}
Expand Down Expand Up @@ -142,4 +145,18 @@ export default class IfStatement extends StatementBase implements DeoptimizableE
context.brokenFlow < consequentBrokenFlow ? context.brokenFlow : consequentBrokenFlow;
}
}

private shouldKeepAlternateBranch() {
let currentParent = this.parent;
do {
if (currentParent instanceof IfStatement && currentParent.alternate) {
return true;
}
if (currentParent instanceof BlockStatement) {
return false;
}
currentParent = (currentParent as any).parent;
} while (currentParent);
return false;
}
}
7 changes: 7 additions & 0 deletions test/function/samples/simplified-nested-if/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
description:
'do not catch else branches from parent if statements when simplifiying if-statements',
context: {
unknown: true
}
};
24 changes: 24 additions & 0 deletions test/function/samples/simplified-nested-if/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export let foo = 0;

if (unknown)
for (var x = 1; x-- > 0; )
if (!unknown) foo++;
else 'Side effect free code to be dropped';
else throw new Error("Shouldn't be reached");

if (unknown)
for (var x = 1; x-- > 0; )
if (foo++, false) foo++;
else 'Side effect free code to be dropped';
else throw new Error("Shouldn't be reached");

if (unknown) {
for (var x = 1; x-- > 0; )
if (!unknown) foo++;
else 'Side effect free code to be dropped';
} else throw new Error("Shouldn't be reached");

if (unknown)
for (var x = 1; x-- > 0; )
if (!unknown) foo++;
else 'Side effect free code to be dropped';