6
6
*
7
7
*/
8
8
9
- import { expression as expr , statement as stmt } from '../../../tests/utils' ;
9
+ import { statement as stmt } from '../../../tests/utils' ;
10
10
import getTSType from '../getTSType' ;
11
11
12
- function expression ( code ) {
13
- return expr ( code , {
14
- filename : 'test.ts' ,
15
- babelrc : false ,
16
- } ) ;
17
- }
18
12
function statement ( code ) {
19
13
return stmt ( code , {
20
14
filename : 'test.ts' ,
@@ -43,7 +37,9 @@ describe('getTSType', () => {
43
37
] ;
44
38
45
39
simplePropTypes . forEach ( type => {
46
- const typePath = expression ( 'x: ' + type )
40
+ const typePath = statement ( `let x: ${ type } ;` )
41
+ . get ( 'declarations' , 0 )
42
+ . get ( 'id' )
47
43
. get ( 'typeAnnotation' )
48
44
. get ( 'typeAnnotation' ) ;
49
45
expect ( getTSType ( typePath ) ) . toEqual ( { name : type } ) ;
@@ -54,7 +50,9 @@ describe('getTSType', () => {
54
50
const literalTypes = [ '"foo"' , 1234 , true ] ;
55
51
56
52
literalTypes . forEach ( value => {
57
- const typePath = expression ( `x: ${ value } ` )
53
+ const typePath = statement ( `let x: ${ value } ;` )
54
+ . get ( 'declarations' , 0 )
55
+ . get ( 'id' )
58
56
. get ( 'typeAnnotation' )
59
57
. get ( 'typeAnnotation' ) ;
60
58
expect ( getTSType ( typePath ) ) . toEqual ( {
@@ -65,14 +63,18 @@ describe('getTSType', () => {
65
63
} ) ;
66
64
67
65
it ( 'detects external type' , ( ) => {
68
- const typePath = expression ( 'x: xyz' )
66
+ const typePath = statement ( 'let x: xyz;' )
67
+ . get ( 'declarations' , 0 )
68
+ . get ( 'id' )
69
69
. get ( 'typeAnnotation' )
70
70
. get ( 'typeAnnotation' ) ;
71
71
expect ( getTSType ( typePath ) ) . toEqual ( { name : 'xyz' } ) ;
72
72
} ) ;
73
73
74
74
it ( 'detects array type shorthand' , ( ) => {
75
- const typePath = expression ( 'x: number[]' )
75
+ const typePath = statement ( 'let x: number[];' )
76
+ . get ( 'declarations' , 0 )
77
+ . get ( 'id' )
76
78
. get ( 'typeAnnotation' )
77
79
. get ( 'typeAnnotation' ) ;
78
80
expect ( getTSType ( typePath ) ) . toEqual ( {
@@ -83,7 +85,9 @@ describe('getTSType', () => {
83
85
} ) ;
84
86
85
87
it ( 'detects array type' , ( ) => {
86
- const typePath = expression ( 'x: Array<number>' )
88
+ const typePath = statement ( 'let x: Array<number>;' )
89
+ . get ( 'declarations' , 0 )
90
+ . get ( 'id' )
87
91
. get ( 'typeAnnotation' )
88
92
. get ( 'typeAnnotation' ) ;
89
93
expect ( getTSType ( typePath ) ) . toEqual ( {
@@ -94,7 +98,9 @@ describe('getTSType', () => {
94
98
} ) ;
95
99
96
100
it ( 'detects array type with multiple types' , ( ) => {
97
- const typePath = expression ( 'x: Array<number, xyz>' )
101
+ const typePath = statement ( 'let x: Array<number, xyz>;' )
102
+ . get ( 'declarations' , 0 )
103
+ . get ( 'id' )
98
104
. get ( 'typeAnnotation' )
99
105
. get ( 'typeAnnotation' ) ;
100
106
expect ( getTSType ( typePath ) ) . toEqual ( {
@@ -105,7 +111,9 @@ describe('getTSType', () => {
105
111
} ) ;
106
112
107
113
it ( 'detects class type' , ( ) => {
108
- const typePath = expression ( 'x: Class<Boolean>' )
114
+ const typePath = statement ( 'let x: Class<Boolean>;' )
115
+ . get ( 'declarations' , 0 )
116
+ . get ( 'id' )
109
117
. get ( 'typeAnnotation' )
110
118
. get ( 'typeAnnotation' ) ;
111
119
expect ( getTSType ( typePath ) ) . toEqual ( {
@@ -116,7 +124,9 @@ describe('getTSType', () => {
116
124
} ) ;
117
125
118
126
it ( 'detects function type with subtype' , ( ) => {
119
- const typePath = expression ( 'x: Function<xyz>' )
127
+ const typePath = statement ( 'let x: Function<xyz>;' )
128
+ . get ( 'declarations' , 0 )
129
+ . get ( 'id' )
120
130
. get ( 'typeAnnotation' )
121
131
. get ( 'typeAnnotation' ) ;
122
132
expect ( getTSType ( typePath ) ) . toEqual ( {
@@ -127,7 +137,9 @@ describe('getTSType', () => {
127
137
} ) ;
128
138
129
139
it ( 'detects object types' , ( ) => {
130
- const typePath = expression ( 'x: { a: string, b?: xyz }' )
140
+ const typePath = statement ( 'let x: { a: string, b?: xyz };' )
141
+ . get ( 'declarations' , 0 )
142
+ . get ( 'id' )
131
143
. get ( 'typeAnnotation' )
132
144
. get ( 'typeAnnotation' ) ;
133
145
expect ( getTSType ( typePath ) ) . toEqual ( {
@@ -144,7 +156,9 @@ describe('getTSType', () => {
144
156
} ) ;
145
157
146
158
it ( 'detects union type' , ( ) => {
147
- const typePath = expression ( 'x: string | xyz | "foo" | void' )
159
+ const typePath = statement ( 'let x: string | xyz | "foo" | void;' )
160
+ . get ( 'declarations' , 0 )
161
+ . get ( 'id' )
148
162
. get ( 'typeAnnotation' )
149
163
. get ( 'typeAnnotation' ) ;
150
164
expect ( getTSType ( typePath ) ) . toEqual ( {
@@ -160,7 +174,9 @@ describe('getTSType', () => {
160
174
} ) ;
161
175
162
176
it ( 'detects intersection type' , ( ) => {
163
- const typePath = expression ( 'x: string & xyz & "foo" & void' )
177
+ const typePath = statement ( 'let x: string & xyz & "foo" & void;' )
178
+ . get ( 'declarations' , 0 )
179
+ . get ( 'id' )
164
180
. get ( 'typeAnnotation' )
165
181
. get ( 'typeAnnotation' ) ;
166
182
expect ( getTSType ( typePath ) ) . toEqual ( {
@@ -176,9 +192,11 @@ describe('getTSType', () => {
176
192
} ) ;
177
193
178
194
it ( 'detects function signature type' , ( ) => {
179
- const typePath = expression (
180
- 'x: (p1: number, p2: string, ...rest: Array<string>) => boolean' ,
195
+ const typePath = statement (
196
+ 'let x: (p1: number, p2: string, ...rest: Array<string>) => boolean; ' ,
181
197
)
198
+ . get ( 'declarations' , 0 )
199
+ . get ( 'id' )
182
200
. get ( 'typeAnnotation' )
183
201
. get ( 'typeAnnotation' ) ;
184
202
expect ( getTSType ( typePath ) ) . toEqual ( {
@@ -205,7 +223,9 @@ describe('getTSType', () => {
205
223
} ) ;
206
224
207
225
it ( 'detects function signature type with `this` parameter' , ( ) => {
208
- const typePath = expression ( 'x: (this: Foo, p1: number) => boolean' )
226
+ const typePath = statement ( 'let x: (this: Foo, p1: number) => boolean;' )
227
+ . get ( 'declarations' , 0 )
228
+ . get ( 'id' )
209
229
. get ( 'typeAnnotation' )
210
230
. get ( 'typeAnnotation' ) ;
211
231
expect ( getTSType ( typePath ) ) . toEqual ( {
@@ -221,7 +241,11 @@ describe('getTSType', () => {
221
241
} ) ;
222
242
223
243
it ( 'detects callable signature type' , ( ) => {
224
- const typePath = expression ( 'x: { (str: string): string, token: string }' )
244
+ const typePath = statement (
245
+ 'let x: { (str: string): string, token: string };' ,
246
+ )
247
+ . get ( 'declarations' , 0 )
248
+ . get ( 'id' )
225
249
. get ( 'typeAnnotation' )
226
250
. get ( 'typeAnnotation' ) ;
227
251
expect ( getTSType ( typePath ) ) . toEqual ( {
@@ -246,9 +270,11 @@ describe('getTSType', () => {
246
270
} ) ;
247
271
248
272
it ( 'detects map signature' , ( ) => {
249
- const typePath = expression (
250
- 'x: { [key: string]: number, [key: "xl"]: string, token: "a" | "b" }' ,
273
+ const typePath = statement (
274
+ 'let x: { [key: string]: number, [key: "xl"]: string, token: "a" | "b" }; ' ,
251
275
)
276
+ . get ( 'declarations' , 0 )
277
+ . get ( 'id' )
252
278
. get ( 'typeAnnotation' )
253
279
. get ( 'typeAnnotation' ) ;
254
280
expect ( getTSType ( typePath ) ) . toEqual ( {
@@ -283,7 +309,9 @@ describe('getTSType', () => {
283
309
} ) ;
284
310
285
311
it ( 'detects tuple signature' , ( ) => {
286
- const typePath = expression ( 'x: [string, number]' )
312
+ const typePath = statement ( 'let x: [string, number];' )
313
+ . get ( 'declarations' , 0 )
314
+ . get ( 'id' )
287
315
. get ( 'typeAnnotation' )
288
316
. get ( 'typeAnnotation' ) ;
289
317
expect ( getTSType ( typePath ) ) . toEqual ( {
@@ -294,7 +322,9 @@ describe('getTSType', () => {
294
322
} ) ;
295
323
296
324
it ( 'detects tuple in union signature' , ( ) => {
297
- const typePath = expression ( 'x: [string, number] | [number, string]' )
325
+ const typePath = statement ( 'let x: [string, number] | [number, string];' )
326
+ . get ( 'declarations' , 0 )
327
+ . get ( 'id' )
298
328
. get ( 'typeAnnotation' )
299
329
. get ( 'typeAnnotation' ) ;
300
330
expect ( getTSType ( typePath ) ) . toEqual ( {
0 commit comments