Skip to content

Commit 338a881

Browse files
committed
bugfixes
1 parent ebb65a8 commit 338a881

File tree

3 files changed

+71
-38
lines changed

3 files changed

+71
-38
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"prepublishOnly": "npm run clean && npm run lint && npm run build -- -d",
1919
"pretest": "npm run build",
2020
"tdd": "concurrently -k 'npm run build:watch' 'npm run test:watch'",
21-
"test": "ava",
21+
"test": "ava --verbose",
2222
"test:debug": "node --inspect-brk ./node_modules/ava/profile.js ./dist/test/test.js",
2323
"test:watch": "ava -w"
2424
},

src/convert.ts

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,45 @@ import { booleanLiteral, Flow, FlowTypeAnnotation, FunctionTypeAnnotation, Ident
22
import { generateFreeIdentifier } from './utils'
33

44
// TODO: Add overloads
5-
export function toTs(node: Flow): TSType {
5+
export function toTs(node: Flow | TSType): TSType {
66
switch (node.type) {
7+
8+
// TS types
9+
// TODO: Why does tsTs get called with TSTypes? It should only get called with Flow types.
10+
case 'TSAnyKeyword':
11+
case 'TSArrayType':
12+
case 'TSBooleanKeyword':
13+
case 'TSConstructorType':
14+
case 'TSExpressionWithTypeArguments':
15+
case 'TSFunctionType':
16+
case 'TSIndexedAccessType':
17+
case 'TSIntersectionType':
18+
case 'TSLiteralType':
19+
case 'TSMappedType':
20+
case 'TSNeverKeyword':
21+
case 'TSNullKeyword':
22+
case 'TSNumberKeyword':
23+
case 'TSObjectKeyword':
24+
case 'TSParenthesizedType':
25+
case 'TSStringKeyword':
26+
case 'TSSymbolKeyword':
27+
case 'TSThisType':
28+
case 'TSTupleType':
29+
case 'TSTypeAnnotation':
30+
case 'TSTypeLiteral':
31+
case 'TSTypeOperator':
32+
case 'TSTypePredicate':
33+
case 'TSTypeQuery':
34+
case 'TSTypeReference':
35+
case 'TSUndefinedKeyword':
36+
case 'TSUnionType':
37+
case 'TSVoidKeyword':
38+
case 'TSTypeParameterDeclaration':
39+
case 'TSAsExpression':
40+
case 'TSPropertySignature':
41+
return node
42+
43+
// Flow types
744
case 'AnyTypeAnnotation':
845
case 'ArrayTypeAnnotation':
946
case 'BooleanTypeAnnotation':
@@ -89,7 +126,7 @@ export function toTsType(node: FlowTypeAnnotation): TSType {
89126
case 'ThisTypeAnnotation': return tSThisType()
90127
case 'TupleTypeAnnotation': return tSTupleType(node.types.map(toTsType))
91128
case 'TypeofTypeAnnotation': return tSTypeQuery(getId(node.argument))
92-
case 'TypeAnnotation': return toTsType(node.typeAnnotation)
129+
case 'TypeAnnotation': return tSTypeAnnotation(toTsType(node.typeAnnotation))
93130
case 'ObjectTypeAnnotation': return tSTypeLiteral([
94131
...node.properties.map(_ => {
95132
let s = tSPropertySignature(_.key, tSTypeAnnotation(toTsType(_.value)))
@@ -116,8 +153,10 @@ function getId(node: FlowTypeAnnotation): Identifier {
116153

117154
function functionToTsType(node: FunctionTypeAnnotation): TSFunctionType {
118155

119-
let f = tSFunctionType(tSTypeParameterDeclaration(
120-
node.typeParameters.params.map(_ => {
156+
let typeParams = undefined
157+
158+
if (node.typeParameters) {
159+
typeParams = tSTypeParameterDeclaration(node.typeParameters.params.map(_ => {
121160

122161
// TODO: How is this possible?
123162
if (isTSTypeParameter(_)) {
@@ -129,29 +168,38 @@ function functionToTsType(node: FunctionTypeAnnotation): TSFunctionType {
129168
let param = tSTypeParameter(constraint, default_)
130169
param.name = _.name
131170
return param
132-
})
133-
))
171+
}))
172+
}
173+
174+
let f = tSFunctionType(typeParams)
134175

135176
// Params
136-
// TODO: Rest params
137-
let paramNames = node.params.map(_ => _.name).filter(_ => _ !== null).map(_ => _.name)
138-
f.parameters = node.params.map(_ => {
139-
let name = _.name.name
177+
if (node.params) {
178+
// TODO: Rest params
179+
let paramNames = node.params.map(_ => _.name).filter(_ => _ !== null).map(_ => (_ as Identifier).name)
180+
f.parameters = node.params.map(_ => {
181+
let name = _.name && _.name.name
182+
183+
// Generate param name? (Required in TS, optional in Flow)
184+
if (name == null) {
185+
name = generateFreeIdentifier(paramNames)
186+
paramNames.push(name)
187+
}
140188

141-
// Generate param name? (Required in TS, optional in Flow)
142-
if (name == null) {
143-
name = generateFreeIdentifier(paramNames)
144-
paramNames.push(name)
145-
}
189+
let id = identifier(name)
146190

147-
let id = identifier(_.name.name || name)
148-
id.typeAnnotation = tSTypeAnnotation(toTsType(_.typeAnnotation))
191+
if (_.typeAnnotation) {
192+
id.typeAnnotation = tSTypeAnnotation(toTsType(_.typeAnnotation))
193+
}
149194

150-
return id
151-
})
195+
return id
196+
})
197+
}
152198

153199
// Return type
154-
f.typeAnnotation = tSTypeAnnotation(toTsType(node.returnType))
200+
if (node.returnType) {
201+
f.typeAnnotation = tSTypeAnnotation(toTsType(node.returnType))
202+
}
155203

156204
return f
157205
}

src/rules/Functions.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
1-
import { functionTypeAnnotation, functionTypeParam, identifier } from 'babel/packages/babel-types'
21
import { addRule } from '../'
3-
import { generateFreeIdentifier } from '../utils'
2+
import { toTs } from '../convert'
43

54
addRule('Functions', () => ({
65
FunctionTypeAnnotation(path) {
7-
8-
if (path.node.params.every(_ => _.name !== null)) {
9-
return
10-
}
11-
12-
let paramNames = path.node.params.map(_ => _.name).filter(_ => _ !== null).map(_ => _.name)
13-
let params = path.node.params.map(param => {
14-
if (param.name === null) {
15-
let id = generateFreeIdentifier(paramNames)
16-
paramNames.push(id)
17-
return functionTypeParam(identifier(id), param.typeAnnotation)
18-
}
19-
return param
20-
})
21-
path.replaceWith(functionTypeAnnotation(path.node.typeParameters, params, path.node.rest, path.node.returnType))
6+
path.replaceWith(toTs(path.node))
227
}
238
}))

0 commit comments

Comments
 (0)