/
Node.ts
369 lines (333 loc) · 11.6 KB
/
Node.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import type * as acorn from 'acorn';
import { locate, type Location } from 'locate-character';
import type MagicString from 'magic-string';
import type { AstContext } from '../../../Module';
import type { NormalizedTreeshakingOptions } from '../../../rollup/types';
import type { RollupAnnotation } from '../../../utils/commentAnnotations';
import { ANNOTATION_KEY, INVALID_COMMENT_KEY } from '../../../utils/commentAnnotations';
import type { NodeRenderOptions, RenderOptions } from '../../../utils/renderHelpers';
import type { DeoptimizableEntity } from '../../DeoptimizableEntity';
import type { Entity } from '../../Entity';
import {
createHasEffectsContext,
type HasEffectsContext,
type InclusionContext
} from '../../ExecutionContext';
import type { NodeInteractionAssigned } from '../../NodeInteractions';
import { INTERACTION_ASSIGNED } from '../../NodeInteractions';
import { getAndCreateKeys, keys } from '../../keys';
import type ChildScope from '../../scopes/ChildScope';
import { EMPTY_PATH, UNKNOWN_PATH } from '../../utils/PathTracker';
import type Variable from '../../variables/Variable';
import type * as NodeType from '../NodeType';
import type { InclusionOptions } from './Expression';
import { ExpressionEntity } from './Expression';
export interface GenericEsTreeNode extends acorn.Node {
[key: string]: any;
}
export const INCLUDE_PARAMETERS = 'variables' as const;
export type IncludeChildren = boolean | typeof INCLUDE_PARAMETERS;
export interface Node extends Entity {
annotations?: acorn.Comment[];
context: AstContext;
end: number;
esTreeNode: GenericEsTreeNode | null;
included: boolean;
keys: string[];
needsBoundaries?: boolean;
parent: Node | { type?: string };
preventChildBlockScope?: boolean;
start: number;
type: string;
variable?: Variable | null;
addExportedVariables(
variables: readonly Variable[],
exportNamesByVariable: ReadonlyMap<Variable, readonly string[]>
): void;
/**
* Called once all nodes have been initialised and the scopes have been
* populated.
*/
bind(): void;
/**
* Determine if this Node would have an effect on the bundle. This is usually
* true for already included nodes. Exceptions are e.g. break statements which
* only have an effect if their surrounding loop or switch statement is
* included.
* The options pass on information like this about the current execution path.
*/
hasEffects(context: HasEffectsContext): boolean;
/**
* Special version of hasEffects for assignment left-hand sides which ensures
* that accessor effects are checked as well. This is necessary to do from the
* child so that member expressions can use the correct this value.
* setAssignedValue needs to be called during initialise to use this.
*/
hasEffectsAsAssignmentTarget(context: HasEffectsContext, checkAccess: boolean): boolean;
/**
* Includes the node in the bundle. If the flag is not set, children are
* usually included if they are necessary for this node (e.g. a function body)
* or if they have effects. Necessary variables need to be included as well.
*/
include(
context: InclusionContext,
includeChildrenRecursively: IncludeChildren,
options?: InclusionOptions
): void;
/**
* Special version of include for assignment left-hand sides which ensures
* that accessors are handled correctly. This is necessary to do from the
* child so that member expressions can use the correct this value.
* setAssignedValue needs to be called during initialise to use this.
*/
includeAsAssignmentTarget(
context: InclusionContext,
includeChildrenRecursively: IncludeChildren,
deoptimizeAccess: boolean
): void;
render(code: MagicString, options: RenderOptions, nodeRenderOptions?: NodeRenderOptions): void;
/**
* Sets the assigned value e.g. for assignment expression left. This must be
* called during initialise in case hasEffects/includeAsAssignmentTarget are
* used.
*/
setAssignedValue(value: ExpressionEntity): void;
/**
* Start a new execution path to determine if this node has an effect on the
* bundle and should therefore be included. Included nodes should always be
* included again in subsequent visits as the inclusion of additional
* variables may require the inclusion of more child nodes in e.g. block
* statements.
*/
shouldBeIncluded(context: InclusionContext): boolean;
}
export type StatementNode = Node;
export interface ExpressionNode extends ExpressionEntity, Node {
isSkippedAsOptional?(origin: DeoptimizableEntity): boolean;
}
export interface ChainElement extends ExpressionNode {
optional: boolean;
isSkippedAsOptional(origin: DeoptimizableEntity): boolean;
}
export class NodeBase extends ExpressionEntity implements ExpressionNode {
/** Marked with #__NO_SIDE_EFFECTS__ annotation */
declare annotationNoSideEffects?: boolean;
/** Marked with #__PURE__ annotation */
declare annotationPure?: boolean;
declare annotations?: RollupAnnotation[];
context: AstContext;
declare end: number;
esTreeNode: acorn.Node | null;
keys: string[];
parent: Node | { context: AstContext; type: string };
declare scope: ChildScope;
declare start: number;
declare type: keyof typeof NodeType;
/**
* This will be populated during initialise if setAssignedValue is called.
*/
protected declare assignmentInteraction: NodeInteractionAssigned;
/**
* Nodes can apply custom deoptimizations once they become part of the
* executed code. To do this, they must initialize this as false, implement
* applyDeoptimizations and call this from include and hasEffects if they have
* custom handlers
*/
protected deoptimized = false;
constructor(
esTreeNode: GenericEsTreeNode,
parent: Node | { context: AstContext; type: string },
parentScope: ChildScope,
keepEsTreeNode = false
) {
super();
// Nodes can opt-in to keep the AST if needed during the build pipeline.
// Avoid true when possible as large AST takes up memory.
this.esTreeNode = keepEsTreeNode ? esTreeNode : null;
this.keys = keys[esTreeNode.type] || getAndCreateKeys(esTreeNode);
this.parent = parent;
this.context = parent.context;
this.createScope(parentScope);
this.parseNode(esTreeNode);
this.initialise();
this.context.magicString.addSourcemapLocation(this.start);
this.context.magicString.addSourcemapLocation(this.end);
}
addExportedVariables(
_variables: readonly Variable[],
_exportNamesByVariable: ReadonlyMap<Variable, readonly string[]>
): void {}
/**
* Override this to bind assignments to variables and do any initialisations
* that require the scopes to be populated with variables.
*/
bind(): void {
for (const key of this.keys) {
const value = (this as GenericEsTreeNode)[key];
if (Array.isArray(value)) {
for (const child of value) {
child?.bind();
}
} else if (value) {
value.bind();
}
}
}
/**
* Override if this node should receive a different scope than the parent
* scope.
*/
createScope(parentScope: ChildScope): void {
this.scope = parentScope;
}
hasEffects(context: HasEffectsContext): boolean {
if (!this.deoptimized) this.applyDeoptimizations();
for (const key of this.keys) {
const value = (this as GenericEsTreeNode)[key];
if (value === null) continue;
if (Array.isArray(value)) {
for (const child of value) {
if (child?.hasEffects(context)) return true;
}
} else if (value.hasEffects(context)) return true;
}
return false;
}
hasEffectsAsAssignmentTarget(context: HasEffectsContext, _checkAccess: boolean): boolean {
return (
this.hasEffects(context) ||
this.hasEffectsOnInteractionAtPath(EMPTY_PATH, this.assignmentInteraction, context)
);
}
include(
context: InclusionContext,
includeChildrenRecursively: IncludeChildren,
_options?: InclusionOptions
): void {
if (!this.deoptimized) this.applyDeoptimizations();
this.included = true;
for (const key of this.keys) {
const value = (this as GenericEsTreeNode)[key];
if (value === null) continue;
if (Array.isArray(value)) {
for (const child of value) {
child?.include(context, includeChildrenRecursively);
}
} else {
value.include(context, includeChildrenRecursively);
}
}
}
includeAsAssignmentTarget(
context: InclusionContext,
includeChildrenRecursively: IncludeChildren,
_deoptimizeAccess: boolean
) {
this.include(context, includeChildrenRecursively);
}
/**
* Override to perform special initialisation steps after the scope is
* initialised
*/
initialise(): void {}
insertSemicolon(code: MagicString): void {
if (code.original[this.end - 1] !== ';') {
code.appendLeft(this.end, ';');
}
}
parseNode(esTreeNode: GenericEsTreeNode, keepEsTreeNodeKeys?: string[]): void {
for (const [key, value] of Object.entries(esTreeNode)) {
// That way, we can override this function to add custom initialisation and then call super.parseNode
if (this.hasOwnProperty(key)) continue;
if (key.charCodeAt(0) === 95 /* _ */) {
if (key === ANNOTATION_KEY) {
const annotations = value as RollupAnnotation[];
this.annotations = annotations;
if ((this.context.options.treeshake as NormalizedTreeshakingOptions).annotations) {
this.annotationNoSideEffects = annotations.some(
comment => comment.annotationType === 'noSideEffects'
);
this.annotationPure = annotations.some(comment => comment.annotationType === 'pure');
}
} else if (key === INVALID_COMMENT_KEY) {
for (const { start, end } of value as acorn.Comment[])
this.context.magicString.remove(start, end);
}
} else if (typeof value !== 'object' || value === null) {
(this as GenericEsTreeNode)[key] = value;
} else if (Array.isArray(value)) {
(this as GenericEsTreeNode)[key] = [];
for (const child of value) {
(this as GenericEsTreeNode)[key].push(
child === null
? null
: new (this.context.getNodeConstructor(child.type))(
child,
this,
this.scope,
keepEsTreeNodeKeys?.includes(key)
)
);
}
} else {
(this as GenericEsTreeNode)[key] = new (this.context.getNodeConstructor(value.type))(
value,
this,
this.scope,
keepEsTreeNodeKeys?.includes(key)
);
}
}
}
render(code: MagicString, options: RenderOptions): void {
for (const key of this.keys) {
const value = (this as GenericEsTreeNode)[key];
if (value === null) continue;
if (Array.isArray(value)) {
for (const child of value) {
child?.render(code, options);
}
} else {
value.render(code, options);
}
}
}
setAssignedValue(value: ExpressionEntity): void {
this.assignmentInteraction = { args: [null, value], type: INTERACTION_ASSIGNED };
}
shouldBeIncluded(context: InclusionContext): boolean {
return this.included || (!context.brokenFlow && this.hasEffects(createHasEffectsContext()));
}
/**
* Just deoptimize everything by default so that when e.g. we do not track
* something properly, it is deoptimized.
* @protected
*/
protected applyDeoptimizations(): void {
this.deoptimized = true;
for (const key of this.keys) {
const value = (this as GenericEsTreeNode)[key];
if (value === null) continue;
if (Array.isArray(value)) {
for (const child of value) {
child?.deoptimizePath(UNKNOWN_PATH);
}
} else {
value.deoptimizePath(UNKNOWN_PATH);
}
}
this.context.requestTreeshakingPass();
}
}
export { NodeBase as StatementBase };
export function locateNode(node: Node): Location & { file: string } {
const location = locate(node.context.code, node.start, { offsetLine: 1 }) as Location & {
file: string;
};
location.file = node.context.fileName;
location.toString = () => JSON.stringify(location);
return location;
}
export function logNode(node: Node): string {
return node.context.code.slice(node.start, node.end);
}