From 5b870cc094562194dbd8b2f6a964b729056fc8a1 Mon Sep 17 00:00:00 2001 From: pgonzal Date: Sun, 23 Dec 2018 13:03:53 -0800 Subject: [PATCH] Fix up ApiModelGenerator and ReviewFileGenerator to handle reexported declarations (i.e. where both CollectorEntity.exported=true and AstSymbol.imported=true) --- .../src/generators/ApiModelGenerator.ts | 8 ++++- .../src/generators/ReviewFileGenerator.ts | 31 ++++++++++++++----- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/apps/api-extractor/src/generators/ApiModelGenerator.ts b/apps/api-extractor/src/generators/ApiModelGenerator.ts index af93271619b..f61bc211b04 100644 --- a/apps/api-extractor/src/generators/ApiModelGenerator.ts +++ b/apps/api-extractor/src/generators/ApiModelGenerator.ts @@ -64,7 +64,13 @@ export class ApiModelGenerator { for (const entity of this._collector.entities) { for (const astDeclaration of entity.astSymbol.astDeclarations) { if (entity.exported) { - this._processDeclaration(astDeclaration, entity.nameForEmit, apiEntryPoint); + if (!entity.astSymbol.imported) { + this._processDeclaration(astDeclaration, entity.nameForEmit, apiEntryPoint); + } else { + // TODO: Figure out how to represent reexported definitions. Basically we need to introduce a new + // ApiItem subclass for "export alias", similar to a type alias, but representing declarations of the + // form "export { X } from 'external-package'". We can also use this to solve GitHub issue #950. + } } } } diff --git a/apps/api-extractor/src/generators/ReviewFileGenerator.ts b/apps/api-extractor/src/generators/ReviewFileGenerator.ts index f82915a3533..43bd0f6f777 100644 --- a/apps/api-extractor/src/generators/ReviewFileGenerator.ts +++ b/apps/api-extractor/src/generators/ReviewFileGenerator.ts @@ -14,6 +14,7 @@ import { DeclarationMetadata } from '../collector/DeclarationMetadata'; import { SymbolMetadata } from '../collector/SymbolMetadata'; import { ReleaseTag } from '../aedoc/ReleaseTag'; import { Text, InternalError } from '@microsoft/node-core-library'; +import { AstImport } from '../analyzer/AstImport'; export class ReviewFileGenerator { /** @@ -35,15 +36,31 @@ export class ReviewFileGenerator { for (const entity of collector.entities) { if (entity.exported) { - // Emit all the declarations for this entry - for (const astDeclaration of entity.astSymbol.astDeclarations || []) { + if (!entity.astSymbol.astImport) { + // Emit all the declarations for this entry + for (const astDeclaration of entity.astSymbol.astDeclarations || []) { - output.append(ReviewFileGenerator._getAedocSynopsis(collector, astDeclaration)); + output.append(ReviewFileGenerator._getAedocSynopsis(collector, astDeclaration)); - const span: Span = new Span(astDeclaration.declaration); - ReviewFileGenerator._modifySpan(collector, span, entity, astDeclaration); - span.writeModifiedText(output); - output.append('\n\n'); + const span: Span = new Span(astDeclaration.declaration); + ReviewFileGenerator._modifySpan(collector, span, entity, astDeclaration); + span.writeModifiedText(output); + output.append('\n\n'); + } + } else { + // This definition is reexported from another package, so write it as an "export" line + // In general, we don't report on external packages; if that's important we assume API Extractor + // would be enabled for the upstream project. But see GitHub issue #896 for a possible exception. + const astImport: AstImport = entity.astSymbol.astImport; + + if (astImport.exportName === '*') { + output.append(`export * as ${entity.nameForEmit}`); + } else if (entity.nameForEmit !== astImport.exportName) { + output.append(`export { ${astImport.exportName} as ${entity.nameForEmit} }`); + } else { + output.append(`export { ${astImport.exportName} }`); + } + output.append(` from '${astImport.modulePath}';\n`); } } }