From 3f49f0f88af6ca70d52dbba9783d8d262b7b0219 Mon Sep 17 00:00:00 2001 From: Lubrsy706 Date: Thu, 14 May 2026 20:02:06 +0800 Subject: [PATCH] fix(filesystem): treat edit replacements literally --- src/filesystem/__tests__/lib.test.ts | 16 ++++++++++++++++ src/filesystem/lib.ts | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/filesystem/__tests__/lib.test.ts b/src/filesystem/__tests__/lib.test.ts index f7e585af22..54e79c519c 100644 --- a/src/filesystem/__tests__/lib.test.ts +++ b/src/filesystem/__tests__/lib.test.ts @@ -472,6 +472,22 @@ describe('Lib Functions', () => { ); }); + it('treats replacement text as literal content', async () => { + const edits = [ + { oldText: 'line2', newText: 'price=$5; match=$&; group=$1; dollar=$$' } + ]; + + mockFs.rename.mockResolvedValueOnce(undefined); + + await applyFileEdits('/test/file.txt', edits, false); + + expect(mockFs.writeFile).toHaveBeenCalledWith( + expect.stringMatching(/\/test\/file\.txt\.[a-f0-9]+\.tmp$/), + 'line1\nprice=$5; match=$&; group=$1; dollar=$$\nline3\n', + 'utf-8' + ); + }); + it('handles whitespace-flexible matching', async () => { mockFs.readFile.mockResolvedValue(' line1\n line2\n line3\n'); diff --git a/src/filesystem/lib.ts b/src/filesystem/lib.ts index 17e4654cd5..ce4af9f38a 100644 --- a/src/filesystem/lib.ts +++ b/src/filesystem/lib.ts @@ -207,7 +207,7 @@ export async function applyFileEdits( // If exact match exists, use it if (modifiedContent.includes(normalizedOld)) { - modifiedContent = modifiedContent.replace(normalizedOld, normalizedNew); + modifiedContent = modifiedContent.replace(normalizedOld, () => normalizedNew); continue; }