Skip to content

Commit

Permalink
(fix) add prefix/suffix to rename (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm authored Jan 13, 2021
1 parent f9db434 commit 092e90c
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,23 @@ export class RenameProviderImpl implements RenameProvider {
return null;
}

const renameLocations = lang.findRenameLocations(tsDoc.filePath, offset, false, false);
const renameLocations = lang.findRenameLocations(
tsDoc.filePath,
offset,
false,
false,
true
);
if (!renameLocations) {
return null;
}

const docs = new Map<string, SnapshotFragment>([[tsDoc.filePath, fragment]]);
let convertedRenameLocations: Array<ts.RenameLocation & {
range: Range;
}> = await this.mapAndFilterRenameLocations(renameLocations, docs);
let convertedRenameLocations: Array<
ts.RenameLocation & {
range: Range;
}
> = await this.mapAndFilterRenameLocations(renameLocations, docs);
// eslint-disable-next-line max-len
const additionalRenameForPropRenameInsideComponentWithProp = await this.getAdditionLocationsForRenameOfPropInsideComponentWithProp(
document,
Expand Down Expand Up @@ -94,7 +102,10 @@ export class RenameProviderImpl implements RenameProvider {
if (!acc.changes[uri]) {
acc.changes[uri] = [];
}
acc.changes[uri].push({ newText: newName, range: loc.range });
acc.changes[uri].push({
newText: (loc.prefixText || '') + newName + (loc.suffixText || ''),
range: loc.range
});
return acc;
},
<Required<Pick<WorkspaceEdit, 'changes'>>>{ changes: {} }
Expand Down Expand Up @@ -137,7 +148,8 @@ export class RenameProviderImpl implements RenameProvider {
/**
* If user renames prop of component A inside component A,
* we need to handle the rename of the prop of A ourselves.
* Reason: the rename will do {oldPropName: newPropName}, we have to handle
* Reason: the rename will do {oldPropName: newPropName}, meaning
* the rename will not propagate further, so we have to handle
* the conversion to {newPropName: newPropName} ourselves.
*/
private async getAdditionLocationsForRenameOfPropInsideComponentWithProp(
Expand Down Expand Up @@ -196,8 +208,9 @@ export class RenameProviderImpl implements RenameProvider {
* If user renames prop of component A inside component B,
* we need to handle the rename of the prop of A ourselves.
* Reason: the rename will rename the prop in the computed svelte2tsx code,
* but not the `export let X` code in the original. This additional logic
* is done in this method.
* but not the `export let X` code in the original because the
* rename does not propagate further than the prop.
* This additional logic/propagation is done in this method.
*/
private async getAdditionalLocationsForRenameOfPropInsideOtherComponent(
convertedRenameLocations: Array<ts.RenameLocation & { range: Range }>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const testDir = path.join(__dirname, '..');

describe('RenameProvider', () => {
function getFullPath(filename: string) {
return path.join(testDir, 'testfiles', filename);
return path.join(testDir, 'testfiles', 'rename', filename);
}

function getUri(filename: string) {
Expand All @@ -34,7 +34,17 @@ describe('RenameProvider', () => {
const renameDoc3 = await openDoc('rename3.svelte');
const renameDoc4 = await openDoc('rename4.svelte');
const renameDoc5 = await openDoc('rename5.svelte');
return { provider, renameDoc1, renameDoc2, renameDoc3, renameDoc4, renameDoc5, docManager };
const renameDoc6 = await openDoc('rename6.svelte');
return {
provider,
renameDoc1,
renameDoc2,
renameDoc3,
renameDoc4,
renameDoc5,
renameDoc6,
docManager
};

async function openDoc(filename: string) {
const filePath = getFullPath(filename);
Expand Down Expand Up @@ -408,4 +418,55 @@ describe('RenameProvider', () => {

assert.deepStrictEqual(result, null);
});

it('should rename with prefix', async () => {
const { provider, renameDoc6 } = await setup();
const result = await provider.rename(renameDoc6, Position.create(3, 9), 'newName');

assert.deepStrictEqual(result, {
changes: {
[getUri('rename6.svelte')]: [
{
newText: 'newName',
range: {
start: {
character: 8,
line: 3
},
end: {
character: 11,
line: 3
}
}
},
{
newText: 'foo: newName',
range: {
start: {
character: 16,
line: 4
},
end: {
character: 19,
line: 4
}
}
},
{
newText: 'foo: newName',
range: {
start: {
character: 18,
line: 7
},
end: {
character: 21,
line: 7
}
}
}
]
}
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script lang="ts">
function action(_: any, props: {foo: number}) {}
const foo = 1;
action(null, {foo});
</script>

<div use:action={{foo}} />

0 comments on commit 092e90c

Please sign in to comment.