Skip to content

Commit

Permalink
Handle out-of-order binding of identifiers to improve tree-shaking (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Apr 10, 2019
1 parent 479bf73 commit c3d73ff
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ast/nodes/Identifier.ts
Expand Up @@ -86,6 +86,7 @@ export default class Identifier extends NodeBase implements PatternNode {
recursionTracker: ImmutableEntityPathTracker,
origin: DeoptimizableEntity
): LiteralValueOrUnknown {
if (!this.bound) this.bind();
if (this.variable !== null) {
return this.variable.getLiteralValueAtPath(path, recursionTracker, origin);
}
Expand All @@ -97,6 +98,7 @@ export default class Identifier extends NodeBase implements PatternNode {
recursionTracker: ImmutableEntityPathTracker,
origin: DeoptimizableEntity
) {
if (!this.bound) this.bind();
if (this.variable !== null) {
return this.variable.getReturnExpressionWhenCalledAtPath(path, recursionTracker, origin);
}
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/simplify-return-expression/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'Simplifies conditionals in return expression'
};
14 changes: 14 additions & 0 deletions test/form/samples/simplify-return-expression/_expected.js
@@ -0,0 +1,14 @@
const test = () => {
console.log(foo());
console.log(bar());
};

const foo = () => {
return A;
};

const bar = () => {
return A;
};

export { test };
16 changes: 16 additions & 0 deletions test/form/samples/simplify-return-expression/main.js
@@ -0,0 +1,16 @@
export const test = () => {
console.log(foo());
console.log(bar());
};

const foo = () => {
return BUILD ? A : B;
};

const bar = () => {
return getBuild() ? A : B;
};

const getBuild = () => BUILD;

const BUILD = true;

0 comments on commit c3d73ff

Please sign in to comment.