Skip to content

Commit 94bb950

Browse files
authored
feat(49358): use filename based on exported name (#49875)
1 parent 5b0eea4 commit 94bb950

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/services/refactors/moveToNewFile.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ namespace ts.refactor {
6262

6363
const currentDirectory = getDirectoryPath(oldFile.fileName);
6464
const extension = extensionFromPath(oldFile.fileName);
65-
const newModuleName = makeUniqueModuleName(getNewModuleName(usage.movedSymbols), extension, currentDirectory, host);
65+
const newModuleName = makeUniqueModuleName(getNewModuleName(usage.oldFileImportsFromNewFile, usage.movedSymbols), extension, currentDirectory, host);
6666
const newFileNameWithExtension = newModuleName + extension;
6767

6868
// If previous file was global, this is easy.
@@ -478,8 +478,8 @@ namespace ts.refactor {
478478
}
479479
}
480480

481-
function getNewModuleName(movedSymbols: ReadonlySymbolSet): string {
482-
return movedSymbols.forEachEntry(symbolNameNoDefault) || "newFile";
481+
function getNewModuleName(importsFromNewFile: ReadonlySymbolSet, movedSymbols: ReadonlySymbolSet): string {
482+
return importsFromNewFile.forEachEntry(symbolNameNoDefault) || movedSymbols.forEachEntry(symbolNameNoDefault) || "newFile";
483483
}
484484

485485
interface UsageInfo {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /a.ts
4+
////[|type Props = {
5+
//// a: number;
6+
////}
7+
////class Foo {
8+
//// readonly a: number;
9+
//// constructor({ a }: Props) {
10+
//// this.a = a;
11+
//// }
12+
////}|]
13+
////
14+
////export default function f() {
15+
//// return new Foo({ a: 1 });
16+
////}
17+
18+
verify.moveToNewFile({
19+
newFileContents: {
20+
"/a.ts":
21+
`import { Foo } from "./Foo";
22+
23+
export default function f() {
24+
return new Foo({ a: 1 });
25+
}`,
26+
27+
"/Foo.ts":
28+
`type Props = {
29+
a: number;
30+
};
31+
export class Foo {
32+
readonly a: number;
33+
constructor({ a }: Props) {
34+
this.a = a;
35+
}
36+
}
37+
`,
38+
},
39+
});

0 commit comments

Comments
 (0)