Skip to content

extractMethod: Support renameLocation #18351

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 1 commit into from
Sep 9, 2017
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
2 changes: 1 addition & 1 deletion Gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ gulp.task("browserify", "Runs browserify on run.js to produce a file suitable fo
const file = new Vinyl({ contents, path: bundlePath });
console.log(`Fixing sourcemaps for ${file.path}`);
// assumes contents is a Buffer, since that's what browserify yields
const maps = convertMap.fromSource(stringContent, /*largeSource*/ true).toObject();
const maps = convertMap.fromSource(stringContent).toObject();
delete maps.sourceRoot;
maps.sources = maps.sources.map(s => path.resolve(s === "_stream_0.js" ? "built/local/_stream_0.js" : s));
// Strip browserify's inline comments away (could probably just let sorcery do this, but then we couldn't fix the paths)
Expand Down
38 changes: 34 additions & 4 deletions src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2736,11 +2736,11 @@ namespace FourSlash {
}
}

private getSelection() {
return ({
private getSelection(): ts.TextRange {
return {
pos: this.currentCaretPosition,
end: this.selectionEnd === -1 ? this.currentCaretPosition : this.selectionEnd
});
};
}

public verifyRefactorAvailable(negative: boolean, name: string, actionName?: string) {
Expand Down Expand Up @@ -2781,7 +2781,7 @@ namespace FourSlash {
}
}

public applyRefactor({ refactorName, actionName, actionDescription }: FourSlashInterface.ApplyRefactorOptions) {
public applyRefactor({ refactorName, actionName, actionDescription, newContent: newContentWithRenameMarker }: FourSlashInterface.ApplyRefactorOptions) {
const range = this.getSelection();
const refactors = this.languageService.getApplicableRefactors(this.activeFile.fileName, range);
const refactor = refactors.find(r => r.name === refactorName);
Expand All @@ -2801,6 +2801,35 @@ namespace FourSlash {
for (const edit of editInfo.edits) {
this.applyEdits(edit.fileName, edit.textChanges, /*isFormattingEdit*/ false);
}

const { renamePosition, newContent } = parseNewContent();

this.verifyCurrentFileContent(newContent);

if (renamePosition === undefined) {
if (editInfo.renameLocation !== undefined) {
this.raiseError(`Did not expect a rename location, got ${editInfo.renameLocation}`);
}
}
else {
// TODO: test editInfo.renameFilename value
assert.isDefined(editInfo.renameFilename);
if (renamePosition !== editInfo.renameLocation) {
this.raiseError(`Expected rename position of ${renamePosition}, but got ${editInfo.renameLocation}`);
}
}

function parseNewContent(): { renamePosition: number | undefined, newContent: string } {
const renamePosition = newContentWithRenameMarker.indexOf("/*RENAME*/");
if (renamePosition === -1) {
return { renamePosition: undefined, newContent: newContentWithRenameMarker };
}
else {
const newContent = newContentWithRenameMarker.slice(0, renamePosition) + newContentWithRenameMarker.slice(renamePosition + "/*RENAME*/".length);
return { renamePosition, newContent };
}
}

}

public verifyFileAfterApplyingRefactorAtMarker(
Expand Down Expand Up @@ -4291,5 +4320,6 @@ namespace FourSlashInterface {
refactorName: string;
actionName: string;
actionDescription: string;
newContent: string;
}
}
11 changes: 7 additions & 4 deletions src/harness/unittests/extractMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ namespace ts {
testExtractRange(`
function f() {
while (true) {
[#|
[#|
if (x) {
return;
} |]
Expand All @@ -234,7 +234,7 @@ namespace ts {
testExtractRange(`
function f() {
while (true) {
[#|
[#|
[$|if (x) {
}
return;|]
Expand Down Expand Up @@ -580,9 +580,12 @@ namespace A {
data.push(`==ORIGINAL==`);
data.push(sourceFile.text);
for (const r of results) {
const changes = refactor.extractMethod.getPossibleExtractions(result.targetRange, context, results.indexOf(r))[0].changes;
const { renameLocation, edits } = refactor.extractMethod.getExtractionAtIndex(result.targetRange, context, results.indexOf(r));
assert.lengthOf(edits, 1);
data.push(`==SCOPE::${r.scopeDescription}==`);
data.push(textChanges.applyChanges(sourceFile.text, changes[0].textChanges));
const newText = textChanges.applyChanges(sourceFile.text, edits[0].textChanges);
const newTextWithRename = newText.slice(0, renameLocation) + "/*RENAME*/" + newText.slice(renameLocation);
data.push(newTextWithRename);
}
return data.join(newLineCharacter);
});
Expand Down
4 changes: 1 addition & 3 deletions src/server/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,7 @@ namespace ts.server {
const response = this.processResponse<protocol.GetEditsForRefactorResponse>(request);

if (!response.body) {
return {
edits: []
};
return { edits: [], renameFilename: undefined, renameLocation: undefined };
}

const edits: FileTextChanges[] = this.convertCodeEditsToTextChanges(response.body.edits);
Expand Down
4 changes: 3 additions & 1 deletion src/services/refactors/convertFunctionToEs6Class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ namespace ts.refactor.convertFunctionToES6Class {
}

return {
edits: changeTracker.getChanges()
edits: changeTracker.getChanges(),
renameFilename: undefined,
renameLocation: undefined,
};

function deleteNode(node: Node, inList = false) {
Expand Down
Loading