Skip to content

fix(54155): "Move to file" does not carry all function overload and implementation signatures #54164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/services/refactors/moveToFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
find,
FindAllReferences,
findIndex,
findLastIndex,
firstDefined,
flatMap,
forEachKey,
Expand All @@ -51,6 +52,7 @@ import {
getRangesWhere,
getRefactorContextSpan,
getRelativePathFromFile,
getSourceFileOfNode,
getSynthesizedDeepClone,
getUniqueName,
hasSyntacticModifier,
Expand All @@ -67,6 +69,7 @@ import {
isDeclarationName,
isExpressionStatement,
isExternalModuleReference,
isFunctionLikeDeclaration,
isIdentifier,
isImportDeclaration,
isImportEqualsDeclaration,
Expand All @@ -78,6 +81,7 @@ import {
isPropertyAssignment,
isRequireCall,
isSourceFile,
isStatement,
isStringLiteral,
isStringLiteralLike,
isValidTypeOnlyAliasUseSite,
Expand Down Expand Up @@ -884,6 +888,11 @@ function getRangeToMove(context: RefactorContext): RangeToMove | undefined {
return { toMove: [statements[startNodeIndex]], afterLast: statements[startNodeIndex + 1] };
}

const overloadRangeToMove = getOverloadRangeToMove(file, startStatement);
if (overloadRangeToMove) {
return overloadRangeToMove;
}

// Can't only partially include the start node or be partially into the next node
if (range.pos > startStatement.getStart(file)) return undefined;
const afterEndNodeIndex = findIndex(statements, s => s.end > range.end, startNodeIndex);
Expand Down Expand Up @@ -1103,4 +1112,16 @@ function isNonVariableTopLevelDeclaration(node: Node): node is NonVariableTopLev
}
}


function getOverloadRangeToMove(sourceFile: SourceFile, statement: Statement) {
if (isFunctionLikeDeclaration(statement)) {
const declarations = statement.symbol.declarations;
if (declarations === undefined || length(declarations) <= 1 || !contains(declarations, statement)) {
return undefined;
}
const lastDecl = declarations[length(declarations) - 1];
const statementsToMove = mapDefined(declarations, d => getSourceFileOfNode(d) === sourceFile && isStatement(d) ? d : undefined);
const end = findLastIndex(sourceFile.statements, s => s.end > lastDecl.end);
return { toMove: statementsToMove, afterLast: end >= 0 ? sourceFile.statements[end] : undefined };
}
return undefined;
}
25 changes: 25 additions & 0 deletions tests/cases/fourslash/moveToFile_overloads1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference path='fourslash.ts' />

// @Filename: /add.ts
////

// @Filename: /a.ts
////[|function add(x: number, y: number): number;|]
////function add(x: string, y: string): string;
////function add(x: any, y: any) {
//// return x + y;
////}

verify.moveToFile({
newFileContents: {
"/a.ts": "",
"/add.ts":
`function add(x: number, y: number): number;
function add(x: string, y: string): string;
function add(x: any, y: any) {
return x + y;
}
`
},
interactiveRefactorArguments: { targetFile: "/add.ts" }
});
25 changes: 25 additions & 0 deletions tests/cases/fourslash/moveToFile_overloads2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference path='fourslash.ts' />

// @Filename: /add.ts
////

// @Filename: /a.ts
////function add(x: number, y: number): number;
////function add(x: string, y: string): string;
////[|function add(x: any, y: any) {
//// return x + y;
////}|]

verify.moveToFile({
newFileContents: {
"/a.ts": "",
"/add.ts":
`function add(x: number, y: number): number;
function add(x: string, y: string): string;
function add(x: any, y: any) {
return x + y;
}
`
},
interactiveRefactorArguments: { targetFile: "/add.ts" }
});
25 changes: 25 additions & 0 deletions tests/cases/fourslash/moveToFile_overloads3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference path='fourslash.ts' />

// @Filename: /add.ts
////

// @Filename: /a.ts
////function add(x: number, y: number): number;
////[|function add(x: string, y: string): string;
////function add(x: any, y: any) {
//// return x + y;
////}|]

verify.moveToFile({
newFileContents: {
"/a.ts": "",
"/add.ts":
`function add(x: number, y: number): number;
function add(x: string, y: string): string;
function add(x: any, y: any) {
return x + y;
}
`
},
interactiveRefactorArguments: { targetFile: "/add.ts" },
});
26 changes: 26 additions & 0 deletions tests/cases/fourslash/moveToFile_overloads4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// <reference path='fourslash.ts' />

// @Filename: /add.ts
////

// @Filename: /a.ts
////function add(x: number, y: number): number;
////[|function add(x: string, y: string): string;
////function add(x: any, y: any) {
//// return x + y;
////}|]
////function remove() {}

verify.moveToFile({
newFileContents: {
"/a.ts": "function remove() {}",
"/add.ts":
`function add(x: number, y: number): number;
function add(x: string, y: string): string;
function add(x: any, y: any) {
return x + y;
}
`
},
interactiveRefactorArguments: { targetFile: "/add.ts" },
});
29 changes: 29 additions & 0 deletions tests/cases/fourslash/moveToFile_overloads5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <reference path='fourslash.ts' />

// @Filename: /add.ts
////

// @Filename: /a.ts
////function add(x: number, y: number): number;
////[|function add(x: string, y: string): string;
////function add(x: any, y: any) {
//// return x + y;
////}|]
////export const a = add();

verify.moveToFile({
newFileContents: {
"/a.ts":
`import { add } from "./add";

export const a = add();`,
"/add.ts":
`export function add(x: number, y: number): number;
export function add(x: string, y: string): string;
export function add(x: any, y: any) {
return x + y;
}
`
},
interactiveRefactorArguments: { targetFile: "/add.ts" },
});
21 changes: 21 additions & 0 deletions tests/cases/fourslash/moveToNewFile_overloads1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
////[|function add(x: number, y: number): number;|]
////function add(x: string, y: string): string;
////function add(x: any, y: any) {
//// return x + y;
////}

verify.moveToNewFile({
newFileContents: {
"/a.ts": "",
"/add.ts":
`function add(x: number, y: number): number;
function add(x: string, y: string): string;
function add(x: any, y: any) {
return x + y;
}
`
},
});
21 changes: 21 additions & 0 deletions tests/cases/fourslash/moveToNewFile_overloads2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
////function add(x: number, y: number): number;
////function add(x: string, y: string): string;
////[|function add(x: any, y: any) {
//// return x + y;
////}|]

verify.moveToNewFile({
newFileContents: {
"/a.ts": "",
"/add.ts":
`function add(x: number, y: number): number;
function add(x: string, y: string): string;
function add(x: any, y: any) {
return x + y;
}
`
},
});
21 changes: 21 additions & 0 deletions tests/cases/fourslash/moveToNewFile_overloads3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
////function add(x: number, y: number): number;
////[|function add(x: string, y: string): string;
////function add(x: any, y: any) {
//// return x + y;
////}|]

verify.moveToNewFile({
newFileContents: {
"/a.ts": "",
"/add.ts":
`function add(x: number, y: number): number;
function add(x: string, y: string): string;
function add(x: any, y: any) {
return x + y;
}
`
},
});
22 changes: 22 additions & 0 deletions tests/cases/fourslash/moveToNewFile_overloads4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
////function add(x: number, y: number): number;
////[|function add(x: string, y: string): string;
////function add(x: any, y: any) {
//// return x + y;
////}|]
////function remove() {}

verify.moveToNewFile({
newFileContents: {
"/a.ts": "function remove() {}",
"/add.ts":
`function add(x: number, y: number): number;
function add(x: string, y: string): string;
function add(x: any, y: any) {
return x + y;
}
`
},
});