From 53d2b7ed601969830a3d21911e478c63219c5cc0 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Thu, 7 Nov 2019 14:55:54 -0800 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20assume=20importing=20some=20JSO?= =?UTF-8?q?N=20means=20you=20want=20everything=20imported=20with=20.js=20e?= =?UTF-8?q?xtensions=20(#34961)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/compiler/moduleSpecifiers.ts | 4 ++-- src/compiler/utilities.ts | 4 ---- .../cases/fourslash/importNameCodeFix_withJson.ts | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 tests/cases/fourslash/importNameCodeFix_withJson.ts diff --git a/src/compiler/moduleSpecifiers.ts b/src/compiler/moduleSpecifiers.ts index 564cb4db2c4..1850220a411 100644 --- a/src/compiler/moduleSpecifiers.ts +++ b/src/compiler/moduleSpecifiers.ts @@ -30,7 +30,7 @@ namespace ts.moduleSpecifiers { function getPreferencesForUpdate(compilerOptions: CompilerOptions, oldImportSpecifier: string): Preferences { return { relativePreference: isExternalModuleNameRelative(oldImportSpecifier) ? RelativePreference.Relative : RelativePreference.NonRelative, - ending: hasJSOrJsonFileExtension(oldImportSpecifier) ? + ending: hasJSFileExtension(oldImportSpecifier) ? Ending.JsExtension : getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.NodeJs || endsWith(oldImportSpecifier, "index") ? Ending.Index : Ending.Minimal, }; @@ -163,7 +163,7 @@ namespace ts.moduleSpecifiers { } function usesJsExtensionOnImports({ imports }: SourceFile): boolean { - return firstDefined(imports, ({ text }) => pathIsRelative(text) ? hasJSOrJsonFileExtension(text) : undefined) || false; + return firstDefined(imports, ({ text }) => pathIsRelative(text) ? hasJSFileExtension(text) : undefined) || false; } function numberOfDirectorySeparators(str: string) { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 7ee5dcd8278..935c528ebe2 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -8131,10 +8131,6 @@ namespace ts { return some(supportedJSExtensions, extension => fileExtensionIs(fileName, extension)); } - export function hasJSOrJsonFileExtension(fileName: string): boolean { - return supportedJSAndJsonExtensions.some(ext => fileExtensionIs(fileName, ext)); - } - export function hasTSFileExtension(fileName: string): boolean { return some(supportedTSExtensions, extension => fileExtensionIs(fileName, extension)); } diff --git a/tests/cases/fourslash/importNameCodeFix_withJson.ts b/tests/cases/fourslash/importNameCodeFix_withJson.ts new file mode 100644 index 00000000000..49d449b7b04 --- /dev/null +++ b/tests/cases/fourslash/importNameCodeFix_withJson.ts @@ -0,0 +1,15 @@ +/// + +// @Filename: /a.ts +////export const a = 'a'; + +// @Filename: /b.ts +////import "./anything.json"; +//// +////a/**/ + +goTo.file("/b.ts"); +verify.importFixAtPosition([`import "./anything.json"; +import { a } from "./a"; + +a`]);