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(); +` + }, +});