Skip to content
Open
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
32 changes: 31 additions & 1 deletion src/filesystem/__tests__/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,11 +498,41 @@ describe('Lib Functions', () => {
const edits = [
{ oldText: 'nonexistent line', newText: 'replacement' }
];

await expect(applyFileEdits('/test/file.txt', edits, false))
.rejects.toThrow('Could not find exact match for edit');
});

// Regression test for #4157: newText containing String.prototype.replace
// replacement-pattern characters ($$, $&, $`, $', $<name>) must be treated
// as a literal string, not a pattern. Otherwise content like "$$100 USD"
// silently corrupts to "$100 USD" on the exact-match path.
it('preserves literal $ in newText (issue #4157)', async () => {
mockFs.readFile.mockResolvedValue('price: PLACEHOLDER\n');

const cases: Array<[string, string]> = [
['$$100 USD', 'price: $$100 USD\n'],
['cost: $&', 'price: cost: $&\n'],
['var: $`x$\'', 'price: var: $`x$\'\n'],
['template: $<x>', 'price: template: $<x>\n'],
];

for (const [newText, expected] of cases) {
mockFs.writeFile.mockClear();
mockFs.rename.mockResolvedValueOnce(undefined);

await applyFileEdits('/test/file.txt', [
{ oldText: 'PLACEHOLDER', newText }
], false);

expect(mockFs.writeFile).toHaveBeenCalledWith(
expect.stringMatching(/\/test\/file\.txt\.[a-f0-9]+\.tmp$/),
expected,
'utf-8'
);
}
});

it('handles complex multi-line edits with indentation', async () => {
mockFs.readFile.mockResolvedValue('function test() {\n console.log("hello");\n return true;\n}');

Expand Down
11 changes: 9 additions & 2 deletions src/filesystem/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,16 @@ export async function applyFileEdits(
const normalizedOld = normalizeLineEndings(edit.oldText);
const normalizedNew = normalizeLineEndings(edit.newText);

// If exact match exists, use it
// If exact match exists, use it.
//
// Using the function form of replace() is important: with a string
// replacement, JavaScript interprets `$$`, `$&`, `` $` ``, `$'`, and
// `$<name>` as replacement-pattern syntax, which silently corrupts
// content that contains literal `$` characters (e.g. JS/TS code,
// currency amounts, shell variables). The arrow function returns
// the replacement as a literal string and bypasses the pattern.
if (modifiedContent.includes(normalizedOld)) {
modifiedContent = modifiedContent.replace(normalizedOld, normalizedNew);
modifiedContent = modifiedContent.replace(normalizedOld, () => normalizedNew);
continue;
}

Expand Down
Loading