Skip to content

Commit aeb74cc

Browse files
authored
moveToNewFile: fix quotes for module specifiers (#60203)
1 parent c003609 commit aeb74cc

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

src/services/codefixes/importFixes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,8 @@ function createImportAdderWorker(sourceFile: SourceFile | FutureSourceFile, prog
494494

495495
function writeFixes(changeTracker: textChanges.ChangeTracker, oldFileQuotePreference?: QuotePreference) {
496496
let quotePreference: QuotePreference;
497-
if (isFullSourceFile(sourceFile) && sourceFile.imports.length === 0 && oldFileQuotePreference !== undefined) {
498-
// If the target file has no imports, we must use the same quote preference as the file we are importing from.
497+
if (sourceFile.imports !== undefined && sourceFile.imports.length === 0 && oldFileQuotePreference !== undefined) {
498+
// If the target file (including future files) has no imports, we must use the same quote preference as the file we are importing from.
499499
quotePreference = oldFileQuotePreference;
500500
}
501501
else {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a.ts
4+
////export const x = 0;
5+
////x;
6+
7+
// @Filename: /b.ts
8+
////import { x } from './a'
9+
////
10+
////[|x|];
11+
12+
13+
verify.moveToNewFile({
14+
newFileContents: {
15+
"/newFile.ts": // module specifier should have the same quotes as import in the original file
16+
`import { x } from './a';
17+
18+
x;
19+
`,
20+
"/b.ts":
21+
`
22+
`
23+
},
24+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /other.ts
4+
//// export function foo() { return 1 };
5+
6+
// @Filename: /a.ts
7+
////import { foo as oFoo } from './other';
8+
////[|export const x = oFoo();|]
9+
////export const a = 0;
10+
11+
verify.moveToNewFile({
12+
newFileContents: {
13+
"/a.ts":
14+
`export const a = 0;`,
15+
16+
"/x.ts":
17+
`import { foo as oFoo } from './other';
18+
19+
export const x = oFoo();
20+
`
21+
},
22+
});

0 commit comments

Comments
 (0)