feat(49358): use filename based on exported name (#49875)

This commit is contained in:
Oleksandr T 2022-07-28 01:41:31 +03:00 committed by GitHub
parent 5b0eea48e9
commit 94bb950008
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 3 deletions

View File

@ -62,7 +62,7 @@ namespace ts.refactor {
const currentDirectory = getDirectoryPath(oldFile.fileName);
const extension = extensionFromPath(oldFile.fileName);
const newModuleName = makeUniqueModuleName(getNewModuleName(usage.movedSymbols), extension, currentDirectory, host);
const newModuleName = makeUniqueModuleName(getNewModuleName(usage.oldFileImportsFromNewFile, usage.movedSymbols), extension, currentDirectory, host);
const newFileNameWithExtension = newModuleName + extension;
// If previous file was global, this is easy.
@ -478,8 +478,8 @@ namespace ts.refactor {
}
}
function getNewModuleName(movedSymbols: ReadonlySymbolSet): string {
return movedSymbols.forEachEntry(symbolNameNoDefault) || "newFile";
function getNewModuleName(importsFromNewFile: ReadonlySymbolSet, movedSymbols: ReadonlySymbolSet): string {
return importsFromNewFile.forEachEntry(symbolNameNoDefault) || movedSymbols.forEachEntry(symbolNameNoDefault) || "newFile";
}
interface UsageInfo {

View File

@ -0,0 +1,39 @@
/// <reference path="fourslash.ts" />
// @Filename: /a.ts
////[|type Props = {
//// a: number;
////}
////class Foo {
//// readonly a: number;
//// constructor({ a }: Props) {
//// this.a = a;
//// }
////}|]
////
////export default function f() {
//// return new Foo({ a: 1 });
////}
verify.moveToNewFile({
newFileContents: {
"/a.ts":
`import { Foo } from "./Foo";
export default function f() {
return new Foo({ a: 1 });
}`,
"/Foo.ts":
`type Props = {
a: number;
};
export class Foo {
readonly a: number;
constructor({ a }: Props) {
this.a = a;
}
}
`,
},
});