Fix dollar sign replacement bug in file editing (#7871)

Co-authored-by: Jacob Richman <jacob314@gmail.com>
This commit is contained in:
fuyou
2025-09-13 14:05:43 +08:00
committed by GitHub
parent 2c2c6549a2
commit 331fcfa893
8 changed files with 239 additions and 24 deletions
+80
View File
@@ -194,6 +194,86 @@ describe('EditTool', () => {
'hello world',
);
});
it('should treat $ literally and not as replacement pattern', () => {
const current = "price is $100 and pattern end is ' '";
const oldStr = 'price is $100';
const newStr = 'price is $200';
const result = applyReplacement(current, oldStr, newStr, false);
expect(result).toBe("price is $200 and pattern end is ' '");
});
it("should treat $' literally and not as a replacement pattern", () => {
const current = 'foo';
const oldStr = 'foo';
const newStr = "bar$'baz";
const result = applyReplacement(current, oldStr, newStr, false);
expect(result).toBe("bar$'baz");
});
it('should treat $& literally and not as a replacement pattern', () => {
const current = 'hello world';
const oldStr = 'hello';
const newStr = '$&-replacement';
const result = applyReplacement(current, oldStr, newStr, false);
expect(result).toBe('$&-replacement world');
});
it('should treat $` literally and not as a replacement pattern', () => {
const current = 'prefix-middle-suffix';
const oldStr = 'middle';
const newStr = 'new$`content';
const result = applyReplacement(current, oldStr, newStr, false);
expect(result).toBe('prefix-new$`content-suffix');
});
it('should treat $1, $2 capture groups literally', () => {
const current = 'test string';
const oldStr = 'test';
const newStr = '$1$2replacement';
const result = applyReplacement(current, oldStr, newStr, false);
expect(result).toBe('$1$2replacement string');
});
it('should use replaceAll for normal strings without problematic $ sequences', () => {
const current = 'normal text replacement';
const oldStr = 'text';
const newStr = 'string';
const result = applyReplacement(current, oldStr, newStr, false);
expect(result).toBe('normal string replacement');
});
it('should handle multiple occurrences with problematic $ sequences', () => {
const current = 'foo bar foo baz';
const oldStr = 'foo';
const newStr = "test$'end";
const result = applyReplacement(current, oldStr, newStr, false);
expect(result).toBe("test$'end bar test$'end baz");
});
it('should handle complex regex patterns with $ at end', () => {
const current = "| select('match', '^[sv]d[a-z]$')";
const oldStr = "'^[sv]d[a-z]$'";
const newStr = "'^[sv]d[a-z]$' # updated";
const result = applyReplacement(current, oldStr, newStr, false);
expect(result).toBe("| select('match', '^[sv]d[a-z]$' # updated)");
});
it('should handle empty replacement with problematic $ in newString', () => {
const current = 'test content';
const oldStr = 'nothing';
const newStr = "replacement$'text";
const result = applyReplacement(current, oldStr, newStr, false);
expect(result).toBe('test content'); // No replacement because oldStr not found
});
it('should handle $$ (escaped dollar) correctly', () => {
const current = 'price value';
const oldStr = 'value';
const newStr = '$$100';
const result = applyReplacement(current, oldStr, newStr, false);
expect(result).toBe('price $$100');
});
});
describe('validateToolParams', () => {