Skip to content

Commit 3fb3e63

Browse files
benjiebcherny
authored andcommitted
Autoformat codebase with prettier (#13)
* Add prettier * Autoformat with 'yarn prettier:fix' * Assert prettier
1 parent 06d18be commit 3fb3e63

File tree

15 files changed

+218
-107
lines changed

15 files changed

+218
-107
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
"build": "tsc",
1515
"build:watch": "tsc -w",
1616
"clean": "rm -rf ./dist",
17-
"lint": "tslint src/**/*.ts",
17+
"lint": "tslint src/**/*.ts && npm run prettier:check",
18+
"prettier": "prettier 'src/**/*.[tj]s'",
19+
"prettier:fix": "yarn prettier --write",
20+
"prettier:check": "yarn prettier -l",
1821
"prepublishOnly": "npm run clean && npm run lint && npm run build -- -d",
1922
"pretest": "npm run build",
2023
"tdd": "concurrently -k 'npm run build:watch' 'npm run test:watch'",
@@ -38,6 +41,7 @@
3841
"ava": "^0.24.0",
3942
"concurrently": "^3.5.1",
4043
"flow-bin": "^0.59.0",
44+
"prettier": "^1.15.3",
4145
"tslint": "^5.8.0",
4246
"typescript": "^2.6.2"
4347
},

prettier.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
printWidth: 80,
3+
semi: false,
4+
singleQuote: true,
5+
trailingComma: 'none',
6+
}

src/cli.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ import { resolve } from 'path'
66
import stdin = require('stdin')
77
import { compile } from './index'
88

9-
main(minimist(process.argv.slice(2), {
10-
alias: {
11-
help: ['h'],
12-
input: ['i'],
13-
output: ['o']
14-
}
15-
}))
9+
main(
10+
minimist(process.argv.slice(2), {
11+
alias: {
12+
help: ['h'],
13+
input: ['i'],
14+
output: ['o']
15+
}
16+
})
17+
)
1618

1719
async function main(argv: minimist.ParsedArgs) {
18-
1920
if (argv.help) {
2021
printHelp()
2122
process.exit(0)
@@ -32,7 +33,6 @@ async function main(argv: minimist.ParsedArgs) {
3233
process.stderr.write(e.message)
3334
process.exit(1)
3435
}
35-
3636
}
3737

3838
function readInput(argIn?: string) {
@@ -58,7 +58,7 @@ function printHelp() {
5858
const pkg = require('../../package.json')
5959

6060
process.stdout.write(
61-
`
61+
`
6262
${pkg.name} ${pkg.version}
6363
Usage: flow2ts [--input, -i] [IN_FILE] [--output, -o] [OUT_FILE]
6464

src/convert.ts

Lines changed: 123 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,48 @@
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'
241
import { generateFreeIdentifier } from './utils'
342

443
// TODO: Add overloads
544
export function toTs(node: Flow | TSType): TSType {
645
switch (node.type) {
7-
846
// TS types
947
// TODO: Why does tsTs get called with TSTypes? It should only get called with Flow types.
1048
case 'TSAnyKeyword':
@@ -75,7 +113,7 @@ export function toTs(node: Flow | TSType): TSType {
75113

76114
case 'TypeParameterDeclaration':
77115
let params = node.params.map(_ => {
78-
let d = (_ as any as TypeParameter).default
116+
let d = ((_ as any) as TypeParameter).default
79117
let p = tsTypeParameter(
80118
hasBound(_) ? toTsType(_.bound.typeAnnotation) : undefined,
81119
d ? toTs(d) : undefined
@@ -109,76 +147,108 @@ export function toTs(node: Flow | TSType): TSType {
109147

110148
export function toTsType(node: FlowType): TSType {
111149
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()
146211
}
147212
}
148213

149214
function getId(node: FlowType): Identifier {
150215
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')
153220
}
154221
}
155222

156223
function functionToTsType(node: FunctionTypeAnnotation): TSFunctionType {
157-
158224
let typeParams = undefined
159225

160226
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+
}
167233

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+
)
174241
}
175242

176243
let f = tsFunctionType(typeParams)
177244

178245
// Params
179246
if (node.params) {
180247
// 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)
182252
f.parameters = node.params.map(_ => {
183253
let name = _.name && _.name.name
184254

src/index.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,38 @@ export function addRule(ruleName: string, rule: Rule) {
2020
}
2121

2222
export async function compile(code: string, filename: string) {
23-
2423
let [warnings, ast] = await convert(
25-
parse(code, { plugins: ['classProperties', 'flow', 'objectRestSpread'], sourceType: 'module' })
24+
parse(code, {
25+
plugins: ['classProperties', 'flow', 'objectRestSpread'],
26+
sourceType: 'module'
27+
})
2628
)
2729

2830
warnings.forEach(([message, issueURL, line, column]) => {
29-
console.log(`Warning: ${message} (at ${relative(__dirname, filename)}: line ${line}, column ${column}). See ${issueURL}`)
31+
console.log(
32+
`Warning: ${message} (at ${relative(
33+
__dirname,
34+
filename
35+
)}: line ${line}, column ${column}). See ${issueURL}`
36+
)
3037
})
3138

32-
return addTrailingSpace(trimLeadingNewlines(generate(stripAtFlowAnnotation(ast)).code))
39+
return addTrailingSpace(
40+
trimLeadingNewlines(generate(stripAtFlowAnnotation(ast)).code)
41+
)
3342
}
3443

3544
/**
3645
* @internal
3746
*/
3847
export async function convert<T extends Node>(ast: T): Promise<[Warning[], T]> {
39-
4048
// load rules directory
41-
await Promise.all(sync(resolve(__dirname, './rules/*.js')).map(_ => import(_)))
49+
await Promise.all(
50+
sync(resolve(__dirname, './rules/*.js')).map(_ => import(_))
51+
)
4252

4353
let warnings: Warning[] = []
44-
rules.forEach(visitor =>
45-
traverse(ast, visitor(warnings))
46-
)
54+
rules.forEach(visitor => traverse(ast, visitor(warnings)))
4755

4856
return [warnings, ast]
4957
}

src/rules/$Exact.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { addRule } from '../'
22

33
addRule('$Exact', warnings => ({
44
GenericTypeAnnotation(path) {
5-
65
if (path.node.id.name !== '$Exact') {
76
return
87
}

src/rules/$Keys.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import { GenericTypeAnnotation, tsTypeOperator, tsTypeReference } from '@babel/types'
1+
import {
2+
GenericTypeAnnotation,
3+
tsTypeOperator,
4+
tsTypeReference
5+
} from '@babel/types'
26
import { addRule } from '../'
37

48
addRule('$Keys', () => ({
59
GenericTypeAnnotation(path) {
610
if (path.node.id.name !== '$Keys') {
711
return
812
}
9-
let { id } = (path.node.typeParameters.params[0] as GenericTypeAnnotation)
13+
let { id } = path.node.typeParameters.params[0] as GenericTypeAnnotation
1014
let op = tsTypeOperator(tsTypeReference(id))
1115
path.replaceWith(op)
1216
}

src/rules/$ReadOnly.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ import { addRule } from '../'
33

44
addRule('$ReadOnly', () => ({
55
GenericTypeAnnotation(path) {
6-
76
if (path.node.id.name !== '$ReadOnly') {
87
return
98
}
109

11-
path.replaceWith(genericTypeAnnotation(
12-
identifier('Readonly'),
13-
path.node.typeParameters
14-
))
10+
path.replaceWith(
11+
genericTypeAnnotation(identifier('Readonly'), path.node.typeParameters)
12+
)
1513
}
1614
}))

src/rules/Bounds.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { isTypeParameter, Node, TypeAnnotation, TypeParameter } from '@babel/types'
1+
import {
2+
isTypeParameter,
3+
Node,
4+
TypeAnnotation,
5+
TypeParameter
6+
} from '@babel/types'
27
import { addRule } from '../'
38
import { toTs } from '../convert'
49

510
addRule('Bounds', () => ({
611
TypeParameterDeclaration(path) {
7-
812
if (path.node.params.every(_ => !hasBound(_))) {
913
return
1014
}

0 commit comments

Comments
 (0)