From 9b25861d8cb2e894876126ff04f084d5529b94ac Mon Sep 17 00:00:00 2001 From: Chris Holt Date: Wed, 25 May 2022 10:17:12 -0700 Subject: [PATCH 1/3] 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 b7c9ad7ab97..b7b78d7bf50 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 4c35cc1ded9..545357b0724 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 00000000000..1e0fd855c4e --- /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 From 18b51b52dad1cfacdf865a114e1059a87005b586 Mon Sep 17 00:00:00 2001 From: Chris Holt Date: Wed, 25 May 2022 11:09:03 -0700 Subject: [PATCH 2/3] add return type for getModeForUsageLocation --- apps/api-extractor/src/analyzer/ExportAnalyzer.ts | 4 ++-- apps/api-extractor/src/analyzer/TypeScriptInternals.ts | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/apps/api-extractor/src/analyzer/ExportAnalyzer.ts b/apps/api-extractor/src/analyzer/ExportAnalyzer.ts index b7b78d7bf50..33867923bb5 100644 --- a/apps/api-extractor/src/analyzer/ExportAnalyzer.ts +++ b/apps/api-extractor/src/analyzer/ExportAnalyzer.ts @@ -262,7 +262,7 @@ export class ExportAnalyzer { const specifier: ts.TypeNode | ts.Expression | undefined = ts.isImportTypeNode(importOrExportDeclaration) ? importOrExportDeclaration.argument : importOrExportDeclaration.moduleSpecifier; - const mode: ts.StringLiteralLike = + const mode: ts.ModuleKind.CommonJS | ts.ModuleKind.ESNext | undefined = specifier && ts.isStringLiteralLike(specifier) ? TypeScriptInternals.getModeForUsageLocation(importOrExportDeclaration.getSourceFile(), specifier) : undefined; @@ -872,7 +872,7 @@ export class ExportAnalyzer { exportSymbol: ts.Symbol ): AstModule { const moduleSpecifier: string = this._getModuleSpecifier(importOrExportDeclaration); - const mode: ts.StringLiteralLike | undefined = + const mode: ts.ModuleKind.CommonJS | ts.ModuleKind.ESNext | undefined = importOrExportDeclaration.moduleSpecifier && ts.isStringLiteralLike(importOrExportDeclaration.moduleSpecifier) ? TypeScriptInternals.getModeForUsageLocation( diff --git a/apps/api-extractor/src/analyzer/TypeScriptInternals.ts b/apps/api-extractor/src/analyzer/TypeScriptInternals.ts index 545357b0724..ae3c0e04316 100644 --- a/apps/api-extractor/src/analyzer/TypeScriptInternals.ts +++ b/apps/api-extractor/src/analyzer/TypeScriptInternals.ts @@ -84,10 +84,10 @@ export class TypeScriptInternals { public static getResolvedModule( sourceFile: ts.SourceFile, moduleNameText: string, - mode: ts.StringLiteralLike | undefined + mode: ts.ModuleKind.CommonJS | ts.ModuleKind.ESNext | undefined ): ts.ResolvedModuleFull | undefined { // Compiler internal: - // https://github.com/microsoft/TypeScript/blob/v3.2.2/src/compiler/utilities.ts#L218 + // https://github.com/microsoft/TypeScript/blob/v4.7.2/src/compiler/utilities.ts#L161 return (ts as any).getResolvedModule(sourceFile, moduleNameText, mode); } @@ -97,12 +97,12 @@ export class TypeScriptInternals { */ public static getModeForUsageLocation( file: { impliedNodeFormat?: ts.SourceFile['impliedNodeFormat'] }, - usage: ts.StringLiteralLike - ) { + usage: ts.StringLiteralLike | undefined + ): ts.ModuleKind.CommonJS | ts.ModuleKind.ESNext | undefined { // Compiler internal: // https://github.com/microsoft/TypeScript/blob/v4.7.2/src/compiler/program.ts#L568 - return (ts as any).getModeForUsageLocation(file, usage); + return (ts as any).getModeForUsageLocation?.(file, usage); } /** From 65588633bde613806b972fc1c54004dddee5e882 Mon Sep 17 00:00:00 2001 From: Ian Clanton-Thuon Date: Wed, 25 May 2022 15:01:09 -0700 Subject: [PATCH 3/3] Rush change. --- ...s-chhol-fix-getResolvedModule-params_2022-05-25-17-20.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 index 1e0fd855c4e..354890a1249 100644 --- 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 @@ -2,8 +2,8 @@ "changes": [ { "packageName": "@microsoft/api-extractor", - "comment": "add missing required parameter for getResolvedModule to ensure typescript can accurately resolve module kind", - "type": "minor" + "comment": "Fix an issue where API Extractor would fail to run on a project where `\"moduleResolution\"` is set to `\"Node16\"` in `tsconfig.json`", + "type": "patch" } ], "packageName": "@microsoft/api-extractor"