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
+21
View File
@@ -4,6 +4,27 @@
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Safely replaces text with literal strings, avoiding ECMAScript GetSubstitution issues.
* Escapes $ characters to prevent template interpretation.
*/
export function safeLiteralReplace(
str: string,
oldString: string,
newString: string,
): string {
if (oldString === '' || !str.includes(oldString)) {
return str;
}
if (!newString.includes('$')) {
return str.replaceAll(oldString, newString);
}
const escapedNewString = newString.replaceAll('$', '$$$$');
return str.replaceAll(oldString, escapedNewString);
}
/**
* Checks if a Buffer is likely binary by testing for the presence of a NULL byte.
* The presence of a NULL byte is a strong indicator that the data is not plain text.