@@ -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+ }
0 commit comments