-
Notifications
You must be signed in to change notification settings - Fork 12.6k
/
Copy pathfactory.ts
331 lines (281 loc) · 13.8 KB
/
factory.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
/// <reference path="core.ts"/>
/// <reference path="utilities.ts"/>
/* @internal */
namespace ts {
let NodeConstructor: new (kind: SyntaxKind, pos: number, end: number) => Node;
let SourceFileConstructor: new (kind: SyntaxKind, pos: number, end: number) => Node;
function createNode(kind: SyntaxKind, location?: TextRange): Node {
const ConstructorForKind = kind === SyntaxKind.SourceFile
? (SourceFileConstructor || (SourceFileConstructor = objectAllocator.getSourceFileConstructor()))
: (NodeConstructor || (NodeConstructor = objectAllocator.getNodeConstructor()));
return location
? new ConstructorForKind(kind, location.pos, location.end)
: new ConstructorForKind(kind, /*pos*/ -1, /*end*/ -1);
}
export function createNodeArray<T extends Node>(elements?: T[], pos?: number, end?: number): NodeArray<T> {
const array = <NodeArray<T>>(elements || []);
array.pos = pos;
array.end = end;
array.arrayKind = ArrayKind.NodeArray;
return array;
}
export function createModifiersArray(elements?: Modifier[], pos?: number, end?: number): ModifiersArray {
const array = <ModifiersArray>(elements || []);
array.pos = pos;
array.end = end;
array.arrayKind = ArrayKind.ModifiersArray;
array.flags = 0;
return array;
}
export function createSynthesizedNode(kind: SyntaxKind, startsOnNewLine?: boolean): Node {
const node = <SynthesizedNode>createNode(kind, /*location*/ undefined);
node.startsOnNewLine = startsOnNewLine;
return node;
}
export function createSynthesizedNodeArray<T extends Node>(elements?: T[]): NodeArray<T> {
return createNodeArray(elements, /*pos*/ -1, /*end*/ -1);
}
export function createSynthesizedModifiersArray(elements?: Modifier[]): ModifiersArray {
return createModifiersArray(elements, /*pos*/ -1, /*end*/ -1);
}
/**
* Creates a shallow, memberwise clone of a node. The "kind", "pos", "end", "flags", and "parent"
* properties are excluded by default, and can be provided via the "location", "flags", and
* "parent" parameters.
*
* @param node The node to clone.
* @param location An optional TextRange to use to supply the new position.
* @param flags The NodeFlags to use for the cloned node.
* @param parent The parent for the new node.
* @param original An optional pointer to the original source tree node.
*/
export function cloneNode<T extends Node>(node: T, location?: TextRange, flags?: NodeFlags, parent?: Node, original?: Node): T {
// We don't use "clone" from core.ts here, as we need to preserve the prototype chain of
// the original node. We also need to exclude specific properties and only include own-
// properties (to skip members already defined on the shared prototype).
const clone = <T>createNode(node.kind, location);
for (const key in node) {
if (clone.hasOwnProperty(key) || !node.hasOwnProperty(key)) {
continue;
}
(<any>clone)[key] = (<any>node)[key];
}
if (flags !== undefined) {
clone.flags = flags;
}
if (parent !== undefined) {
clone.parent = parent;
}
if (original !== undefined) {
clone.original = original;
}
return clone;
}
export function createNodeArrayNode<T extends Node>(elements: T[]): NodeArrayNode<T> {
const node = <NodeArrayNode<T>>createSynthesizedNode(SyntaxKind.NodeArrayNode);
node.nodes = createNodeArray(elements);
return node;
}
export function createReturn(expression?: Expression): ReturnStatement {
const node = <ReturnStatement>createSynthesizedNode(SyntaxKind.ReturnStatement);
node.expression = expression;
return node;
}
export function createStatement(expression: Expression): ExpressionStatement {
const node = <ExpressionStatement>createSynthesizedNode(SyntaxKind.ExpressionStatement);
node.expression = expression;
return node;
}
export function createVariableStatement(declarationList: VariableDeclarationList): VariableStatement {
const node = <VariableStatement>createSynthesizedNode(SyntaxKind.VariableStatement);
node.declarationList = declarationList;
return node;
}
export function createVariableDeclarationList(declarations: VariableDeclaration[]): VariableDeclarationList {
const node = <VariableDeclarationList>createSynthesizedNode(SyntaxKind.VariableDeclarationList);
node.declarations = createNodeArray(declarations);
return node;
}
export function createBlock(statements: Statement[]): Block {
const block = <Block>createSynthesizedNode(SyntaxKind.Block);
block.statements = createNodeArray(statements);
return block;
}
export function createVariableDeclaration(name: BindingPattern | Identifier, initializer?: Expression, location?: TextRange): VariableDeclaration {
const node = <VariableDeclaration>createNode(SyntaxKind.VariableDeclaration, location);
node.name = name;
node.initializer = initializer;
return node;
}
export function createIdentifier(text: string): Identifier {
const node = <Identifier>createNode(SyntaxKind.Identifier);
node.text = text;
return node;
}
export function createTempVariable(tempKind: TempVariableKind): Identifier {
const name = <Identifier>createNode(SyntaxKind.Identifier);
name.tempKind = tempKind;
getNodeId(name);
return name;
}
export function createLiteral(value: string): StringLiteral;
export function createLiteral(value: number): LiteralExpression;
export function createLiteral(value: string | number | boolean): PrimaryExpression;
export function createLiteral<T extends PrimaryExpression>(value: string | number | boolean): T {
if (typeof value === "number") {
const node = <T & LiteralExpression>createNode(SyntaxKind.NumericLiteral);
node.text = value.toString();
return node;
}
else if (typeof value === "boolean") {
return <T>createNode(value ? SyntaxKind.TrueKeyword : SyntaxKind.FalseKeyword);
}
else {
const node = <T & StringLiteral>createNode(SyntaxKind.StringLiteral);
node.text = String(value);
return node;
}
}
export function createVoid(expression: UnaryExpression) {
const node = <VoidExpression>createNode(SyntaxKind.VoidExpression);
node.expression = expression;
return node;
}
export function createVoidZero() {
return createVoid(createLiteral(0));
}
export function createPropertyAccess(expression: Expression, name: string | Identifier) {
const node = <PropertyAccessExpression>createNode(SyntaxKind.PropertyAccessExpression);
node.expression = parenthesizeForAccess(expression);
node.dotToken = createSynthesizedNode(SyntaxKind.DotToken);
node.name = coerceIdentifier(name);
return node;
}
export function createElementAccess(expression: Expression, index: string | number | Expression) {
const node = <ElementAccessExpression>createNode(SyntaxKind.ElementAccessExpression);
node.expression = parenthesizeForAccess(expression);
node.argumentExpression = coerceExpression(index);
return node;
}
export function createConditional(condition: Expression, whenTrue: Expression, whenFalse: Expression) {
const node = <ConditionalExpression>createNode(SyntaxKind.ConditionalExpression);
node.condition = condition;
node.questionToken = createSynthesizedNode(SyntaxKind.QualifiedName);
node.whenTrue = whenTrue;
node.colonToken = createSynthesizedNode(SyntaxKind.ColonToken);
node.whenFalse = whenFalse;
return node;
}
export function createBinary(left: Expression, operator: SyntaxKind, right: Expression, location?: TextRange) {
const node = <BinaryExpression>createNode(SyntaxKind.BinaryExpression, location);
node.left = parenthesizeForBinary(left, operator, BinaryOperand.Left);
node.operatorToken = createSynthesizedNode(operator);
node.right = parenthesizeForBinary(right, operator, BinaryOperand.Right);
return node;
}
export function createAssignment(left: Expression, right: Expression, location?: TextRange) {
return createBinary(left, SyntaxKind.EqualsToken, right, location);
}
export function createStrictEquality(left: Expression, right: Expression) {
return createBinary(left, SyntaxKind.EqualsEqualsEqualsToken, right);
}
export function createComma(left: Expression, right: Expression) {
return <Expression>createBinary(left, SyntaxKind.CommaToken, right);
}
export function createCall(expression: Expression, argumentsArray: Expression[]) {
const node = <CallExpression>createNode(SyntaxKind.CallExpression);
node.expression = parenthesizeForAccess(expression);
node.arguments = createNodeArray(argumentsArray);
return node;
}
export function createArraySlice(array: Expression, start?: number | Expression) {
const argumentsList: Expression[] = start !== undefined ? [coerceExpression(start)] : [];
return createCall(createPropertyAccess(array, "slice"), argumentsList);
}
export function parenthesizeExpression(expression: Expression) {
const node = <ParenthesizedExpression>createNode(SyntaxKind.ParenthesizedExpression);
node.expression = expression;
return node;
}
export function inlineExpressions(expressions: Expression[]) {
return reduceLeft(expressions, createComma);
}
function coerceIdentifier(value: string | Identifier) {
if (typeof value === "string") {
return createIdentifier(value);
}
else {
return value;
}
}
function coerceExpression(value: string | number | boolean | Expression): Expression {
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
return createLiteral(value);
}
else {
return value;
}
}
const enum BinaryOperand {
Left,
Right
}
function parenthesizeForBinary(operand: Expression, operator: SyntaxKind, side: BinaryOperand) {
// When diagnosing whether the expression needs parentheses, the decision should be based
// on the innermost expression in a chain of nested type assertions.
while (operand.kind === SyntaxKind.TypeAssertionExpression || operand.kind === SyntaxKind.AsExpression) {
operand = (<AssertionExpression>operand).expression;
}
// If the resulting expression is already parenthesized, we do not need to do any further processing.
if (operand.kind === SyntaxKind.ParenthesizedExpression) {
return operand;
}
return needsParenthesesForBinary(operand, operator, side)
? parenthesizeExpression(operand)
: operand;
}
function needsParenthesesForBinary(operand: Expression, operator: SyntaxKind, side: BinaryOperand) {
const operandPrecedence = getExpressionPrecedence(operand);
const operatorPrecedence = getOperatorPrecedence(SyntaxKind.BinaryExpression, operator);
switch (compareValues(operandPrecedence, operatorPrecedence)) {
case Comparison.LessThan:
return true;
case Comparison.EqualTo:
return isRightAssociativeOperandOnLeftHandSide(operand, side)
|| isModuloOperandOnRightHandSide(operand, operator, side);
case Comparison.GreaterThan:
return false;
}
}
function isRightAssociativeOperandOnLeftHandSide(operand: Expression, side: BinaryOperand) {
return side === BinaryOperand.Left
&& getExpressionAssociativity(operand) === Associativity.Right;
}
function isModuloOperandOnRightHandSide(operand: Expression, operator: SyntaxKind, side: BinaryOperand) {
return side === BinaryOperand.Right
&& operator !== SyntaxKind.PercentToken
&& operand.kind === SyntaxKind.BinaryExpression
&& (<BinaryExpression>operand).operatorToken.kind === SyntaxKind.PercentToken;
}
function parenthesizeForAccess(expr: Expression): LeftHandSideExpression {
// When diagnosing whether the expression needs parentheses, the decision should be based
// on the innermost expression in a chain of nested type assertions.
while (expr.kind === SyntaxKind.TypeAssertionExpression || expr.kind === SyntaxKind.AsExpression) {
expr = (<AssertionExpression>expr).expression;
}
// isLeftHandSideExpression is almost the correct criterion for when it is not necessary
// to parenthesize the expression before a dot. The known exceptions are:
//
// NewExpression:
// new C.x -> not the same as (new C).x
// NumberLiteral
// 1.x -> not the same as (1).x
//
if (isLeftHandSideExpression(expr) &&
expr.kind !== SyntaxKind.NewExpression &&
expr.kind !== SyntaxKind.NumericLiteral) {
return <LeftHandSideExpression>expr;
}
return parenthesizeExpression(expr);
}
}