Skip to content

Commit

Permalink
moveToNewFile: fix quotes for module specifiers (#60203)
Browse files Browse the repository at this point in the history
  • Loading branch information
iisaduan authored Oct 14, 2024
1 parent c003609 commit aeb74cc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions tests/cases/fourslash/moveToNewFile_inferQuoteStyle2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// <reference path='fourslash.ts' />

// @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":
`
`
},
});
22 changes: 22 additions & 0 deletions tests/cases/fourslash/moveToNewFile_moveNamedImport2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference path='fourslash.ts' />

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

0 comments on commit aeb74cc

Please sign in to comment.