/
Node.ts
256 lines (226 loc) · 7.92 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
import * as acorn from 'acorn';
import { locate } from 'locate-character';
import MagicString from 'magic-string';
import { AstContext } from '../../../Module';
import { NodeRenderOptions, RenderOptions } from '../../../utils/renderHelpers';
import { Entity } from '../../Entity';
import {
createHasEffectsContext,
HasEffectsContext,
InclusionContext
} from '../../ExecutionContext';
import { getAndCreateKeys, keys } from '../../keys';
import ChildScope from '../../scopes/ChildScope';
import Variable from '../../variables/Variable';
import * as NodeType from '../NodeType';
import { ExpressionEntity } from './Expression';
export interface GenericEsTreeNode extends acorn.Node {
[key: string]: any;
}
export const INCLUDE_PARAMETERS: 'variables' = 'variables';
export type IncludeChildren = boolean | typeof INCLUDE_PARAMETERS;
export interface Annotation {
comment?: acorn.Comment;
pure?: boolean;
}
export interface Node extends Entity {
annotations?: Annotation[];
context: AstContext;
end: number;
esTreeNode: GenericEsTreeNode;
included: boolean;
keys: string[];
needsBoundaries?: boolean;
parent: Node | { type?: string };
preventChildBlockScope?: boolean;
start: number;
type: string;
variable?: Variable | null;
addExportedVariables(variables: Variable[], exportNamesByVariable: Map<Variable, 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;
/**
* 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): void;
/**
* Alternative version of include to override the default behaviour of
* declarations to not include the id by default if the declarator has an effect.
*/
includeAsSingleStatement(
context: InclusionContext,
includeChildrenRecursively: IncludeChildren
): void;
render(code: MagicString, options: RenderOptions, nodeRenderOptions?: NodeRenderOptions): 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 interface StatementNode extends Node {}
export interface ExpressionNode extends ExpressionEntity, Node {}
export class NodeBase extends ExpressionEntity implements ExpressionNode {
annotations?: Annotation[];
context: AstContext;
end!: number;
esTreeNode: acorn.Node;
keys: string[];
parent: Node | { context: AstContext; type: string };
scope!: ChildScope;
start!: number;
type!: keyof typeof NodeType;
// 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?: boolean;
constructor(
esTreeNode: GenericEsTreeNode,
parent: Node | { context: AstContext; type: string },
parentScope: ChildScope
) {
super();
this.esTreeNode = esTreeNode;
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: Variable[],
_exportNamesByVariable: Map<Variable, string[]>
): void {}
/**
* Override this to bind assignments to variables and do any initialisations that
* require the scopes to be populated with variables.
*/
bind() {
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 !== null) child.bind();
}
} else {
value.bind();
}
}
}
/**
* Override if this node should receive a different scope than the parent scope.
*/
createScope(parentScope: ChildScope) {
this.scope = parentScope;
}
hasEffects(context: HasEffectsContext): boolean {
if (this.deoptimized === false) 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 !== null && child.hasEffects(context)) return true;
}
} else if (value.hasEffects(context)) return true;
}
return false;
}
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren) {
if (this.deoptimized === false) 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) {
if (child !== null) child.include(context, includeChildrenRecursively);
}
} else {
value.include(context, includeChildrenRecursively);
}
}
}
includeAsSingleStatement(context: InclusionContext, includeChildrenRecursively: IncludeChildren) {
this.include(context, includeChildrenRecursively);
}
/**
* Override to perform special initialisation steps after the scope is initialised
*/
initialise() {}
insertSemicolon(code: MagicString) {
if (code.original[this.end - 1] !== ';') {
code.appendLeft(this.end, ';');
}
}
parseNode(esTreeNode: GenericEsTreeNode) {
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 === '_rollupAnnotations') {
this.annotations = value;
} 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.nodeConstructors[child.type] ||
this.context.nodeConstructors.UnknownNode)(child, this, this.scope)
);
}
} else {
(this as GenericEsTreeNode)[key] = new (this.context.nodeConstructors[value.type] ||
this.context.nodeConstructors.UnknownNode)(value, this, this.scope);
}
}
}
render(code: MagicString, options: RenderOptions) {
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 !== null) child.render(code, options);
}
} else {
value.render(code, options);
}
}
}
shouldBeIncluded(context: InclusionContext): boolean {
return this.included || (!context.brokenFlow && this.hasEffects(createHasEffectsContext()));
}
protected applyDeoptimizations(): void {}
}
export { NodeBase as StatementBase };
// useful for debugging
export function locateNode(node: Node) {
const location = locate(node.context.code, node.start, { offsetLine: 1 });
(location as any).file = node.context.fileName;
location.toString = () => JSON.stringify(location);
return location;
}
export function logNode(node: Node) {
return node.context.code.slice(node.start, node.end);
}