Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 23 additions & 27 deletions apps/api-extractor/src/analyzer/ExportAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,18 @@ export class ExportAnalyzer {
importOrExportDeclaration: ts.ImportDeclaration | ts.ExportDeclaration | ts.ImportTypeNode,
moduleSpecifier: string
): boolean {
const resolvedModule: ts.ResolvedModuleFull = this._getResolvedModule(
importOrExportDeclaration,
const resolvedModule: ts.ResolvedModuleFull | undefined = TypeScriptInternals.getResolvedModule(
importOrExportDeclaration.getSourceFile(),
moduleSpecifier
);

if (resolvedModule === undefined) {
// The TS compiler API `getResolvedModule` cannot resolve ambient modules. Thus, to match API Extractor's
// previous behavior, simply treat all ambient modules as external. This bug is tracked by
// https://github.com/microsoft/rushstack/issues/3335.
return true;
}

// Either something like `jquery` or `@microsoft/api-extractor`.
const packageName: string | undefined = resolvedModule.packageId?.name;
if (packageName !== undefined && this._bundledPackageNames.has(packageName)) {
Expand Down Expand Up @@ -856,11 +863,23 @@ export class ExportAnalyzer {
exportSymbol: ts.Symbol
): AstModule {
const moduleSpecifier: string = this._getModuleSpecifier(importOrExportDeclaration);
const resolvedModule: ts.ResolvedModuleFull = this._getResolvedModule(
importOrExportDeclaration,
const resolvedModule: ts.ResolvedModuleFull | undefined = TypeScriptInternals.getResolvedModule(
importOrExportDeclaration.getSourceFile(),
moduleSpecifier
);

if (resolvedModule === undefined) {
// Encountered in https://github.com/microsoft/rushstack/issues/1914.
//
// It's also possible for this to occur with ambient modules. However, in practice this doesn't happen
// as API Extractor treats all ambient modules as external per the logic in `_isExternalModulePath`, and
// thus this code path is never reached for ambient modules.
throw new InternalError(
`getResolvedModule() could not resolve module name ${JSON.stringify(moduleSpecifier)}\n` +
SourceFileLocationFormatter.formatDeclaration(importOrExportDeclaration)
);
}

// Map the filename back to the corresponding SourceFile. This circuitous approach is needed because
// we have no way to access the compiler's internal resolveExternalModuleName() function
const moduleSourceFile: ts.SourceFile | undefined = this._program.getSourceFile(
Expand Down Expand Up @@ -919,29 +938,6 @@ export class ExportAnalyzer {
return astImport;
}

private _getResolvedModule(
importOrExportDeclaration: ts.ImportDeclaration | ts.ExportDeclaration | ts.ImportTypeNode,
moduleSpecifier: string
): ts.ResolvedModuleFull {
const resolvedModule: ts.ResolvedModuleFull | undefined = TypeScriptInternals.getResolvedModule(
importOrExportDeclaration.getSourceFile(),
moduleSpecifier
);

if (resolvedModule === undefined) {
// This should not happen, since getResolvedModule() specifically looks up names that the compiler
// found in export declarations for this source file
//
// Encountered in https://github.com/microsoft/rushstack/issues/1914
throw new InternalError(
`getResolvedModule() could not resolve module name ${JSON.stringify(moduleSpecifier)}\n` +
SourceFileLocationFormatter.formatDeclaration(importOrExportDeclaration)
);
}

return resolvedModule;
}

private _getModuleSpecifier(
importOrExportDeclaration: ts.ImportDeclaration | ts.ExportDeclaration | ts.ImportTypeNode
): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"inconsistentReleaseTags",
"internationalCharacters",
"namedDefaultImport",
"pathMappings",
"preapproved",
"spanSorting",
"typeOf",
Expand Down
Loading