Skip to content

Commit 31bbac5

Browse files
authored
Merge pull request #131 from aohua/aohua/support-namespace-export
Add support for NamespaceExport
2 parents b89c994 + b076d4d commit 31bbac5

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ declare export { module as newModule };
77
declare export { GeneratorOptions } from \\"@babel/generator\\";
88
declare export { GeneratorOptions as NewGeneratorOptions } from \\"@babel/generator\\";
99
declare export * from \\"typescript\\"
10+
declare export * as t from \\"@babel/types\\";
1011
"
1112
`;
1213

src/__tests__/exports.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ export { module as newModule }
99
export { GeneratorOptions } from "@babel/generator";
1010
export { GeneratorOptions as NewGeneratorOptions } from "@babel/generator";
1111
export * from 'typescript';
12+
export * as t from "@babel/types";
1213
//enable when typescript supports
1314
//export traverse, { Visitor, NodePath } from "@babel/traverse";
14-
//export template from "@babel/template";
15-
//export * as t from "@babel/types";`;
15+
//export template from "@babel/template";`;
1616
const result = compiler.compileDefinitionString(ts, { quiet: true });
1717
expect(beautify(result)).toMatchSnapshot();
1818
expect(result).not.toBeValidFlowTypeDeclarations(); // cannot-resolve-module

src/nodes/export-declaration.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { RawNode } from "./node";
22
import type { Expression, ExportDeclaration as RawExport } from "typescript";
3+
import { isNamespaceExport } from "typescript";
34
import * as printers from "../printers";
45
import Node from "./node";
56

@@ -17,14 +18,17 @@ export default class ExportDeclaration extends Node<ExportDeclarationType> {
1718
print(): string {
1819
//TODO: move to printers
1920
if (this.raw.exportClause) {
20-
// @ts-expect-error todo(flow->ts)
21-
const elements = this.raw.exportClause.elements;
2221
const isTypeImport = this.raw.isTypeOnly;
2322

2423
let specifier = "";
2524
if (this.raw.moduleSpecifier)
2625
specifier = `from '${this.raw.moduleSpecifier.text}';`;
2726

27+
if (isNamespaceExport(this.raw.exportClause)) {
28+
return `declare export * as ${this.raw.exportClause.name.escapedText} ${specifier}\n`;
29+
}
30+
const elements = this.raw.exportClause.elements;
31+
2832
const generateOutput = prefix => {
2933
return `${prefix} {
3034
${elements.map(node => printers.node.printType(node))}

src/parse/ast.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ export const parseNameFromNode = (node: RawNode): string => {
3737
names.push(parseNameFromNode(child));
3838
});
3939
return names.join(",");
40+
} else if (ts.isIdentifier(node)) {
41+
/*
42+
* Parse name for NamespaceExport, please refer to the PR: https://github.com/joarwilk/flowgen/pull/131
43+
* Based on the test, seems it only affects NamespaceExport
44+
* May need someone to update the implementation later if there are any issues
45+
*/
46+
if (node.escapedText && typeof node.escapedText === "string") {
47+
return node.escapedText;
48+
}
4049
}
4150
switch (node.kind) {
4251
case ts.SyntaxKind.FunctionDeclaration:

0 commit comments

Comments
 (0)