10000 Allow treeshaking of arguments of functions that are returned by cond… · rollup/rollup@a2d8267 · GitHub
[go: up one dir, main page]

Skip to content

Commit a2d8267

Browse files
authored
Allow treeshaking of arguments of functions that are returned by conditional expressions (#3680)
1 parent aa68627 commit a2d8267

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

src/ast/nodes/ConditionalExpression.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import * as NodeType from './NodeType';
2323
import { ExpressionEntity } from './shared/Expression';
2424
import { MultiExpression } from './shared/MultiExpression';
2525
import { ExpressionNode, IncludeChildren, NodeBase } from './shared/Node';
26+
import SpreadElement from './SpreadElement';
2627

2728
export default class ConditionalExpression extends NodeBase implements DeoptimizableEntity {
2829
alternate!: ExpressionNode;
@@ -154,6 +155,15 @@ export default class ConditionalExpression extends NodeBase implements Deoptimiz
154155
}
155156
}
156157

158+
includeCallArguments(context: InclusionContext, args: (ExpressionNode | SpreadElement)[]): void {
159+
if (this.usedBranch === null) {
160+
this.consequent.includeCallArguments(context, args);
161+
this.alternate.includeCallArguments(context, args);
162+
} else {
163+
this.usedBranch.includeCallArguments(context, args);
164+
}
165+
}
166+
157167
render(
158168
code: MagicString,
159169
options: RenderOptions,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
description: 'tracks tree-shaking of arguments through simplified conditionals'
3+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function ignoringArgs() {
2+
return 'no args';
3+
}
4+
5+
const handler = ignoringArgs;
6+
7+
console.log(handler());
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function ignoringArgs() {
2+
return 'no args';
3+
}
4+
5+
function takingArgs(arg) {
6+
return arg;
7+
}
8+
9+
const handler = false ? takingArgs : ignoringArgs;
10+
11+
console.log(handler('should be removed'));

0 commit comments

Comments
 (0)
0