@@ -14,7 +14,9 @@ export const processApplicableRefactors = (refactor: ts.ApplicableRefactorInfo |
1414 name : `${ blockScopeRefactor . name } _local_arrow` ,
1515 } )
1616 }
17- const globalScopeRefactor = functionExtractors . find ( e => e . description === 'Extract to function in global scope' )
17+ const globalScopeRefactor = functionExtractors . find ( e =>
18+ [ 'Extract to function in global scope' , 'Extract to function in module scope' ] . includes ( e . description ) ,
19+ )
1820 if ( globalScopeRefactor ) {
1921 refactor ! . actions . push ( {
2022 description : 'Extract to arrow function in global scope above' ,
@@ -37,7 +39,7 @@ export const handleFunctionRefactorEdits = (
3739) : ts . RefactorEditInfo | undefined => {
3840 if ( ! actionName . endsWith ( '_arrow' ) ) return
3941 const originalAcitonName = actionName . replace ( '_local_arrow' , '' ) . replace ( '_arrow' , '' )
40- const { edits, renameLocation , renameFilename } = languageService . getEditsForRefactor (
42+ const { edits : originalEdits , renameFilename } = languageService . getEditsForRefactor (
4143 fileName ,
4244 formatOptions ,
4345 positionOrRange ,
@@ -46,9 +48,11 @@ export const handleFunctionRefactorEdits = (
4648 preferences ,
4749 ) !
4850 // has random number of edits because imports can be added
49- const { textChanges } = edits [ 0 ] !
51+ const { textChanges } = originalEdits [ 0 ] !
5052 const functionChange = textChanges . at ( - 1 ) !
51- functionChange . newText = functionChange . newText
53+ const oldFunctionText = functionChange . newText
54+ const functionName = oldFunctionText . slice ( oldFunctionText . indexOf ( 'function ' ) + 'function ' . length , oldFunctionText . indexOf ( '(' ) )
55+ functionChange . newText = oldFunctionText
5256 . replace ( / f u n c t i o n / , 'const ' )
5357 . replace ( '(' , ' = (' )
5458 . replace ( / \{ \n / , '=> {\n' )
@@ -79,11 +83,15 @@ export const handleFunctionRefactorEdits = (
7983 functionChange . span . start = pos
8084 }
8185 }
86+ const fileEdits = [
87+ {
88+ fileName,
89+ textChanges : [ ...textChanges . slice ( 0 , - 2 ) , textChanges . at ( - 1 ) ! , textChanges . at ( - 2 ) ! ] ,
90+ } ,
91+ ]
8292 return {
83- edits : [ { fileName, textChanges } ] ,
84- // TODO since ts making edit after current location, it doesn't expect renameLocation to be changed (lets fix it)
85- // renameLocation: renameLocation! + functionChange.newText.length + 1,
86- renameLocation : functionChange . span . start + functionChange . newText . indexOf ( 'const ' ) + 'const ' . length ,
93+ edits : fileEdits ,
94+ renameLocation : tsFull . getRenameLocation ( fileEdits , fileName , functionName , /*preferLastLocation*/ true ) ,
8795 renameFilename,
8896 }
8997}
0 commit comments