Skip to content

Commit c3d73ff

Browse files
authored
Handle out-of-order binding of identifiers to improve tree-shaking (#2803)
1 parent 479bf73 commit c3d73ff

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

src/ast/nodes/Identifier.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export default class Identifier extends NodeBase implements PatternNode {
8686
recursionTracker: ImmutableEntityPathTracker,
8787
origin: DeoptimizableEntity
8888
): LiteralValueOrUnknown {
89+
if (!this.bound) this.bind();
8990
if (this.variable !== null) {
9091
return this.variable.getLiteralValueAtPath(path, recursionTracker, origin);
9192
}
@@ -97,6 +98,7 @@ export default class Identifier extends NodeBase implements PatternNode {
9798
recursionTracker: ImmutableEntityPathTracker,
9899
origin: DeoptimizableEntity
99100
) {
101+
if (!this.bound) this.bind();
100102
if (this.variable !== null) {
101103
return this.variable.getReturnExpressionWhenCalledAtPath(path, recursionTracker, origin);
102104
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
description: 'Simplifies conditionals in return expression'
3+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const test = () => {
2+
console.log(foo());
3+
console.log(bar());
4+
};
5+
6+
const foo = () => {
7+
return A;
8+
};
9+
10+
const bar = () => {
11+
return A;
12+
};
13+
14+
export { test };
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export const test = () => {
2+
console.log(foo());
3+
console.log(bar());
4+
};
5+
6+
const foo = () => {
7+
return BUILD ? A : B;
8+
};
9+
10+
const bar = () => {
11+
return getBuild() ? A : B;
12+
};
13+
14+
const getBuild = () => BUILD;
15+
16+
const BUILD = true;

0 commit comments

Comments
 (0)