-
-
Notifications
You must be signed in to change notification settings - Fork 466
/
ts.rs
1358 lines (1258 loc) · 37.9 KB
/
ts.rs
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
//! TypeScript Definitions
//!
//! [AST Spec](https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/ast-spec)
//! [Archived TypeScript spec](https://github.com/microsoft/TypeScript/blob/3c99d50da5a579d9fa92d02664b1b66d4ff55944/doc/spec-ARCHIVED.md)
// NB: `#[span]`, `#[scope(...)]` and `#[visit(...)]` do NOT do anything to the code.
// They are purely markers for codegen used in
// `tasks/ast_codegen` and `crates/oxc_traverse/scripts`. See docs in those crates.
// Silence erroneous warnings from Rust Analyser for `#[derive(Tsify)]`
#![allow(non_snake_case)]
use std::{cell::Cell, hash::Hash};
use oxc_allocator::{Box, Vec};
use oxc_ast_macros::ast;
use oxc_span::{Atom, Span};
use oxc_syntax::scope::ScopeId;
#[cfg(feature = "serialize")]
use serde::Serialize;
#[cfg(feature = "serialize")]
use tsify::Tsify;
use super::{inherit_variants, js::*, jsx::*, literal::*};
#[cfg(feature = "serialize")]
#[wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)]
const TS_APPEND_CONTENT: &'static str = r#"
export interface TSIndexSignatureName extends Span {
type: "Identifier",
name: Atom,
typeAnnotation: TSTypeAnnotation,
}
"#;
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSThisParameter<'a> {
#[serde(flatten)]
pub span: Span,
pub this: IdentifierName<'a>,
pub type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
}
/// Enum Declaration
///
/// `const_opt` enum `BindingIdentifier` { `EnumBody_opt` }
///
/// ## Examples
///
/// ```ts
/// enum Foo {
/// A,
/// B
/// }
/// // `Bar` has `r#const` set to `true`
/// const enum Bar {
/// A,
/// B
/// }
/// ```
#[ast(visit)]
#[scope]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
pub struct TSEnumDeclaration<'a> {
#[serde(flatten)]
pub span: Span,
pub id: BindingIdentifier<'a>,
#[scope(enter_before)]
pub members: Vec<'a, TSEnumMember<'a>>,
pub r#const: bool,
pub declare: bool,
pub scope_id: Cell<Option<ScopeId>>,
}
/// Enum Member
///
/// ## Example
///
/// ```ts
/// enum Foo {
/// // _ id
/// A = 1,
/// // ^ initializer
/// B // initializer will be `None`
///
/// }
/// ```
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
pub struct TSEnumMember<'a> {
#[serde(flatten)]
pub span: Span,
pub id: TSEnumMemberName<'a>,
pub initializer: Option<Expression<'a>>,
}
inherit_variants! {
/// TS Enum Member Name
///
/// Inherits variants from [`Expression`]. See [`ast` module docs] for explanation of inheritance.
///
/// [`ast` module docs]: `super`
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
pub enum TSEnumMemberName<'a> {
StaticIdentifier(Box<'a, IdentifierName<'a>>) = 64,
StaticStringLiteral(Box<'a, StringLiteral<'a>>) = 65,
// Invalid Grammar `enum E { 1 }`
StaticNumericLiteral(Box<'a, NumericLiteral<'a>>) = 66,
// Invalid Grammar `enum E { [computed] }`
// `Expression` variants added here by `inherit_variants!` macro
@inherit Expression
}
}
/// TypeScript Type Annotation
///
/// An annotation on a variable declaration, parameter, etc.
///
/// ## Example
/// ```ts
/// const x: number = 1;
/// // ^^^^^^^^
///
/// function foo(x: number): number { return x; }
/// // ^^^^^^^^ ^^^^^^^^
/// ```
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSTypeAnnotation<'a> {
#[serde(flatten)]
/// starts at the `:` token and ends at the end of the type annotation
pub span: Span,
pub type_annotation: TSType<'a>,
}
/// TypeScript Literal Type
///
/// A type that is a literal value. Wraps a [`TSLiteral`].
///
/// ## Example
/// ```ts
/// const x: 'foo' = 'foo';
/// // ^^^^^
///
/// type NonZero<N> = N extends 0 ? never : N;
/// // ^
/// type Three = NonZero<3>;
/// // ^
/// ```
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSLiteralType<'a> {
#[serde(flatten)]
pub span: Span,
pub literal: TSLiteral<'a>,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged, rename_all = "camelCase")]
pub enum TSLiteral<'a> {
BooleanLiteral(Box<'a, BooleanLiteral>),
NullLiteral(Box<'a, NullLiteral>),
NumericLiteral(Box<'a, NumericLiteral<'a>>),
BigIntLiteral(Box<'a, BigIntLiteral<'a>>),
RegExpLiteral(Box<'a, RegExpLiteral<'a>>),
StringLiteral(Box<'a, StringLiteral<'a>>),
TemplateLiteral(Box<'a, TemplateLiteral<'a>>),
UnaryExpression(Box<'a, UnaryExpression<'a>>),
}
/// TypeScript Type
///
/// This is the root-level type for TypeScript types, kind of like [`Expression`] is for
/// expressions.
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged, rename_all = "camelCase")]
pub enum TSType<'a> {
// Keyword
TSAnyKeyword(Box<'a, TSAnyKeyword>) = 0,
TSBigIntKeyword(Box<'a, TSBigIntKeyword>) = 1,
TSBooleanKeyword(Box<'a, TSBooleanKeyword>) = 2,
TSIntrinsicKeyword(Box<'a, TSIntrinsicKeyword>) = 3,
TSNeverKeyword(Box<'a, TSNeverKeyword>) = 4,
TSNullKeyword(Box<'a, TSNullKeyword>) = 5,
TSNumberKeyword(Box<'a, TSNumberKeyword>) = 6,
TSObjectKeyword(Box<'a, TSObjectKeyword>) = 7,
TSStringKeyword(Box<'a, TSStringKeyword>) = 8,
TSSymbolKeyword(Box<'a, TSSymbolKeyword>) = 9,
TSUndefinedKeyword(Box<'a, TSUndefinedKeyword>) = 11,
TSUnknownKeyword(Box<'a, TSUnknownKeyword>) = 12,
TSVoidKeyword(Box<'a, TSVoidKeyword>) = 13,
// Compound
TSArrayType(Box<'a, TSArrayType<'a>>) = 14,
TSConditionalType(Box<'a, TSConditionalType<'a>>) = 15,
TSConstructorType(Box<'a, TSConstructorType<'a>>) = 16,
TSFunctionType(Box<'a, TSFunctionType<'a>>) = 17,
TSImportType(Box<'a, TSImportType<'a>>) = 18,
TSIndexedAccessType(Box<'a, TSIndexedAccessType<'a>>) = 19,
TSInferType(Box<'a, TSInferType<'a>>) = 20,
TSIntersectionType(Box<'a, TSIntersectionType<'a>>) = 21,
TSLiteralType(Box<'a, TSLiteralType<'a>>) = 22,
TSMappedType(Box<'a, TSMappedType<'a>>) = 23,
TSNamedTupleMember(Box<'a, TSNamedTupleMember<'a>>) = 24,
TSQualifiedName(Box<'a, TSQualifiedName<'a>>) = 25,
TSTemplateLiteralType(Box<'a, TSTemplateLiteralType<'a>>) = 26,
TSThisType(Box<'a, TSThisType>) = 10,
TSTupleType(Box<'a, TSTupleType<'a>>) = 27,
TSTypeLiteral(Box<'a, TSTypeLiteral<'a>>) = 28,
TSTypeOperatorType(Box<'a, TSTypeOperator<'a>>) = 29,
TSTypePredicate(Box<'a, TSTypePredicate<'a>>) = 30,
TSTypeQuery(Box<'a, TSTypeQuery<'a>>) = 31,
TSTypeReference(Box<'a, TSTypeReference<'a>>) = 32,
TSUnionType(Box<'a, TSUnionType<'a>>) = 33,
TSParenthesizedType(Box<'a, TSParenthesizedType<'a>>) = 34,
// JSDoc
JSDocNullableType(Box<'a, JSDocNullableType<'a>>) = 35,
JSDocNonNullableType(Box<'a, JSDocNonNullableType<'a>>) = 36,
JSDocUnknownType(Box<'a, JSDocUnknownType>) = 37,
}
/// Macro for matching `TSType`'s variants.
#[macro_export]
macro_rules! match_ts_type {
($ty:ident) => {
$ty::TSAnyKeyword(_)
| $ty::TSBigIntKeyword(_)
| $ty::TSBooleanKeyword(_)
| $ty::TSIntrinsicKeyword(_)
| $ty::TSNeverKeyword(_)
| $ty::TSNullKeyword(_)
| $ty::TSNumberKeyword(_)
| $ty::TSObjectKeyword(_)
| $ty::TSStringKeyword(_)
| $ty::TSSymbolKeyword(_)
| $ty::TSThisType(_)
| $ty::TSUndefinedKeyword(_)
| $ty::TSUnknownKeyword(_)
| $ty::TSVoidKeyword(_)
| $ty::TSArrayType(_)
| $ty::TSConditionalType(_)
| $ty::TSConstructorType(_)
| $ty::TSFunctionType(_)
| $ty::TSImportType(_)
| $ty::TSIndexedAccessType(_)
| $ty::TSInferType(_)
| $ty::TSIntersectionType(_)
| $ty::TSLiteralType(_)
| $ty::TSMappedType(_)
| $ty::TSNamedTupleMember(_)
| $ty::TSQualifiedName(_)
| $ty::TSTemplateLiteralType(_)
| $ty::TSTupleType(_)
| $ty::TSTypeLiteral(_)
| $ty::TSTypeOperatorType(_)
| $ty::TSTypePredicate(_)
| $ty::TSTypeQuery(_)
| $ty::TSTypeReference(_)
| $ty::TSUnionType(_)
| $ty::TSParenthesizedType(_)
| $ty::JSDocNullableType(_)
| $ty::JSDocNonNullableType(_)
| $ty::JSDocUnknownType(_)
};
}
pub use match_ts_type;
/// TypeScript Conditional Type
///
/// ## Example
///
/// ```ts
/// SomeType extends OtherType ? TrueType : FalseType;
/// ```
///
/// <https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#handbook-content>
#[ast(visit)]
#[scope]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSConditionalType<'a> {
#[serde(flatten)]
pub span: Span,
pub check_type: TSType<'a>,
pub extends_type: TSType<'a>,
pub true_type: TSType<'a>,
pub false_type: TSType<'a>,
pub scope_id: Cell<Option<ScopeId>>,
}
/// TypeScript Union Type
///
/// ## Example
///
/// ```ts
/// string | string[] | (() => string) | { s: string }
/// ```
///
/// <https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes-func.html#unions>
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
pub struct TSUnionType<'a> {
#[serde(flatten)]
pub span: Span,
pub types: Vec<'a, TSType<'a>>,
}
/// type `ColorfulCircle` = Colorful & Circle;
///
/// <https://www.typescriptlang.org/docs/handbook/2/objects.html#intersection-types>
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
pub struct TSIntersectionType<'a> {
#[serde(flatten)]
pub span: Span,
pub types: Vec<'a, TSType<'a>>,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
pub struct TSParenthesizedType<'a> {
#[serde(flatten)]
pub span: Span,
pub type_annotation: TSType<'a>,
}
/// TypeScript Type Operators
///
/// Includes
/// - `keyof`
/// - `unique`
/// - `readonly`
///
/// <https://www.typescriptlang.org/docs/handbook/2/keyof-types.html>
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSTypeOperator<'a> {
#[serde(flatten)]
pub span: Span,
pub operator: TSTypeOperatorOperator,
pub type_annotation: TSType<'a>,
}
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(rename_all = "camelCase")]
pub enum TSTypeOperatorOperator {
Keyof,
Unique,
Readonly,
}
/// TypeScript Array Type
///
/// Does not include tuple types, which are stored as [`TSTupleType`].
///
/// ## Example
///
/// ```ts
/// let myArray: string[] = ["hello", "world"];
/// ```
///
/// <https://www.typescriptlang.org/docs/handbook/2/objects.html#the-array-type>
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSArrayType<'a> {
#[serde(flatten)]
pub span: Span,
pub element_type: TSType<'a>,
}
/// TypeScript Index Access Type
///
/// This is the type equivalent to expression member access.
///
/// ## Example
///
/// ```ts
/// type I1 = Person["age" | "name"];
/// ```
///
/// <https://www.typescriptlang.org/docs/handbook/2/indexed-access-types.html#handbook-content>
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSIndexedAccessType<'a> {
#[serde(flatten)]
pub span: Span,
pub object_type: TSType<'a>,
pub index_type: TSType<'a>,
}
/// TypeScript Tuple Type
///
/// ## Example
///
/// ```ts
/// type `StringNumberPair` = [string, number];
/// ```
///
/// <https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types>
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSTupleType<'a> {
#[serde(flatten)]
pub span: Span,
pub element_types: Vec<'a, TSTupleElement<'a>>,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSNamedTupleMember<'a> {
#[serde(flatten)]
pub span: Span,
pub element_type: TSTupleElement<'a>,
pub label: IdentifierName<'a>,
pub optional: bool,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSOptionalType<'a> {
#[serde(flatten)]
pub span: Span,
pub type_annotation: TSType<'a>,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSRestType<'a> {
#[serde(flatten)]
pub span: Span,
pub type_annotation: TSType<'a>,
}
inherit_variants! {
/// TS Tuple Element
///
/// Inherits variants from [`TSType`]. See [`ast` module docs] for explanation of inheritance.
///
/// See [`TSNamedTupleMember`] for named tuple elements.
///
/// [`ast` module docs]: `super`
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged, rename_all = "camelCase")]
pub enum TSTupleElement<'a> {
// Discriminants start at 64, so that `TSTupleElement::is_ts_type` is a single
// bitwise AND operation on the discriminant (`discriminant & 63 != 0`).
TSOptionalType(Box<'a, TSOptionalType<'a>>) = 64,
TSRestType(Box<'a, TSRestType<'a>>) = 65,
// `TSType` variants added here by `inherit_variants!` macro
@inherit TSType
}
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
pub struct TSAnyKeyword {
#[serde(flatten)]
pub span: Span,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
pub struct TSStringKeyword {
#[serde(flatten)]
pub span: Span,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
pub struct TSBooleanKeyword {
#[serde(flatten)]
pub span: Span,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
pub struct TSNumberKeyword {
#[serde(flatten)]
pub span: Span,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
pub struct TSNeverKeyword {
#[serde(flatten)]
pub span: Span,
}
/// `type Uppercase<T extends character> = intrinsic;`
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
pub struct TSIntrinsicKeyword {
#[serde(flatten)]
pub span: Span,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
pub struct TSUnknownKeyword {
#[serde(flatten)]
pub span: Span,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
pub struct TSNullKeyword {
#[serde(flatten)]
pub span: Span,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
pub struct TSUndefinedKeyword {
#[serde(flatten)]
pub span: Span,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
pub struct TSVoidKeyword {
#[serde(flatten)]
pub span: Span,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
pub struct TSSymbolKeyword {
#[serde(flatten)]
pub span: Span,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
pub struct TSThisType {
#[serde(flatten)]
pub span: Span,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
pub struct TSObjectKeyword {
#[serde(flatten)]
pub span: Span,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type")]
pub struct TSBigIntKeyword {
#[serde(flatten)]
pub span: Span,
}
/// type C = A;
/// type D = B.a;
/// type E = D.c.b.a;
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSTypeReference<'a> {
#[serde(flatten)]
pub span: Span,
pub type_name: TSTypeName<'a>,
pub type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>,
}
/// TypeName:
/// IdentifierReference
/// NamespaceName . IdentifierReference
#[ast(visit)]
#[repr(C, u8)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
pub enum TSTypeName<'a> {
IdentifierReference(Box<'a, IdentifierReference<'a>>) = 0,
QualifiedName(Box<'a, TSQualifiedName<'a>>) = 1,
}
/// Macro for matching `TSTypeName`'s variants.
#[macro_export]
macro_rules! match_ts_type_name {
($ty:ident) => {
$ty::IdentifierReference(_) | $ty::QualifiedName(_)
};
}
pub use match_ts_type_name;
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSQualifiedName<'a> {
#[serde(flatten)]
pub span: Span,
pub left: TSTypeName<'a>,
pub right: IdentifierName<'a>,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSTypeParameterInstantiation<'a> {
#[serde(flatten)]
pub span: Span,
pub params: Vec<'a, TSType<'a>>,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSTypeParameter<'a> {
#[serde(flatten)]
pub span: Span,
pub name: BindingIdentifier<'a>,
pub constraint: Option<TSType<'a>>,
pub default: Option<TSType<'a>>,
pub r#in: bool,
pub out: bool,
pub r#const: bool,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSTypeParameterDeclaration<'a> {
#[serde(flatten)]
pub span: Span,
pub params: Vec<'a, TSTypeParameter<'a>>,
}
#[ast(visit)]
#[scope]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSTypeAliasDeclaration<'a> {
#[serde(flatten)]
pub span: Span,
pub id: BindingIdentifier<'a>,
#[scope(enter_before)]
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub type_annotation: TSType<'a>,
pub declare: bool,
pub scope_id: Cell<Option<ScopeId>>,
}
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(rename_all = "camelCase")]
pub enum TSAccessibility {
Private,
Protected,
Public,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSClassImplements<'a> {
#[serde(flatten)]
pub span: Span,
pub expression: TSTypeName<'a>,
pub type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>,
}
/// Interface Declaration
///
/// interface `BindingIdentifier` `TypeParameters_opt` `InterfaceExtendsClause_opt` `ObjectType`
#[ast(visit)]
#[scope]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSInterfaceDeclaration<'a> {
#[serde(flatten)]
pub span: Span,
/// The identifier (name) of the interface.
pub id: BindingIdentifier<'a>,
#[scope(enter_before)]
pub extends: Option<Vec<'a, TSInterfaceHeritage<'a>>>,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub body: Box<'a, TSInterfaceBody<'a>>,
pub declare: bool,
pub scope_id: Cell<Option<ScopeId>>,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSInterfaceBody<'a> {
#[serde(flatten)]
pub span: Span,
pub body: Vec<'a, TSSignature<'a>>,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSPropertySignature<'a> {
#[serde(flatten)]
pub span: Span,
pub computed: bool,
pub optional: bool,
pub readonly: bool,
pub key: PropertyKey<'a>,
pub type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged, rename_all = "camelCase")]
pub enum TSSignature<'a> {
TSIndexSignature(Box<'a, TSIndexSignature<'a>>),
TSPropertySignature(Box<'a, TSPropertySignature<'a>>),
TSCallSignatureDeclaration(Box<'a, TSCallSignatureDeclaration<'a>>),
TSConstructSignatureDeclaration(Box<'a, TSConstructSignatureDeclaration<'a>>),
TSMethodSignature(Box<'a, TSMethodSignature<'a>>),
}
/// An index signature within a class, type alias, etc.
///
/// ## Example
/// [playground link](https://oxc-project.github.io/oxc/playground/?code=3YCAAIC9gICAgICAgIC6nsrEgtem3AB/pQsrWlLnujiFhkHVtfeFMq5RMD7X5AzJnZ5R/ecQ5KG1FUFjzXvrxFXH0m6HpS+Ob3TC8gQXeRQygA%3D%3D)
/// ```ts
/// type MapOf<T> = {
/// // _________ parameters (vec with 1 element)
/// [K: string]: T
/// // - type_annotation
/// }
/// ```
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSIndexSignature<'a> {
#[serde(flatten)]
pub span: Span,
pub parameters: Vec<'a, TSIndexSignatureName<'a>>,
pub type_annotation: Box<'a, TSTypeAnnotation<'a>>,
pub readonly: bool,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSCallSignatureDeclaration<'a> {
#[serde(flatten)]
pub span: Span,
pub this_param: Option<TSThisParameter<'a>>,
pub params: Box<'a, FormalParameters<'a>>,
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
}
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(rename_all = "camelCase")]
pub enum TSMethodSignatureKind {
Method,
Get,
Set,
}
#[ast(visit)]
#[scope]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSMethodSignature<'a> {
#[serde(flatten)]
pub span: Span,
pub key: PropertyKey<'a>,
pub computed: bool,
pub optional: bool,
pub kind: TSMethodSignatureKind,
pub this_param: Option<TSThisParameter<'a>>,
pub params: Box<'a, FormalParameters<'a>>,
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub scope_id: Cell<Option<ScopeId>>,
}
#[ast(visit)]
#[scope]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSConstructSignatureDeclaration<'a> {
#[serde(flatten)]
pub span: Span,
pub params: Box<'a, FormalParameters<'a>>,
pub return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub scope_id: Cell<Option<ScopeId>>,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[serde(tag = "type", rename = "Identifier", rename_all = "camelCase")]
pub struct TSIndexSignatureName<'a> {
#[serde(flatten)]
pub span: Span,
pub name: Atom<'a>,
pub type_annotation: Box<'a, TSTypeAnnotation<'a>>,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSInterfaceHeritage<'a> {
#[serde(flatten)]
pub span: Span,
pub expression: Expression<'a>,
pub type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSTypePredicate<'a> {
#[serde(flatten)]
pub span: Span,
pub parameter_name: TSTypePredicateName<'a>,
pub asserts: bool,
pub type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged, rename_all = "camelCase")]
pub enum TSTypePredicateName<'a> {
Identifier(Box<'a, IdentifierName<'a>>),
This(TSThisType),
}
#[ast(visit)]
#[scope(
flags(ScopeFlags::TsModuleBlock),
strict_if(self.body.as_ref().is_some_and(TSModuleDeclarationBody::is_strict)),
)]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSModuleDeclaration<'a> {
#[serde(flatten)]
pub span: Span,
pub id: TSModuleDeclarationName<'a>,
#[scope(enter_before)]
pub body: Option<TSModuleDeclarationBody<'a>>,
/// The keyword used to define this module declaration
/// ```text
/// namespace Foo {}
/// ^^^^^^^^^
/// module 'foo' {}
/// ^^^^^^
/// declare global {}
/// ^^^^^^
/// ```
pub kind: TSModuleDeclarationKind,
pub declare: bool,
pub scope_id: Cell<Option<ScopeId>>,
}
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(rename_all = "camelCase")]
pub enum TSModuleDeclarationKind {
Global,
Module,
Namespace,
}
impl TSModuleDeclarationKind {
pub fn is_global(self) -> bool {
matches!(self, TSModuleDeclarationKind::Global)
}
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
pub enum TSModuleDeclarationName<'a> {
Identifier(IdentifierName<'a>),
StringLiteral(StringLiteral<'a>),
}
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(untagged)]
pub enum TSModuleDeclarationBody<'a> {
TSModuleDeclaration(Box<'a, TSModuleDeclaration<'a>>),
TSModuleBlock(Box<'a, TSModuleBlock<'a>>),
}
// See serializer in serialize.rs
#[ast(visit)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct TSModuleBlock<'a> {
#[serde(flatten)]
pub span: Span,
#[serde(skip)]
pub directives: Vec<'a, Directive<'a>>,
pub body: Vec<'a, Statement<'a>>,
}
#[ast(visit)]
#[derive(Debug, Hash)]