Skip to content

Commit 00ce9cf

Browse files
kgarg2468olaservo
andauthored
fix(filesystem): preserve literal dollar replacements (#4225)
Co-authored-by: Ola Hungerford <olahungerford@gmail.com>
1 parent ce3b44e commit 00ce9cf

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/filesystem/__tests__/lib.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,22 @@ describe('Lib Functions', () => {
440440
);
441441
});
442442

443+
it('treats dollar signs in replacement text literally', async () => {
444+
const edits = [
445+
{ oldText: 'line2', newText: "price=$$; match=$&; before=$`; after=$'" }
446+
];
447+
448+
mockFs.rename.mockResolvedValueOnce(undefined);
449+
450+
await applyFileEdits('/test/file.txt', edits, false);
451+
452+
expect(mockFs.writeFile).toHaveBeenCalledWith(
453+
expect.stringMatching(/\/test\/file\.txt\.[a-f0-9]+\.tmp$/),
454+
"line1\nprice=$$; match=$&; before=$`; after=$'\nline3\n",
455+
'utf-8'
456+
);
457+
});
458+
443459
it('handles dry run mode', async () => {
444460
const edits = [
445461
{ oldText: 'line2', newText: 'modified line2' }

src/filesystem/lib.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export async function applyFileEdits(
207207

208208
// If exact match exists, use it
209209
if (modifiedContent.includes(normalizedOld)) {
210-
modifiedContent = modifiedContent.replace(normalizedOld, normalizedNew);
210+
modifiedContent = modifiedContent.replace(normalizedOld, () => normalizedNew);
211211
continue;
212212
}
213213

0 commit comments

Comments
 (0)