|
1 | | -import { booleanLiteral, Flow, FlowType, FunctionTypeAnnotation, identifier, Identifier, isSpreadProperty, isTSTypeParameter, isTypeParameter, Node, numericLiteral, stringLiteral, tsAnyKeyword, tsArrayType, tsAsExpression, tsBooleanKeyword, tsFunctionType, TSFunctionType, tsIntersectionType, tsLiteralType, tsNullKeyword, tsNumberKeyword, tsPropertySignature, tsStringKeyword, tsThisType, tsTupleType, TSType, tsTypeAnnotation, tsTypeLiteral, tsTypeParameter, tsTypeParameterDeclaration, tsTypeQuery, tsTypeReference, tsUndefinedKeyword, tsUnionType, tsVoidKeyword, TypeAnnotation, TypeParameter } from '@babel/types' |
| 1 | +import { |
| 2 | + booleanLiteral, |
| 3 | + Flow, |
| 4 | + FlowType, |
| 5 | + FunctionTypeAnnotation, |
| 6 | + identifier, |
| 7 | + Identifier, |
| 8 | + isSpreadProperty, |
| 9 | + isTSTypeParameter, |
| 10 | + isTypeParameter, |
| 11 | + Node, |
| 12 | + numericLiteral, |
| 13 | + stringLiteral, |
| 14 | + tsAnyKeyword, |
| 15 | + tsArrayType, |
| 16 | + tsAsExpression, |
| 17 | + tsBooleanKeyword, |
| 18 | + tsFunctionType, |
| 19 | + TSFunctionType, |
| 20 | + tsIntersectionType, |
| 21 | + tsLiteralType, |
| 22 | + tsNullKeyword, |
| 23 | + tsNumberKeyword, |
| 24 | + tsPropertySignature, |
| 25 | + tsStringKeyword, |
| 26 | + tsThisType, |
| 27 | + tsTupleType, |
| 28 | + TSType, |
| 29 | + tsTypeAnnotation, |
| 30 | + tsTypeLiteral, |
| 31 | + tsTypeParameter, |
| 32 | + tsTypeParameterDeclaration, |
| 33 | + tsTypeQuery, |
| 34 | + tsTypeReference, |
| 35 | + tsUndefinedKeyword, |
| 36 | + tsUnionType, |
| 37 | + tsVoidKeyword, |
| 38 | + TypeAnnotation, |
| 39 | + TypeParameter |
| 40 | +} from '@babel/types' |
2 | 41 | import { generateFreeIdentifier } from './utils' |
3 | 42 |
|
4 | 43 | // TODO: Add overloads |
5 | 44 | export function toTs(node: Flow | TSType): TSType { |
6 | 45 | switch (node.type) { |
7 | | - |
8 | 46 | // TS types |
9 | 47 | // TODO: Why does tsTs get called with TSTypes? It should only get called with Flow types. |
10 | 48 | case 'TSAnyKeyword': |
@@ -75,7 +113,7 @@ export function toTs(node: Flow | TSType): TSType { |
75 | 113 |
|
76 | 114 | case 'TypeParameterDeclaration': |
77 | 115 | let params = node.params.map(_ => { |
78 | | - let d = (_ as any as TypeParameter).default |
| 116 | + let d = ((_ as any) as TypeParameter).default |
79 | 117 | let p = tsTypeParameter( |
80 | 118 | hasBound(_) ? toTsType(_.bound.typeAnnotation) : undefined, |
81 | 119 | d ? toTs(d) : undefined |
@@ -109,76 +147,108 @@ export function toTs(node: Flow | TSType): TSType { |
109 | 147 |
|
110 | 148 | export function toTsType(node: FlowType): TSType { |
111 | 149 | switch (node.type) { |
112 | | - case 'AnyTypeAnnotation': return tsAnyKeyword() |
113 | | - case 'ArrayTypeAnnotation': return tsArrayType(toTsType(node.elementType)) |
114 | | - case 'BooleanTypeAnnotation': return tsBooleanKeyword() |
115 | | - case 'BooleanLiteralTypeAnnotation': return tsLiteralType(booleanLiteral(node.value!)) |
116 | | - case 'FunctionTypeAnnotation': return functionToTsType(node) |
117 | | - case 'GenericTypeAnnotation': return tsTypeReference(node.id) |
118 | | - case 'IntersectionTypeAnnotation': return tsIntersectionType(node.types.map(toTsType)) |
119 | | - case 'MixedTypeAnnotation': return tsAnyKeyword() |
120 | | - case 'NullLiteralTypeAnnotation': return tsNullKeyword() |
121 | | - case 'NullableTypeAnnotation': return tsUnionType([toTsType(node.typeAnnotation), tsNullKeyword(), tsUndefinedKeyword()]) |
122 | | - case 'NumberLiteralTypeAnnotation': return tsLiteralType(numericLiteral(node.value!)) |
123 | | - case 'NumberTypeAnnotation': return tsNumberKeyword() |
124 | | - case 'StringLiteralTypeAnnotation': return tsLiteralType(stringLiteral(node.value!)) |
125 | | - case 'StringTypeAnnotation': return tsStringKeyword() |
126 | | - case 'ThisTypeAnnotation': return tsThisType() |
127 | | - case 'TupleTypeAnnotation': return tsTupleType(node.types.map(toTsType)) |
128 | | - case 'TypeofTypeAnnotation': return tsTypeQuery(getId(node.argument)) |
129 | | - case 'ObjectTypeAnnotation': return tsTypeLiteral([ |
130 | | - ...node.properties.map(_ => { |
131 | | - if (isSpreadProperty(_)) { |
132 | | - return _ |
133 | | - } |
134 | | - let s = tsPropertySignature(_.key, tsTypeAnnotation(toTsType(_.value))) |
135 | | - s.optional = _.optional |
136 | | - return s |
137 | | - // TODO: anonymous indexers |
138 | | - // TODO: named indexers |
139 | | - // TODO: call properties |
140 | | - // TODO: variance |
141 | | - }) |
142 | | - // ...node.indexers.map(_ => tSIndexSignature()) |
143 | | - ]) |
144 | | - case 'UnionTypeAnnotation': return tsUnionType(node.types.map(toTsType)) |
145 | | - case 'VoidTypeAnnotation': return tsVoidKeyword() |
| 150 | + case 'AnyTypeAnnotation': |
| 151 | + return tsAnyKeyword() |
| 152 | + case 'ArrayTypeAnnotation': |
| 153 | + return tsArrayType(toTsType(node.elementType)) |
| 154 | + case 'BooleanTypeAnnotation': |
| 155 | + return tsBooleanKeyword() |
| 156 | + case 'BooleanLiteralTypeAnnotation': |
| 157 | + return tsLiteralType(booleanLiteral(node.value!)) |
| 158 | + case 'FunctionTypeAnnotation': |
| 159 | + return functionToTsType(node) |
| 160 | + case 'GenericTypeAnnotation': |
| 161 | + return tsTypeReference(node.id) |
| 162 | + case 'IntersectionTypeAnnotation': |
| 163 | + return tsIntersectionType(node.types.map(toTsType)) |
| 164 | + case 'MixedTypeAnnotation': |
| 165 | + return tsAnyKeyword() |
| 166 | + case 'NullLiteralTypeAnnotation': |
| 167 | + return tsNullKeyword() |
| 168 | + case 'NullableTypeAnnotation': |
| 169 | + return tsUnionType([ |
| 170 | + toTsType(node.typeAnnotation), |
| 171 | + tsNullKeyword(), |
| 172 | + tsUndefinedKeyword() |
| 173 | + ]) |
| 174 | + case 'NumberLiteralTypeAnnotation': |
| 175 | + return tsLiteralType(numericLiteral(node.value!)) |
| 176 | + case 'NumberTypeAnnotation': |
| 177 | + return tsNumberKeyword() |
| 178 | + case 'StringLiteralTypeAnnotation': |
| 179 | + return tsLiteralType(stringLiteral(node.value!)) |
| 180 | + case 'StringTypeAnnotation': |
| 181 | + return tsStringKeyword() |
| 182 | + case 'ThisTypeAnnotation': |
| 183 | + return tsThisType() |
| 184 | + case 'TupleTypeAnnotation': |
| 185 | + return tsTupleType(node.types.map(toTsType)) |
| 186 | + case 'TypeofTypeAnnotation': |
| 187 | + return tsTypeQuery(getId(node.argument)) |
| 188 | + case 'ObjectTypeAnnotation': |
| 189 | + return tsTypeLiteral([ |
| 190 | + ...node.properties.map(_ => { |
| 191 | + if (isSpreadProperty(_)) { |
| 192 | + return _ |
| 193 | + } |
| 194 | + let s = tsPropertySignature( |
| 195 | + _.key, |
| 196 | + tsTypeAnnotation(toTsType(_.value)) |
| 197 | + ) |
| 198 | + s.optional = _.optional |
| 199 | + return s |
| 200 | + // TODO: anonymous indexers |
| 201 | + // TODO: named indexers |
| 202 | + // TODO: call properties |
| 203 | + // TODO: variance |
| 204 | + }) |
| 205 | + // ...node.indexers.map(_ => tSIndexSignature()) |
| 206 | + ]) |
| 207 | + case 'UnionTypeAnnotation': |
| 208 | + return tsUnionType(node.types.map(toTsType)) |
| 209 | + case 'VoidTypeAnnotation': |
| 210 | + return tsVoidKeyword() |
146 | 211 | } |
147 | 212 | } |
148 | 213 |
|
149 | 214 | function getId(node: FlowType): Identifier { |
150 | 215 | switch (node.type) { |
151 | | - case 'GenericTypeAnnotation': return node.id |
152 | | - default: throw ReferenceError('typeof query must reference a node that has an id') |
| 216 | + case 'GenericTypeAnnotation': |
| 217 | + return node.id |
| 218 | + default: |
| 219 | + throw ReferenceError('typeof query must reference a node that has an id') |
153 | 220 | } |
154 | 221 | } |
155 | 222 |
|
156 | 223 | function functionToTsType(node: FunctionTypeAnnotation): TSFunctionType { |
157 | | - |
158 | 224 | let typeParams = undefined |
159 | 225 |
|
160 | 226 | if (node.typeParameters) { |
161 | | - typeParams = tsTypeParameterDeclaration(node.typeParameters.params.map(_ => { |
162 | | - |
163 | | - // TODO: How is this possible? |
164 | | - if (isTSTypeParameter(_)) { |
165 | | - return _ |
166 | | - } |
| 227 | + typeParams = tsTypeParameterDeclaration( |
| 228 | + node.typeParameters.params.map(_ => { |
| 229 | + // TODO: How is this possible? |
| 230 | + if (isTSTypeParameter(_)) { |
| 231 | + return _ |
| 232 | + } |
167 | 233 |
|
168 | | - let constraint = _.bound ? toTs(_.bound) : undefined |
169 | | - let default_ = _.default ? toTs(_.default) : undefined |
170 | | - let param = tsTypeParameter(constraint, default_) |
171 | | - param.name = _.name |
172 | | - return param |
173 | | - })) |
| 234 | + let constraint = _.bound ? toTs(_.bound) : undefined |
| 235 | + let default_ = _.default ? toTs(_.default) : undefined |
| 236 | + let param = tsTypeParameter(constraint, default_) |
| 237 | + param.name = _.name |
| 238 | + return param |
| 239 | + }) |
| 240 | + ) |
174 | 241 | } |
175 | 242 |
|
176 | 243 | let f = tsFunctionType(typeParams) |
177 | 244 |
|
178 | 245 | // Params |
179 | 246 | if (node.params) { |
180 | 247 | // TODO: Rest params |
181 | | - let paramNames = node.params.map(_ => _.name).filter(_ => _ !== null).map(_ => (_ as Identifier).name) |
| 248 | + let paramNames = node.params |
| 249 | + .map(_ => _.name) |
| 250 | + .filter(_ => _ !== null) |
| 251 | + .map(_ => (_ as Identifier).name) |
182 | 252 | f.parameters = node.params.map(_ => { |
183 | 253 | let name = _.name && _.name.name |
184 | 254 |
|
|
0 commit comments