Skip to content

Commit

Permalink
Make sure logical expressions always check the left argument for side…
Browse files Browse the repository at this point in the history
…-effects (#3438)
  • Loading branch information
lukastaegert committed Mar 12, 2020
1 parent 2895a55 commit 5a45128
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
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');

0 comments on commit 5a45128

Please sign in to comment.