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

Make sure logical expressions always check the left argument for side effects #3438

Merged
merged 1 commit into from Mar 12, 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
9 changes: 6 additions & 3 deletions src/ast/nodes/LogicalExpression.ts
Expand Up @@ -97,10 +97,13 @@ export default class LogicalExpression extends NodeBase implements Deoptimizable
}

hasEffects(context: HasEffectsContext): boolean {
if (this.usedBranch === null) {
return this.left.hasEffects(context) || this.right.hasEffects(context);
if (this.left.hasEffects(context)) {
return true;
}
if (this.usedBranch !== this.left) {
return this.right.hasEffects(context);
}
return this.usedBranch.hasEffects(context);
return false;
}

hasEffectsWhenAccessedAtPath(path: ObjectPath, context: HasEffectsContext): boolean {
Expand Down
3 changes: 3 additions & 0 deletions test/function/samples/reassigned-return-expression/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'does not bind return expressions before assignments are bound (#3382)'
};
17 changes: 17 additions & 0 deletions test/function/samples/reassigned-return-expression/main.js
@@ -0,0 +1,17 @@
function getIcon() {
var icon = undefined;
icon = { code: true };
return icon;
}

function main() {
var a = getIcon() || {
code: undefined
};
if (!a.code) {
return 'broken';
}
return 'works';
}

assert.strictEqual(main(), 'works');