Skip to content

Commit 34c78ce

Browse files
authored
Merge pull request #138 from SebastienGllmt/fix-export-list
bugfix: export list of types
2 parents 4cf4788 + 0f4e94f commit 34c78ce

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

src/__tests__/__snapshots__/type-exports.spec.ts.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`should handle export list syntax 1`] = `
4+
"declare type ComplexType =
5+
| {
6+
type: number,
7+
...
8+
}
9+
| {
10+
type: string,
11+
...
12+
};
13+
export type { ComplexType };
14+
declare var foo: 5;
15+
declare export { foo };
16+
"
17+
`;
18+
319
exports[`should handle exported types 1`] = `
420
"export type FactoryOrValue<T> = T | (() => T);
521
export type Maybe<T> =

src/__tests__/type-exports.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,19 @@ export type Maybe<T> = {type: 'just', value: T} | {type: 'nothing'}
1010
expect(beautify(result)).toMatchSnapshot();
1111
expect(result).toBeValidFlowTypeDeclarations();
1212
});
13+
14+
it("should handle export list syntax", () => {
15+
const ts = `
16+
declare type ComplexType = {
17+
type: number
18+
} | {
19+
type: string
20+
};
21+
export type { ComplexType };
22+
const foo = 5;
23+
export { foo };
24+
`;
25+
const result = compiler.compileDefinitionString(ts, { quiet: true });
26+
expect(beautify(result)).toMatchSnapshot();
27+
expect(result).toBeValidFlowTypeDeclarations();
28+
});

src/nodes/export-declaration.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,21 @@ export default class ExportDeclaration extends Node<ExportDeclarationType> {
1919
if (this.raw.exportClause) {
2020
// @ts-expect-error todo(flow->ts)
2121
const elements = this.raw.exportClause.elements;
22+
const isTypeImport = this.raw.isTypeOnly;
23+
2224
let specifier = "";
2325
if (this.raw.moduleSpecifier)
2426
specifier = `from '${this.raw.moduleSpecifier.text}';`;
25-
return `declare export {
26-
${elements.map(node => printers.node.printType(node))}
27-
}${specifier}\n`;
27+
28+
const generateOutput = prefix => {
29+
return `${prefix} {
30+
${elements.map(node => printers.node.printType(node))}
31+
}${specifier}\n`;
32+
};
33+
34+
return isTypeImport
35+
? generateOutput(`export type`)
36+
: generateOutput(`declare export`);
2837
} else {
2938
return `declare export * from '${this.raw.moduleSpecifier.text}';\n`;
3039
}

0 commit comments

Comments
 (0)