-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: many more fixes, close to being usable on our projects
- Loading branch information
1 parent
b92760a
commit 90c16de
Showing
12 changed files
with
403 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import * as fs from 'fs-extra' | ||
import resolve from 'resolve' | ||
import { parse } from '@babel/parser' | ||
import { promisify } from 'util' | ||
import { ConversionContext } from './convert/index' | ||
import printDiff from 'print-diff' | ||
import * as recast from 'recast' | ||
import * as t from '@babel/types' | ||
import * as Path from 'path' | ||
import yargs from 'yargs' | ||
import prettier from 'prettier' | ||
|
||
const { _: files } = yargs.argv | ||
|
||
async function parseFile(file: string): Promise<t.File> { | ||
return recast.parse(await fs.readFile(file, 'utf8'), { | ||
parser: { | ||
parse: (code: string): any => | ||
parse(code, { | ||
plugins: [ | ||
/\.tsx?$/.test(file) ? 'typescript' : ['flow', { all: true }], | ||
'jsx', | ||
'classProperties', | ||
'exportDefaultFrom', | ||
'asyncGenerators', | ||
'objectRestSpread', | ||
'optionalChaining', | ||
'exportDefaultFrom', | ||
'exportNamespaceFrom', | ||
'dynamicImport', | ||
'nullishCoalescingOperator', | ||
'bigint' as any, | ||
], | ||
tokens: true, | ||
sourceType: 'unambiguous', | ||
}), | ||
}, | ||
}) | ||
} | ||
|
||
async function go() { | ||
const context = new ConversionContext({ | ||
parseFile, | ||
resolve: promisify(resolve) as any, | ||
}) | ||
try { | ||
for (const file of files) { | ||
if (typeof file !== 'string') continue | ||
// eslint-disable-next-line no-console | ||
console.log(file) | ||
await context.forFile(Path.resolve(file)).processFile() | ||
} | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error(error.stack) | ||
} | ||
for (const [file, ast] of context.fileASTs.entries()) { | ||
const prettierOptions = { | ||
parser: /\.tsx?$/.test(file) ? 'typescript' : 'babel', | ||
} | ||
const printed = prettier.format(recast.print(ast).code, prettierOptions) | ||
const orig = prettier.format( | ||
await fs.readFile(file, 'utf8'), | ||
prettierOptions | ||
) | ||
if (orig === printed) { | ||
// eslint-disable-next-line no-console | ||
console.log(file) | ||
continue | ||
} | ||
// eslint-disable-next-line no-console | ||
console.log(`\n\n${file}\n======================================\n\n`) | ||
printDiff(orig, printed) | ||
} | ||
} | ||
|
||
go() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as t from '@babel/types' | ||
import { NodePath } from '@babel/traverse' | ||
import template from '@babel/template' | ||
import { FileConversionContext } from './index' | ||
import getTypeParams from './getTypeParams' | ||
|
||
const templates = { | ||
array: template.expression`T.array(TYPE)`, | ||
} | ||
|
||
export default async function convertUtilityFlowType( | ||
context: FileConversionContext, | ||
path: NodePath<t.GenericTypeAnnotation> | ||
): Promise<t.Expression | void> { | ||
const id = path.get('id') | ||
if (!id.isIdentifier()) return | ||
switch (id.node.name) { | ||
case '$ReadOnlyArray': | ||
case 'Array': { | ||
const [elementType] = getTypeParams(context, path, 1) | ||
return templates.array({ | ||
T: await context.importT(), | ||
TYPE: await context.convert(elementType), | ||
}) | ||
} | ||
case '$ReadOnly': | ||
return await context.convert(getTypeParams(context, path, 1)[0]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import * as t from '@babel/types' | ||
import { NodePath } from '@babel/traverse' | ||
import { FileConversionContext } from './index' | ||
import NodeConversionError from '../NodeConversionError' | ||
|
||
export default function getTypeParams( | ||
context: FileConversionContext, | ||
path: NodePath<t.GenericTypeAnnotation>, | ||
required: boolean | number = true | ||
): NodePath<t.FlowType>[] { | ||
const typeParameters = path.get('typeParameters') | ||
if (!typeParameters.isTypeParameterInstantiation()) { | ||
if (!required) return [] | ||
throw new NodeConversionError( | ||
`Missing required type parameter(s)`, | ||
context.file, | ||
path | ||
) | ||
} | ||
const params = (typeParameters as NodePath<t.TypeParameterInstantiation>).get( | ||
'params' | ||
) | ||
if ( | ||
typeof required === 'number' | ||
? params.length !== required | ||
: required | ||
? params.length | ||
: false | ||
) { | ||
throw new NodeConversionError( | ||
`Missing required type parameter(s)`, | ||
context.file, | ||
path | ||
) | ||
} | ||
return params | ||
} |
Oops, something went wrong.