-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathast.go
More file actions
11164 lines (9154 loc) · 374 KB
/
Copy pathast.go
File metadata and controls
11164 lines (9154 loc) · 374 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
package ast
import (
"fmt"
"iter"
"strings"
"sync"
"sync/atomic"
"github.com/microsoft/typescript-go/internal/collections"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/tspath"
)
// Visitor
type Visitor func(*Node) bool
func visit(v Visitor, node *Node) bool {
if node != nil {
return v(node)
}
return false
}
func visitNodes(v Visitor, nodes []*Node) bool {
for _, node := range nodes { //nolint:modernize
if v(node) {
return true
}
}
return false
}
func visitNodeList(v Visitor, nodeList *NodeList) bool {
if nodeList != nil {
return visitNodes(v, nodeList.Nodes)
}
return false
}
func visitModifiers(v Visitor, modifiers *ModifierList) bool {
if modifiers != nil {
return visitNodes(v, modifiers.Nodes)
}
return false
}
// NodeFactory
type NodeFactory struct {
hooks NodeFactoryHooks
arrayTypeNodePool core.Pool[ArrayTypeNode]
binaryExpressionPool core.Pool[BinaryExpression]
blockPool core.Pool[Block]
callExpressionPool core.Pool[CallExpression]
conditionalExpressionPool core.Pool[ConditionalExpression]
constructSignatureDeclarationPool core.Pool[ConstructSignatureDeclaration]
elementAccessExpressionPool core.Pool[ElementAccessExpression]
expressionStatementPool core.Pool[ExpressionStatement]
expressionWithTypeArgumentsPool core.Pool[ExpressionWithTypeArguments]
functionDeclarationPool core.Pool[FunctionDeclaration]
functionTypeNodePool core.Pool[FunctionTypeNode]
heritageClausePool core.Pool[HeritageClause]
identifierPool core.Pool[Identifier]
ifStatementPool core.Pool[IfStatement]
importSpecifierPool core.Pool[ImportSpecifier]
indexedAccessTypeNodePool core.Pool[IndexedAccessTypeNode]
interfaceDeclarationPool core.Pool[InterfaceDeclaration]
intersectionTypeNodePool core.Pool[IntersectionTypeNode]
jsdocDeprecatedTagPool core.Pool[JSDocDeprecatedTag]
jsdocParameterOrPropertyTagPool core.Pool[JSDocParameterOrPropertyTag]
jsdocPool core.Pool[JSDoc]
jsdocTextPool core.Pool[JSDocText]
jsdocUnknownTagPool core.Pool[JSDocUnknownTag]
keywordExpressionPool core.Pool[KeywordExpression]
keywordTypeNodePool core.Pool[KeywordTypeNode]
literalTypeNodePool core.Pool[LiteralTypeNode]
methodSignatureDeclarationPool core.Pool[MethodSignatureDeclaration]
modifierListPool core.Pool[ModifierList]
nodeListPool core.Pool[NodeList]
numericLiteralPool core.Pool[NumericLiteral]
parameterDeclarationPool core.Pool[ParameterDeclaration]
parenthesizedExpressionPool core.Pool[ParenthesizedExpression]
parenthesizedTypeNodePool core.Pool[ParenthesizedTypeNode]
prefixUnaryExpressionPool core.Pool[PrefixUnaryExpression]
propertyAccessExpressionPool core.Pool[PropertyAccessExpression]
propertyAssignmentPool core.Pool[PropertyAssignment]
propertySignatureDeclarationPool core.Pool[PropertySignatureDeclaration]
returnStatementPool core.Pool[ReturnStatement]
stringLiteralPool core.Pool[StringLiteral]
tokenPool core.Pool[Token]
typeAliasDeclarationPool core.Pool[TypeAliasDeclaration]
typeLiteralNodePool core.Pool[TypeLiteralNode]
typeOperatorNodePool core.Pool[TypeOperatorNode]
typeParameterDeclarationPool core.Pool[TypeParameterDeclaration]
typeReferenceNodePool core.Pool[TypeReferenceNode]
unionTypeNodePool core.Pool[UnionTypeNode]
variableDeclarationListPool core.Pool[VariableDeclarationList]
variableDeclarationPool core.Pool[VariableDeclaration]
variableStatementPool core.Pool[VariableStatement]
nodeCount int
textCount int
}
type NodeFactoryHooks struct {
OnCreate func(node *Node) // Hooks the creation of a node.
OnUpdate func(node *Node, original *Node) // Hooks the updating of a node.
OnClone func(node *Node, original *Node) // Hooks the cloning of a node.
}
type NodeFactoryCoercible interface {
AsNodeFactory() *NodeFactory
}
func NewNodeFactory(hooks NodeFactoryHooks) *NodeFactory {
return &NodeFactory{hooks: hooks}
}
func newNode(kind Kind, data nodeData, hooks NodeFactoryHooks) *Node {
n := data.AsNode()
n.Loc = core.UndefinedTextRange()
n.Kind = kind
n.data = data
if hooks.OnCreate != nil {
hooks.OnCreate(n)
}
return n
}
func (f *NodeFactory) newNode(kind Kind, data nodeData) *Node {
f.nodeCount++
return newNode(kind, data, f.hooks)
}
func (f *NodeFactory) NodeCount() int {
return f.nodeCount
}
func (f *NodeFactory) TextCount() int {
return f.textCount
}
func (f *NodeFactory) AsNodeFactory() *NodeFactory {
return f
}
func updateNode(updated *Node, original *Node, hooks NodeFactoryHooks) *Node {
if updated != original {
updated.Flags = original.Flags
updated.Loc = original.Loc
if hooks.OnUpdate != nil {
hooks.OnUpdate(updated, original)
}
}
return updated
}
func cloneNode(updated *Node, original *Node, hooks NodeFactoryHooks) *Node {
updateNode(updated, original, hooks)
if updated != original && hooks.OnClone != nil {
hooks.OnClone(updated, original)
}
return updated
}
// NodeList
type NodeList struct {
Loc core.TextRange
Nodes []*Node
}
func (f *NodeFactory) NewNodeList(nodes []*Node) *NodeList {
list := f.nodeListPool.New()
list.Loc = core.UndefinedTextRange()
list.Nodes = nodes
return list
}
func (list *NodeList) Pos() int { return list.Loc.Pos() }
func (list *NodeList) End() int { return list.Loc.End() }
func (list *NodeList) HasTrailingComma() bool {
if len(list.Nodes) == 0 {
return false
}
last := list.Nodes[len(list.Nodes)-1]
return last.End() < list.End()
}
func (list *NodeList) Clone(f NodeFactoryCoercible) *NodeList {
result := f.AsNodeFactory().NewNodeList(list.Nodes)
result.Loc = list.Loc
return result
}
// ModifierList
type ModifierList struct {
NodeList
ModifierFlags ModifierFlags
}
func (f *NodeFactory) NewModifierList(nodes []*Node) *ModifierList {
list := f.modifierListPool.New()
list.Loc = core.UndefinedTextRange()
list.Nodes = nodes
list.ModifierFlags = ModifiersToFlags(nodes)
return list
}
func (list *ModifierList) Clone(f *NodeFactory) *ModifierList {
res := f.modifierListPool.New()
res.Loc = list.Loc
res.Nodes = list.Nodes
res.ModifierFlags = list.ModifierFlags
return res
}
// AST Node
// Interface values stored in AST nodes are never typed nil values. Construction code must ensure that
// interface valued properties either store a true nil or a reference to a non-nil struct.
type Node struct {
Kind Kind
Flags NodeFlags
Loc core.TextRange
id atomic.Uint64
Parent *Node
data nodeData
}
// Node accessors. Some accessors are implemented as methods on NodeData, others are implemented though
// type switches. Either approach is fine. Interface methods are likely more performant, but have higher
// code size costs because we have hundreds of implementations of the NodeData interface.
func (n *Node) AsNode() *Node { return n }
func (n *Node) Pos() int { return n.Loc.Pos() }
func (n *Node) End() int { return n.Loc.End() }
func (n *Node) ForEachChild(v Visitor) bool { return n.data.ForEachChild(v) }
func (n *Node) IterChildren() iter.Seq[*Node] { return n.data.IterChildren() }
func (n *Node) Clone(f NodeFactoryCoercible) *Node { return n.data.Clone(f) }
func (n *Node) VisitEachChild(v *NodeVisitor) *Node { return n.data.VisitEachChild(v) }
func (n *Node) Name() *DeclarationName { return n.data.Name() }
func (n *Node) Modifiers() *ModifierList { return n.data.Modifiers() }
func (n *Node) FlowNodeData() *FlowNodeBase { return n.data.FlowNodeData() }
func (n *Node) DeclarationData() *DeclarationBase { return n.data.DeclarationData() }
func (n *Node) ExportableData() *ExportableBase { return n.data.ExportableData() }
func (n *Node) LocalsContainerData() *LocalsContainerBase { return n.data.LocalsContainerData() }
func (n *Node) FunctionLikeData() *FunctionLikeBase { return n.data.FunctionLikeData() }
func (n *Node) ParameterList() *ParameterList { return n.data.FunctionLikeData().Parameters }
func (n *Node) Parameters() []*ParameterDeclarationNode { return n.ParameterList().Nodes }
func (n *Node) ClassLikeData() *ClassLikeBase { return n.data.ClassLikeData() }
func (n *Node) BodyData() *BodyBase { return n.data.BodyData() }
func (n *Node) SubtreeFacts() SubtreeFacts { return n.data.SubtreeFacts() }
func (n *Node) propagateSubtreeFacts() SubtreeFacts { return n.data.propagateSubtreeFacts() }
func (n *Node) LiteralLikeData() *LiteralLikeBase { return n.data.LiteralLikeData() }
func (n *Node) TemplateLiteralLikeData() *TemplateLiteralLikeBase {
return n.data.TemplateLiteralLikeData()
}
func (n *Node) KindString() string { return n.Kind.String() }
func (n *Node) KindValue() int16 { return int16(n.Kind) }
type MutableNode Node
func (n *Node) AsMutable() *MutableNode { return (*MutableNode)(n) }
func (n *MutableNode) SetModifiers(modifiers *ModifierList) { n.data.setModifiers(modifiers) }
func (n *Node) Symbol() *Symbol {
data := n.DeclarationData()
if data != nil {
return data.Symbol
}
return nil
}
func (n *Node) LocalSymbol() *Symbol {
data := n.ExportableData()
if data != nil {
return data.LocalSymbol
}
return nil
}
func (n *Node) Locals() SymbolTable {
data := n.LocalsContainerData()
if data != nil {
return data.Locals
}
return nil
}
func (n *Node) Body() *Node {
data := n.BodyData()
if data != nil {
return data.Body
}
return nil
}
func (n *Node) Text() string {
switch n.Kind {
case KindIdentifier:
return n.AsIdentifier().Text
case KindPrivateIdentifier:
return n.AsPrivateIdentifier().Text
case KindStringLiteral:
return n.AsStringLiteral().Text
case KindNumericLiteral:
return n.AsNumericLiteral().Text
case KindBigIntLiteral:
return n.AsBigIntLiteral().Text
case KindMetaProperty:
return n.AsMetaProperty().Name().Text()
case KindNoSubstitutionTemplateLiteral:
return n.AsNoSubstitutionTemplateLiteral().Text
case KindTemplateHead:
return n.AsTemplateHead().Text
case KindTemplateMiddle:
return n.AsTemplateMiddle().Text
case KindTemplateTail:
return n.AsTemplateTail().Text
case KindJsxNamespacedName:
return n.AsJsxNamespacedName().Namespace.Text() + ":" + n.AsJsxNamespacedName().name.Text()
case KindRegularExpressionLiteral:
return n.AsRegularExpressionLiteral().Text
case KindJSDocText:
return strings.Join(n.AsJSDocText().text, "")
case KindJSDocLink:
return strings.Join(n.AsJSDocLink().text, "")
case KindJSDocLinkCode:
return strings.Join(n.AsJSDocLinkCode().text, "")
case KindJSDocLinkPlain:
return strings.Join(n.AsJSDocLinkPlain().text, "")
}
panic(fmt.Sprintf("Unhandled case in Node.Text: %T", n.data))
}
func (n *Node) Expression() *Node {
switch n.Kind {
case KindPropertyAccessExpression:
return n.AsPropertyAccessExpression().Expression
case KindElementAccessExpression:
return n.AsElementAccessExpression().Expression
case KindParenthesizedExpression:
return n.AsParenthesizedExpression().Expression
case KindCallExpression:
return n.AsCallExpression().Expression
case KindNewExpression:
return n.AsNewExpression().Expression
case KindExpressionWithTypeArguments:
return n.AsExpressionWithTypeArguments().Expression
case KindComputedPropertyName:
return n.AsComputedPropertyName().Expression
case KindNonNullExpression:
return n.AsNonNullExpression().Expression
case KindTypeAssertionExpression:
return n.AsTypeAssertion().Expression
case KindAsExpression:
return n.AsAsExpression().Expression
case KindSatisfiesExpression:
return n.AsSatisfiesExpression().Expression
case KindTypeOfExpression:
return n.AsTypeOfExpression().Expression
case KindSpreadAssignment:
return n.AsSpreadAssignment().Expression
case KindSpreadElement:
return n.AsSpreadElement().Expression
case KindTemplateSpan:
return n.AsTemplateSpan().Expression
case KindDeleteExpression:
return n.AsDeleteExpression().Expression
case KindVoidExpression:
return n.AsVoidExpression().Expression
case KindAwaitExpression:
return n.AsAwaitExpression().Expression
case KindYieldExpression:
return n.AsYieldExpression().Expression
case KindPartiallyEmittedExpression:
return n.AsPartiallyEmittedExpression().Expression
case KindIfStatement:
return n.AsIfStatement().Expression
case KindDoStatement:
return n.AsDoStatement().Expression
case KindWhileStatement:
return n.AsWhileStatement().Expression
case KindWithStatement:
return n.AsWithStatement().Expression
case KindForInStatement, KindForOfStatement:
return n.AsForInOrOfStatement().Expression
case KindSwitchStatement:
return n.AsSwitchStatement().Expression
case KindCaseClause:
return n.AsCaseOrDefaultClause().Expression
case KindExpressionStatement:
return n.AsExpressionStatement().Expression
case KindReturnStatement:
return n.AsReturnStatement().Expression
case KindThrowStatement:
return n.AsThrowStatement().Expression
case KindExternalModuleReference:
return n.AsExternalModuleReference().Expression
case KindExportAssignment, KindJSExportAssignment:
return n.AsExportAssignment().Expression
case KindDecorator:
return n.AsDecorator().Expression
case KindJsxExpression:
return n.AsJsxExpression().Expression
case KindJsxSpreadAttribute:
return n.AsJsxSpreadAttribute().Expression
}
panic("Unhandled case in Node.Expression: " + n.Kind.String())
}
func (n *Node) RawText() string {
switch n.Kind {
case KindTemplateHead:
return n.AsTemplateHead().RawText
case KindTemplateMiddle:
return n.AsTemplateMiddle().RawText
case KindTemplateTail:
return n.AsTemplateTail().RawText
}
panic("Unhandled case in Node.RawText: " + n.Kind.String())
}
func (m *MutableNode) SetExpression(expr *Node) {
n := (*Node)(m)
switch n.Kind {
case KindPropertyAccessExpression:
n.AsPropertyAccessExpression().Expression = expr
case KindElementAccessExpression:
n.AsElementAccessExpression().Expression = expr
case KindParenthesizedExpression:
n.AsParenthesizedExpression().Expression = expr
case KindCallExpression:
n.AsCallExpression().Expression = expr
case KindNewExpression:
n.AsNewExpression().Expression = expr
case KindExpressionWithTypeArguments:
n.AsExpressionWithTypeArguments().Expression = expr
case KindComputedPropertyName:
n.AsComputedPropertyName().Expression = expr
case KindNonNullExpression:
n.AsNonNullExpression().Expression = expr
case KindTypeAssertionExpression:
n.AsTypeAssertion().Expression = expr
case KindAsExpression:
n.AsAsExpression().Expression = expr
case KindSatisfiesExpression:
n.AsSatisfiesExpression().Expression = expr
case KindTypeOfExpression:
n.AsTypeOfExpression().Expression = expr
case KindSpreadAssignment:
n.AsSpreadAssignment().Expression = expr
case KindSpreadElement:
n.AsSpreadElement().Expression = expr
case KindTemplateSpan:
n.AsTemplateSpan().Expression = expr
case KindDeleteExpression:
n.AsDeleteExpression().Expression = expr
case KindVoidExpression:
n.AsVoidExpression().Expression = expr
case KindAwaitExpression:
n.AsAwaitExpression().Expression = expr
case KindYieldExpression:
n.AsYieldExpression().Expression = expr
case KindPartiallyEmittedExpression:
n.AsPartiallyEmittedExpression().Expression = expr
case KindIfStatement:
n.AsIfStatement().Expression = expr
case KindDoStatement:
n.AsDoStatement().Expression = expr
case KindWhileStatement:
n.AsWhileStatement().Expression = expr
case KindWithStatement:
n.AsWithStatement().Expression = expr
case KindForInStatement, KindForOfStatement:
n.AsForInOrOfStatement().Expression = expr
case KindSwitchStatement:
n.AsSwitchStatement().Expression = expr
case KindCaseClause:
n.AsCaseOrDefaultClause().Expression = expr
case KindExpressionStatement:
n.AsExpressionStatement().Expression = expr
case KindReturnStatement:
n.AsReturnStatement().Expression = expr
case KindThrowStatement:
n.AsThrowStatement().Expression = expr
case KindExternalModuleReference:
n.AsExternalModuleReference().Expression = expr
case KindExportAssignment, KindJSExportAssignment:
n.AsExportAssignment().Expression = expr
case KindDecorator:
n.AsDecorator().Expression = expr
case KindJsxExpression:
n.AsJsxExpression().Expression = expr
case KindJsxSpreadAttribute:
n.AsJsxSpreadAttribute().Expression = expr
default:
panic("Unhandled case in mutableNode.SetExpression: " + n.Kind.String())
}
}
func (n *Node) ArgumentList() *NodeList {
switch n.Kind {
case KindCallExpression:
return n.AsCallExpression().Arguments
case KindNewExpression:
return n.AsNewExpression().Arguments
}
panic("Unhandled case in Node.Arguments: " + n.Kind.String())
}
func (n *Node) Arguments() []*Node {
list := n.ArgumentList()
if list != nil {
return list.Nodes
}
return nil
}
func (n *Node) TypeArgumentList() *NodeList {
switch n.Kind {
case KindCallExpression:
return n.AsCallExpression().TypeArguments
case KindNewExpression:
return n.AsNewExpression().TypeArguments
case KindTaggedTemplateExpression:
return n.AsTaggedTemplateExpression().TypeArguments
case KindTypeReference:
return n.AsTypeReference().TypeArguments
case KindExpressionWithTypeArguments:
return n.AsExpressionWithTypeArguments().TypeArguments
case KindImportType:
return n.AsImportTypeNode().TypeArguments
case KindTypeQuery:
return n.AsTypeQueryNode().TypeArguments
case KindJsxOpeningElement:
return n.AsJsxOpeningElement().TypeArguments
case KindJsxSelfClosingElement:
return n.AsJsxSelfClosingElement().TypeArguments
}
panic("Unhandled case in Node.TypeArguments")
}
func (n *Node) TypeArguments() []*Node {
list := n.TypeArgumentList()
if list != nil {
return list.Nodes
}
return nil
}
func (n *Node) TypeParameterList() *NodeList {
switch n.Kind {
case KindClassDeclaration:
return n.AsClassDeclaration().TypeParameters
case KindClassExpression:
return n.AsClassExpression().TypeParameters
case KindInterfaceDeclaration:
return n.AsInterfaceDeclaration().TypeParameters
case KindTypeAliasDeclaration, KindJSTypeAliasDeclaration:
return n.AsTypeAliasDeclaration().TypeParameters
case KindJSDocTemplateTag:
return n.AsJSDocTemplateTag().TypeParameters
default:
funcLike := n.FunctionLikeData()
if funcLike != nil {
return funcLike.TypeParameters
}
}
panic("Unhandled case in Node.TypeParameterList")
}
func (n *Node) TypeParameters() []*Node {
list := n.TypeParameterList()
if list != nil {
return list.Nodes
}
return nil
}
func (n *Node) MemberList() *NodeList {
switch n.Kind {
case KindClassDeclaration:
return n.AsClassDeclaration().Members
case KindClassExpression:
return n.AsClassExpression().Members
case KindInterfaceDeclaration:
return n.AsInterfaceDeclaration().Members
case KindEnumDeclaration:
return n.AsEnumDeclaration().Members
case KindTypeLiteral:
return n.AsTypeLiteralNode().Members
case KindMappedType:
return n.AsMappedTypeNode().Members
}
panic("Unhandled case in Node.MemberList: " + n.Kind.String())
}
func (n *Node) Members() []*Node {
list := n.MemberList()
if list != nil {
return list.Nodes
}
return nil
}
func (n *Node) StatementList() *NodeList {
switch n.Kind {
case KindSourceFile:
return n.AsSourceFile().Statements
case KindBlock:
return n.AsBlock().Statements
case KindModuleBlock:
return n.AsModuleBlock().Statements
case KindCaseClause, KindDefaultClause:
return n.AsCaseOrDefaultClause().Statements
}
panic("Unhandled case in Node.StatementList: " + n.Kind.String())
}
func (n *Node) Statements() []*Node {
list := n.StatementList()
if list != nil {
return list.Nodes
}
return nil
}
func (n *Node) CanHaveStatements() bool {
switch n.Kind {
case KindSourceFile, KindBlock, KindModuleBlock, KindCaseClause, KindDefaultClause:
return true
default:
return false
}
}
func (n *Node) ModifierFlags() ModifierFlags {
modifiers := n.Modifiers()
if modifiers != nil {
return modifiers.ModifierFlags
}
return ModifierFlagsNone
}
func (n *Node) ModifierNodes() []*Node {
modifiers := n.Modifiers()
if modifiers != nil {
return modifiers.Nodes
}
return nil
}
func (n *Node) Type() *Node {
switch n.Kind {
case KindVariableDeclaration:
return n.AsVariableDeclaration().Type
case KindParameter:
return n.AsParameterDeclaration().Type
case KindPropertySignature:
return n.AsPropertySignatureDeclaration().Type
case KindPropertyDeclaration:
return n.AsPropertyDeclaration().Type
case KindPropertyAssignment:
return n.AsPropertyAssignment().Type
case KindShorthandPropertyAssignment:
return n.AsShorthandPropertyAssignment().Type
case KindTypePredicate:
return n.AsTypePredicateNode().Type
case KindParenthesizedType:
return n.AsParenthesizedTypeNode().Type
case KindTypeOperator:
return n.AsTypeOperatorNode().Type
case KindMappedType:
return n.AsMappedTypeNode().Type
case KindTypeAssertionExpression:
return n.AsTypeAssertion().Type
case KindAsExpression:
return n.AsAsExpression().Type
case KindSatisfiesExpression:
return n.AsSatisfiesExpression().Type
case KindTypeAliasDeclaration, KindJSTypeAliasDeclaration:
return n.AsTypeAliasDeclaration().Type
case KindNamedTupleMember:
return n.AsNamedTupleMember().Type
case KindOptionalType:
return n.AsOptionalTypeNode().Type
case KindRestType:
return n.AsRestTypeNode().Type
case KindTemplateLiteralTypeSpan:
return n.AsTemplateLiteralTypeSpan().Type
case KindJSDocTypeExpression:
return n.AsJSDocTypeExpression().Type
case KindJSDocParameterTag, KindJSDocPropertyTag:
return n.AsJSDocParameterOrPropertyTag().TypeExpression
case KindJSDocNullableType:
return n.AsJSDocNullableType().Type
case KindJSDocNonNullableType:
return n.AsJSDocNonNullableType().Type
case KindJSDocOptionalType:
return n.AsJSDocOptionalType().Type
case KindExportAssignment, KindJSExportAssignment:
return n.AsExportAssignment().Type
case KindCommonJSExport:
return n.AsCommonJSExport().Type
case KindBinaryExpression:
return n.AsBinaryExpression().Type
default:
if funcLike := n.FunctionLikeData(); funcLike != nil {
return funcLike.Type
}
}
return nil
}
func (m *MutableNode) SetType(t *Node) {
n := (*Node)(m)
switch m.Kind {
case KindVariableDeclaration:
n.AsVariableDeclaration().Type = t
case KindParameter:
n.AsParameterDeclaration().Type = t
case KindPropertySignature:
n.AsPropertySignatureDeclaration().Type = t
case KindPropertyDeclaration:
n.AsPropertyDeclaration().Type = t
case KindPropertyAssignment:
n.AsPropertyAssignment().Type = t
case KindShorthandPropertyAssignment:
n.AsShorthandPropertyAssignment().Type = t
case KindTypePredicate:
n.AsTypePredicateNode().Type = t
case KindParenthesizedType:
n.AsParenthesizedTypeNode().Type = t
case KindTypeOperator:
n.AsTypeOperatorNode().Type = t
case KindMappedType:
n.AsMappedTypeNode().Type = t
case KindTypeAssertionExpression:
n.AsTypeAssertion().Type = t
case KindAsExpression:
n.AsAsExpression().Type = t
case KindSatisfiesExpression:
n.AsSatisfiesExpression().Type = t
case KindTypeAliasDeclaration, KindJSTypeAliasDeclaration:
n.AsTypeAliasDeclaration().Type = t
case KindNamedTupleMember:
n.AsNamedTupleMember().Type = t
case KindOptionalType:
n.AsOptionalTypeNode().Type = t
case KindRestType:
n.AsRestTypeNode().Type = t
case KindTemplateLiteralTypeSpan:
n.AsTemplateLiteralTypeSpan().Type = t
case KindJSDocTypeExpression:
n.AsJSDocTypeExpression().Type = t
case KindJSDocParameterTag, KindJSDocPropertyTag:
n.AsJSDocParameterOrPropertyTag().TypeExpression = t
case KindJSDocNullableType:
n.AsJSDocNullableType().Type = t
case KindJSDocNonNullableType:
n.AsJSDocNonNullableType().Type = t
case KindJSDocOptionalType:
n.AsJSDocOptionalType().Type = t
case KindExportAssignment, KindJSExportAssignment:
n.AsExportAssignment().Type = t
case KindCommonJSExport:
n.AsCommonJSExport().Type = t
case KindBinaryExpression:
n.AsBinaryExpression().Type = t
default:
if funcLike := n.FunctionLikeData(); funcLike != nil {
funcLike.Type = t
} else {
panic("Unhandled case in mutableNode.SetType: " + n.Kind.String())
}
}
}
func (n *Node) Initializer() *Node {
switch n.Kind {
case KindVariableDeclaration:
return n.AsVariableDeclaration().Initializer
case KindParameter:
return n.AsParameterDeclaration().Initializer
case KindBindingElement:
return n.AsBindingElement().Initializer
case KindPropertyDeclaration:
return n.AsPropertyDeclaration().Initializer
case KindPropertySignature:
return n.AsPropertySignatureDeclaration().Initializer
case KindPropertyAssignment:
return n.AsPropertyAssignment().Initializer
case KindEnumMember:
return n.AsEnumMember().Initializer
case KindForStatement:
return n.AsForStatement().Initializer
case KindForInStatement, KindForOfStatement:
return n.AsForInOrOfStatement().Initializer
case KindJsxAttribute:
return n.AsJsxAttribute().Initializer
case KindCommonJSExport:
return n.AsCommonJSExport().Initializer
}
panic("Unhandled case in Node.Initializer")
}
func (m *MutableNode) SetInitializer(initializer *Node) {
n := (*Node)(m)
switch n.Kind {
case KindVariableDeclaration:
n.AsVariableDeclaration().Initializer = initializer
case KindParameter:
n.AsParameterDeclaration().Initializer = initializer
case KindBindingElement:
n.AsBindingElement().Initializer = initializer
case KindPropertyDeclaration:
n.AsPropertyDeclaration().Initializer = initializer
case KindPropertySignature:
n.AsPropertySignatureDeclaration().Initializer = initializer
case KindPropertyAssignment:
n.AsPropertyAssignment().Initializer = initializer
case KindEnumMember:
n.AsEnumMember().Initializer = initializer
case KindForStatement:
n.AsForStatement().Initializer = initializer
case KindForInStatement, KindForOfStatement:
n.AsForInOrOfStatement().Initializer = initializer
case KindJsxAttribute:
n.AsJsxAttribute().Initializer = initializer
case KindCommonJSExport:
n.AsCommonJSExport().Initializer = initializer
default:
panic("Unhandled case in mutableNode.SetInitializer")
}
}
func (n *Node) TagName() *Node {
switch n.Kind {
case KindJsxOpeningElement:
return n.AsJsxOpeningElement().TagName
case KindJsxClosingElement:
return n.AsJsxClosingElement().TagName
case KindJsxSelfClosingElement:
return n.AsJsxSelfClosingElement().TagName
case KindJSDocTag:
return n.AsJSDocUnknownTag().TagName
case KindJSDocAugmentsTag:
return n.AsJSDocAugmentsTag().TagName
case KindJSDocImplementsTag:
return n.AsJSDocImplementsTag().TagName
case KindJSDocDeprecatedTag:
return n.AsJSDocDeprecatedTag().TagName
case KindJSDocPublicTag:
return n.AsJSDocPublicTag().TagName
case KindJSDocPrivateTag:
return n.AsJSDocPrivateTag().TagName
case KindJSDocProtectedTag:
return n.AsJSDocProtectedTag().TagName
case KindJSDocReadonlyTag:
return n.AsJSDocReadonlyTag().TagName
case KindJSDocOverrideTag:
return n.AsJSDocOverrideTag().TagName
case KindJSDocCallbackTag:
return n.AsJSDocCallbackTag().TagName
case KindJSDocOverloadTag:
return n.AsJSDocOverloadTag().TagName
case KindJSDocParameterTag, KindJSDocPropertyTag:
return n.AsJSDocParameterOrPropertyTag().TagName
case KindJSDocReturnTag:
return n.AsJSDocReturnTag().TagName
case KindJSDocThisTag:
return n.AsJSDocThisTag().TagName
case KindJSDocTypeTag:
return n.AsJSDocTypeTag().TagName
case KindJSDocTemplateTag:
return n.AsJSDocTemplateTag().TagName
case KindJSDocTypedefTag:
return n.AsJSDocTypedefTag().TagName
case KindJSDocSeeTag:
return n.AsJSDocSeeTag().TagName
case KindJSDocSatisfiesTag:
return n.AsJSDocSatisfiesTag().TagName
case KindJSDocImportTag:
return n.AsJSDocImportTag().TagName
}
panic("Unhandled case in Node.TagName: " + n.Kind.String())
}
func (n *Node) PropertyName() *Node {
switch n.Kind {
case KindImportSpecifier:
return n.AsImportSpecifier().PropertyName
case KindExportSpecifier:
return n.AsExportSpecifier().PropertyName
case KindBindingElement:
return n.AsBindingElement().PropertyName
}
return nil
}
func (n *Node) PropertyNameOrName() *Node {
name := n.PropertyName()
if name == nil {
name = n.Name()
}
return name
}
func (n *Node) IsTypeOnly() bool {
switch n.Kind {
case KindImportEqualsDeclaration:
return n.AsImportEqualsDeclaration().IsTypeOnly
case KindImportSpecifier:
return n.AsImportSpecifier().IsTypeOnly
case KindImportClause:
return n.AsImportClause().PhaseModifier == KindTypeKeyword
case KindExportDeclaration:
return n.AsExportDeclaration().IsTypeOnly
case KindExportSpecifier:
return n.AsExportSpecifier().IsTypeOnly
}
return false
}
// If updating this function, also update `hasComment`.
func (n *Node) CommentList() *NodeList {
switch n.Kind {
case KindJSDoc:
return n.AsJSDoc().Comment
case KindJSDocTag:
return n.AsJSDocUnknownTag().Comment
case KindJSDocAugmentsTag:
return n.AsJSDocAugmentsTag().Comment
case KindJSDocImplementsTag:
return n.AsJSDocImplementsTag().Comment
case KindJSDocDeprecatedTag:
return n.AsJSDocDeprecatedTag().Comment
case KindJSDocPublicTag:
return n.AsJSDocPublicTag().Comment
case KindJSDocPrivateTag:
return n.AsJSDocPrivateTag().Comment
case KindJSDocProtectedTag:
return n.AsJSDocProtectedTag().Comment
case KindJSDocReadonlyTag:
return n.AsJSDocReadonlyTag().Comment
case KindJSDocOverrideTag:
return n.AsJSDocOverrideTag().Comment
case KindJSDocCallbackTag:
return n.AsJSDocCallbackTag().Comment
case KindJSDocOverloadTag:
return n.AsJSDocOverloadTag().Comment
case KindJSDocParameterTag, KindJSDocPropertyTag:
return n.AsJSDocParameterOrPropertyTag().Comment
case KindJSDocReturnTag:
return n.AsJSDocReturnTag().Comment
case KindJSDocThisTag:
return n.AsJSDocThisTag().Comment
case KindJSDocTypeTag:
return n.AsJSDocTypeTag().Comment
case KindJSDocTemplateTag:
return n.AsJSDocTemplateTag().Comment
case KindJSDocTypedefTag:
return n.AsJSDocTypedefTag().Comment
case KindJSDocSeeTag:
return n.AsJSDocSeeTag().Comment
case KindJSDocSatisfiesTag:
return n.AsJSDocSatisfiesTag().Comment
case KindJSDocImportTag:
return n.AsJSDocImportTag().Comment
}
panic("Unhandled case in Node.CommentList: " + n.Kind.String())
}
func (n *Node) Comments() []*Node {
list := n.CommentList()
if list != nil {
return list.Nodes
}
return nil
}
func (n *Node) Label() *Node {
switch n.Kind {
case KindLabeledStatement:
return n.AsLabeledStatement().Label
case KindBreakStatement:
return n.AsBreakStatement().Label
case KindContinueStatement:
return n.AsContinueStatement().Label
}
panic("Unhandled case in Node.Label: " + n.Kind.String())
}
func (n *Node) Attributes() *Node {