tsrun 0.1.23

A TypeScript interpreter designed for embedding in applications
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
//! Abstract Syntax Tree types for TypeScript

use crate::parser::Span;
use crate::prelude::*;
use crate::value::JsString;

/// A complete program (script or module)
#[derive(Debug, Clone)]
pub struct Program {
    pub body: Rc<[Statement]>,
    pub source_type: SourceType,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SourceType {
    Script,
    Module,
}

// ============ STATEMENTS ============

#[derive(Debug, Clone)]
pub enum Statement {
    // Declarations
    VariableDeclaration(VariableDeclaration),
    FunctionDeclaration(Box<FunctionDeclaration>),
    ClassDeclaration(Box<ClassDeclaration>),

    // TypeScript (no-op at runtime)
    TypeAlias(Box<TypeAliasDeclaration>),
    InterfaceDeclaration(Box<InterfaceDeclaration>),
    EnumDeclaration(Box<EnumDeclaration>),
    NamespaceDeclaration(Box<NamespaceDeclaration>),

    // Control Flow
    Block(BlockStatement),
    If(IfStatement),
    Switch(SwitchStatement),
    For(Box<ForStatement>),
    ForIn(Box<ForInStatement>),
    ForOf(Box<ForOfStatement>),
    While(WhileStatement),
    DoWhile(DoWhileStatement),
    Try(Box<TryStatement>),

    // Jump
    Return(ReturnStatement),
    Break(BreakStatement),
    Continue(ContinueStatement),
    Throw(ThrowStatement),

    // Module
    Import(Box<ImportDeclaration>),
    Export(Box<ExportDeclaration>),

    // Other
    Expression(ExpressionStatement),
    Empty,
    Debugger,
    Labeled(LabeledStatement),
}

#[derive(Debug, Clone)]
pub struct ExpressionStatement {
    pub expression: Rc<Expression>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct BlockStatement {
    pub body: Rc<[Statement]>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct VariableDeclaration {
    pub kind: VariableKind,
    pub declarations: Rc<[VariableDeclarator]>,
    pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VariableKind {
    Let,
    Const,
    Var,
}

#[derive(Debug, Clone)]
pub struct VariableDeclarator {
    pub id: Pattern,
    pub type_annotation: Option<Box<TypeAnnotation>>,
    pub init: Option<Rc<Expression>>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct FunctionDeclaration {
    pub id: Option<Identifier>,
    pub params: Rc<[FunctionParam]>,
    pub return_type: Option<Box<TypeAnnotation>>,
    pub type_parameters: Option<TypeParameters>,
    pub body: Rc<BlockStatement>,
    pub generator: bool,
    pub async_: bool,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct FunctionParam {
    pub pattern: Pattern,
    pub type_annotation: Option<Box<TypeAnnotation>>,
    pub optional: bool,
    pub decorators: Vec<Decorator>,
    /// TypeScript parameter property: `constructor(public x: number)`
    pub accessibility: Option<Accessibility>,
    /// TypeScript readonly parameter property: `constructor(readonly x: number)`
    pub readonly: bool,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct ClassDeclaration {
    pub id: Option<Identifier>,
    pub type_parameters: Option<TypeParameters>,
    pub super_class: Option<Rc<Expression>>,
    pub implements: Vec<TypeReference>,
    pub body: ClassBody,
    pub decorators: Vec<Decorator>,
    pub abstract_: bool,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct ClassBody {
    pub members: Vec<ClassMember>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum ClassMember {
    Method(Box<ClassMethod>),
    Property(Box<ClassProperty>),
    Constructor(Box<ClassConstructor>),
    StaticBlock(BlockStatement),
}

#[derive(Debug, Clone)]
pub struct ClassMethod {
    pub key: ObjectPropertyKey,
    pub value: FunctionExpression,
    pub kind: MethodKind,
    pub computed: bool,
    pub static_: bool,
    pub accessibility: Option<Accessibility>,
    pub decorators: Vec<Decorator>,
    pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MethodKind {
    Method,
    Get,
    Set,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Accessibility {
    Public,
    Private,
    Protected,
}

#[derive(Debug, Clone)]
pub struct ClassProperty {
    pub key: ObjectPropertyKey,
    pub value: Option<Box<Expression>>,
    pub type_annotation: Option<Box<TypeAnnotation>>,
    pub computed: bool,
    pub static_: bool,
    pub readonly: bool,
    pub optional: bool,
    pub accessor: bool,
    pub accessibility: Option<Accessibility>,
    pub decorators: Vec<Decorator>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct ClassConstructor {
    pub params: Vec<FunctionParam>,
    pub body: BlockStatement,
    pub accessibility: Option<Accessibility>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct Decorator {
    pub expression: Expression,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct IfStatement {
    pub test: Rc<Expression>,
    pub consequent: Rc<Statement>,
    pub alternate: Option<Rc<Statement>>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct SwitchStatement {
    pub discriminant: Rc<Expression>,
    pub cases: Rc<[SwitchCase]>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct SwitchCase {
    pub test: Option<Rc<Expression>>, // None for default
    pub consequent: Rc<[Statement]>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct ForStatement {
    pub init: Option<ForInit>,
    pub test: Option<Rc<Expression>>,
    pub update: Option<Rc<Expression>>,
    pub body: Rc<Statement>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum ForInit {
    Variable(VariableDeclaration),
    Expression(Rc<Expression>),
}

#[derive(Debug, Clone)]
pub struct ForInStatement {
    pub left: ForInOfLeft,
    pub right: Rc<Expression>,
    pub body: Rc<Statement>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct ForOfStatement {
    pub left: ForInOfLeft,
    pub right: Rc<Expression>,
    pub body: Rc<Statement>,
    pub await_: bool,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum ForInOfLeft {
    Variable(VariableDeclaration),
    Pattern(Pattern),
}

#[derive(Debug, Clone)]
pub struct WhileStatement {
    pub test: Rc<Expression>,
    pub body: Rc<Statement>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct DoWhileStatement {
    pub body: Rc<Statement>,
    pub test: Rc<Expression>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct TryStatement {
    pub block: BlockStatement,
    pub handler: Option<CatchClause>,
    pub finalizer: Option<BlockStatement>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct CatchClause {
    pub param: Option<Pattern>,
    pub body: BlockStatement,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct ReturnStatement {
    pub argument: Option<Rc<Expression>>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct BreakStatement {
    pub label: Option<Identifier>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct ContinueStatement {
    pub label: Option<Identifier>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct ThrowStatement {
    pub argument: Rc<Expression>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct LabeledStatement {
    pub label: Identifier,
    pub body: Rc<Statement>,
    pub span: Span,
}

// TypeScript declarations

#[derive(Debug, Clone)]
pub struct TypeAliasDeclaration {
    pub id: Identifier,
    pub type_parameters: Option<TypeParameters>,
    pub type_annotation: Box<TypeAnnotation>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct InterfaceDeclaration {
    pub id: Identifier,
    pub type_parameters: Option<TypeParameters>,
    pub extends: Vec<TypeReference>,
    pub body: Vec<TypeMember>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct EnumDeclaration {
    pub id: Identifier,
    pub members: Vec<EnumMember>,
    pub const_: bool,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct EnumMember {
    pub id: Identifier,
    pub initializer: Option<Expression>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct NamespaceDeclaration {
    pub id: Identifier,
    pub body: Rc<[Statement]>,
    pub span: Span,
}

// Module declarations

#[derive(Debug, Clone)]
pub struct ImportDeclaration {
    pub specifiers: Vec<ImportSpecifier>,
    pub source: StringLiteral,
    pub type_only: bool,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum ImportSpecifier {
    Named {
        local: Identifier,
        imported: Identifier,
        span: Span,
    },
    Default {
        local: Identifier,
        span: Span,
    },
    Namespace {
        local: Identifier,
        span: Span,
    },
}

#[derive(Debug, Clone)]
pub struct ExportDeclaration {
    pub declaration: Option<Box<Statement>>,
    pub specifiers: Vec<ExportSpecifier>,
    pub source: Option<StringLiteral>,
    /// For `export * as ns from "module"` - the namespace identifier
    pub namespace_export: Option<Identifier>,
    pub default: bool,
    pub type_only: bool,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct ExportSpecifier {
    pub local: Identifier,
    pub exported: Identifier,
    pub span: Span,
}

// ============ EXPRESSIONS ============

#[derive(Debug, Clone)]
pub enum Expression {
    // Literals
    Literal(Box<Literal>),
    Array(ArrayExpression),
    Object(ObjectExpression),
    Function(Box<FunctionExpression>),
    ArrowFunction(Box<ArrowFunctionExpression>),
    Class(Box<ClassExpression>),
    Template(Box<TemplateLiteral>),
    TaggedTemplate(Box<TaggedTemplateExpression>),

    // Identifiers
    Identifier(Identifier),
    This(Span),
    Super(Span),

    // Operations
    Unary(UnaryExpression),
    Binary(BinaryExpression),
    Logical(LogicalExpression),
    Conditional(ConditionalExpression),
    Assignment(Box<AssignmentExpression>),
    Update(UpdateExpression),
    Sequence(SequenceExpression),

    // Access
    Member(Box<MemberExpression>),
    OptionalChain(OptionalChainExpression),
    Call(Box<CallExpression>),
    New(Box<NewExpression>),

    // TypeScript
    TypeAssertion(TypeAssertionExpression),
    NonNull(NonNullExpression),

    // Special
    Spread(SpreadElement),
    Yield(YieldExpression),
    Await(AwaitExpression),

    // Parenthesized (for preserving source structure)
    Parenthesized(Rc<Expression>, Span),
}

impl Expression {
    pub fn span(&self) -> Span {
        match self {
            Expression::Literal(l) => l.span,
            Expression::Array(a) => a.span,
            Expression::Object(o) => o.span,
            Expression::Function(f) => f.span,
            Expression::ArrowFunction(a) => a.span,
            Expression::Class(c) => c.span,
            Expression::Template(t) => t.span,
            Expression::TaggedTemplate(t) => t.span,
            Expression::Identifier(i) => i.span,
            Expression::This(s) | Expression::Super(s) => *s,
            Expression::Unary(u) => u.span,
            Expression::Binary(b) => b.span,
            Expression::Logical(l) => l.span,
            Expression::Conditional(c) => c.span,
            Expression::Assignment(a) => a.span,
            Expression::Update(u) => u.span,
            Expression::Sequence(s) => s.span,
            Expression::Member(m) => m.span,
            Expression::OptionalChain(o) => o.span,
            Expression::Call(c) => c.span,
            Expression::New(n) => n.span,
            Expression::TypeAssertion(t) => t.span,
            Expression::NonNull(n) => n.span,
            Expression::Spread(s) => s.span,
            Expression::Yield(y) => y.span,
            Expression::Await(a) => a.span,
            Expression::Parenthesized(_, s) => *s,
        }
    }
}

#[derive(Debug, Clone)]
pub struct Literal {
    pub value: LiteralValue,
    pub span: Span,
}

#[derive(Debug, Clone, PartialEq)]
pub enum LiteralValue {
    Null,
    Undefined,
    Boolean(bool),
    Number(f64),
    String(JsString),
    BigInt(String), // Store as string to preserve arbitrary precision
    RegExp { pattern: String, flags: String },
}

#[derive(Debug, Clone)]
pub struct StringLiteral {
    pub value: JsString,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct Identifier {
    pub name: JsString,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct ArrayExpression {
    pub elements: Vec<Option<ArrayElement>>,
    pub span: Span,
}

#[derive(Debug, Clone)]
#[allow(clippy::large_enum_variant)]
pub enum ArrayElement {
    Expression(Expression),
    Spread(SpreadElement),
}

#[derive(Debug, Clone)]
pub struct ObjectExpression {
    pub properties: Vec<ObjectProperty>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum ObjectProperty {
    Property(Box<Property>),
    Spread(SpreadElement),
}

#[derive(Debug, Clone)]
pub struct Property {
    pub key: ObjectPropertyKey,
    pub value: Expression,
    pub kind: PropertyKind,
    pub computed: bool,
    pub shorthand: bool,
    pub method: bool,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum ObjectPropertyKey {
    Identifier(Identifier),
    String(StringLiteral),
    Number(Literal),
    Computed(Rc<Expression>),
    PrivateIdentifier(Identifier),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PropertyKind {
    Init,
    Get,
    Set,
}

#[derive(Debug, Clone)]
pub struct FunctionExpression {
    pub id: Option<Identifier>,
    pub params: Rc<[FunctionParam]>,
    pub return_type: Option<Box<TypeAnnotation>>,
    pub type_parameters: Option<TypeParameters>,
    pub body: Rc<BlockStatement>,
    pub generator: bool,
    pub async_: bool,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct ArrowFunctionExpression {
    pub params: Rc<[FunctionParam]>,
    pub return_type: Option<Box<TypeAnnotation>>,
    pub type_parameters: Option<TypeParameters>,
    pub body: Box<ArrowFunctionBody>,
    pub async_: bool,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum ArrowFunctionBody {
    Expression(Rc<Expression>),
    Block(Rc<BlockStatement>),
}

#[derive(Debug, Clone)]
pub struct ClassExpression {
    pub id: Option<Identifier>,
    pub type_parameters: Option<TypeParameters>,
    pub super_class: Option<Rc<Expression>>,
    pub implements: Vec<TypeReference>,
    pub body: ClassBody,
    pub decorators: Vec<Decorator>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct TemplateLiteral {
    pub quasis: Vec<TemplateElement>,
    pub expressions: Vec<Expression>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct TemplateElement {
    pub value: JsString,
    pub tail: bool,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct TaggedTemplateExpression {
    pub tag: Rc<Expression>,
    pub quasi: TemplateLiteral,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct UnaryExpression {
    pub operator: UnaryOp,
    pub argument: Rc<Expression>,
    pub prefix: bool,
    pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnaryOp {
    Minus,  // -
    Plus,   // +
    Not,    // !
    BitNot, // ~
    Typeof, // typeof
    Void,   // void
    Delete, // delete
}

#[derive(Debug, Clone)]
pub struct BinaryExpression {
    pub operator: BinaryOp,
    pub left: Rc<Expression>,
    pub right: Rc<Expression>,
    pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryOp {
    // Arithmetic
    Add, // +
    Sub, // -
    Mul, // *
    Div, // /
    Mod, // %
    Exp, // **

    // Comparison
    Eq,          // ==
    NotEq,       // !=
    StrictEq,    // ===
    StrictNotEq, // !==
    Lt,          // <
    LtEq,        // <=
    Gt,          // >
    GtEq,        // >=

    // Bitwise
    BitAnd,  // &
    BitOr,   // |
    BitXor,  // ^
    LShift,  // <<
    RShift,  // >>
    URShift, // >>>

    // Other
    In,         // in
    Instanceof, // instanceof
}

#[derive(Debug, Clone)]
pub struct LogicalExpression {
    pub operator: LogicalOp,
    pub left: Rc<Expression>,
    pub right: Rc<Expression>,
    pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogicalOp {
    And,               // &&
    Or,                // ||
    NullishCoalescing, // ??
}

#[derive(Debug, Clone)]
pub struct ConditionalExpression {
    pub test: Rc<Expression>,
    pub consequent: Rc<Expression>,
    pub alternate: Rc<Expression>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct AssignmentExpression {
    pub operator: AssignmentOp,
    pub left: AssignmentTarget,
    pub right: Rc<Expression>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum AssignmentTarget {
    Identifier(Identifier),
    Member(MemberExpression),
    Pattern(Pattern),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AssignmentOp {
    Assign,        // =
    AddAssign,     // +=
    SubAssign,     // -=
    MulAssign,     // *=
    DivAssign,     // /=
    ModAssign,     // %=
    ExpAssign,     // **=
    BitAndAssign,  // &=
    BitOrAssign,   // |=
    BitXorAssign,  // ^=
    LShiftAssign,  // <<=
    RShiftAssign,  // >>=
    URShiftAssign, // >>>=
    AndAssign,     // &&=
    OrAssign,      // ||=
    NullishAssign, // ??=
}

#[derive(Debug, Clone)]
pub struct UpdateExpression {
    pub operator: UpdateOp,
    pub argument: Rc<Expression>,
    pub prefix: bool,
    pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UpdateOp {
    Increment, // ++
    Decrement, // --
}

#[derive(Debug, Clone)]
pub struct SequenceExpression {
    pub expressions: Vec<Expression>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct MemberExpression {
    pub object: Rc<Expression>,
    pub property: MemberProperty,
    pub computed: bool,
    /// Whether this is optional chaining (obj?.prop)
    pub optional: bool,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum MemberProperty {
    Identifier(Identifier),
    Expression(Rc<Expression>),
    PrivateIdentifier(Identifier),
}

#[derive(Debug, Clone)]
pub struct OptionalChainExpression {
    pub base: Rc<Expression>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct CallExpression {
    pub callee: Rc<Expression>,
    pub arguments: Vec<Argument>,
    pub type_arguments: Option<TypeArguments>,
    pub optional: bool,
    pub span: Span,
}

#[derive(Debug, Clone)]
#[allow(clippy::large_enum_variant)]
pub enum Argument {
    Expression(Expression),
    Spread(SpreadElement),
}

#[derive(Debug, Clone)]
pub struct NewExpression {
    pub callee: Rc<Expression>,
    pub arguments: Vec<Argument>,
    pub type_arguments: Option<TypeArguments>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct SpreadElement {
    pub argument: Rc<Expression>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct YieldExpression {
    pub argument: Option<Rc<Expression>>,
    pub delegate: bool,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct AwaitExpression {
    pub argument: Rc<Expression>,
    pub span: Span,
}

// TypeScript expressions

#[derive(Debug, Clone)]
pub struct TypeAssertionExpression {
    pub expression: Rc<Expression>,
    pub type_annotation: Box<TypeAnnotation>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct NonNullExpression {
    pub expression: Rc<Expression>,
    pub span: Span,
}

// ============ PATTERNS ============

#[derive(Debug, Clone)]
pub enum Pattern {
    Identifier(Identifier),
    Object(ObjectPattern),
    Array(ArrayPattern),
    Rest(RestElement),
    Assignment(AssignmentPattern),
}

impl Pattern {
    pub fn span(&self) -> Span {
        match self {
            Pattern::Identifier(i) => i.span,
            Pattern::Object(o) => o.span,
            Pattern::Array(a) => a.span,
            Pattern::Rest(r) => r.span,
            Pattern::Assignment(a) => a.span,
        }
    }
}

#[derive(Debug, Clone)]
pub struct ObjectPattern {
    pub properties: Vec<ObjectPatternProperty>,
    pub type_annotation: Option<Box<TypeAnnotation>>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum ObjectPatternProperty {
    KeyValue {
        key: ObjectPropertyKey,
        value: Pattern,
        shorthand: bool,
        span: Span,
    },
    Rest(RestElement),
}

#[derive(Debug, Clone)]
pub struct ArrayPattern {
    pub elements: Vec<Option<Pattern>>,
    pub type_annotation: Option<Box<TypeAnnotation>>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct RestElement {
    pub argument: Box<Pattern>,
    pub type_annotation: Option<Box<TypeAnnotation>>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct AssignmentPattern {
    pub left: Box<Pattern>,
    pub right: Rc<Expression>,
    pub span: Span,
}

// ============ TYPE ANNOTATIONS ============

#[derive(Debug, Clone)]
pub enum TypeAnnotation {
    Keyword(TypeKeyword),
    Reference(TypeReference),
    Literal(TypeLiteral),
    Object(ObjectType),
    Array(ArrayType),
    Tuple(TupleType),
    Union(UnionType),
    Intersection(IntersectionType),
    Function(FunctionType),
    Conditional(ConditionalType),
    Infer(InferType),
    Mapped(MappedType),
    Indexed(IndexedAccessType),
    Typeof(TypeofType),
    Keyof(KeyofType),
    TemplateLiteral(TemplateLiteralType),
    TypePredicate(TypePredicateType),
    Parenthesized(Box<TypeAnnotation>),
    This,
}

/// Template literal type: `Hello ${string}`
#[derive(Debug, Clone)]
pub struct TemplateLiteralType {
    pub quasis: Vec<JsString>,
    pub types: Vec<TypeAnnotation>,
    pub span: Span,
}

/// Type predicate: `param is Type` or assertion predicate: `asserts param` / `asserts param is Type`
#[derive(Debug, Clone)]
pub struct TypePredicateType {
    pub parameter_name: Identifier,
    /// The type annotation. Optional for `asserts param` (without `is Type`)
    pub type_annotation: Option<Box<TypeAnnotation>>,
    /// Whether this is an assertion predicate (`asserts param` or `asserts param is Type`)
    pub asserts: bool,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct TypeKeyword {
    pub keyword: TypeKeywordKind,
    pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TypeKeywordKind {
    Any,
    Unknown,
    Never,
    Void,
    Null,
    Undefined,
    Boolean,
    Number,
    String,
    Symbol,
    BigInt,
    Object,
}

#[derive(Debug, Clone)]
pub struct TypeReference {
    pub name: Identifier,
    pub type_arguments: Option<TypeArguments>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct TypeArguments {
    pub params: Vec<TypeAnnotation>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct TypeParameters {
    pub params: Vec<TypeParameter>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct TypeParameter {
    pub name: Identifier,
    pub constraint: Option<Box<TypeAnnotation>>,
    pub default: Option<Box<TypeAnnotation>>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct TypeLiteral {
    pub value: LiteralValue,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct ObjectType {
    pub members: Vec<TypeMember>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum TypeMember {
    Property(PropertySignature),
    Method(MethodSignature),
    Index(IndexSignature),
    Call(CallSignature),
    Construct(ConstructSignature),
}

#[derive(Debug, Clone)]
pub struct PropertySignature {
    pub key: ObjectPropertyKey,
    pub type_annotation: Option<Box<TypeAnnotation>>,
    pub optional: bool,
    pub readonly: bool,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct MethodSignature {
    pub key: ObjectPropertyKey,
    pub params: Vec<FunctionParam>,
    pub return_type: Option<Box<TypeAnnotation>>,
    pub type_parameters: Option<TypeParameters>,
    pub optional: bool,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct IndexSignature {
    pub key: Identifier,
    pub key_type: Box<TypeAnnotation>,
    pub value_type: Box<TypeAnnotation>,
    pub readonly: bool,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct CallSignature {
    pub params: Vec<FunctionParam>,
    pub return_type: Option<Box<TypeAnnotation>>,
    pub type_parameters: Option<TypeParameters>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct ConstructSignature {
    pub params: Vec<FunctionParam>,
    pub return_type: Option<Box<TypeAnnotation>>,
    pub type_parameters: Option<TypeParameters>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct ArrayType {
    pub element_type: Box<TypeAnnotation>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct TupleType {
    pub element_types: Vec<TypeAnnotation>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct UnionType {
    pub types: Vec<TypeAnnotation>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct IntersectionType {
    pub types: Vec<TypeAnnotation>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct FunctionType {
    pub params: Vec<FunctionParam>,
    pub return_type: Box<TypeAnnotation>,
    pub type_parameters: Option<TypeParameters>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct ConditionalType {
    pub check_type: Box<TypeAnnotation>,
    pub extends_type: Box<TypeAnnotation>,
    pub true_type: Box<TypeAnnotation>,
    pub false_type: Box<TypeAnnotation>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct InferType {
    pub type_parameter: TypeParameter,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct MappedType {
    pub type_parameter: TypeParameter,
    pub name_type: Option<Box<TypeAnnotation>>,
    pub type_annotation: Option<Box<TypeAnnotation>>,
    pub readonly: Option<MappedTypeModifier>,
    pub optional: Option<MappedTypeModifier>,
    pub span: Span,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MappedTypeModifier {
    Add,
    Remove,
}

#[derive(Debug, Clone)]
pub struct IndexedAccessType {
    pub object_type: Box<TypeAnnotation>,
    pub index_type: Box<TypeAnnotation>,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct TypeofType {
    pub expression: Identifier,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub struct KeyofType {
    pub type_annotation: Box<TypeAnnotation>,
    pub span: Span,
}