forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.ts
More file actions
1986 lines (1733 loc) · 88 KB
/
Copy pathfactory.ts
File metadata and controls
1986 lines (1733 loc) · 88 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// <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, flags?: NodeFlags): Node {
const ConstructorForKind = kind === SyntaxKind.SourceFile
? (SourceFileConstructor || (SourceFileConstructor = objectAllocator.getSourceFileConstructor()))
: (NodeConstructor || (NodeConstructor = objectAllocator.getNodeConstructor()));
const node = location
? new ConstructorForKind(kind, location.pos, location.end)
: new ConstructorForKind(kind, /*pos*/ -1, /*end*/ -1);
node.flags = flags | NodeFlags.Synthesized;
return node;
}
function updateNode<T extends Node>(updated: T, original: T): T {
updated.original = original;
if (original.transformId) {
updated.transformId = original.transformId;
updated.emitFlags = original.emitFlags;
updated.commentRange = original.commentRange;
updated.sourceMapRange = original.sourceMapRange;
}
if (original.startsOnNewLine) {
updated.startsOnNewLine = true;
}
return updated;
}
export function createNodeArray<T extends Node>(elements?: T[], location?: TextRange, hasTrailingComma?: boolean): NodeArray<T> {
if (elements) {
if (isNodeArray(elements)) {
return elements;
}
}
else {
elements = [];
}
const array = <NodeArray<T>>elements;
if (location) {
array.pos = location.pos;
array.end = location.end;
}
else {
array.pos = -1;
array.end = -1;
}
if (hasTrailingComma) {
array.hasTrailingComma = true;
}
return array;
}
export function createSynthesizedNode(kind: SyntaxKind, startsOnNewLine?: boolean): Node {
const node = createNode(kind, /*location*/ undefined);
node.startsOnNewLine = startsOnNewLine;
return node;
}
export function createSynthesizedNodeArray<T extends Node>(elements?: T[]): NodeArray<T> {
return createNodeArray(elements, /*location*/ undefined);
}
/**
* Creates a shallow, memberwise clone of a node with no source map location.
*/
export function getSynthesizedClone<T extends Node>(node: T): 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*/ undefined, node.flags);
clone.original = node;
for (const key in node) {
if (clone.hasOwnProperty(key) || !node.hasOwnProperty(key)) {
continue;
}
(<any>clone)[key] = (<any>node)[key];
}
return clone;
}
/**
* Creates a shallow, memberwise clone of a node for mutation.
*/
export function getMutableClone<T extends Node>(node: T): T {
const clone = getSynthesizedClone(node);
clone.pos = node.pos;
clone.end = node.end;
clone.parent = node.parent;
return clone;
}
// Literals
export function createLiteral(textSource: StringLiteral | Identifier, location?: TextRange): StringLiteral;
export function createLiteral(value: string, location?: TextRange): StringLiteral;
export function createLiteral(value: number, location?: TextRange): LiteralExpression;
export function createLiteral(value: string | number | boolean, location?: TextRange): PrimaryExpression;
export function createLiteral(value: string | number | boolean | StringLiteral | Identifier, location?: TextRange): PrimaryExpression {
if (typeof value === "number") {
const node = <LiteralExpression>createNode(SyntaxKind.NumericLiteral, location, /*flags*/ undefined);
node.text = value.toString();
return node;
}
else if (typeof value === "boolean") {
return <PrimaryExpression>createNode(value ? SyntaxKind.TrueKeyword : SyntaxKind.FalseKeyword, location, /*flags*/ undefined);
}
else if (typeof value === "string") {
const node = <StringLiteral>createNode(SyntaxKind.StringLiteral, location, /*flags*/ undefined);
node.text = value;
return node;
}
else {
const node = <StringLiteral>createNode(SyntaxKind.StringLiteral, location, /*flags*/ undefined);
node.textSourceNode = value;
node.text = value.text;
return node;
}
}
// Identifiers
let nextAutoGenerateId = 0;
export function createIdentifier(text: string, location?: TextRange): Identifier {
const node = <Identifier>createNode(SyntaxKind.Identifier, location);
node.text = escapeIdentifier(text);
node.originalKeywordKind = stringToToken(text);
node.autoGenerateKind = GeneratedIdentifierKind.None;
node.autoGenerateId = 0;
return node;
}
export function createTempVariable(recordTempVariable: (node: Identifier) => void, location?: TextRange): Identifier {
const name = <Identifier>createNode(SyntaxKind.Identifier, location);
name.text = "";
name.originalKeywordKind = SyntaxKind.Unknown;
name.autoGenerateKind = GeneratedIdentifierKind.Auto;
name.autoGenerateId = nextAutoGenerateId;
nextAutoGenerateId++;
if (recordTempVariable) {
recordTempVariable(name);
}
return name;
}
export function createLoopVariable(location?: TextRange): Identifier {
const name = <Identifier>createNode(SyntaxKind.Identifier, location);
name.text = "";
name.originalKeywordKind = SyntaxKind.Unknown;
name.autoGenerateKind = GeneratedIdentifierKind.Loop;
name.autoGenerateId = nextAutoGenerateId;
nextAutoGenerateId++;
return name;
}
export function createUniqueName(text: string, location?: TextRange): Identifier {
const name = <Identifier>createNode(SyntaxKind.Identifier, location);
name.text = text;
name.originalKeywordKind = SyntaxKind.Unknown;
name.autoGenerateKind = GeneratedIdentifierKind.Unique;
name.autoGenerateId = nextAutoGenerateId;
nextAutoGenerateId++;
return name;
}
export function getGeneratedNameForNode(node: Node, location?: TextRange): Identifier {
const name = <Identifier>createNode(SyntaxKind.Identifier, location);
name.original = node;
name.text = "";
name.originalKeywordKind = SyntaxKind.Unknown;
name.autoGenerateKind = GeneratedIdentifierKind.Node;
name.autoGenerateId = nextAutoGenerateId;
nextAutoGenerateId++;
return name;
}
// Reserved words
export function createSuper() {
const node = <PrimaryExpression>createNode(SyntaxKind.SuperKeyword);
return node;
}
export function createThis(location?: TextRange) {
const node = <PrimaryExpression>createNode(SyntaxKind.ThisKeyword, location);
return node;
}
export function createNull() {
const node = <PrimaryExpression>createNode(SyntaxKind.NullKeyword);
return node;
}
// Names
export function createComputedPropertyName(expression: Expression, location?: TextRange) {
const node = <ComputedPropertyName>createNode(SyntaxKind.ComputedPropertyName, location);
node.expression = expression;
return node;
}
// Type members
export function createMethod(decorators: Decorator[], modifiers: Modifier[], asteriskToken: Node, name: string | PropertyName, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block, location?: TextRange, flags?: NodeFlags) {
const node = <MethodDeclaration>createNode(SyntaxKind.MethodDeclaration, location, flags);
node.decorators = decorators ? createNodeArray(decorators) : undefined;
node.modifiers = modifiers ? createNodeArray(modifiers) : undefined;
node.asteriskToken = asteriskToken;
node.name = typeof name === "string" ? createIdentifier(name) : name;
node.typeParameters = typeParameters ? createNodeArray(typeParameters) : undefined;
node.parameters = createNodeArray(parameters);
node.type = type;
node.body = body;
return node;
}
export function updateMethod(node: MethodDeclaration, decorators: Decorator[], modifiers: Modifier[], asteriskToken: Node, name: PropertyName, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block) {
if (node.decorators !== decorators || node.modifiers !== modifiers || node.asteriskToken !== asteriskToken || node.name !== name || node.typeParameters !== typeParameters || node.parameters !== parameters || node.type !== type || node.body !== body) {
return updateNode(createMethod(decorators, modifiers, asteriskToken, name, typeParameters, parameters, type, body, /*location*/ node, node.flags), node);
}
return node;
}
export function createConstructor(decorators: Decorator[], modifiers: Modifier[], parameters: ParameterDeclaration[], body: Block, location?: TextRange, flags?: NodeFlags) {
const node = <ConstructorDeclaration>createNode(SyntaxKind.Constructor, location, flags);
node.decorators = decorators ? createNodeArray(decorators) : undefined;
node.modifiers = modifiers ? createNodeArray(modifiers) : undefined;
node.typeParameters = undefined;
node.parameters = createNodeArray(parameters);
node.type = undefined;
node.body = body;
return node;
}
export function updateConstructor(node: ConstructorDeclaration, decorators: Decorator[], modifiers: Modifier[], parameters: ParameterDeclaration[], body: Block) {
if (node.decorators !== decorators || node.modifiers !== modifiers || node.parameters !== parameters || node.body !== body) {
return updateNode(createConstructor(decorators, modifiers, parameters, body, /*location*/ node, node.flags), node);
}
return node;
}
export function createGetAccessor(decorators: Decorator[], modifiers: Modifier[], name: string | PropertyName, parameters: ParameterDeclaration[], type: TypeNode, body: Block, location?: TextRange, flags?: NodeFlags) {
const node = <GetAccessorDeclaration>createNode(SyntaxKind.GetAccessor, location, flags);
node.decorators = decorators ? createNodeArray(decorators) : undefined;
node.modifiers = modifiers ? createNodeArray(modifiers) : undefined;
node.name = typeof name === "string" ? createIdentifier(name) : name;
node.typeParameters = undefined;
node.parameters = createNodeArray(parameters);
node.type = type;
node.body = body;
return node;
}
export function updateGetAccessor(node: GetAccessorDeclaration, decorators: Decorator[], modifiers: Modifier[], name: PropertyName, parameters: ParameterDeclaration[], type: TypeNode, body: Block) {
if (node.decorators !== decorators || node.modifiers !== modifiers || node.name !== name || node.parameters !== parameters || node.type !== type || node.body !== body) {
return updateNode(createGetAccessor(decorators, modifiers, name, parameters, type, body, /*location*/ node, node.flags), node);
}
return node;
}
export function createSetAccessor(decorators: Decorator[], modifiers: Modifier[], name: string | PropertyName, parameters: ParameterDeclaration[], body: Block, location?: TextRange, flags?: NodeFlags) {
const node = <SetAccessorDeclaration>createNode(SyntaxKind.SetAccessor, location, flags);
node.decorators = decorators ? createNodeArray(decorators) : undefined;
node.modifiers = modifiers ? createNodeArray(modifiers) : undefined;
node.name = typeof name === "string" ? createIdentifier(name) : name;
node.typeParameters = undefined;
node.parameters = createNodeArray(parameters);
node.body = body;
return node;
}
export function updateSetAccessor(node: SetAccessorDeclaration, decorators: Decorator[], modifiers: Modifier[], name: PropertyName, parameters: ParameterDeclaration[], body: Block) {
if (node.decorators !== decorators || node.modifiers !== modifiers || node.name !== name || node.parameters !== parameters || node.body !== body) {
return updateNode(createSetAccessor(decorators, modifiers, name, parameters, body, /*location*/ node, node.flags), node);
}
return node;
}
export function createParameter(name: string | Identifier | BindingPattern, initializer?: Expression, location?: TextRange) {
return createParameterDeclaration(
/*decorators*/ undefined,
/*modifiers*/ undefined,
/*dotDotDotToken*/ undefined,
name,
/*questionToken*/ undefined,
/*type*/ undefined,
initializer,
location
);
}
export function createParameterDeclaration(decorators: Decorator[], modifiers: Modifier[], dotDotDotToken: Node, name: string | Identifier | BindingPattern, questionToken: Node, type: TypeNode, initializer: Expression, location?: TextRange, flags?: NodeFlags) {
const node = <ParameterDeclaration>createNode(SyntaxKind.Parameter, location, flags);
node.decorators = decorators ? createNodeArray(decorators) : undefined;
node.modifiers = modifiers ? createNodeArray(modifiers) : undefined;
node.dotDotDotToken = dotDotDotToken;
node.name = typeof name === "string" ? createIdentifier(name) : name;
node.questionToken = questionToken;
node.type = type;
node.initializer = initializer ? parenthesizeExpressionForList(initializer) : undefined;
return node;
}
export function updateParameterDeclaration(node: ParameterDeclaration, decorators: Decorator[], modifiers: Modifier[], dotDotDotToken: Node, name: BindingName, questionToken: Node, type: TypeNode, initializer: Expression) {
if (node.decorators !== decorators || node.modifiers !== modifiers || node.dotDotDotToken !== dotDotDotToken || node.name !== name || node.questionToken !== questionToken || node.type !== type || node.initializer !== initializer) {
return updateNode(createParameterDeclaration(decorators, modifiers, dotDotDotToken, name, questionToken, type, initializer, /*location*/ node, /*flags*/ node.flags), node);
}
return node;
}
// Expression
export function createArrayLiteral(elements?: Expression[], location?: TextRange, multiLine?: boolean) {
const node = <ArrayLiteralExpression>createNode(SyntaxKind.ArrayLiteralExpression, location);
node.elements = parenthesizeListElements(createNodeArray(elements));
if (multiLine) {
node.multiLine = multiLine;
}
return node;
}
export function createObjectLiteral(properties?: ObjectLiteralElement[], location?: TextRange, multiLine?: boolean) {
const node = <ObjectLiteralExpression>createNode(SyntaxKind.ObjectLiteralExpression, location);
node.properties = createNodeArray(properties);
if (multiLine) {
node.multiLine = multiLine;
}
return node;
}
export function createPropertyAccess(expression: Expression, name: string | Identifier, location?: TextRange) {
return createPropertyAccessWithDotToken(expression, createSynthesizedNode(SyntaxKind.DotToken), name, location, /*flags*/ undefined);
}
export function createPropertyAccessWithDotToken(expression: Expression, dotToken: Node, name: string | Identifier, location?: TextRange, flags?: NodeFlags) {
const node = <PropertyAccessExpression>createNode(SyntaxKind.PropertyAccessExpression, location, flags);
node.expression = parenthesizeForAccess(expression);
node.dotToken = dotToken;
node.name = typeof name === "string" ? createIdentifier(name) : name;
return node;
}
export function updatePropertyAccess(node: PropertyAccessExpression, expression: Expression, name: Identifier) {
if (node.expression !== expression || node.name !== name) {
return updateNode(createPropertyAccessWithDotToken(expression, node.dotToken, name, /*location*/ node, node.flags), node);
}
return node;
}
export function createElementAccess(expression: Expression, index: number | Expression, location?: TextRange) {
const node = <ElementAccessExpression>createNode(SyntaxKind.ElementAccessExpression, location);
node.expression = parenthesizeForAccess(expression);
node.argumentExpression = typeof index === "number" ? createLiteral(index) : index;
return node;
}
export function createCall(expression: Expression, typeArguments: TypeNode[], argumentsArray: Expression[], location?: TextRange, flags?: NodeFlags) {
const node = <CallExpression>createNode(SyntaxKind.CallExpression, location, flags);
node.expression = parenthesizeForAccess(expression);
if (typeArguments) {
node.typeArguments = createNodeArray(typeArguments);
}
node.arguments = parenthesizeListElements(createNodeArray(argumentsArray));
return node;
}
export function updateCall(node: CallExpression, expression: Expression, typeArguments: TypeNode[], argumentsArray: Expression[]) {
if (expression !== node.expression || typeArguments !== node.typeArguments || argumentsArray !== node.arguments) {
return updateNode(createCall(expression, typeArguments, argumentsArray, /*location*/ node, node.flags), node);
}
return node;
}
export function createNew(expression: Expression, typeArguments: TypeNode[], argumentsArray: Expression[], location?: TextRange, flags?: NodeFlags) {
const node = <NewExpression>createNode(SyntaxKind.NewExpression, location, flags);
node.expression = parenthesizeForNew(expression);
node.typeArguments = typeArguments ? createNodeArray(typeArguments) : undefined;
node.arguments = argumentsArray ? parenthesizeListElements(createNodeArray(argumentsArray)) : undefined;
return node;
}
export function updateNew(node: NewExpression, expression: Expression, typeArguments: TypeNode[], argumentsArray: Expression[]) {
if (node.expression !== expression || node.typeArguments !== typeArguments || node.arguments !== argumentsArray) {
return updateNode(createNew(expression, typeArguments, argumentsArray, /*location*/ node, node.flags), node);
}
return node;
}
export function createParen(expression: Expression, location?: TextRange) {
const node = <ParenthesizedExpression>createNode(SyntaxKind.ParenthesizedExpression, location);
node.expression = expression;
return node;
}
export function createFunctionExpression(asteriskToken: Node, name: string | Identifier, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block, location?: TextRange, flags?: NodeFlags) {
const node = <FunctionExpression>createNode(SyntaxKind.FunctionExpression, location, flags);
node.modifiers = undefined;
node.asteriskToken = asteriskToken;
node.name = typeof name === "string" ? createIdentifier(name) : name;
node.typeParameters = typeParameters ? createNodeArray(typeParameters) : undefined;
node.parameters = createNodeArray(parameters);
node.type = type;
node.body = body;
return node;
}
export function updateFunctionExpression(node: FunctionExpression, name: Identifier, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block) {
if (node.name !== name || node.typeParameters !== typeParameters || node.parameters !== parameters || node.type !== type || node.body !== body) {
return updateNode(createFunctionExpression(node.asteriskToken, name, typeParameters, parameters, type, body, /*location*/ node, node.flags), node);
}
return node;
}
export function createArrowFunction(modifiers: Modifier[], typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, equalsGreaterThanToken: Node, body: ConciseBody, location?: TextRange, flags?: NodeFlags) {
const node = <ArrowFunction>createNode(SyntaxKind.ArrowFunction, location, flags);
node.modifiers = modifiers ? createNodeArray(modifiers) : undefined;
node.typeParameters = typeParameters ? createNodeArray(typeParameters) : undefined;
node.parameters = createNodeArray(parameters);
node.type = type;
node.equalsGreaterThanToken = equalsGreaterThanToken || createNode(SyntaxKind.EqualsGreaterThanToken);
node.body = parenthesizeConciseBody(body);
return node;
}
export function updateArrowFunction(node: ArrowFunction, modifiers: Modifier[], typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: ConciseBody) {
if (node.modifiers !== modifiers || node.typeParameters !== typeParameters || node.parameters !== parameters || node.type !== type || node.body !== body) {
return updateNode(createArrowFunction(modifiers, typeParameters, parameters, type, node.equalsGreaterThanToken, body, /*location*/ node, node.flags), node);
}
return node;
}
export function createTypeOf(expression: Expression) {
const node = <TypeOfExpression>createNode(SyntaxKind.TypeOfExpression);
node.expression = parenthesizePrefixOperand(expression);
return node;
}
export function createVoid(expression: Expression) {
const node = <VoidExpression>createNode(SyntaxKind.VoidExpression);
node.expression = parenthesizePrefixOperand(expression);
return node;
}
export function createPrefix(operator: SyntaxKind, operand: Expression, location?: TextRange) {
const node = <PrefixUnaryExpression>createNode(SyntaxKind.PrefixUnaryExpression, location);
node.operator = operator;
node.operand = parenthesizePrefixOperand(operand);
return node;
}
export function createPostfix(operand: Expression, operator: SyntaxKind, location?: TextRange) {
const node = <PostfixUnaryExpression>createNode(SyntaxKind.PostfixUnaryExpression, location);
node.operand = parenthesizePostfixOperand(operand);
node.operator = operator;
return node;
}
export function createBinary(left: Expression, operator: SyntaxKind, right: Expression, location?: TextRange) {
return createBinaryWithOperatorToken(left, createSynthesizedNode(operator), right, location);
}
export function createBinaryWithOperatorToken(left: Expression, operatorToken: Node, right: Expression, location?: TextRange) {
const node = <BinaryExpression>createNode(SyntaxKind.BinaryExpression, location);
node.left = parenthesizeBinaryOperand(operatorToken.kind, left, /*isLeftSideOfBinary*/ true, /*leftOperand*/ undefined);
node.operatorToken = operatorToken;
node.right = parenthesizeBinaryOperand(operatorToken.kind, right, /*isLeftSideOfBinary*/ false, node.left);
return node;
}
export function updateBinary(node: BinaryExpression, left: Expression, right: Expression) {
if (node.left !== left || node.right !== right) {
return updateNode(createBinaryWithOperatorToken(left, node.operatorToken, right, /*location*/ node), node);
}
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 createYield(expression: Expression, location?: TextRange) {
const node = <YieldExpression>createNode(SyntaxKind.YieldExpression, location);
node.expression = expression;
return node;
}
export function createSpread(expression: Expression) {
const node = <SpreadElementExpression>createNode(SyntaxKind.SpreadElementExpression);
node.expression = parenthesizeExpressionForList(expression);
return node;
}
export function createClassExpression(name: Identifier, heritageClauses: HeritageClause[], members: ClassElement[], location?: TextRange) {
const node = <ClassExpression>createNode(SyntaxKind.ClassExpression, location);
node.decorators = undefined;
node.modifiers = undefined;
node.name = name;
node.typeParameters = undefined;
node.heritageClauses = createNodeArray(heritageClauses);
node.members = createNodeArray(members);
return node;
}
export function createOmittedExpression(location?: TextRange) {
const node = <OmittedExpression>createNode(SyntaxKind.OmittedExpression, location);
return node;
}
export function createExpressionWithTypeArguments(expression: Expression, location?: TextRange) {
const node = <ExpressionWithTypeArguments>createNode(SyntaxKind.ExpressionWithTypeArguments, location);
node.typeArguments = undefined;
node.expression = parenthesizeForAccess(expression);
return node;
}
// Element
export function createBlock(statements: Statement[], location?: TextRange, multiLine?: boolean, flags?: NodeFlags): Block {
const block = <Block>createNode(SyntaxKind.Block, location, flags);
block.statements = createNodeArray(statements);
if (multiLine) {
block.multiLine = true;
}
return block;
}
export function updateBlock(node: Block, statements: Statement[]) {
if (statements !== node.statements) {
return updateNode(createBlock(statements, /*location*/ node, node.multiLine, node.flags), node);
}
return node;
}
export function createVariableStatement(modifiers: Modifier[], declarationList: VariableDeclarationList | VariableDeclaration[], location?: TextRange, flags?: NodeFlags): VariableStatement {
const node = <VariableStatement>createNode(SyntaxKind.VariableStatement, location, flags);
node.decorators = undefined;
node.modifiers = modifiers ? createNodeArray(modifiers) : undefined;
node.declarationList = isArray(declarationList) ? createVariableDeclarationList(declarationList) : declarationList;
return node;
}
export function updateVariableStatement(node: VariableStatement, modifiers: Modifier[], declarationList: VariableDeclarationList): VariableStatement {
if (node.modifiers !== modifiers || node.declarationList !== declarationList) {
return updateNode(createVariableStatement(modifiers, declarationList, /*location*/ node, node.flags), node);
}
return node;
}
export function createVariableDeclarationList(declarations: VariableDeclaration[], location?: TextRange, flags?: NodeFlags): VariableDeclarationList {
const node = <VariableDeclarationList>createNode(SyntaxKind.VariableDeclarationList, location, flags);
node.declarations = createNodeArray(declarations);
return node;
}
export function updateVariableDeclarationList(node: VariableDeclarationList, declarations: VariableDeclaration[]) {
if (node.declarations !== declarations) {
return updateNode(createVariableDeclarationList(declarations, /*location*/ node, node.flags), node);
}
return node;
}
export function createLetDeclarationList(declarations: VariableDeclaration[], location?: TextRange) {
return createVariableDeclarationList(declarations, location, NodeFlags.Let);
}
export function createConstDeclarationList(declarations: VariableDeclaration[], location?: TextRange) {
return createVariableDeclarationList(declarations, location, NodeFlags.Const);
}
export function createVariableDeclaration(name: string | BindingPattern | Identifier, type?: TypeNode, initializer?: Expression, location?: TextRange, flags?: NodeFlags): VariableDeclaration {
const node = <VariableDeclaration>createNode(SyntaxKind.VariableDeclaration, location, flags);
node.name = typeof name === "string" ? createIdentifier(name) : name;
node.type = type;
node.initializer = initializer !== undefined ? parenthesizeExpressionForList(initializer) : undefined;
return node;
}
export function updateVariableDeclaration(node: VariableDeclaration, name: BindingName, type: TypeNode, initializer: Expression) {
if (node.name !== name || node.type !== type || node.initializer !== initializer) {
return updateNode(createVariableDeclaration(name, type, initializer, /*location*/ node, node.flags), node);
}
return node;
}
export function createEmptyStatement(location: TextRange) {
return <EmptyStatement>createNode(SyntaxKind.EmptyStatement, location);
}
export function createStatement(expression: Expression, location?: TextRange, flags?: NodeFlags): ExpressionStatement {
const node = <ExpressionStatement>createNode(SyntaxKind.ExpressionStatement, location, flags);
node.expression = parenthesizeExpressionForExpressionStatement(expression);
return node;
}
export function updateStatement(node: ExpressionStatement, expression: Expression) {
if (node.expression !== expression) {
return updateNode(createStatement(expression, /*location*/ node, node.flags), node);
}
return node;
}
export function createIf(expression: Expression, thenStatement: Statement, elseStatement?: Statement, location?: TextRange) {
const node = <IfStatement>createNode(SyntaxKind.IfStatement, location);
node.expression = expression;
node.thenStatement = thenStatement;
node.elseStatement = elseStatement;
return node;
}
export function updateIf(node: IfStatement, expression: Expression, thenStatement: Statement, elseStatement: Statement) {
if (node.expression !== expression || node.thenStatement !== thenStatement || node.elseStatement !== elseStatement) {
return updateNode(createIf(expression, thenStatement, elseStatement, /*location*/ node), node);
}
return node;
}
export function createSwitch(expression: Expression, caseBlock: CaseBlock, location?: TextRange): SwitchStatement {
const node = <SwitchStatement>createNode(SyntaxKind.SwitchStatement, location);
node.expression = parenthesizeExpressionForList(expression);
node.caseBlock = caseBlock;
return node;
}
export function createCaseBlock(clauses: CaseClause[], location?: TextRange): CaseBlock {
const node = <CaseBlock>createNode(SyntaxKind.CaseBlock, location);
node.clauses = createNodeArray(clauses);
return node;
}
export function createFor(initializer: ForInitializer, condition: Expression, incrementor: Expression, statement: Statement, location?: TextRange) {
const node = <ForStatement>createNode(SyntaxKind.ForStatement, location, /*flags*/ undefined);
node.initializer = initializer;
node.condition = condition;
node.incrementor = incrementor;
node.statement = statement;
return node;
}
export function createLabel(label: string | Identifier, statement: Statement, location?: TextRange) {
const node = <LabeledStatement>createNode(SyntaxKind.LabeledStatement, location);
node.label = typeof label === "string" ? createIdentifier(label) : label;
node.statement = statement;
return node;
}
export function createDo(expression: Expression, statement: Statement, location?: TextRange) {
const node = <DoStatement>createNode(SyntaxKind.DoStatement, location);
node.expression = expression;
node.statement = statement;
return node;
}
export function createWhile(statement: Statement, expression: Expression, location?: TextRange) {
const node = <WhileStatement>createNode(SyntaxKind.WhileStatement, location);
node.statement = statement;
node.expression = expression;
return node;
}
export function createForIn(initializer: ForInitializer, expression: Expression, statement: Statement, location?: TextRange) {
const node = <ForInStatement>createNode(SyntaxKind.ForInStatement, location);
node.initializer = initializer;
node.expression = expression;
node.statement = statement;
return node;
}
export function createForOf(initializer: ForInitializer, expression: Expression, statement: Statement, location?: TextRange) {
const node = <ForOfStatement>createNode(SyntaxKind.ForOfStatement, location);
node.initializer = initializer;
node.expression = expression;
node.statement = statement;
return node;
}
export function createReturn(expression?: Expression, location?: TextRange): ReturnStatement {
const node = <ReturnStatement>createNode(SyntaxKind.ReturnStatement, location);
node.expression = expression;
return node;
}
export function updateReturn(node: ReturnStatement, expression: Expression) {
if (node.expression !== expression) {
return updateNode(createReturn(expression, /*location*/ node), node);
}
return node;
}
export function createFunctionDeclaration(decorators: Decorator[], modifiers: Modifier[], asteriskToken: Node, name: string | Identifier, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block, location?: TextRange, flags?: NodeFlags) {
const node = <FunctionDeclaration>createNode(SyntaxKind.FunctionDeclaration, location, flags);
node.decorators = decorators ? createNodeArray(decorators) : undefined;
node.modifiers = modifiers ? createNodeArray(modifiers) : undefined;
node.asteriskToken = asteriskToken;
node.name = typeof name === "string" ? createIdentifier(name) : name;
node.typeParameters = typeParameters ? createNodeArray(typeParameters) : undefined;
node.parameters = createNodeArray(parameters);
node.type = type;
node.body = body;
return node;
}
export function updateFunctionDeclaration(node: FunctionDeclaration, decorators: Decorator[], modifiers: Modifier[], name: Identifier, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block) {
if (node.decorators !== decorators || node.modifiers !== modifiers || node.name !== name || node.typeParameters !== typeParameters || node.parameters !== parameters || node.type !== type || node.body !== body) {
return updateNode(createFunctionDeclaration(decorators, modifiers, node.asteriskToken, name, typeParameters, parameters, type, body, /*location*/ node, node.flags), node);
}
return node;
}
export function createClassDeclaration(modifiers: Modifier[], name: Identifier, heritageClauses: HeritageClause[], members: ClassElement[], location?: TextRange) {
const node = <ClassDeclaration>createNode(SyntaxKind.ClassDeclaration, location);
node.decorators = undefined;
node.modifiers = modifiers ? createNodeArray(modifiers) : undefined;
node.name = name;
node.typeParameters = undefined;
node.heritageClauses = createNodeArray(heritageClauses);
node.members = createNodeArray(members);
return node;
}
export function createExportDefault(expression: Expression) {
const node = <ExportAssignment>createNode(SyntaxKind.ExportAssignment);
node.isExportEquals = false;
node.expression = expression;
return node;
}
export function createExportDeclaration(exportClause: NamedExports, moduleSpecifier?: Expression) {
const node = <ExportDeclaration>createNode(SyntaxKind.ExportDeclaration);
node.exportClause = exportClause;
node.moduleSpecifier = moduleSpecifier;
return node;
}
export function createNamedExports(elements: ExportSpecifier[]) {
const node = <NamedExports>createNode(SyntaxKind.NamedExports);
node.elements = createNodeArray(elements);
return node;
}
export function createExportSpecifier(name: string | Identifier, propertyName?: string | Identifier) {
const node = <ExportSpecifier>createNode(SyntaxKind.ExportSpecifier);
node.name = typeof name === "string" ? createIdentifier(name) : name;
node.propertyName = typeof propertyName === "string" ? createIdentifier(propertyName) : propertyName;
return node;
}
// Clauses
export function createHeritageClause(token: SyntaxKind, types: ExpressionWithTypeArguments[], location?: TextRange) {
const node = <HeritageClause>createNode(SyntaxKind.HeritageClause, location);
node.token = token;
node.types = createNodeArray(types);
return node;
}
export function createCaseClause(expression: Expression, statements: Statement[], location?: TextRange) {
const node = <CaseClause>createNode(SyntaxKind.CaseClause, location);
node.expression = parenthesizeExpressionForList(expression);
node.statements = createNodeArray(statements);
return node;
}
// Property assignments
export function createPropertyAssignment(name: string | PropertyName, initializer: Expression, location?: TextRange) {
const node = <PropertyAssignment>createNode(SyntaxKind.PropertyAssignment, location);
node.name = typeof name === "string" ? createIdentifier(name) : name;
node.questionToken = undefined;
node.initializer = initializer !== undefined ? parenthesizeExpressionForList(initializer) : undefined;
return node;
}
// Top-level nodes
export function updateSourceFileNode(node: SourceFile, statements: Statement[]) {
if (node.statements !== statements) {
const updated = <SourceFile>createNode(SyntaxKind.SourceFile, /*location*/ node, node.flags);
updated.statements = createNodeArray(statements);
updated.endOfFileToken = node.endOfFileToken;
updated.fileName = node.fileName;
updated.path = node.path;
updated.text = node.text;
updated.amdDependencies = node.amdDependencies;
updated.moduleName = node.moduleName;
updated.referencedFiles = node.referencedFiles;
updated.typeReferenceDirectives = node.typeReferenceDirectives;
updated.languageVariant = node.languageVariant;
updated.isDeclarationFile = node.isDeclarationFile;
updated.renamedDependencies = node.renamedDependencies;
updated.hasNoDefaultLib = node.hasNoDefaultLib;
updated.languageVersion = node.languageVersion;
updated.scriptKind = node.scriptKind;
updated.externalModuleIndicator = node.externalModuleIndicator;
updated.commonJsModuleIndicator = node.commonJsModuleIndicator;
updated.identifiers = node.identifiers;
updated.nodeCount = node.nodeCount;
updated.identifierCount = node.identifierCount;
updated.symbolCount = node.symbolCount;
updated.parseDiagnostics = node.parseDiagnostics;
updated.bindDiagnostics = node.bindDiagnostics;
updated.lineMap = node.lineMap;
updated.classifiableNames = node.classifiableNames;
updated.resolvedModules = node.resolvedModules;
updated.resolvedTypeReferenceDirectiveNames = node.resolvedTypeReferenceDirectiveNames;
updated.imports = node.imports;
updated.moduleAugmentations = node.moduleAugmentations;
return updateNode(updated, node);
}
return node;
}
// Transformation nodes
/**
* Creates a synthetic statement to act as a placeholder for a not-emitted statement in
* order to preserve comments.
*
* @param original The original statement.
*/
export function createNotEmittedStatement(original: Node) {
const node = <NotEmittedStatement>createNode(SyntaxKind.NotEmittedStatement, /*location*/ original);
node.original = original;
return node;
}
/**
* Creates a synthetic expression to act as a placeholder for a not-emitted expression in
* order to preserve comments or sourcemap positions.
*
* @param expression The inner expression to emit.
* @param original The original outer expression.
* @param location The location for the expression. Defaults to the positions from "original" if provided.
*/
export function createPartiallyEmittedExpression(expression: Expression, original?: Node, location?: TextRange) {
const node = <PartiallyEmittedExpression>createNode(SyntaxKind.PartiallyEmittedExpression, /*location*/ location || original);
node.expression = expression;
node.original = original;
return node;
}
// Compound nodes
export function createComma(left: Expression, right: Expression) {
return <Expression>createBinary(left, SyntaxKind.CommaToken, right);
}
export function createLessThan(left: Expression, right: Expression, location?: TextRange) {
return <Expression>createBinary(left, SyntaxKind.LessThanToken, right, location);
}
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 createStrictInequality(left: Expression, right: Expression) {
return createBinary(left, SyntaxKind.ExclamationEqualsEqualsToken, right);
}
export function createAdd(left: Expression, right: Expression) {
return createBinary(left, SyntaxKind.PlusToken, right);
}
export function createSubtract(left: Expression, right: Expression) {
return createBinary(left, SyntaxKind.MinusToken, right);
}
export function createPostfixIncrement(operand: Expression, location?: TextRange) {
return createPostfix(operand, SyntaxKind.PlusPlusToken, location);
}
export function createLogicalAnd(left: Expression, right: Expression) {
return createBinary(left, SyntaxKind.AmpersandAmpersandToken, right);
}
export function createLogicalOr(left: Expression, right: Expression) {
return createBinary(left, SyntaxKind.BarBarToken, right);
}
export function createLogicalNot(operand: Expression) {
return createPrefix(SyntaxKind.ExclamationToken, operand);
}
export function createVoidZero() {
return createVoid(createLiteral(0));
}
export function createImportDeclaration(importClause: ImportClause, moduleSpecifier?: Expression, location?: TextRange): ImportDeclaration {
const node = <ImportDeclaration>createNode(SyntaxKind.ImportDeclaration, location);
node.importClause = importClause;
node.moduleSpecifier = moduleSpecifier;
return node;
}
export function createImportClause(name: Identifier, namedBindings: NamedImportBindings, location?: TextRange): ImportClause {
const node = <ImportClause>createNode(SyntaxKind.ImportClause, location);
node.name = name;
node.namedBindings = namedBindings;
return node;
}
export function createNamedImports(elements: NodeArray<ImportSpecifier>, location?: TextRange): NamedImports {
const node = <NamedImports>createNode(SyntaxKind.NamedImports, location);
node.elements = elements;
return node;
}
export function createMemberAccessForPropertyName(target: Expression, memberName: PropertyName, setNodeEmitFlags: (node: Node, flags: NodeEmitFlags) => void, location?: TextRange): MemberExpression {
if (isIdentifier(memberName)) {
const expression = createPropertyAccess(target, memberName, location);
setNodeEmitFlags(expression, NodeEmitFlags.NoNestedSourceMaps);
return expression;
}
else if (isComputedPropertyName(memberName)) {
return createElementAccess(target, memberName.expression, location);
}
else {
return createElementAccess(target, memberName, location);
}
}
export function createRestParameter(name: string | Identifier) {
return createParameterDeclaration(
/*decorators*/ undefined,
/*modifiers*/ undefined,
createSynthesizedNode(SyntaxKind.DotDotDotToken),
name,
/*questionToken*/ undefined,
/*type*/ undefined,
/*initializer*/ undefined
);
}
export function createFunctionCall(func: Expression, thisArg: Expression, argumentsList: Expression[], location?: TextRange) {
return createCall(
createPropertyAccess(func, "call"),
/*typeArguments*/ undefined,
[
thisArg,
...argumentsList
],
location
);
}
export function createBreak(label?: Identifier, location?: TextRange): BreakStatement {
const node = <BreakStatement>createNode(SyntaxKind.BreakStatement, location);
if (label) {
node.label = label;
}
return node;
}
export function createContinue(label?: Identifier, location?: TextRange): BreakStatement {
const node = <ContinueStatement>createNode(SyntaxKind.ContinueStatement, location);
if (label) {
node.label = label;
}
return node;
}
export function createFunctionApply(func: Expression, thisArg: Expression, argumentsExpression: Expression, location?: TextRange) {
return createCall(
createPropertyAccess(func, "apply"),
/*typeArguments*/ undefined,
[
thisArg,
argumentsExpression
],
location
);