@@ -2,8 +2,9 @@ const path = require("path");
2
2
const t = require ( "../babel-types/lib/index.js" ) ;
3
3
4
4
const declare = require ( "./transforms/declare.js" ) ;
5
- const react = require ( "./transforms/react.js" ) ;
5
+ const reactTypes = require ( "./transforms/react-types .js" ) ;
6
6
const objectType = require ( "./transforms/object-type.js" ) ;
7
+ const utilityTypes = require ( "./transforms/utility-types.js" ) ;
7
8
8
9
const { trackComments } = require ( "./util.js" ) ;
9
10
@@ -30,69 +31,6 @@ const transformFunction = (path) => {
30
31
}
31
32
} ;
32
33
33
- // TODO: figure out how to template these inline definitions
34
- const utilityTypes = {
35
- $Keys : ( T ) => {
36
- // $Keys<T> -> keyof T
37
- // TODO: patch @babel /types - tsTypeOperator should accept two arguments
38
- // return t.tsTypeOperator(typeAnnotation, "keyof");
39
- return {
40
- type : "TSTypeOperator" ,
41
- typeAnnotation : T ,
42
- operator : "keyof" ,
43
- } ;
44
- } ,
45
- $Values : ( T ) => {
46
- // $Keys<T> -> T[keyof T]
47
- return t . tsIndexedAccessType (
48
- T ,
49
- {
50
- type : "TSTypeOperator" ,
51
- typeAnnotation : T ,
52
- operator : "keyof" ,
53
- }
54
- // TODO: patch @babel /types - tsTypeOperator should accept two arguments
55
- //t.tsTypeOperator(typeAnnotation, "keyof"),
56
- ) ;
57
- } ,
58
- $ReadOnly : ( T ) => {
59
- // $ReadOnly<T> -> Readonly<T>
60
- const typeName = t . identifier ( "Readonly" ) ;
61
- const typeParameters = t . tsTypeParameterInstantiation ( [ T ] ) ;
62
- return t . tsTypeReference ( typeName , typeParameters ) ;
63
- } ,
64
- $Shape : ( T ) => {
65
- // $Shape<T> -> Partial<T>
66
- const typeName = t . identifier ( "Partial" ) ;
67
- const typeParameters = t . tsTypeParameterInstantiation ( [ T ] ) ;
68
- return t . tsTypeReference ( typeName , typeParameters ) ;
69
- } ,
70
- $NonMaybeType : ( T ) => {
71
- // $NonMaybeType<T> -> NonNullable<T>
72
- const typeName = t . identifier ( "NonNullable" ) ;
73
- const typeParameters = t . tsTypeParameterInstantiation ( [ T ] ) ;
74
- return t . tsTypeReference ( typeName , typeParameters ) ;
75
- } ,
76
- $Exact : ( T ) => {
77
- // $Exact<T> -> T
78
- return T ;
79
- } ,
80
- $PropertyType : ( T , name ) => {
81
- // $PropertyType<T, "name"> -> T["name"]
82
- return t . tsIndexedAccessType ( T , name ) ;
83
- } ,
84
- Class : null , // TODO
85
-
86
- // These are too complicated to inline so we'll leave them as imports
87
- $Diff : null ,
88
- $ElementType : null ,
89
- $Call : null ,
90
-
91
- // The behavior of $Rest only differs when exact object types are involved.
92
- // And since TypeScript doesn't have exact object types using $Diff is okay.
93
- $Rest : "$Diff" ,
94
- } ;
95
-
96
34
const transform = {
97
35
Program : {
98
36
enter ( path , state ) {
@@ -472,28 +410,15 @@ const transform = {
472
410
return ;
473
411
}
474
412
475
- if ( typeName . name in utilityTypes ) {
476
- if (
477
- ( state . options . inlineUtilityTypes &&
478
- typeof utilityTypes [ typeName . name ] === "function" ) ||
479
- typeName . name === "$Exact" // $Exact doesn't exist in utility-types so we always inline it.
480
- ) {
481
- const inline = utilityTypes [ typeName . name ] ;
482
- path . replaceWith ( inline ( ...typeParameters . params ) ) ;
483
- } else if ( typeof utilityTypes [ typeName . name ] === "string" ) {
484
- const replacementName = utilityTypes [ typeName . name ] ;
485
- path . replaceWith (
486
- t . tsTypeReference ( t . identifier ( replacementName ) , typeParameters )
487
- ) ;
488
- state . usedUtilityTypes . add ( replacementName ) ;
489
- } else {
490
- state . usedUtilityTypes . add ( typeName . name ) ;
491
- }
413
+ let replacement ;
492
414
415
+ replacement = utilityTypes . GenericTypeAnnotation . exit ( path , state ) ;
416
+ if ( replacement ) {
417
+ path . replaceWith ( replacement ) ;
493
418
return ;
494
419
}
495
420
496
- const replacement = react . GenericTypeAnnotation . exit ( path , state ) ;
421
+ replacement = reactTypes . GenericTypeAnnotation . exit ( path , state ) ;
497
422
if ( replacement ) {
498
423
path . replaceWith ( replacement ) ;
499
424
return ;
@@ -509,7 +434,7 @@ const transform = {
509
434
const left = qualification ;
510
435
const right = id ;
511
436
512
- const replacement = react . QualifiedTypeIdentifier . exit ( path , state ) ;
437
+ const replacement = reactTypes . QualifiedTypeIdentifier . exit ( path , state ) ;
513
438
if ( replacement ) {
514
439
path . replaceWith ( replacement ) ;
515
440
return ;
0 commit comments