Skip to content

Commit

Permalink
fix: default type imports/exports
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Jan 7, 2021
1 parent d1e4ef9 commit 4b53916
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/convert/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export class FileConversionContext {
ExportSpecifier: (path: NodePath<t.ExportSpecifier>) => {
if (getImportOrExportName(path.node.exported) === 'default') {
path.stop()
pathToConvert = path.get('exported')
pathToConvert = path.get('local')
}
},
})
Expand Down Expand Up @@ -224,18 +224,26 @@ export class FileConversionContext {
pathToConvert.parentPath.isExportSpecifier() &&
result.kind === 'type'
) {
const exportTypeSpecifier = pathToConvert.parentPath
.node as t.ExportSpecifier
if (result.converted.type !== 'Identifier') {
throw new NodeConversionError(
`need converted export to be an identifier`,
this.file,
pathToConvert
)
}
const exported =
getImportOrExportName(exportTypeSpecifier.exported) === 'default'
? this.getValidatorIdentifier('default')
: result.converted
pathToConvert.parentPath.parentPath.insertAfter(
t.exportNamedDeclaration(null, [
t.exportSpecifier(result.converted, result.converted),
t.exportSpecifier(result.converted, exported),
])
)
if (getImportOrExportName(exportTypeSpecifier.exported) === 'default')
return { converted: exported, kind: result.kind }
}
return result
}
Expand Down
38 changes: 38 additions & 0 deletions test/convertFlowType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,44 @@ describe(`convertFlowType`, function() {
}
)
})
it.only(`converts default type import that's indirectly exported`, async function() {
await integrationTest(
{
'/a': `
import {reify, type Type} from 'flow-runtime'
import type Foob from './foo'
const FooType = (reify: Type<Foob>)
`,
'/foo': `
type Foo = {|
foo: number
|}
export type {Foo as default}
`,
},
{
'/a': `
import {type default as Foob, defaultType as FoobType} from './foo'
import * as t from 'typed-validators'
const FooType = t.ref(() => FoobType)
`,
'/foo': `
import * as t from 'typed-validators'
type Foo = {|
foo: number
|}
const FooType = t.alias(
'Foo',
t.object({
foo: t.number(),
})
)
export type {Foo as default}
export {FooType as defaultType}
`,
}
)
})
it(`converts default class import`, async function() {
await integrationTest(
{
Expand Down

0 comments on commit 4b53916

Please sign in to comment.