From e8df495a1a266aa12932d6b0f23f6f4d360a21b6 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Mon, 9 May 2022 11:33:08 -0700 Subject: [PATCH] Fix module specifier generation crash from typesVersions --- src/compiler/moduleSpecifiers.ts | 1 + .../importNameCodeFix_typesVersions.ts | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 tests/cases/fourslash/importNameCodeFix_typesVersions.ts diff --git a/src/compiler/moduleSpecifiers.ts b/src/compiler/moduleSpecifiers.ts index ed8d9f90b0ae2..44d20c9ff59ef 100644 --- a/src/compiler/moduleSpecifiers.ts +++ b/src/compiler/moduleSpecifiers.ts @@ -810,6 +810,7 @@ namespace ts.moduleSpecifiers { function removeExtensionAndIndexPostFix(fileName: string, ending: Ending, options: CompilerOptions, host?: ModuleSpecifierResolutionHost): string { if (fileExtensionIsOneOf(fileName, [Extension.Json, Extension.Mjs, Extension.Cjs])) return fileName; const noExtension = removeFileExtension(fileName); + if (fileName === noExtension) return fileName; if (fileExtensionIsOneOf(fileName, [Extension.Dmts, Extension.Mts, Extension.Dcts, Extension.Cts])) return noExtension + getJSExtensionForFile(fileName, options); switch (ending) { case Ending.Minimal: diff --git a/tests/cases/fourslash/importNameCodeFix_typesVersions.ts b/tests/cases/fourslash/importNameCodeFix_typesVersions.ts new file mode 100644 index 0000000000000..9f69e3eb44f8d --- /dev/null +++ b/tests/cases/fourslash/importNameCodeFix_typesVersions.ts @@ -0,0 +1,34 @@ +/// + +// @module: commonjs +// @checkJs: true + +// @Filename: /node_modules/unified/package.json +//// { +//// "name": "unified", +//// "types": "types/ts3.4/index.d.ts", +//// "typesVersions": { +//// ">=4.0": { +//// "types/ts3.4/*": [ +//// "types/ts4.0/*" +//// ] +//// } +//// } +//// } + +// @Filename: /node_modules/unified/types/ts3.4/index.d.ts +//// export declare const x: number; + +// @Filename: /node_modules/unified/types/ts4.0/index.d.ts +//// export declare const x: number; + +// @Filename: /foo.js +//// import {} from "unified"; + +// @Filename: /index.js +//// x/**/ + +verify.importFixModuleSpecifiers("", [ + "unified", + "unified/types/ts3.4/", // TODO: this is wrong #49034 +], { importModuleSpecifierEnding: "js" });