Skip to content

Commit 01dfb8a

Browse files
committed
fix: a lot of fixes to Extract into JSX component code action!
1 parent 54b4acd commit 01dfb8a

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

typescript/src/codeActions/functionExtractors.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,17 @@ export const handleFunctionRefactorEdits = (
100100
const oldFunctionText = functionChange.newText
101101
const sourceFile = languageService.getProgram()!.getSourceFile(fileName)!
102102
if (actionName.endsWith('_jsx')) {
103+
// refactor.extract.jsx implementation
103104
const lines = oldFunctionText.trimStart().split('\n')
104105
const oldFunctionSignature = lines[0]!
105106
const componentName = tsFull.getUniqueName('ExtractedComponent', sourceFile as unknown as FullSourceFile)
106-
const newFunctionSignature = changeArgumentsToDestructured(oldFunctionSignature, formatOptions, sourceFile, componentName)
107-
108107
const insertChange = textChanges.at(-2)!
109-
let args = insertChange.newText.slice(1, -2)
110-
args = args.slice(args.indexOf('(') + 1)
108+
const args = insertChange.newText.slice(insertChange.newText.indexOf('(') + 1, insertChange.newText.lastIndexOf(')'))
109+
110+
const newFunctionSignature = changeArgumentsToDestructured(oldFunctionSignature, formatOptions, sourceFile, componentName).replace('{}: {}', '')
111+
112+
const oldSpan = sourceFile.text.slice(0, functionChange.span.start).length
113+
111114
const fileEdits = [
112115
{
113116
fileName,
@@ -130,11 +133,17 @@ export const handleFunctionRefactorEdits = (
130133
],
131134
},
132135
]
136+
const diff = fileEdits[0]!.textChanges.slice(0, -1).reduce((diff, { newText, span }) => {
137+
const oldText = sourceFile.text.slice(span.start, span.start + span.length)
138+
const newSpan = newText.length
139+
const oldSpan = oldText.length
140+
diff += newSpan - oldSpan
141+
return diff
142+
}, 0)
133143
return {
134144
edits: fileEdits,
135145
renameFilename,
136-
renameLocation: insertChange.span.start + 1,
137-
// renameLocation: tsFull.getRenameLocation(fileEdits, fileName, componentName, /*preferLastLocation*/ false),
146+
renameLocation: functionChange.span.start + diff,
138147
}
139148
}
140149

0 commit comments

Comments
 (0)