Skip to content

Commit 29786f2

Browse files
committed
fix(common/utils): invalid escape character parsing
1 parent 27657a1 commit 29786f2

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/common/utils/common.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@ const parseStringRecursively = (str: string): string => {
6262
}
6363
catch (error) {
6464
if (error instanceof SyntaxError) {
65-
const charIndex = Number(error.message.split(' ').slice(-1)[0])
65+
// Node.js 20: Bad escaped character in JSON at position 3
66+
// Node.js 22: Bad escaped character in JSON at position 3 (line 1 column 4)
67+
const positionMatch = error.message.match(/at position (\d+)/)
68+
const charIndex = positionMatch && Number(positionMatch[1])
6669
// invalid escape character or number
67-
if (str[charIndex - 1] === '\\') {
70+
if (charIndex && str[charIndex - 1] === '\\') {
6871
return parseStringRecursively(str.slice(0, charIndex - 1) + str.slice(charIndex))
6972
}
7073
}

0 commit comments

Comments
 (0)