diff --git a/src/convert/index.ts b/src/convert/index.ts index dfe0369..d0d5448 100644 --- a/src/convert/index.ts +++ b/src/convert/index.ts @@ -193,7 +193,7 @@ export class FileConversionContext { ExportSpecifier: (path: NodePath) => { if (getImportOrExportName(path.node.exported) === 'default') { path.stop() - pathToConvert = path.get('exported') + pathToConvert = path.get('local') } }, }) @@ -224,6 +224,8 @@ 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`, @@ -231,11 +233,17 @@ export class FileConversionContext { 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 } diff --git a/test/convertFlowType.ts b/test/convertFlowType.ts index cabd743..4995438 100644 --- a/test/convertFlowType.ts +++ b/test/convertFlowType.ts @@ -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) + `, + '/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( {