Skip to content

Commit e6f128b

Browse files
committed
use @babel/types
1 parent 338a881 commit e6f128b

File tree

16 files changed

+152
-63
lines changed

16 files changed

+152
-63
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.DS_Store
22
dist
33
node_modules
4+
yarn-error.log

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
"test:watch": "ava -w"
2424
},
2525
"dependencies": {
26-
"babel": "bcherny/babel",
26+
"@babel/generator": "7.0.0-beta.38",
27+
"@babel/traverse": "7.0.0-beta.38",
28+
"@babel/types": "7.0.0-beta.38",
2729
"glob": "^7.1.2",
2830
"lodash": "^4.17.4",
2931
"mz": "^2.7.0"

src/convert.ts

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { booleanLiteral, Flow, FlowTypeAnnotation, FunctionTypeAnnotation, Identifier, identifier, 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/packages/babel-types/lib'
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'
22
import { generateFreeIdentifier } from './utils'
33

44
// TODO: Add overloads
@@ -65,26 +65,26 @@ export function toTs(node: Flow | TSType): TSType {
6565
return toTsType(node)
6666

6767
case 'ObjectTypeProperty':
68-
let _ = tSPropertySignature(node.key, tSTypeAnnotation(toTs(node.value)))
68+
let _ = tsPropertySignature(node.key, tsTypeAnnotation(toTs(node.value)))
6969
_.optional = node.optional
7070
_.readonly = node.variance && node.variance.kind === 'minus'
7171
return _
7272

7373
case 'TypeCastExpression':
74-
return tSAsExpression(node.expression, toTs(node.typeAnnotation))
74+
return tsAsExpression(node.expression, toTs(node.typeAnnotation))
7575

7676
case 'TypeParameterDeclaration':
7777
let params = node.params.map(_ => {
7878
let d = (_ as any as TypeParameter).default
79-
let p = tSTypeParameter(
79+
let p = tsTypeParameter(
8080
hasBound(_) ? toTsType(_.bound.typeAnnotation) : undefined,
8181
d ? toTs(d) : undefined
8282
)
8383
p.name = _.name
8484
return p
8585
})
8686

87-
return tSTypeParameterDeclaration(params)
87+
return tsTypeParameterDeclaration(params)
8888

8989
case 'ClassImplements':
9090
case 'ClassProperty':
@@ -107,44 +107,46 @@ export function toTs(node: Flow | TSType): TSType {
107107
}
108108
}
109109

110-
export function toTsType(node: FlowTypeAnnotation): TSType {
110+
export function toTsType(node: FlowType): TSType {
111111
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))
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!))
116116
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 'NumericLiteralTypeAnnotation': 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 'TypeAnnotation': return tSTypeAnnotation(toTsType(node.typeAnnotation))
130-
case 'ObjectTypeAnnotation': return tSTypeLiteral([
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([
131130
...node.properties.map(_ => {
132-
let s = tSPropertySignature(_.key, tSTypeAnnotation(toTsType(_.value)))
133-
s.optional = _.optional
134-
return s
135-
// TODO: anonymous indexers
136-
// TODO: named indexers
137-
// TODO: call properties
138-
// TODO: variance
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
139141
})
140142
// ...node.indexers.map(_ => tSIndexSignature())
141143
])
142-
case 'UnionTypeAnnotation': return tSUnionType(node.types.map(toTsType))
143-
case 'VoidTypeAnnotation': return tSVoidKeyword()
144+
case 'UnionTypeAnnotation': return tsUnionType(node.types.map(toTsType))
145+
case 'VoidTypeAnnotation': return tsVoidKeyword()
144146
}
145147
}
146148

147-
function getId(node: FlowTypeAnnotation): Identifier {
149+
function getId(node: FlowType): Identifier {
148150
switch (node.type) {
149151
case 'GenericTypeAnnotation': return node.id
150152
default: throw ReferenceError('typeof query must reference a node that has an id')
@@ -156,7 +158,7 @@ function functionToTsType(node: FunctionTypeAnnotation): TSFunctionType {
156158
let typeParams = undefined
157159

158160
if (node.typeParameters) {
159-
typeParams = tSTypeParameterDeclaration(node.typeParameters.params.map(_ => {
161+
typeParams = tsTypeParameterDeclaration(node.typeParameters.params.map(_ => {
160162

161163
// TODO: How is this possible?
162164
if (isTSTypeParameter(_)) {
@@ -165,13 +167,13 @@ function functionToTsType(node: FunctionTypeAnnotation): TSFunctionType {
165167

166168
let constraint = _.bound ? toTs(_.bound) : undefined
167169
let default_ = _.default ? toTs(_.default) : undefined
168-
let param = tSTypeParameter(constraint, default_)
170+
let param = tsTypeParameter(constraint, default_)
169171
param.name = _.name
170172
return param
171173
}))
172174
}
173175

174-
let f = tSFunctionType(typeParams)
176+
let f = tsFunctionType(typeParams)
175177

176178
// Params
177179
if (node.params) {
@@ -189,7 +191,7 @@ function functionToTsType(node: FunctionTypeAnnotation): TSFunctionType {
189191
let id = identifier(name)
190192

191193
if (_.typeAnnotation) {
192-
id.typeAnnotation = tSTypeAnnotation(toTsType(_.typeAnnotation))
194+
id.typeAnnotation = tsTypeAnnotation(toTsType(_.typeAnnotation))
193195
}
194196

195197
return id
@@ -198,7 +200,7 @@ function functionToTsType(node: FunctionTypeAnnotation): TSFunctionType {
198200

199201
// Return type
200202
if (node.returnType) {
201-
f.typeAnnotation = tSTypeAnnotation(toTsType(node.returnType))
203+
f.typeAnnotation = tsTypeAnnotation(toTsType(node.returnType))
202204
}
203205

204206
return f

src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import generate from 'babel/packages/babel-generator/lib'
2-
import traverse, { Node, Visitor } from 'babel/packages/babel-traverse/lib'
3-
import { File } from 'babel/packages/babel-types/lib'
4-
import { parse } from 'babel/packages/babylon/lib'
1+
import { parse } from '@babel/babylon'
2+
import generate from '@babel/generator'
3+
import traverse, { Node, Visitor } from '@babel/traverse'
4+
import { File } from '@babel/types'
55
import { sync } from 'glob'
6-
import { dropWhile } from 'lodash'
6+
import { dropWhile, pullAt } from 'lodash'
77
import { EOL } from 'os'
88
import { relative, resolve } from 'path'
99

@@ -53,7 +53,7 @@ function stripAtFlowAnnotation(ast: File): File {
5353
if (leadingComments) {
5454
let index = leadingComments.findIndex(_ => _.value.trim() === '@flow')
5555
if (index > -1) {
56-
leadingComments.splice(index, 1)
56+
pullAt(leadingComments, index)
5757
}
5858
}
5959
return ast

src/rules/$Keys.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GenericTypeAnnotation, tSTypeOperator, tSTypeReference } from 'babel/packages/babel-types'
1+
import { GenericTypeAnnotation, tsTypeOperator, tsTypeReference } from '@babel/types'
22
import { addRule } from '../'
33

44
addRule('$Keys', () => ({
@@ -7,7 +7,7 @@ addRule('$Keys', () => ({
77
return
88
}
99
let { id } = (path.node.typeParameters.params[0] as GenericTypeAnnotation)
10-
let op = tSTypeOperator(tSTypeReference(id))
10+
let op = tsTypeOperator(tsTypeReference(id))
1111
path.replaceWith(op)
1212
}
1313
}))

src/rules/$ReadOnly.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { genericTypeAnnotation, identifier } from 'babel/packages/babel-types'
1+
import { genericTypeAnnotation, identifier } from '@babel/types'
22
import { addRule } from '../'
33

44
addRule('$ReadOnly', () => ({

src/rules/Bounds.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isTypeParameter, Node, TypeAnnotation, TypeParameter } from 'babel/packages/babel-types/lib'
1+
import { isTypeParameter, Node, TypeAnnotation, TypeParameter } from '@babel/types'
22
import { addRule } from '../'
33
import { toTs } from '../convert'
44

src/rules/Exact.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { objectTypeAnnotation } from 'babel/packages/babel-types'
1+
import { objectTypeAnnotation } from '@babel/types'
22
import { addRule } from '../'
33

44
addRule('Exact', warnings => ({

src/rules/Indexer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { identifier, objectTypeIndexer } from 'babel/packages/babel-types'
1+
import { identifier, objectTypeIndexer } from '@babel/types'
22
import { addRule } from '../'
33
import { generateFreeIdentifier } from '../utils'
44

src/rules/Maybe.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { genericTypeAnnotation, identifier, nullLiteralTypeAnnotation, unionTypeAnnotation } from 'babel/packages/babel-types'
1+
import { genericTypeAnnotation, identifier, nullLiteralTypeAnnotation, unionTypeAnnotation } from '@babel/types'
22
import { addRule } from '../'
33

44
addRule('Maybe', () => ({

0 commit comments

Comments
 (0)