-
Notifications
You must be signed in to change notification settings - Fork 189
/
TypeGuards.ts
2442 lines (2279 loc) · 96.3 KB
/
TypeGuards.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
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
// dprint-ignore-file
// -----------------------
// WARNING - DO NOT modify the "is" methods of the TypeGuards class directly. It is code generated by createTypeGuardsUtility.ts
//
// Note: This file is excluded from code coverage reports because it's automatically maintained (low risk).
// -----------------------
import { SyntaxKind } from "@ts-morph/common";
import * as compiler from "../compiler";
import { Structure } from "../structures";
import { KindToNodeMappings } from "../compiler";
/**
* Type guards for checking the type of a node.
*/
export class TypeGuards {
private constructor() {
}
/**
* Gets if the node has an expression.
* @param node - Node to check.
*/
static hasExpression<T extends compiler.Node>(node: T): node is T & { getExpression(): compiler.Expression; } {
// this method is manually maintained
return (node as any).getExpression?.() != null;
}
/**
* Gets if the node has a name.
* @param node - Node to check.
*/
static hasName<T extends compiler.Node>(node: T): node is T & { getName(): string; getNameNode(): compiler.Node; } {
// this method is manually maintained
return typeof (node as any).getName?.() === "string";
}
/**
* Gets if the node has a body.
* @param node - Node to check.
*/
static hasBody<T extends compiler.Node>(node: T): node is T & { getBody(): compiler.Node; } {
// this method is manually maintained
return (node as any).getBody?.() != null;
}
/**
* Creates a type guard for syntax kinds.
*/
static is<TKind extends keyof KindToNodeMappings>(kind: TKind): (node: compiler.Node) => node is KindToNodeMappings[TKind] {
return (node: compiler.Node): node is KindToNodeMappings[TKind] => {
return node.getKind() == kind;
};
}
/**
* Gets if the provided value is a Node.
*/
static isNode(value: unknown): value is compiler.Node {
return value != null && (value as any).compilerNode != null
}
/**
* Gets if the provided node is a comment node.
*/
static isCommentNode(node: compiler.Node): node is compiler.CommentStatement | compiler.CommentClassElement | compiler.CommentTypeElement | compiler.CommentObjectLiteralElement | compiler.CommentEnumMember {
const kind = node.getKind();
return kind === SyntaxKind.SingleLineCommentTrivia || kind === SyntaxKind.MultiLineCommentTrivia;
}
/**
* Gets if the provided node is a CommentStatement.
*/
static isCommentStatement(node: compiler.Node): node is compiler.CommentStatement {
return (node.compilerNode as compiler.CompilerCommentStatement)._commentKind === compiler.CommentNodeKind.Statement;
}
/**
* Gets if the provided node is a CommentClassElement.
*/
static isCommentClassElement(node: compiler.Node): node is compiler.CommentClassElement {
return (node.compilerNode as compiler.CompilerCommentClassElement)._commentKind === compiler.CommentNodeKind.ClassElement;
}
/**
* Gets if the provided value is a CommentTypeElement.
*/
static isCommentTypeElement(node: compiler.Node): node is compiler.CommentTypeElement {
return (node.compilerNode as compiler.CompilerCommentTypeElement)._commentKind === compiler.CommentNodeKind.TypeElement;
}
/**
* Gets if the provided node is a CommentObjectLiteralElement.
*/
static isCommentObjectLiteralElement(node: compiler.Node): node is compiler.CommentObjectLiteralElement {
return (node.compilerNode as compiler.CompilerCommentObjectLiteralElement)._commentKind === compiler.CommentNodeKind.ObjectLiteralElement;
}
/**
* Gets if the provided node is a CommentEnumMember.
*/
static isCommentEnumMember(node: compiler.Node): node is compiler.CommentEnumMember {
return (node.compilerNode as compiler.CompilerCommentEnumMember)._commentKind == compiler.CommentNodeKind.EnumMember;
}
/**
* Gets if the node is an AbstractableNode.
* @param node - Node to check.
*/
static isAbstractableNode<T extends compiler.Node>(node: T): node is compiler.AbstractableNode & compiler.AbstractableNodeExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression:
case SyntaxKind.GetAccessor:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.SetAccessor:
return true;
default:
return false;
}
}
/**
* Gets if the node is an AmbientableNode.
* @param node - Node to check.
*/
static isAmbientableNode<T extends compiler.Node>(node: T): node is compiler.AmbientableNode & compiler.AmbientableNodeExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.VariableStatement:
case SyntaxKind.TypeAliasDeclaration:
return true;
default:
return false;
}
}
/**
* Gets if the node is an AnyKeyword.
*/
static readonly isAnyKeyword: (node: compiler.Node) => node is compiler.Expression = TypeGuards.is(SyntaxKind.AnyKeyword);
/**
* Gets if the node is an ArgumentedNode.
* @param node - Node to check.
*/
static isArgumentedNode<T extends compiler.Node>(node: T): node is compiler.ArgumentedNode & compiler.ArgumentedNodeExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.CallExpression:
case SyntaxKind.NewExpression:
return true;
default:
return false;
}
}
/**
* Gets if the node is an ArrayBindingPattern.
*/
static readonly isArrayBindingPattern: (node: compiler.Node) => node is compiler.ArrayBindingPattern = TypeGuards.is(SyntaxKind.ArrayBindingPattern);
/**
* Gets if the node is an ArrayLiteralExpression.
*/
static readonly isArrayLiteralExpression: (node: compiler.Node) => node is compiler.ArrayLiteralExpression = TypeGuards.is(SyntaxKind.ArrayLiteralExpression);
/**
* Gets if the node is an ArrayTypeNode.
* @param node - Node to check.
*/
static isArrayTypeNode(node: compiler.Node): node is compiler.ArrayTypeNode {
return node.getKind() === SyntaxKind.ArrayType;
}
/**
* Gets if the node is an ArrowFunction.
*/
static readonly isArrowFunction: (node: compiler.Node) => node is compiler.ArrowFunction = TypeGuards.is(SyntaxKind.ArrowFunction);
/**
* Gets if the node is an AsExpression.
*/
static readonly isAsExpression: (node: compiler.Node) => node is compiler.AsExpression = TypeGuards.is(SyntaxKind.AsExpression);
/**
* Gets if the node is an AsyncableNode.
* @param node - Node to check.
*/
static isAsyncableNode<T extends compiler.Node>(node: T): node is compiler.AsyncableNode & compiler.AsyncableNodeExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.MethodDeclaration:
case SyntaxKind.ArrowFunction:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
return true;
default:
return false;
}
}
/**
* Gets if the node is an AwaitExpression.
*/
static readonly isAwaitExpression: (node: compiler.Node) => node is compiler.AwaitExpression = TypeGuards.is(SyntaxKind.AwaitExpression);
/**
* Gets if the node is an AwaitableNode.
* @param node - Node to check.
*/
static isAwaitableNode<T extends compiler.Node>(node: T): node is compiler.AwaitableNode & compiler.AwaitableNodeExtensionType & T {
return node.getKind() === SyntaxKind.ForOfStatement;
}
/**
* Gets if the node is a BinaryExpression.
*/
static readonly isBinaryExpression: (node: compiler.Node) => node is compiler.BinaryExpression = TypeGuards.is(SyntaxKind.BinaryExpression);
/**
* Gets if the node is a BindingElement.
*/
static readonly isBindingElement: (node: compiler.Node) => node is compiler.BindingElement = TypeGuards.is(SyntaxKind.BindingElement);
/**
* Gets if the node is a BindingNamedNode.
* @param node - Node to check.
*/
static isBindingNamedNode<T extends compiler.Node>(node: T): node is compiler.BindingNamedNode & compiler.BindingNamedNodeExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.BindingElement:
case SyntaxKind.Parameter:
case SyntaxKind.VariableDeclaration:
return true;
default:
return false;
}
}
/**
* Gets if the node is a Block.
*/
static readonly isBlock: (node: compiler.Node) => node is compiler.Block = TypeGuards.is(SyntaxKind.Block);
/**
* Gets if the node is a BodiedNode.
* @param node - Node to check.
*/
static isBodiedNode<T extends compiler.Node>(node: T): node is compiler.BodiedNode & compiler.BodiedNodeExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.ArrowFunction:
case SyntaxKind.FunctionExpression:
case SyntaxKind.ModuleDeclaration:
return true;
default:
return false;
}
}
/**
* Gets if the node is a BodyableNode.
* @param node - Node to check.
*/
static isBodyableNode<T extends compiler.Node>(node: T): node is compiler.BodyableNode & compiler.BodyableNodeExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.Constructor:
case SyntaxKind.GetAccessor:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.SetAccessor:
case SyntaxKind.FunctionDeclaration:
return true;
default:
return false;
}
}
/**
* Gets if the node is a BooleanKeyword.
*/
static readonly isBooleanKeyword: (node: compiler.Node) => node is compiler.Expression = TypeGuards.is(SyntaxKind.BooleanKeyword);
/**
* Gets if the node is a BooleanLiteral.
* @param node - Node to check.
*/
static isBooleanLiteral(node: compiler.Node): node is compiler.BooleanLiteral {
switch (node.getKind()) {
case SyntaxKind.FalseKeyword:
case SyntaxKind.TrueKeyword:
return true;
default:
return false;
}
}
/**
* Gets if the node is a BreakStatement.
*/
static readonly isBreakStatement: (node: compiler.Node) => node is compiler.BreakStatement = TypeGuards.is(SyntaxKind.BreakStatement);
/**
* Gets if the node is a CallExpression.
*/
static readonly isCallExpression: (node: compiler.Node) => node is compiler.CallExpression = TypeGuards.is(SyntaxKind.CallExpression);
/**
* Gets if the node is a CallSignatureDeclaration.
* @param node - Node to check.
*/
static isCallSignatureDeclaration(node: compiler.Node): node is compiler.CallSignatureDeclaration {
return node.getKind() === SyntaxKind.CallSignature;
}
/**
* Gets if the node is a CaseBlock.
*/
static readonly isCaseBlock: (node: compiler.Node) => node is compiler.CaseBlock = TypeGuards.is(SyntaxKind.CaseBlock);
/**
* Gets if the node is a CaseClause.
*/
static readonly isCaseClause: (node: compiler.Node) => node is compiler.CaseClause = TypeGuards.is(SyntaxKind.CaseClause);
/**
* Gets if the node is a CatchClause.
*/
static readonly isCatchClause: (node: compiler.Node) => node is compiler.CatchClause = TypeGuards.is(SyntaxKind.CatchClause);
/**
* Gets if the node is a ChildOrderableNode.
* @param node - Node to check.
*/
static isChildOrderableNode<T extends compiler.Node>(node: T): node is compiler.ChildOrderableNode & compiler.ChildOrderableNodeExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.Constructor:
case SyntaxKind.GetAccessor:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.SetAccessor:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.CallSignature:
case SyntaxKind.ConstructSignature:
case SyntaxKind.IndexSignature:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.MethodSignature:
case SyntaxKind.PropertySignature:
case SyntaxKind.ExportAssignment:
case SyntaxKind.ExportDeclaration:
case SyntaxKind.ImportDeclaration:
case SyntaxKind.ImportEqualsDeclaration:
case SyntaxKind.ModuleBlock:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.Block:
case SyntaxKind.BreakStatement:
case SyntaxKind.ContinueStatement:
case SyntaxKind.DebuggerStatement:
case SyntaxKind.DoStatement:
case SyntaxKind.EmptyStatement:
case SyntaxKind.ExpressionStatement:
case SyntaxKind.ForInStatement:
case SyntaxKind.ForOfStatement:
case SyntaxKind.ForStatement:
case SyntaxKind.IfStatement:
case SyntaxKind.LabeledStatement:
case SyntaxKind.NotEmittedStatement:
case SyntaxKind.ReturnStatement:
case SyntaxKind.SwitchStatement:
case SyntaxKind.ThrowStatement:
case SyntaxKind.TryStatement:
case SyntaxKind.VariableStatement:
case SyntaxKind.WhileStatement:
case SyntaxKind.WithStatement:
case SyntaxKind.TypeAliasDeclaration:
return true;
default:
return false;
}
}
/**
* Gets if the node is a ClassDeclaration.
*/
static readonly isClassDeclaration: (node: compiler.Node) => node is compiler.ClassDeclaration = TypeGuards.is(SyntaxKind.ClassDeclaration);
/**
* Gets if the node is a ClassExpression.
*/
static readonly isClassExpression: (node: compiler.Node) => node is compiler.ClassExpression = TypeGuards.is(SyntaxKind.ClassExpression);
/**
* Gets if the node is a ClassLikeDeclarationBase.
* @param node - Node to check.
*/
static isClassLikeDeclarationBase<T extends compiler.Node>(node: T): node is compiler.ClassLikeDeclarationBase & compiler.ClassLikeDeclarationBaseExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression:
return true;
default:
return false;
}
}
/**
* Gets if the node is a CommaListExpression.
*/
static readonly isCommaListExpression: (node: compiler.Node) => node is compiler.CommaListExpression = TypeGuards.is(SyntaxKind.CommaListExpression);
/**
* Gets if the node is a ComputedPropertyName.
*/
static readonly isComputedPropertyName: (node: compiler.Node) => node is compiler.ComputedPropertyName = TypeGuards.is(SyntaxKind.ComputedPropertyName);
/**
* Gets if the node is a ConditionalExpression.
*/
static readonly isConditionalExpression: (node: compiler.Node) => node is compiler.ConditionalExpression = TypeGuards.is(SyntaxKind.ConditionalExpression);
/**
* Gets if the node is a ConditionalTypeNode.
* @param node - Node to check.
*/
static isConditionalTypeNode(node: compiler.Node): node is compiler.ConditionalTypeNode {
return node.getKind() === SyntaxKind.ConditionalType;
}
/**
* Gets if the node is a ConstructSignatureDeclaration.
* @param node - Node to check.
*/
static isConstructSignatureDeclaration(node: compiler.Node): node is compiler.ConstructSignatureDeclaration {
return node.getKind() === SyntaxKind.ConstructSignature;
}
/**
* Gets if the node is a ConstructorDeclaration.
* @param node - Node to check.
*/
static isConstructorDeclaration(node: compiler.Node): node is compiler.ConstructorDeclaration {
return node.getKind() === SyntaxKind.Constructor;
}
/**
* Gets if the node is a ConstructorTypeNode.
* @param node - Node to check.
*/
static isConstructorTypeNode(node: compiler.Node): node is compiler.ConstructorTypeNode {
return node.getKind() === SyntaxKind.ConstructorType;
}
/**
* Gets if the node is a ContinueStatement.
*/
static readonly isContinueStatement: (node: compiler.Node) => node is compiler.ContinueStatement = TypeGuards.is(SyntaxKind.ContinueStatement);
/**
* Gets if the node is a DebuggerStatement.
*/
static readonly isDebuggerStatement: (node: compiler.Node) => node is compiler.DebuggerStatement = TypeGuards.is(SyntaxKind.DebuggerStatement);
/**
* Gets if the node is a DecoratableNode.
* @param node - Node to check.
*/
static isDecoratableNode<T extends compiler.Node>(node: T): node is compiler.DecoratableNode & compiler.DecoratableNodeExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression:
case SyntaxKind.GetAccessor:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.SetAccessor:
case SyntaxKind.Parameter:
return true;
default:
return false;
}
}
/**
* Gets if the node is a Decorator.
*/
static readonly isDecorator: (node: compiler.Node) => node is compiler.Decorator = TypeGuards.is(SyntaxKind.Decorator);
/**
* Gets if the node is a DefaultClause.
*/
static readonly isDefaultClause: (node: compiler.Node) => node is compiler.DefaultClause = TypeGuards.is(SyntaxKind.DefaultClause);
/**
* Gets if the node is a DeleteExpression.
*/
static readonly isDeleteExpression: (node: compiler.Node) => node is compiler.DeleteExpression = TypeGuards.is(SyntaxKind.DeleteExpression);
/**
* Gets if the node is a DoStatement.
*/
static readonly isDoStatement: (node: compiler.Node) => node is compiler.DoStatement = TypeGuards.is(SyntaxKind.DoStatement);
/**
* Gets if the node is an ElementAccessExpression.
*/
static readonly isElementAccessExpression: (node: compiler.Node) => node is compiler.ElementAccessExpression = TypeGuards.is(SyntaxKind.ElementAccessExpression);
/**
* Gets if the node is an EmptyStatement.
*/
static readonly isEmptyStatement: (node: compiler.Node) => node is compiler.EmptyStatement = TypeGuards.is(SyntaxKind.EmptyStatement);
/**
* Gets if the node is an EnumDeclaration.
*/
static readonly isEnumDeclaration: (node: compiler.Node) => node is compiler.EnumDeclaration = TypeGuards.is(SyntaxKind.EnumDeclaration);
/**
* Gets if the node is an EnumMember.
*/
static readonly isEnumMember: (node: compiler.Node) => node is compiler.EnumMember = TypeGuards.is(SyntaxKind.EnumMember);
/**
* Gets if the node is an ExclamationTokenableNode.
* @param node - Node to check.
*/
static isExclamationTokenableNode<T extends compiler.Node>(node: T): node is compiler.ExclamationTokenableNode & compiler.ExclamationTokenableNodeExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.VariableDeclaration:
return true;
default:
return false;
}
}
/**
* Gets if the node is an ExportAssignment.
*/
static readonly isExportAssignment: (node: compiler.Node) => node is compiler.ExportAssignment = TypeGuards.is(SyntaxKind.ExportAssignment);
/**
* Gets if the node is an ExportDeclaration.
*/
static readonly isExportDeclaration: (node: compiler.Node) => node is compiler.ExportDeclaration = TypeGuards.is(SyntaxKind.ExportDeclaration);
/**
* Gets if the node is an ExportGetableNode.
* @param node - Node to check.
*/
static isExportGetableNode<T extends compiler.Node>(node: T): node is compiler.ExportGetableNode & compiler.ExportGetableNodeExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.VariableStatement:
case SyntaxKind.TypeAliasDeclaration:
case SyntaxKind.VariableDeclaration:
return true;
default:
return false;
}
}
/**
* Gets if the node is an ExportSpecifier.
*/
static readonly isExportSpecifier: (node: compiler.Node) => node is compiler.ExportSpecifier = TypeGuards.is(SyntaxKind.ExportSpecifier);
/**
* Gets if the node is an ExportableNode.
* @param node - Node to check.
*/
static isExportableNode<T extends compiler.Node>(node: T): node is compiler.ExportableNode & compiler.ExportableNodeExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.VariableStatement:
case SyntaxKind.TypeAliasDeclaration:
return true;
default:
return false;
}
}
/**
* Gets if the node is an Expression.
* @param node - Node to check.
*/
static isExpression(node: compiler.Node): node is compiler.Expression {
switch (node.getKind()) {
case SyntaxKind.AnyKeyword:
case SyntaxKind.BooleanKeyword:
case SyntaxKind.NeverKeyword:
case SyntaxKind.NumberKeyword:
case SyntaxKind.ObjectKeyword:
case SyntaxKind.StringKeyword:
case SyntaxKind.SymbolKeyword:
case SyntaxKind.UndefinedKeyword:
case SyntaxKind.ClassExpression:
case SyntaxKind.Identifier:
case SyntaxKind.AsExpression:
case SyntaxKind.AwaitExpression:
case SyntaxKind.BinaryExpression:
case SyntaxKind.CallExpression:
case SyntaxKind.CommaListExpression:
case SyntaxKind.ConditionalExpression:
case SyntaxKind.DeleteExpression:
case SyntaxKind.ElementAccessExpression:
case SyntaxKind.ImportKeyword:
case SyntaxKind.MetaProperty:
case SyntaxKind.NewExpression:
case SyntaxKind.NonNullExpression:
case SyntaxKind.OmittedExpression:
case SyntaxKind.ParenthesizedExpression:
case SyntaxKind.PartiallyEmittedExpression:
case SyntaxKind.PostfixUnaryExpression:
case SyntaxKind.PrefixUnaryExpression:
case SyntaxKind.PropertyAccessExpression:
case SyntaxKind.SpreadElement:
case SyntaxKind.SuperKeyword:
case SyntaxKind.ThisKeyword:
case SyntaxKind.TypeAssertionExpression:
case SyntaxKind.TypeOfExpression:
case SyntaxKind.VoidExpression:
case SyntaxKind.YieldExpression:
case SyntaxKind.ArrowFunction:
case SyntaxKind.FunctionExpression:
case SyntaxKind.JsxClosingFragment:
case SyntaxKind.JsxElement:
case SyntaxKind.JsxExpression:
case SyntaxKind.JsxFragment:
case SyntaxKind.JsxOpeningElement:
case SyntaxKind.JsxOpeningFragment:
case SyntaxKind.JsxSelfClosingElement:
case SyntaxKind.FalseKeyword:
case SyntaxKind.TrueKeyword:
case SyntaxKind.NullKeyword:
case SyntaxKind.NumericLiteral:
case SyntaxKind.RegularExpressionLiteral:
case SyntaxKind.StringLiteral:
case SyntaxKind.ArrayLiteralExpression:
case SyntaxKind.ObjectLiteralExpression:
case SyntaxKind.NoSubstitutionTemplateLiteral:
case SyntaxKind.TaggedTemplateExpression:
case SyntaxKind.TemplateExpression:
return true;
default:
return false;
}
}
/**
* Gets if the node is an ExpressionStatement.
*/
static readonly isExpressionStatement: (node: compiler.Node) => node is compiler.ExpressionStatement = TypeGuards.is(SyntaxKind.ExpressionStatement);
/**
* Gets if the node is an ExpressionWithTypeArguments.
*/
static readonly isExpressionWithTypeArguments: (node: compiler.Node) => node is compiler.ExpressionWithTypeArguments = TypeGuards.is(SyntaxKind.ExpressionWithTypeArguments);
/**
* Gets if the node is an ExpressionedNode.
* @param node - Node to check.
*/
static isExpressionedNode<T extends compiler.Node>(node: T): node is compiler.ExpressionedNode & compiler.ExpressionedNodeExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.AsExpression:
case SyntaxKind.NonNullExpression:
case SyntaxKind.ParenthesizedExpression:
case SyntaxKind.PartiallyEmittedExpression:
case SyntaxKind.SpreadElement:
case SyntaxKind.SpreadAssignment:
case SyntaxKind.TemplateSpan:
return true;
default:
return false;
}
}
/**
* Gets if the node is an ExtendsClauseableNode.
* @param node - Node to check.
*/
static isExtendsClauseableNode<T extends compiler.Node>(node: T): node is compiler.ExtendsClauseableNode & compiler.ExtendsClauseableNodeExtensionType & T {
return node.getKind() === SyntaxKind.InterfaceDeclaration;
}
/**
* Gets if the node is an ExternalModuleReference.
*/
static readonly isExternalModuleReference: (node: compiler.Node) => node is compiler.ExternalModuleReference = TypeGuards.is(SyntaxKind.ExternalModuleReference);
/**
* Gets if the node is a FalseKeyword.
*/
static readonly isFalseKeyword: (node: compiler.Node) => node is compiler.BooleanLiteral = TypeGuards.is(SyntaxKind.FalseKeyword);
/**
* Gets if the node is a ForInStatement.
*/
static readonly isForInStatement: (node: compiler.Node) => node is compiler.ForInStatement = TypeGuards.is(SyntaxKind.ForInStatement);
/**
* Gets if the node is a ForOfStatement.
*/
static readonly isForOfStatement: (node: compiler.Node) => node is compiler.ForOfStatement = TypeGuards.is(SyntaxKind.ForOfStatement);
/**
* Gets if the node is a ForStatement.
*/
static readonly isForStatement: (node: compiler.Node) => node is compiler.ForStatement = TypeGuards.is(SyntaxKind.ForStatement);
/**
* Gets if the node is a FunctionDeclaration.
*/
static readonly isFunctionDeclaration: (node: compiler.Node) => node is compiler.FunctionDeclaration = TypeGuards.is(SyntaxKind.FunctionDeclaration);
/**
* Gets if the node is a FunctionExpression.
*/
static readonly isFunctionExpression: (node: compiler.Node) => node is compiler.FunctionExpression = TypeGuards.is(SyntaxKind.FunctionExpression);
/**
* Gets if the node is a FunctionLikeDeclaration.
* @param node - Node to check.
*/
static isFunctionLikeDeclaration<T extends compiler.Node>(node: T): node is compiler.FunctionLikeDeclaration & compiler.FunctionLikeDeclarationExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.Constructor:
case SyntaxKind.GetAccessor:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.SetAccessor:
case SyntaxKind.ArrowFunction:
case SyntaxKind.FunctionDeclaration:
return true;
default:
return false;
}
}
/**
* Gets if the node is a FunctionTypeNode.
* @param node - Node to check.
*/
static isFunctionTypeNode(node: compiler.Node): node is compiler.FunctionTypeNode {
return node.getKind() === SyntaxKind.FunctionType;
}
/**
* Gets if the node is a GeneratorableNode.
* @param node - Node to check.
*/
static isGeneratorableNode<T extends compiler.Node>(node: T): node is compiler.GeneratorableNode & compiler.GeneratorableNodeExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.MethodDeclaration:
case SyntaxKind.YieldExpression:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
return true;
default:
return false;
}
}
/**
* Gets if the node is a GetAccessorDeclaration.
* @param node - Node to check.
*/
static isGetAccessorDeclaration(node: compiler.Node): node is compiler.GetAccessorDeclaration {
return node.getKind() === SyntaxKind.GetAccessor;
}
/**
* Gets if the node is a HeritageClause.
*/
static readonly isHeritageClause: (node: compiler.Node) => node is compiler.HeritageClause = TypeGuards.is(SyntaxKind.HeritageClause);
/**
* Gets if the node is a HeritageClauseableNode.
* @param node - Node to check.
*/
static isHeritageClauseableNode<T extends compiler.Node>(node: T): node is compiler.HeritageClauseableNode & compiler.HeritageClauseableNodeExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression:
case SyntaxKind.InterfaceDeclaration:
return true;
default:
return false;
}
}
/**
* Gets if the node is a Identifier.
*/
static readonly isIdentifier: (node: compiler.Node) => node is compiler.Identifier = TypeGuards.is(SyntaxKind.Identifier);
/**
* Gets if the node is a IfStatement.
*/
static readonly isIfStatement: (node: compiler.Node) => node is compiler.IfStatement = TypeGuards.is(SyntaxKind.IfStatement);
/**
* Gets if the node is a ImplementsClauseableNode.
* @param node - Node to check.
*/
static isImplementsClauseableNode<T extends compiler.Node>(node: T): node is compiler.ImplementsClauseableNode & compiler.ImplementsClauseableNodeExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression:
return true;
default:
return false;
}
}
/**
* Gets if the node is a ImportClause.
*/
static readonly isImportClause: (node: compiler.Node) => node is compiler.ImportClause = TypeGuards.is(SyntaxKind.ImportClause);
/**
* Gets if the node is a ImportDeclaration.
*/
static readonly isImportDeclaration: (node: compiler.Node) => node is compiler.ImportDeclaration = TypeGuards.is(SyntaxKind.ImportDeclaration);
/**
* Gets if the node is a ImportEqualsDeclaration.
*/
static readonly isImportEqualsDeclaration: (node: compiler.Node) => node is compiler.ImportEqualsDeclaration = TypeGuards.is(SyntaxKind.ImportEqualsDeclaration);
/**
* Gets if the node is a ImportExpression.
* @param node - Node to check.
*/
static isImportExpression(node: compiler.Node): node is compiler.ImportExpression {
return node.getKind() === SyntaxKind.ImportKeyword;
}
/**
* Gets if the node is a ImportSpecifier.
*/
static readonly isImportSpecifier: (node: compiler.Node) => node is compiler.ImportSpecifier = TypeGuards.is(SyntaxKind.ImportSpecifier);
/**
* Gets if the node is a ImportTypeNode.
* @param node - Node to check.
*/
static isImportTypeNode(node: compiler.Node): node is compiler.ImportTypeNode {
return node.getKind() === SyntaxKind.ImportType;
}
/**
* Gets if the node is a IndexSignatureDeclaration.
* @param node - Node to check.
*/
static isIndexSignatureDeclaration(node: compiler.Node): node is compiler.IndexSignatureDeclaration {
return node.getKind() === SyntaxKind.IndexSignature;
}
/**
* Gets if the node is a IndexedAccessTypeNode.
* @param node - Node to check.
*/
static isIndexedAccessTypeNode(node: compiler.Node): node is compiler.IndexedAccessTypeNode {
return node.getKind() === SyntaxKind.IndexedAccessType;
}
/**
* Gets if the node is a InferKeyword.
*/
static readonly isInferKeyword: (node: compiler.Node) => node is compiler.Node = TypeGuards.is(SyntaxKind.InferKeyword);
/**
* Gets if the node is a InferTypeNode.
* @param node - Node to check.
*/
static isInferTypeNode(node: compiler.Node): node is compiler.InferTypeNode {
return node.getKind() === SyntaxKind.InferType;
}
/**
* Gets if the node is a InitializerExpressionGetableNode.
* @param node - Node to check.
*/
static isInitializerExpressionGetableNode<T extends compiler.Node>(node: T): node is compiler.InitializerExpressionGetableNode & compiler.InitializerExpressionGetableNodeExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.BindingElement:
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.EnumMember:
case SyntaxKind.Parameter:
case SyntaxKind.PropertySignature:
case SyntaxKind.VariableDeclaration:
case SyntaxKind.PropertyAssignment:
case SyntaxKind.ShorthandPropertyAssignment:
return true;
default:
return false;
}
}
/**
* Gets if the node is a InitializerExpressionableNode.
* @param node - Node to check.
*/
static isInitializerExpressionableNode<T extends compiler.Node>(node: T): node is compiler.InitializerExpressionableNode & compiler.InitializerExpressionableNodeExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.BindingElement:
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.EnumMember:
case SyntaxKind.Parameter:
case SyntaxKind.PropertySignature:
case SyntaxKind.VariableDeclaration:
return true;
default:
return false;
}
}
/**
* Gets if the node is a InterfaceDeclaration.
*/
static readonly isInterfaceDeclaration: (node: compiler.Node) => node is compiler.InterfaceDeclaration = TypeGuards.is(SyntaxKind.InterfaceDeclaration);
/**
* Gets if the node is a IntersectionTypeNode.
* @param node - Node to check.
*/
static isIntersectionTypeNode(node: compiler.Node): node is compiler.IntersectionTypeNode {
return node.getKind() === SyntaxKind.IntersectionType;
}
/**
* Gets if the node is a IterationStatement.
* @param node - Node to check.
*/
static isIterationStatement(node: compiler.Node): node is compiler.IterationStatement {
switch (node.getKind()) {
case SyntaxKind.DoStatement:
case SyntaxKind.ForInStatement:
case SyntaxKind.ForOfStatement:
case SyntaxKind.ForStatement:
case SyntaxKind.WhileStatement:
return true;
default:
return false;
}
}
/**
* Gets if the node is a JSDoc.
* @param node - Node to check.
*/
static isJSDoc(node: compiler.Node): node is compiler.JSDoc {
return node.getKind() === SyntaxKind.JSDocComment;
}
/**
* Gets if the node is a JSDocAugmentsTag.
*/
static readonly isJSDocAugmentsTag: (node: compiler.Node) => node is compiler.JSDocAugmentsTag = TypeGuards.is(SyntaxKind.JSDocAugmentsTag);
/**
* Gets if the node is a JSDocClassTag.
*/
static readonly isJSDocClassTag: (node: compiler.Node) => node is compiler.JSDocClassTag = TypeGuards.is(SyntaxKind.JSDocClassTag);
/**
* Gets if the node is a JSDocFunctionType.
*/
static readonly isJSDocFunctionType: (node: compiler.Node) => node is compiler.JSDocFunctionType = TypeGuards.is(SyntaxKind.JSDocFunctionType);
/**
* Gets if the node is a JSDocParameterTag.
*/
static readonly isJSDocParameterTag: (node: compiler.Node) => node is compiler.JSDocParameterTag = TypeGuards.is(SyntaxKind.JSDocParameterTag);
/**
* Gets if the node is a JSDocPropertyLikeTag.
* @param node - Node to check.
*/
static isJSDocPropertyLikeTag<T extends compiler.Node>(node: T): node is compiler.JSDocPropertyLikeTag & compiler.JSDocPropertyLikeTagExtensionType & T {
switch (node.getKind()) {
case SyntaxKind.JSDocParameterTag:
case SyntaxKind.JSDocPropertyTag:
return true;
default:
return false;
}
}
/**
* Gets if the node is a JSDocPropertyTag.
*/
static readonly isJSDocPropertyTag: (node: compiler.Node) => node is compiler.JSDocPropertyTag = TypeGuards.is(SyntaxKind.JSDocPropertyTag);
/**
* Gets if the node is a JSDocReturnTag.
*/
static readonly isJSDocReturnTag: (node: compiler.Node) => node is compiler.JSDocReturnTag = TypeGuards.is(SyntaxKind.JSDocReturnTag);
/**
* Gets if the node is a JSDocSignature.
*/
static readonly isJSDocSignature: (node: compiler.Node) => node is compiler.JSDocSignature = TypeGuards.is(SyntaxKind.JSDocSignature);
/**
* Gets if the node is a JSDocTag.
* @param node - Node to check.
*/
static isJSDocTag(node: compiler.Node): node is compiler.JSDocTag {
switch (node.getKind()) {
case SyntaxKind.JSDocAugmentsTag:
case SyntaxKind.JSDocClassTag:
case SyntaxKind.JSDocParameterTag:
case SyntaxKind.JSDocPropertyTag:
case SyntaxKind.JSDocReturnTag:
case SyntaxKind.JSDocTypedefTag:
case SyntaxKind.JSDocTypeTag:
case SyntaxKind.JSDocTag:
return true;
default:
return false;