forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewExpression.ts
79 lines (72 loc) · 2.65 KB
/
NewExpression.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import type MagicString from 'magic-string';
import type { NormalizedTreeshakingOptions } from '../../rollup/types';
import { renderCallArguments } from '../../utils/renderCallArguments';
import type { RenderOptions } from '../../utils/renderHelpers';
import type { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import type { NodeInteraction, NodeInteractionCalled } from '../NodeInteractions';
import { INTERACTION_ACCESSED, INTERACTION_CALLED } from '../NodeInteractions';
import { EMPTY_PATH, type ObjectPath, SHARED_RECURSION_TRACKER } from '../utils/PathTracker';
import type * as NodeType from './NodeType';
import type { ExpressionNode, IncludeChildren } from './shared/Node';
import { NodeBase } from './shared/Node';
export default class NewExpression extends NodeBase {
declare arguments: ExpressionNode[];
declare callee: ExpressionNode;
declare type: NodeType.tNewExpression;
private declare interaction: NodeInteractionCalled;
/** Marked with #__PURE__ annotation */
declare annotationPure?: boolean;
hasEffects(context: HasEffectsContext): boolean {
if (!this.deoptimized) this.applyDeoptimizations();
for (const argument of this.arguments) {
if (argument.hasEffects(context)) return true;
}
if (this.annotationPure) {
return false;
}
return (
this.callee.hasEffects(context) ||
this.callee.hasEffectsOnInteractionAtPath(EMPTY_PATH, this.interaction, context)
);
}
hasEffectsOnInteractionAtPath(path: ObjectPath, { type }: NodeInteraction): boolean {
return path.length > 0 || type !== INTERACTION_ACCESSED;
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
if (!this.deoptimized) this.applyDeoptimizations();
if (includeChildrenRecursively) {
super.include(context, includeChildrenRecursively);
} else {
this.included = true;
this.callee.include(context, false);
}
this.callee.includeCallArguments(context, this.arguments);
}
initialise(): void {
super.initialise();
this.interaction = {
args: [null, ...this.arguments],
type: INTERACTION_CALLED,
withNew: true
};
if (
this.annotations &&
(this.scope.context.options.treeshake as NormalizedTreeshakingOptions).annotations
) {
this.annotationPure = this.annotations.some(comment => comment.type === 'pure');
}
}
render(code: MagicString, options: RenderOptions) {
this.callee.render(code, options);
renderCallArguments(code, options, this);
}
protected applyDeoptimizations(): void {
this.deoptimized = true;
this.callee.deoptimizeArgumentsOnInteractionAtPath(
this.interaction,
EMPTY_PATH,
SHARED_RECURSION_TRACKER
);
this.scope.context.requestTreeshakingPass();
}
}