@@ -9,7 +9,6 @@ use super::list::{
9
9
} ;
10
10
use crate :: {
11
11
diagnostics,
12
- js:: list:: { ArrayPatternList , ObjectPatternProperties } ,
13
12
lexer:: Kind ,
14
13
list:: { NormalList , SeparatedList } ,
15
14
modifiers:: ModifierFlags ,
@@ -19,18 +18,91 @@ use crate::{
19
18
20
19
impl < ' a > ParserImpl < ' a > {
21
20
pub ( crate ) fn parse_ts_type ( & mut self ) -> Result < TSType < ' a > > {
22
- if self . is_at_constructor_type ( ) {
23
- return self . parse_ts_constructor_type ( ) ;
21
+ if self . is_start_of_function_type_or_constructor_type ( ) {
22
+ return self . parse_function_or_constructor_type ( ) ;
24
23
}
24
+ let left_span = self . start_span ( ) ;
25
+ let left = self . parse_ts_union_type ( ) ?;
26
+ self . parse_ts_conditional_type ( left_span, left)
27
+ }
28
+
29
+ fn parse_function_or_constructor_type ( & mut self ) -> Result < TSType < ' a > > {
30
+ let span = self . start_span ( ) ;
31
+ let r#abstract = self . eat ( Kind :: Abstract ) ;
32
+ let is_constructor_type = self . eat ( Kind :: New ) ;
33
+ let type_parameters = self . parse_ts_type_parameters ( ) ?;
34
+ let ( this_param, params) = self . parse_formal_parameters ( FormalParameterKind :: Signature ) ?;
35
+ self . expect ( Kind :: Arrow ) ?;
36
+ let return_type = {
37
+ let return_type_span = self . start_span ( ) ;
38
+ let return_type = self . parse_ts_return_type ( ) ?;
39
+ self . ast . ts_type_annotation ( self . end_span ( return_type_span) , return_type)
40
+ } ;
25
41
26
- if self . is_at_function_type ( ) {
27
- return self . parse_ts_function_type ( ) ;
42
+ let span = self . end_span ( span) ;
43
+ Ok ( if is_constructor_type {
44
+ if let Some ( this_param) = & this_param {
45
+ // type Foo = new (this: number) => any;
46
+ self . error ( diagnostics:: ts_constructor_this_parameter ( this_param. span ) ) ;
47
+ }
48
+ self . ast . ts_constructor_type ( span, r#abstract, params, return_type, type_parameters)
49
+ } else {
50
+ self . ast . ts_function_type ( span, this_param, params, return_type, type_parameters)
51
+ } )
52
+ }
53
+
54
+ fn is_start_of_function_type_or_constructor_type ( & mut self ) -> bool {
55
+ if self . at ( Kind :: LAngle ) {
56
+ return true ;
57
+ }
58
+ if self . at ( Kind :: LParen ) && self . lookahead ( Self :: is_unambiguously_start_of_function_type) {
59
+ return true ;
28
60
}
61
+ self . at ( Kind :: New ) || ( self . at ( Kind :: Abstract ) && self . peek_at ( Kind :: New ) )
62
+ }
29
63
30
- let left_span = self . start_span ( ) ;
31
- let left = self . parse_ts_union_type ( ) ?;
64
+ fn is_unambiguously_start_of_function_type ( & mut self ) -> bool {
65
+ self . bump_any ( ) ;
66
+ // ( )
67
+ // ( ...
68
+ if matches ! ( self . cur_kind( ) , Kind :: RParen | Kind :: Dot3 ) {
69
+ return true ;
70
+ }
71
+ if self . skip_parameter_start ( ) {
72
+ // ( xxx :
73
+ // ( xxx ,
74
+ // ( xxx ?
75
+ // ( xxx =
76
+ if matches ! ( self . cur_kind( ) , Kind :: Colon | Kind :: Comma | Kind :: Question | Kind :: Eq ) {
77
+ return true ;
78
+ }
79
+ // ( xxx ) =>
80
+ if self . eat ( Kind :: RParen ) && self . at ( Kind :: Arrow ) {
81
+ return true ;
82
+ }
83
+ }
84
+ false
85
+ }
32
86
33
- self . parse_ts_conditional_type ( left_span, left)
87
+ fn skip_parameter_start ( & mut self ) -> bool {
88
+ // Skip modifiers
89
+ loop {
90
+ if self . cur_kind ( ) . is_modifier_kind ( ) && !self . peek_at ( Kind :: Comma ) {
91
+ self . bump_any ( ) ;
92
+ } else {
93
+ break ;
94
+ }
95
+ }
96
+ if self . cur_kind ( ) . is_identifier ( ) || self . at ( Kind :: This ) {
97
+ self . bump_any ( ) ;
98
+ return true ;
99
+ }
100
+ if matches ! ( self . cur_kind( ) , Kind :: LBrack | Kind :: LCurly )
101
+ && self . parse_binding_pattern_kind ( ) . is_ok ( )
102
+ {
103
+ return true ;
104
+ }
105
+ false
34
106
}
35
107
36
108
pub ( crate ) fn parse_ts_type_parameters (
@@ -162,10 +234,6 @@ impl<'a> ParserImpl<'a> {
162
234
Ok ( left)
163
235
}
164
236
165
- fn is_at_constructor_type ( & mut self ) -> bool {
166
- self . at ( Kind :: New ) || ( self . at ( Kind :: Abstract ) && self . peek_at ( Kind :: New ) )
167
- }
168
-
169
237
// test ts ts_union_type
170
238
// type A = string | number;
171
239
// type B = | A | void | null;
@@ -474,51 +542,6 @@ impl<'a> ParserImpl<'a> {
474
542
Ok ( self . ast . ts_tuple_type ( self . end_span ( span) , elements) )
475
543
}
476
544
477
- fn is_at_function_type ( & mut self ) -> bool {
478
- if self . at ( Kind :: LAngle ) {
479
- return true ;
480
- }
481
-
482
- if !self . at ( Kind :: LParen ) {
483
- return false ;
484
- }
485
-
486
- let checkpoint = self . checkpoint ( ) ;
487
-
488
- self . bump_any ( ) ; // bump (
489
-
490
- if self . at ( Kind :: RParen ) || self . at ( Kind :: Dot3 ) {
491
- self . rewind ( checkpoint) ;
492
- return true ;
493
- }
494
-
495
- let mut is_function_parameter_start =
496
- self . at ( Kind :: This ) || self . cur_kind ( ) . is_binding_identifier ( ) ;
497
-
498
- if is_function_parameter_start {
499
- self . bump_any ( ) ;
500
- }
501
-
502
- if match self . cur_kind ( ) {
503
- Kind :: LBrack => ArrayPatternList :: parse ( self ) . is_ok ( ) ,
504
- Kind :: LCurly => ObjectPatternProperties :: parse ( self ) . is_ok ( ) ,
505
- _ => false ,
506
- } {
507
- is_function_parameter_start = true ;
508
- }
509
-
510
- let result = if is_function_parameter_start {
511
- matches ! ( self . cur_kind( ) , Kind :: Colon | Kind :: Eq | Kind :: Comma | Kind :: Question )
512
- || ( self . at ( Kind :: RParen ) && self . peek_at ( Kind :: Arrow ) )
513
- } else {
514
- false
515
- } ;
516
-
517
- self . rewind ( checkpoint) ;
518
-
519
- result
520
- }
521
-
522
545
fn is_at_mapped_type ( & mut self ) -> bool {
523
546
if !self . at ( Kind :: LCurly ) {
524
547
return false ;
@@ -731,49 +754,6 @@ impl<'a> ParserImpl<'a> {
731
754
Ok ( TSImportAttributes { span, elements } )
732
755
}
733
756
734
- fn parse_ts_constructor_type ( & mut self ) -> Result < TSType < ' a > > {
735
- let span = self . start_span ( ) ;
736
- let r#abstract = self . eat ( Kind :: Abstract ) ;
737
- self . expect ( Kind :: New ) ?;
738
- let type_parameters = self . parse_ts_type_parameters ( ) ?;
739
- let ( this_param, params) = self . parse_formal_parameters ( FormalParameterKind :: Signature ) ?;
740
-
741
- if let Some ( this_param) = this_param {
742
- // type Foo = new (this: number) => any;
743
- self . error ( diagnostics:: ts_constructor_this_parameter ( this_param. span ) ) ;
744
- }
745
-
746
- self . expect ( Kind :: Arrow ) ?;
747
- let return_type_span = self . start_span ( ) ;
748
- let return_type = self . parse_ts_return_type ( ) ?;
749
- let return_type = self . ast . ts_type_annotation ( self . end_span ( return_type_span) , return_type) ;
750
-
751
- Ok ( self . ast . ts_constructor_type (
752
- self . end_span ( span) ,
753
- r#abstract,
754
- params,
755
- return_type,
756
- type_parameters,
757
- ) )
758
- }
759
-
760
- fn parse_ts_function_type ( & mut self ) -> Result < TSType < ' a > > {
761
- let span = self . start_span ( ) ;
762
- let type_parameters = self . parse_ts_type_parameters ( ) ?;
763
- let ( this_param, params) = self . parse_formal_parameters ( FormalParameterKind :: Signature ) ?;
764
- let return_type_span = self . start_span ( ) ;
765
- self . expect ( Kind :: Arrow ) ?;
766
- let return_type = self . parse_ts_return_type ( ) ?;
767
- let return_type = self . ast . ts_type_annotation ( self . end_span ( return_type_span) , return_type) ;
768
- Ok ( self . ast . ts_function_type (
769
- self . end_span ( span) ,
770
- this_param,
771
- params,
772
- return_type,
773
- type_parameters,
774
- ) )
775
- }
776
-
777
757
fn parse_ts_infer_type ( & mut self ) -> Result < TSType < ' a > > {
778
758
let span = self . start_span ( ) ;
779
759
self . expect ( Kind :: Infer ) ?;
0 commit comments