From 9b25861d8cb2e894876126ff04f084d5529b94ac Mon Sep 17 00:00:00 2001 From: Chris Holt Date: Wed, 25 May 2022 10:17:12 -0700 Subject: [PATCH] fix: add missing required parameter for getResolvedModule to ensure typescript can resolve accurately --- .../src/analyzer/ExportAnalyzer.ts | 22 +++++++++++++++++-- .../src/analyzer/TypeScriptInternals.ts | 18 +++++++++++++-- ...esolvedModule-params_2022-05-25-17-20.json | 10 +++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 common/changes/@microsoft/api-extractor/users-chhol-fix-getResolvedModule-params_2022-05-25-17-20.json diff --git a/apps/api-extractor/src/analyzer/ExportAnalyzer.ts b/apps/api-extractor/src/analyzer/ExportAnalyzer.ts index b7c9ad7ab9..b7b78d7bf5 100644 --- a/apps/api-extractor/src/analyzer/ExportAnalyzer.ts +++ b/apps/api-extractor/src/analyzer/ExportAnalyzer.ts @@ -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) { @@ -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) { diff --git a/apps/api-extractor/src/analyzer/TypeScriptInternals.ts b/apps/api-extractor/src/analyzer/TypeScriptInternals.ts index 4c35cc1ded..545357b072 100644 --- a/apps/api-extractor/src/analyzer/TypeScriptInternals.ts +++ b/apps/api-extractor/src/analyzer/TypeScriptInternals.ts @@ -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); } /** diff --git a/common/changes/@microsoft/api-extractor/users-chhol-fix-getResolvedModule-params_2022-05-25-17-20.json b/common/changes/@microsoft/api-extractor/users-chhol-fix-getResolvedModule-params_2022-05-25-17-20.json new file mode 100644 index 0000000000..1e0fd855c4 --- /dev/null +++ b/common/changes/@microsoft/api-extractor/users-chhol-fix-getResolvedModule-params_2022-05-25-17-20.json @@ -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" +} \ No newline at end of file