Skip to content

Commit

Permalink
fix: avoid duplicate AstImportInternal entity; add unsupported case f…
Browse files Browse the repository at this point in the history
…or star export inside namespace import module
  • Loading branch information
adventure-yunfei committed Feb 16, 2020
1 parent f6d9605 commit 54a3e15
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
16 changes: 11 additions & 5 deletions apps/api-extractor/src/analyzer/ExportAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class ExportAnalyzer {
private readonly _importableAmbientSourceFiles: Set<ts.SourceFile> = new Set<ts.SourceFile>();

private readonly _astImportsByKey: Map<string, AstImport> = new Map<string, AstImport>();
private readonly _astImportInternalsByAstModule: Map<AstModule, AstImportInternal> = new Map<AstModule, AstImportInternal>();

public constructor(program: ts.Program, typeChecker: ts.TypeChecker, bundledPackageNames: Set<string>,
astSymbolTable: IAstSymbolTable) {
Expand Down Expand Up @@ -452,11 +453,16 @@ export class ExportAnalyzer {

if (externalModulePath === undefined) {
const astModule: AstModule = this._fetchSpecifierAstModule(importDeclaration, declarationSymbol);
return new AstImportInternal({
importKind: AstImportInternalKind.StarImport,
exportName: declarationSymbol.name,
astModule: astModule
});
let astImportInternal: AstImportInternal | undefined = this._astImportInternalsByAstModule.get(astModule);
if (!astImportInternal) {
astImportInternal = new AstImportInternal({
importKind: AstImportInternalKind.StarImport,
exportName: declarationSymbol.name,
astModule: astModule
});
this._astImportInternalsByAstModule.set(astModule, astImportInternal);
}
return astImportInternal;
}

// Here importSymbol=undefined because {@inheritDoc} and such are not going to work correctly for
Expand Down
12 changes: 7 additions & 5 deletions apps/api-extractor/src/generators/DtsRollupGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { StringWriter } from './StringWriter';
import { DtsEmitHelpers } from './DtsEmitHelpers';
import { DeclarationMetadata } from '../collector/DeclarationMetadata';
import { AstImportInternal } from '../analyzer/AstImportInternal';
import { AstModuleExportInfo } from '../analyzer/AstModule';

/**
* Used with DtsRollupGenerator.writeTypingsFile()
Expand Down Expand Up @@ -133,10 +134,7 @@ export class DtsRollupGenerator {
}
}
} else if (entity.astEntity instanceof AstImportInternal) {
if (!entity.astEntity.astModule.astModuleExportInfo) {
// This should never happen
throw new InternalError('local imported module has not been parsed.');
}
const astModuleExportInfo: AstModuleExportInfo = collector.astSymbolTable.fetchAstModuleExportInfo(entity.astEntity.astModule);
if (!entity.nameForEmit) {
// This should never happen
throw new InternalError('referencedEntry.nameForEmit is undefined');
Expand All @@ -150,7 +148,7 @@ export class DtsRollupGenerator {

// all local exports of local imported module are just references to top-level declarations
stringWriter.writeLine(' export {');
entity.astEntity.astModule.astModuleExportInfo.exportedLocalEntities.forEach((exportedEntity, exportedName) => {
astModuleExportInfo.exportedLocalEntities.forEach((exportedEntity, exportedName) => {
const collectorEntity: CollectorEntity | undefined = collector.tryGetCollectorEntity(exportedEntity);
if (!collectorEntity) {
// This should never happen
Expand All @@ -165,6 +163,10 @@ export class DtsRollupGenerator {
});
stringWriter.writeLine(' }'); // end of "export { ... }"

if (astModuleExportInfo.starExportedExternalModules.size > 0) {
throw new Error(`Unsupported star export of external module inside namespace imported module: ${entity.nameForEmit}`);
}

stringWriter.writeLine('}'); // end of "declare namespace { ... }"
}

Expand Down

0 comments on commit 54a3e15

Please sign in to comment.