Skip to content

Commit

Permalink
fix: add missing required parameter for getResolvedModule to ensure t…
Browse files Browse the repository at this point in the history
…ypescript can resolve accurately
  • Loading branch information
chrisdholt committed May 25, 2022
1 parent d3ab07e commit 9b25861
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
22 changes: 20 additions & 2 deletions apps/api-extractor/src/analyzer/ExportAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,18 @@ export class ExportAnalyzer {
importOrExportDeclaration: ts.ImportDeclaration | ts.ExportDeclaration | ts.ImportTypeNode,
moduleSpecifier: string
): boolean {
const specifier: ts.TypeNode | ts.Expression | undefined = ts.isImportTypeNode(importOrExportDeclaration)
? importOrExportDeclaration.argument
: importOrExportDeclaration.moduleSpecifier;
const mode: ts.StringLiteralLike =
specifier && ts.isStringLiteralLike(specifier)
? TypeScriptInternals.getModeForUsageLocation(importOrExportDeclaration.getSourceFile(), specifier)
: undefined;

const resolvedModule: ts.ResolvedModuleFull | undefined = TypeScriptInternals.getResolvedModule(
importOrExportDeclaration.getSourceFile(),
moduleSpecifier
moduleSpecifier,
mode
);

if (resolvedModule === undefined) {
Expand Down Expand Up @@ -863,9 +872,18 @@ export class ExportAnalyzer {
exportSymbol: ts.Symbol
): AstModule {
const moduleSpecifier: string = this._getModuleSpecifier(importOrExportDeclaration);
const mode: ts.StringLiteralLike | undefined =
importOrExportDeclaration.moduleSpecifier &&
ts.isStringLiteralLike(importOrExportDeclaration.moduleSpecifier)
? TypeScriptInternals.getModeForUsageLocation(
importOrExportDeclaration.getSourceFile(),
importOrExportDeclaration.moduleSpecifier
)
: undefined;
const resolvedModule: ts.ResolvedModuleFull | undefined = TypeScriptInternals.getResolvedModule(
importOrExportDeclaration.getSourceFile(),
moduleSpecifier
moduleSpecifier,
mode
);

if (resolvedModule === undefined) {
Expand Down
18 changes: 16 additions & 2 deletions apps/api-extractor/src/analyzer/TypeScriptInternals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,26 @@ export class TypeScriptInternals {
*/
public static getResolvedModule(
sourceFile: ts.SourceFile,
moduleNameText: string
moduleNameText: string,
mode: ts.StringLiteralLike | undefined
): ts.ResolvedModuleFull | undefined {
// Compiler internal:
// https://github.com/microsoft/TypeScript/blob/v3.2.2/src/compiler/utilities.ts#L218

return (ts as any).getResolvedModule(sourceFile, moduleNameText);
return (ts as any).getResolvedModule(sourceFile, moduleNameText, mode);
}

/**
* Gets the mode required for module resolution required with the addition of Node16/nodenext
*/
public static getModeForUsageLocation(
file: { impliedNodeFormat?: ts.SourceFile['impliedNodeFormat'] },
usage: ts.StringLiteralLike
) {
// Compiler internal:
// https://github.com/microsoft/TypeScript/blob/v4.7.2/src/compiler/program.ts#L568

return (ts as any).getModeForUsageLocation(file, usage);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/api-extractor",
"comment": "add missing required parameter for getResolvedModule to ensure typescript can accurately resolve module kind",
"type": "minor"
}
],
"packageName": "@microsoft/api-extractor"
}

0 comments on commit 9b25861

Please sign in to comment.