Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support export-declaration #1079

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 82 additions & 3 deletions src/lib/converter/nodes/export.ts
@@ -1,19 +1,98 @@
import * as ts from 'typescript';

import { Reflection, ReflectionFlag, DeclarationReflection } from '../../models/index';
import { Reflection, ReflectionFlag, DeclarationReflection, ReflectionKind } from '../../models/index';
import { Context } from '../context';
import { Component, ConverterNodeComponent } from '../components';
import { createDeclaration } from '../factories';

@Component({name: 'node:export'})
export class ExportConverter extends ConverterNodeComponent<ts.ExportAssignment> {
/**
* List of supported TypeScript syntax kinds.
*/
supports: ts.SyntaxKind[] = [
ts.SyntaxKind.ExportAssignment
ts.SyntaxKind.ExportAssignment,
ts.SyntaxKind.ExportDeclaration
];

convert(context: Context, node: ts.ExportAssignment): Reflection {
private _convertExportAllDeclaration(context: Context, moduleSpecifier: ts.Expression): Reflection | undefined {
const symbol = context.checker.getSymbolAtLocation(moduleSpecifier);
if (!symbol) {
return context.scope;
}

const valueDeclaration = symbol.valueDeclaration;
if (!valueDeclaration) {
return context.scope;
}

if (!ts.isSourceFile(valueDeclaration)) {
return context.scope;
}

valueDeclaration.statements.forEach((statement) => {
this.owner.convertNode(context, statement);
});

return context.scope;
}

private _convertExportDeclaration(context: Context, node: ts.ExportDeclaration): Reflection | undefined {
if (node.moduleSpecifier && !node.exportClause) {
// export * from 'xxx';
return this._convertExportAllDeclaration(context, node.moduleSpecifier);
}

// export { xx, xx as yy };
node.exportClause!.elements.forEach((element) => {
// export { q as quat };
// q: propertyName
// quat: name
// export { quat };
// quat: name
const exportedSymbol = context.checker.getSymbolAtLocation(element.name);
if (!exportedSymbol) {
return;
}
const originalSymbol = context.checker.getAliasedSymbol(exportedSymbol);
if (!originalSymbol) {
return;
}
const declarations = originalSymbol.getDeclarations();
if (!declarations) {
return;
}
declarations.forEach((declaration) => {
if (ts.isSourceFile(declaration)) {
// import * as xx from 'xx';
// export {xx};
const reflection = createDeclaration(context, element.name, ReflectionKind.Module, exportedSymbol.name);
if (reflection) {
reflection.setFlag(ReflectionFlag.Exported);
context.withScope(reflection, () => {
declaration.statements.forEach((statement) => {
this.owner.convertNode(context, statement);
});
});
}
} else {
const reflection = this.owner.convertNode(context, declaration);
if (reflection) {
reflection.setFlag(ReflectionFlag.Exported);
reflection.name = exportedSymbol.name;
}
}
});
});

return context.scope;
}

convert(context: Context, node: ts.ExportAssignment | ts.ExportDeclaration): Reflection | undefined {
if (ts.isExportDeclaration(node)) {
return this._convertExportDeclaration(context, node);
}

let symbol: ts.Symbol | undefined;

// default export
Expand Down