Skip to content

Commit c7e016b

Browse files
authored
Merge pull request #3339 from zelliott/ambient-module-fix
[api-extractor] API Extractor should treat ambient modules as it did before.
2 parents 35c595e + 97a7a81 commit c7e016b

File tree

9 files changed

+41
-585
lines changed

9 files changed

+41
-585
lines changed

apps/api-extractor/src/analyzer/ExportAnalyzer.ts

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,18 @@ export class ExportAnalyzer {
259259
importOrExportDeclaration: ts.ImportDeclaration | ts.ExportDeclaration | ts.ImportTypeNode,
260260
moduleSpecifier: string
261261
): boolean {
262-
const resolvedModule: ts.ResolvedModuleFull = this._getResolvedModule(
263-
importOrExportDeclaration,
262+
const resolvedModule: ts.ResolvedModuleFull | undefined = TypeScriptInternals.getResolvedModule(
263+
importOrExportDeclaration.getSourceFile(),
264264
moduleSpecifier
265265
);
266266

267+
if (resolvedModule === undefined) {
268+
// The TS compiler API `getResolvedModule` cannot resolve ambient modules. Thus, to match API Extractor's
269+
// previous behavior, simply treat all ambient modules as external. This bug is tracked by
270+
// https://github.com/microsoft/rushstack/issues/3335.
271+
return true;
272+
}
273+
267274
// Either something like `jquery` or `@microsoft/api-extractor`.
268275
const packageName: string | undefined = resolvedModule.packageId?.name;
269276
if (packageName !== undefined && this._bundledPackageNames.has(packageName)) {
@@ -856,11 +863,23 @@ export class ExportAnalyzer {
856863
exportSymbol: ts.Symbol
857864
): AstModule {
858865
const moduleSpecifier: string = this._getModuleSpecifier(importOrExportDeclaration);
859-
const resolvedModule: ts.ResolvedModuleFull = this._getResolvedModule(
860-
importOrExportDeclaration,
866+
const resolvedModule: ts.ResolvedModuleFull | undefined = TypeScriptInternals.getResolvedModule(
867+
importOrExportDeclaration.getSourceFile(),
861868
moduleSpecifier
862869
);
863870

871+
if (resolvedModule === undefined) {
872+
// Encountered in https://github.com/microsoft/rushstack/issues/1914.
873+
//
874+
// It's also possible for this to occur with ambient modules. However, in practice this doesn't happen
875+
// as API Extractor treats all ambient modules as external per the logic in `_isExternalModulePath`, and
876+
// thus this code path is never reached for ambient modules.
877+
throw new InternalError(
878+
`getResolvedModule() could not resolve module name ${JSON.stringify(moduleSpecifier)}\n` +
879+
SourceFileLocationFormatter.formatDeclaration(importOrExportDeclaration)
880+
);
881+
}
882+
864883
// Map the filename back to the corresponding SourceFile. This circuitous approach is needed because
865884
// we have no way to access the compiler's internal resolveExternalModuleName() function
866885
const moduleSourceFile: ts.SourceFile | undefined = this._program.getSourceFile(
@@ -919,29 +938,6 @@ export class ExportAnalyzer {
919938
return astImport;
920939
}
921940

922-
private _getResolvedModule(
923-
importOrExportDeclaration: ts.ImportDeclaration | ts.ExportDeclaration | ts.ImportTypeNode,
924-
moduleSpecifier: string
925-
): ts.ResolvedModuleFull {
926-
const resolvedModule: ts.ResolvedModuleFull | undefined = TypeScriptInternals.getResolvedModule(
927-
importOrExportDeclaration.getSourceFile(),
928-
moduleSpecifier
929-
);
930-
931-
if (resolvedModule === undefined) {
932-
// This should not happen, since getResolvedModule() specifically looks up names that the compiler
933-
// found in export declarations for this source file
934-
//
935-
// Encountered in https://github.com/microsoft/rushstack/issues/1914
936-
throw new InternalError(
937-
`getResolvedModule() could not resolve module name ${JSON.stringify(moduleSpecifier)}\n` +
938-
SourceFileLocationFormatter.formatDeclaration(importOrExportDeclaration)
939-
);
940-
}
941-
942-
return resolvedModule;
943-
}
944-
945941
private _getModuleSpecifier(
946942
importOrExportDeclaration: ts.ImportDeclaration | ts.ExportDeclaration | ts.ImportTypeNode
947943
): string {

build-tests/api-extractor-scenarios/config/build-config.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
"inconsistentReleaseTags",
3535
"internationalCharacters",
3636
"namedDefaultImport",
37-
"pathMappings",
3837
"preapproved",
3938
"spanSorting",
4039
"typeOf",

0 commit comments

Comments
 (0)