Skip to content

Commit 8ea3887

Browse files
authored
fix(54155): "Move to file" does not carry all function overload and implementation signatures (#54164)
1 parent f4c2a22 commit 8ea3887

10 files changed

+237
-1
lines changed

src/services/refactors/moveToFile.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
find,
3636
FindAllReferences,
3737
findIndex,
38+
findLastIndex,
3839
firstDefined,
3940
flatMap,
4041
forEachKey,
@@ -51,6 +52,7 @@ import {
5152
getRangesWhere,
5253
getRefactorContextSpan,
5354
getRelativePathFromFile,
55+
getSourceFileOfNode,
5456
getSynthesizedDeepClone,
5557
getUniqueName,
5658
hasJSFileExtension,
@@ -69,6 +71,7 @@ import {
6971
isDeclarationName,
7072
isExpressionStatement,
7173
isExternalModuleReference,
74+
isFunctionLikeDeclaration,
7275
isIdentifier,
7376
isImportDeclaration,
7477
isImportEqualsDeclaration,
@@ -80,6 +83,7 @@ import {
8083
isPropertyAssignment,
8184
isRequireCall,
8285
isSourceFile,
86+
isStatement,
8387
isStringLiteral,
8488
isStringLiteralLike,
8589
isValidTypeOnlyAliasUseSite,
@@ -901,6 +905,11 @@ function getRangeToMove(context: RefactorContext): RangeToMove | undefined {
901905
return { toMove: [statements[startNodeIndex]], afterLast: statements[startNodeIndex + 1] };
902906
}
903907

908+
const overloadRangeToMove = getOverloadRangeToMove(file, startStatement);
909+
if (overloadRangeToMove) {
910+
return overloadRangeToMove;
911+
}
912+
904913
// Can't only partially include the start node or be partially into the next node
905914
if (range.pos > startStatement.getStart(file)) return undefined;
906915
const afterEndNodeIndex = findIndex(statements, s => s.end > range.end, startNodeIndex);
@@ -1120,4 +1129,16 @@ function isNonVariableTopLevelDeclaration(node: Node): node is NonVariableTopLev
11201129
}
11211130
}
11221131

1123-
1132+
function getOverloadRangeToMove(sourceFile: SourceFile, statement: Statement) {
1133+
if (isFunctionLikeDeclaration(statement)) {
1134+
const declarations = statement.symbol.declarations;
1135+
if (declarations === undefined || length(declarations) <= 1 || !contains(declarations, statement)) {
1136+
return undefined;
1137+
}
1138+
const lastDecl = declarations[length(declarations) - 1];
1139+
const statementsToMove = mapDefined(declarations, d => getSourceFileOfNode(d) === sourceFile && isStatement(d) ? d : undefined);
1140+
const end = findLastIndex(sourceFile.statements, s => s.end > lastDecl.end);
1141+
return { toMove: statementsToMove, afterLast: end >= 0 ? sourceFile.statements[end] : undefined };
1142+
}
1143+
return undefined;
1144+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /add.ts
4+
////
5+
6+
// @Filename: /a.ts
7+
////[|function add(x: number, y: number): number;|]
8+
////function add(x: string, y: string): string;
9+
////function add(x: any, y: any) {
10+
//// return x + y;
11+
////}
12+
13+
verify.moveToFile({
14+
newFileContents: {
15+
"/a.ts": "",
16+
"/add.ts":
17+
`function add(x: number, y: number): number;
18+
function add(x: string, y: string): string;
19+
function add(x: any, y: any) {
20+
return x + y;
21+
}
22+
`
23+
},
24+
interactiveRefactorArguments: { targetFile: "/add.ts" }
25+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /add.ts
4+
////
5+
6+
// @Filename: /a.ts
7+
////function add(x: number, y: number): number;
8+
////function add(x: string, y: string): string;
9+
////[|function add(x: any, y: any) {
10+
//// return x + y;
11+
////}|]
12+
13+
verify.moveToFile({
14+
newFileContents: {
15+
"/a.ts": "",
16+
"/add.ts":
17+
`function add(x: number, y: number): number;
18+
function add(x: string, y: string): string;
19+
function add(x: any, y: any) {
20+
return x + y;
21+
}
22+
`
23+
},
24+
interactiveRefactorArguments: { targetFile: "/add.ts" }
25+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /add.ts
4+
////
5+
6+
// @Filename: /a.ts
7+
////function add(x: number, y: number): number;
8+
////[|function add(x: string, y: string): string;
9+
////function add(x: any, y: any) {
10+
//// return x + y;
11+
////}|]
12+
13+
verify.moveToFile({
14+
newFileContents: {
15+
"/a.ts": "",
16+
"/add.ts":
17+
`function add(x: number, y: number): number;
18+
function add(x: string, y: string): string;
19+
function add(x: any, y: any) {
20+
return x + y;
21+
}
22+
`
23+
},
24+
interactiveRefactorArguments: { targetFile: "/add.ts" },
25+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /add.ts
4+
////
5+
6+
// @Filename: /a.ts
7+
////function add(x: number, y: number): number;
8+
////[|function add(x: string, y: string): string;
9+
////function add(x: any, y: any) {
10+
//// return x + y;
11+
////}|]
12+
////function remove() {}
13+
14+
verify.moveToFile({
15+
newFileContents: {
16+
"/a.ts": "function remove() {}",
17+
"/add.ts":
18+
`function add(x: number, y: number): number;
19+
function add(x: string, y: string): string;
20+
function add(x: any, y: any) {
21+
return x + y;
22+
}
23+
`
24+
},
25+
interactiveRefactorArguments: { targetFile: "/add.ts" },
26+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /add.ts
4+
////
5+
6+
// @Filename: /a.ts
7+
////function add(x: number, y: number): number;
8+
////[|function add(x: string, y: string): string;
9+
////function add(x: any, y: any) {
10+
//// return x + y;
11+
////}|]
12+
////export const a = add();
13+
14+
verify.moveToFile({
15+
newFileContents: {
16+
"/a.ts":
17+
`import { add } from "./add";
18+
19+
export const a = add();`,
20+
"/add.ts":
21+
`export function add(x: number, y: number): number;
22+
export function add(x: string, y: string): string;
23+
export function add(x: any, y: any) {
24+
return x + y;
25+
}
26+
`
27+
},
28+
interactiveRefactorArguments: { targetFile: "/add.ts" },
29+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a.ts
4+
////[|function add(x: number, y: number): number;|]
5+
////function add(x: string, y: string): string;
6+
////function add(x: any, y: any) {
7+
//// return x + y;
8+
////}
9+
10+
verify.moveToNewFile({
11+
newFileContents: {
12+
"/a.ts": "",
13+
"/add.ts":
14+
`function add(x: number, y: number): number;
15+
function add(x: string, y: string): string;
16+
function add(x: any, y: any) {
17+
return x + y;
18+
}
19+
`
20+
},
21+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a.ts
4+
////function add(x: number, y: number): number;
5+
////function add(x: string, y: string): string;
6+
////[|function add(x: any, y: any) {
7+
//// return x + y;
8+
////}|]
9+
10+
verify.moveToNewFile({
11+
newFileContents: {
12+
"/a.ts": "",
13+
"/add.ts":
14+
`function add(x: number, y: number): number;
15+
function add(x: string, y: string): string;
16+
function add(x: any, y: any) {
17+
return x + y;
18+
}
19+
`
20+
},
21+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a.ts
4+
////function add(x: number, y: number): number;
5+
////[|function add(x: string, y: string): string;
6+
////function add(x: any, y: any) {
7+
//// return x + y;
8+
////}|]
9+
10+
verify.moveToNewFile({
11+
newFileContents: {
12+
"/a.ts": "",
13+
"/add.ts":
14+
`function add(x: number, y: number): number;
15+
function add(x: string, y: string): string;
16+
function add(x: any, y: any) {
17+
return x + y;
18+
}
19+
`
20+
},
21+
});
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: /a.ts
4+
////function add(x: number, y: number): number;
5+
////[|function add(x: string, y: string): string;
6+
////function add(x: any, y: any) {
7+
//// return x + y;
8+
////}|]
9+
////function remove() {}
10+
11+
verify.moveToNewFile({
12+
newFileContents: {
13+
"/a.ts": "function remove() {}",
14+
"/add.ts":
15+
`function add(x: number, y: number): number;
16+
function add(x: string, y: string): string;
17+
function add(x: any, y: any) {
18+
return x + y;
19+
}
20+
`
21+
},
22+
});

0 commit comments

Comments
 (0)