forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCallExpression.ts
151 lines (139 loc) · 4.78 KB
/
CallExpression.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import type MagicString from 'magic-string';
import type { NormalizedTreeshakingOptions } from '../../rollup/types';
import { BLANK } from '../../utils/blank';
import { LOGLEVEL_WARN } from '../../utils/logging';
import { logCannotCallNamespace, logEval } from '../../utils/logs';
import { renderCallArguments } from '../../utils/renderCallArguments';
import { type NodeRenderOptions, type RenderOptions } from '../../utils/renderHelpers';
import type { DeoptimizableEntity } from '../DeoptimizableEntity';
import type { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import { INTERACTION_CALLED } from '../NodeInteractions';
import { EMPTY_PATH, type PathTracker, SHARED_RECURSION_TRACKER } from '../utils/PathTracker';
import Identifier from './Identifier';
import MemberExpression from './MemberExpression';
import type * as NodeType from './NodeType';
import type SpreadElement from './SpreadElement';
import type Super from './Super';
import { Flag, isFlagSet, setFlag } from './shared/BitFlags';
import CallExpressionBase from './shared/CallExpressionBase';
import { type ExpressionEntity, UNKNOWN_RETURN_EXPRESSION } from './shared/Expression';
import type { ChainElement, ExpressionNode, IncludeChildren } from './shared/Node';
import { INCLUDE_PARAMETERS } from './shared/Node';
export default class CallExpression
extends CallExpressionBase
implements DeoptimizableEntity, ChainElement
{
declare arguments: (ExpressionNode | SpreadElement)[];
declare callee: ExpressionNode | Super;
declare type: NodeType.tCallExpression;
/** Marked with #__PURE__ annotation */
declare annotationPure?: boolean;
get optional(): boolean {
return isFlagSet(this.flags, Flag.optional);
}
set optional(value: boolean) {
this.flags = setFlag(this.flags, Flag.optional, value);
}
bind(): void {
super.bind();
if (this.callee instanceof Identifier) {
const variable = this.scope.findVariable(this.callee.name);
if (variable.isNamespace) {
this.scope.context.log(LOGLEVEL_WARN, logCannotCallNamespace(this.callee.name), this.start);
}
if (this.callee.name === 'eval') {
this.scope.context.log(LOGLEVEL_WARN, logEval(this.scope.context.module.id), this.start);
}
}
this.interaction = {
args: [
this.callee instanceof MemberExpression && !this.callee.variable
? this.callee.object
: null,
...this.arguments
],
type: INTERACTION_CALLED,
withNew: false
};
}
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)
);
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
if (!this.deoptimized) this.applyDeoptimizations();
if (includeChildrenRecursively) {
super.include(context, includeChildrenRecursively);
if (
includeChildrenRecursively === INCLUDE_PARAMETERS &&
this.callee instanceof Identifier &&
this.callee.variable
) {
this.callee.variable.markCalledFromTryStatement();
}
} else {
this.included = true;
this.callee.include(context, false);
}
this.callee.includeCallArguments(context, this.arguments);
}
initialise() {
super.initialise();
if (
this.annotations &&
(this.scope.context.options.treeshake as NormalizedTreeshakingOptions).annotations
) {
this.annotationPure = this.annotations.some(comment => comment.type === 'pure');
}
}
isSkippedAsOptional(origin: DeoptimizableEntity): boolean {
return (
(this.callee as ExpressionNode).isSkippedAsOptional?.(origin) ||
(this.optional &&
this.callee.getLiteralValueAtPath(EMPTY_PATH, SHARED_RECURSION_TRACKER, origin) == null)
);
}
render(
code: MagicString,
options: RenderOptions,
{ renderedSurroundingElement }: NodeRenderOptions = BLANK
): void {
this.callee.render(code, options, {
isCalleeOfRenderedParent: true,
renderedSurroundingElement
});
renderCallArguments(code, options, this);
}
protected applyDeoptimizations(): void {
this.deoptimized = true;
this.callee.deoptimizeArgumentsOnInteractionAtPath(
this.interaction,
EMPTY_PATH,
SHARED_RECURSION_TRACKER
);
this.scope.context.requestTreeshakingPass();
}
protected getReturnExpression(
recursionTracker: PathTracker = SHARED_RECURSION_TRACKER
): [expression: ExpressionEntity, isPure: boolean] {
if (this.returnExpression === null) {
this.returnExpression = UNKNOWN_RETURN_EXPRESSION;
return (this.returnExpression = this.callee.getReturnExpressionWhenCalledAtPath(
EMPTY_PATH,
this.interaction,
recursionTracker,
this
));
}
return this.returnExpression;
}
}