Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Upgrade TypeScript, Flow, Babel Deps, Fix Some Errors #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
"test:watch": "ava -w"
},
"dependencies": {
"@babel/generator": "7.0.0-beta.38",
"@babel/traverse": "7.0.0-beta.38",
"@babel/types": "7.0.0-beta.38",
"@babel/generator": "^7.4.0",
"@babel/traverse": "^7.4.0",
"@babel/types": "^7.4.0",
"ava": "^1.4.1",
"glob": "^7.1.2",
"lodash": "^4.17.4",
"mz": "^2.7.0"
Expand All @@ -38,12 +39,11 @@
"@types/lodash": "^4.14.86",
"@types/minimist": "^1.2.0",
"@types/mz": "^0.0.32",
"ava": "^0.24.0",
"concurrently": "^3.5.1",
"flow-bin": "^0.59.0",
"flow-bin": "0.81.0",
"prettier": "^1.15.3",
"tslint": "^5.8.0",
"typescript": "^2.6.2"
"typescript": "^3.3.4000"
},
"ava": {
"files": [
Expand Down
42 changes: 23 additions & 19 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function toTs(node: Flow | TSType): TSType {
case 'TSSymbolKeyword':
case 'TSThisType':
case 'TSTupleType':
case 'TSTypeAnnotation':
case ('TSTypeAnnotation' as any):
case 'TSTypeLiteral':
case 'TSTypeOperator':
case 'TSTypePredicate':
Expand All @@ -73,10 +73,10 @@ export function toTs(node: Flow | TSType): TSType {
case 'TSUndefinedKeyword':
case 'TSUnionType':
case 'TSVoidKeyword':
case 'TSTypeParameterDeclaration':
case 'TSAsExpression':
case 'TSPropertySignature':
return node
case ('TSTypeParameterDeclaration' as any):
case ('TSAsExpression' as any):
case ('TSPropertySignature' as any):
return node as TSType

// Flow types
case 'AnyTypeAnnotation':
Expand All @@ -89,7 +89,7 @@ export function toTs(node: Flow | TSType): TSType {
case 'MixedTypeAnnotation':
case 'NullableTypeAnnotation':
case 'NullLiteralTypeAnnotation':
case 'NumericLiteralTypeAnnotation':
case ('NumericLiteralTypeAnnotation' as any):
case 'NumberTypeAnnotation':
case 'StringLiteralTypeAnnotation':
case 'StringTypeAnnotation':
Expand All @@ -100,16 +100,16 @@ export function toTs(node: Flow | TSType): TSType {
case 'ObjectTypeAnnotation':
case 'UnionTypeAnnotation':
case 'VoidTypeAnnotation':
return toTsType(node)
return toTsType(node as FlowType)

case 'ObjectTypeProperty':
let _ = tsPropertySignature(node.key, tsTypeAnnotation(toTs(node.value)))
_.optional = node.optional
_.readonly = node.variance && node.variance.kind === 'minus'
return _
return ((_ as unknown) as TSType)

case 'TypeCastExpression':
return tsAsExpression(node.expression, toTs(node.typeAnnotation))
return ((tsAsExpression(node.expression, toTs(node.typeAnnotation)) as unknown) as TSType)

case 'TypeParameterDeclaration':
let params = node.params.map(_ => {
Expand All @@ -122,17 +122,17 @@ export function toTs(node: Flow | TSType): TSType {
return p
})

return tsTypeParameterDeclaration(params)
return ((tsTypeParameterDeclaration(params) as unknown) as TSType)

case 'ClassImplements':
case 'ClassProperty':
case ('ClassProperty' as any):
case 'DeclareClass':
case 'DeclareFunction':
case 'DeclareInterface':
case 'DeclareModule':
case 'DeclareTypeAlias':
case 'DeclareVariable':
case 'ExistentialTypeParam':
case ('ExistentialTypeParam' as any):
case 'FunctionTypeParam':
case 'InterfaceExtends':
case 'InterfaceDeclaration':
Expand All @@ -142,6 +142,8 @@ export function toTs(node: Flow | TSType): TSType {
case 'ObjectTypeIndexer':
case 'QualifiedTypeIdentifier':
throw 'wut'
default:
throw `unexpected type ${node.type}`
}
}

Expand All @@ -158,7 +160,7 @@ export function toTsType(node: FlowType): TSType {
case 'FunctionTypeAnnotation':
return functionToTsType(node)
case 'GenericTypeAnnotation':
return tsTypeReference(node.id)
return tsTypeReference(node.id as any)
case 'IntersectionTypeAnnotation':
return tsIntersectionType(node.types.map(toTsType))
case 'MixedTypeAnnotation':
Expand Down Expand Up @@ -192,29 +194,31 @@ export function toTsType(node: FlowType): TSType {
return _
}
let s = tsPropertySignature(
_.key,
tsTypeAnnotation(toTsType(_.value))
(_ as any).key,
tsTypeAnnotation(toTsType((_ as any).value))
)
s.optional = _.optional
s.optional = (_ as any).optional
return s
// TODO: anonymous indexers
// TODO: named indexers
// TODO: call properties
// TODO: variance
})
// ...node.indexers.map(_ => tSIndexSignature())
])
] as any)
case 'UnionTypeAnnotation':
return tsUnionType(node.types.map(toTsType))
case 'VoidTypeAnnotation':
return tsVoidKeyword()
default:
throw `unexpected type ${node.type}`
}
}

function getId(node: FlowType): Identifier {
switch (node.type) {
case 'GenericTypeAnnotation':
return node.id
return node.id as Identifier
default:
throw ReferenceError('typeof query must reference a node that has an id')
}
Expand All @@ -240,7 +244,7 @@ function functionToTsType(node: FunctionTypeAnnotation): TSFunctionType {
)
}

let f = tsFunctionType(typeParams)
let f = tsFunctionType(typeParams, [])

// Params
if (node.params) {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/$Exact.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { addRule } from '../'

addRule('$Exact', warnings => ({
GenericTypeAnnotation(path) {
GenericTypeAnnotation(path: any) {
if (path.node.id.name !== '$Exact') {
return
}
Expand Down
4 changes: 2 additions & 2 deletions src/rules/$Keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import {
import { addRule } from '../'

addRule('$Keys', () => ({
GenericTypeAnnotation(path) {
GenericTypeAnnotation(path: any) {
if (path.node.id.name !== '$Keys') {
return
}
let { id } = path.node.typeParameters.params[0] as GenericTypeAnnotation
let op = tsTypeOperator(tsTypeReference(id))
let op = tsTypeOperator(tsTypeReference(id as any))
path.replaceWith(op)
}
}))
2 changes: 1 addition & 1 deletion src/rules/$ReadOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { genericTypeAnnotation, identifier } from '@babel/types'
import { addRule } from '../'

addRule('$ReadOnly', () => ({
GenericTypeAnnotation(path) {
GenericTypeAnnotation(path: any) {
if (path.node.id.name !== '$ReadOnly') {
return
}
Expand Down
4 changes: 2 additions & 2 deletions src/rules/Bounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { addRule } from '../'
import { toTs } from '../convert'

addRule('Bounds', () => ({
TypeParameterDeclaration(path) {
if (path.node.params.every(_ => !hasBound(_))) {
TypeParameterDeclaration(path: any) {
if (path.node.params.every((_: any) => !hasBound(_))) {
return
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/Casting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { addRule } from '../'
import { toTs } from '../convert'

addRule('Casting', () => ({
TypeCastExpression(path) {
TypeCastExpression(path: any) {
path.replaceWith(toTs(path.node))
}
}))
2 changes: 1 addition & 1 deletion src/rules/Exact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { objectTypeAnnotation } from '@babel/types'
import { addRule } from '../'

addRule('Exact', warnings => ({
ObjectTypeAnnotation(path) {
ObjectTypeAnnotation(path: any) {
if ((path.node as any).exact) {
warnings.push([
`Exact types can't be expressed in TypeScript`,
Expand Down
2 changes: 1 addition & 1 deletion src/rules/Functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { addRule } from '../'
import { toTs } from '../convert'

addRule('Functions', () => ({
FunctionTypeAnnotation(path) {
FunctionTypeAnnotation(path: any) {
path.replaceWith(toTs(path.node))
}
}))
2 changes: 1 addition & 1 deletion src/rules/Indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { addRule } from '../'
import { generateFreeIdentifier } from '../utils'

addRule('Indexer', () => ({
ObjectTypeIndexer(path) {
ObjectTypeIndexer(path: any) {
if (path.node.id !== null) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/Maybe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { addRule } from '../'

addRule('Maybe', () => ({
NullableTypeAnnotation(path) {
NullableTypeAnnotation(path: any) {
path.replaceWith(
unionTypeAnnotation([
(path.node as any).typeAnnotation,
Expand Down
2 changes: 1 addition & 1 deletion src/rules/Mixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { objectTypeAnnotation } from '@babel/types'
import { addRule } from '../'

addRule('Mixed', () => ({
MixedTypeAnnotation(path) {
MixedTypeAnnotation(path: any) {
path.replaceWith(objectTypeAnnotation([]))
}
}))
2 changes: 1 addition & 1 deletion src/rules/Opaque.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { typeAlias } from '@babel/types'
import { addRule } from '../'

addRule('Opaque', warnings => ({
enter(path) {
enter(path: any) {
if (path.type === 'OpaqueType') {
warnings.push([
`Opaque types can't be expressed in TypeScript`,
Expand Down
2 changes: 1 addition & 1 deletion src/rules/TypeImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { importDeclaration } from '@babel/types'
import { addRule } from '../'

addRule('TypeImport', () => ({
ImportDeclaration(path) {
ImportDeclaration(path: any) {
if ((path as any).node.importKind === 'type') {
path.replaceWith(
importDeclaration(path.node.specifiers, path.node.source)
Expand Down
2 changes: 1 addition & 1 deletion src/rules/Undefined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { genericTypeAnnotation, identifier } from '@babel/types'
import { addRule } from '../index'

addRule('Undefined', () => ({
VoidTypeAnnotation(path) {
VoidTypeAnnotation(path: any) {
path.replaceWith(genericTypeAnnotation(identifier('undefined')))
}
}))
2 changes: 1 addition & 1 deletion src/rules/Variance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { addRule } from '../'
import { toTs } from '../convert'

addRule('Variance', warnings => ({
ObjectTypeProperty(path) {
ObjectTypeProperty(path: any) {
if (path.node.variance && path.node.variance.kind === 'plus') {
warnings.push([
`Contravariance can't be expressed in TypeScript`,
Expand Down