From aeb74cc721d2d38b57f9c5d49730c46aacb81224 Mon Sep 17 00:00:00 2001 From: Isabel Duan Date: Mon, 14 Oct 2024 10:24:49 -0700 Subject: [PATCH] moveToNewFile: fix quotes for module specifiers (#60203) --- src/services/codefixes/importFixes.ts | 4 ++-- ...e.ts => moveToNewFile_inferQuoteStyle1.ts} | 0 .../moveToNewFile_inferQuoteStyle2.ts | 24 +++++++++++++++++++ ...t.ts => moveToNewFile_moveNamedImport1.ts} | 0 .../moveToNewFile_moveNamedImport2.ts | 22 +++++++++++++++++ 5 files changed, 48 insertions(+), 2 deletions(-) rename tests/cases/fourslash/{moveToNewFile_inferQuoteStyle.ts => moveToNewFile_inferQuoteStyle1.ts} (100%) create mode 100644 tests/cases/fourslash/moveToNewFile_inferQuoteStyle2.ts rename tests/cases/fourslash/{moveToNewFile_moveNamedImport.ts => moveToNewFile_moveNamedImport1.ts} (100%) create mode 100644 tests/cases/fourslash/moveToNewFile_moveNamedImport2.ts diff --git a/src/services/codefixes/importFixes.ts b/src/services/codefixes/importFixes.ts index f0a86cb0a20c8..0387702de5eef 100644 --- a/src/services/codefixes/importFixes.ts +++ b/src/services/codefixes/importFixes.ts @@ -494,8 +494,8 @@ function createImportAdderWorker(sourceFile: SourceFile | FutureSourceFile, prog function writeFixes(changeTracker: textChanges.ChangeTracker, oldFileQuotePreference?: QuotePreference) { let quotePreference: QuotePreference; - if (isFullSourceFile(sourceFile) && sourceFile.imports.length === 0 && oldFileQuotePreference !== undefined) { - // If the target file has no imports, we must use the same quote preference as the file we are importing from. + if (sourceFile.imports !== undefined && sourceFile.imports.length === 0 && oldFileQuotePreference !== undefined) { + // If the target file (including future files) has no imports, we must use the same quote preference as the file we are importing from. quotePreference = oldFileQuotePreference; } else { diff --git a/tests/cases/fourslash/moveToNewFile_inferQuoteStyle.ts b/tests/cases/fourslash/moveToNewFile_inferQuoteStyle1.ts similarity index 100% rename from tests/cases/fourslash/moveToNewFile_inferQuoteStyle.ts rename to tests/cases/fourslash/moveToNewFile_inferQuoteStyle1.ts diff --git a/tests/cases/fourslash/moveToNewFile_inferQuoteStyle2.ts b/tests/cases/fourslash/moveToNewFile_inferQuoteStyle2.ts new file mode 100644 index 0000000000000..191baee3ebc99 --- /dev/null +++ b/tests/cases/fourslash/moveToNewFile_inferQuoteStyle2.ts @@ -0,0 +1,24 @@ +/// + +// @Filename: /a.ts +////export const x = 0; +////x; + +// @Filename: /b.ts +////import { x } from './a' +//// +////[|x|]; + + +verify.moveToNewFile({ + newFileContents: { + "/newFile.ts": // module specifier should have the same quotes as import in the original file +`import { x } from './a'; + +x; +`, + "/b.ts": +` +` + }, +}); diff --git a/tests/cases/fourslash/moveToNewFile_moveNamedImport.ts b/tests/cases/fourslash/moveToNewFile_moveNamedImport1.ts similarity index 100% rename from tests/cases/fourslash/moveToNewFile_moveNamedImport.ts rename to tests/cases/fourslash/moveToNewFile_moveNamedImport1.ts diff --git a/tests/cases/fourslash/moveToNewFile_moveNamedImport2.ts b/tests/cases/fourslash/moveToNewFile_moveNamedImport2.ts new file mode 100644 index 0000000000000..f289d16f7ff0c --- /dev/null +++ b/tests/cases/fourslash/moveToNewFile_moveNamedImport2.ts @@ -0,0 +1,22 @@ +/// + +// @Filename: /other.ts +//// export function foo() { return 1 }; + +// @Filename: /a.ts +////import { foo as oFoo } from './other'; +////[|export const x = oFoo();|] +////export const a = 0; + +verify.moveToNewFile({ + newFileContents: { +"/a.ts": +`export const a = 0;`, + +"/x.ts": +`import { foo as oFoo } from './other'; + +export const x = oFoo(); +` + }, +});