-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Node.ts
269 lines (235 loc) · 7.87 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
import { locate } from 'locate-character';
import MagicString from 'magic-string';
import { AstContext } from '../../../Module';
import { NodeRenderOptions, RenderOptions } from '../../../utils/renderHelpers';
import CallOptions from '../../CallOptions';
import { DeoptimizableEntity } from '../../DeoptimizableEntity';
import { Entity } from '../../Entity';
import { ExecutionPathOptions } from '../../ExecutionPathOptions';
import { getAndCreateKeys, keys } from '../../keys';
import Scope from '../../scopes/Scope';
import { ImmutableEntityPathTracker } from '../../utils/ImmutableEntityPathTracker';
import { LiteralValueOrUnknown, ObjectPath, UNKNOWN_EXPRESSION, UNKNOWN_VALUE } from '../../values';
import Variable from '../../variables/Variable';
import { ExpressionEntity } from './Expression';
export interface GenericEsTreeNode {
type: string;
[key: string]: any;
}
export interface Node extends Entity {
end: number;
included: boolean;
keys: string[];
context: AstContext;
parent: Node | { type?: string };
start: number;
type: string;
needsBoundaries?: boolean;
preventChildBlockScope?: boolean;
variable?: Variable | null;
/**
* Called once all nodes have been initialised and the scopes have been populated.
*/
bind(): void;
/**
* Declare a new variable with the optional initialisation.
*/
declare(kind: string, init: ExpressionEntity | null): 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(options: ExecutionPathOptions): 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(includeAllChildrenRecursively: boolean): void;
/**
* Alternative version of include to override the default behaviour of
* declarations to only include nodes for declarators that have an effect. Necessary
* for for-loops that do not use a declared loop variable.
*/
includeWithAllDeclaredVariables(includeAllChildrenRecursively: boolean): 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(): boolean;
}
export interface StatementNode extends Node {}
export interface ExpressionNode extends ExpressionEntity, Node {}
const NEW_EXECUTION_PATH = ExecutionPathOptions.create();
export class NodeBase implements ExpressionNode {
type: string;
keys: string[];
scope: Scope;
start: number;
end: number;
context: AstContext;
parent: Node | { type: string; context: AstContext };
included: boolean;
constructor(
esTreeNode: GenericEsTreeNode,
// we need to pass down the node constructors to avoid a circular dependency
parent: Node | { type: string; context: AstContext },
parentScope: Scope
) {
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);
}
/**
* 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 = (<GenericEsTreeNode>this)[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: Scope) {
this.scope = parentScope;
}
declare(_kind: string, _init: ExpressionEntity | null) {}
getLiteralValueAtPath(
_path: ObjectPath,
_recursionTracker: ImmutableEntityPathTracker,
_origin: DeoptimizableEntity
): LiteralValueOrUnknown {
return UNKNOWN_VALUE;
}
getReturnExpressionWhenCalledAtPath(
_path: ObjectPath,
_recursionTracker: ImmutableEntityPathTracker,
_origin: DeoptimizableEntity
): ExpressionEntity {
return UNKNOWN_EXPRESSION;
}
hasEffects(options: ExecutionPathOptions): boolean {
for (const key of this.keys) {
const value = (<GenericEsTreeNode>this)[key];
if (value === null) continue;
if (Array.isArray(value)) {
for (const child of value) {
if (child !== null && child.hasEffects(options)) return true;
}
} else if (value.hasEffects(options)) return true;
}
return false;
}
hasEffectsWhenAccessedAtPath(path: ObjectPath, _options: ExecutionPathOptions) {
return path.length > 0;
}
hasEffectsWhenAssignedAtPath(_path: ObjectPath, _options: ExecutionPathOptions) {
return true;
}
hasEffectsWhenCalledAtPath(
_path: ObjectPath,
_callOptions: CallOptions,
_options: ExecutionPathOptions
) {
return true;
}
include(includeAllChildrenRecursively: boolean) {
this.included = true;
for (const key of this.keys) {
const value = (<GenericEsTreeNode>this)[key];
if (value === null) continue;
if (Array.isArray(value)) {
for (const child of value) {
if (child !== null) child.include(includeAllChildrenRecursively);
}
} else {
value.include(includeAllChildrenRecursively);
}
}
}
includeWithAllDeclaredVariables(includeAllChildrenRecursively: boolean) {
this.include(includeAllChildrenRecursively);
}
/**
* Override to perform special initialisation steps after the scope is initialised
*/
initialise() {
this.included = false;
}
insertSemicolon(code: MagicString) {
if (code.original[this.end - 1] !== ';') {
code.appendLeft(this.end, ';');
}
}
locate() {
// useful for debugging
const location = locate(this.context.code, this.start, { offsetLine: 1 });
location.file = this.context.fileName;
location.toString = () => JSON.stringify(location);
return location;
}
parseNode(esTreeNode: GenericEsTreeNode) {
for (const key of Object.keys(esTreeNode)) {
// That way, we can override this function to add custom initialisation and then call super.parseNode
if (this.hasOwnProperty(key)) continue;
const value = esTreeNode[key];
if (typeof value !== 'object' || value === null) {
(<GenericEsTreeNode>this)[key] = value;
} else if (Array.isArray(value)) {
(<GenericEsTreeNode>this)[key] = [];
for (const child of value) {
(<GenericEsTreeNode>this)[key].push(
child === null
? null
: new (this.context.nodeConstructors[child.type] ||
this.context.nodeConstructors.UnknownNode)(child, this, this.scope)
);
}
} else {
(<GenericEsTreeNode>this)[key] = new (this.context.nodeConstructors[value.type] ||
this.context.nodeConstructors.UnknownNode)(value, this, this.scope);
}
}
}
deoptimizePath(_path: ObjectPath) {}
render(code: MagicString, options: RenderOptions) {
for (const key of this.keys) {
const value = (<GenericEsTreeNode>this)[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(): boolean {
return this.included || this.hasEffects(NEW_EXECUTION_PATH);
}
toString() {
return this.context.code.slice(this.start, this.end);
}
}
export { NodeBase as StatementBase };