Skip to content

Commit 0b81963

Browse files
committed
fix: handle escaped backslashes before quotes in JSONC parser
The previous check only looked at one preceding character, failing for cases like "test\\" where \\ is an escaped backslash and the final " is the real closing quote. Now counts consecutive backslashes - even count means quote is real, odd count means quote is escaped.
1 parent 38bf301 commit 0b81963

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

src/services/jsonc.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,19 @@ export function stripJsoncComments(content: string): string {
1414
const nextChar = content[i + 1];
1515

1616
if (!inSingleLineComment && !inMultiLineComment) {
17-
if (char === '"' && (i === 0 || content[i - 1] !== "\\")) {
18-
inString = !inString;
17+
if (char === '"') {
18+
// Count consecutive backslashes before this quote
19+
let backslashCount = 0;
20+
let j = i - 1;
21+
while (j >= 0 && content[j] === "\\") {
22+
backslashCount++;
23+
j--;
24+
}
25+
// Quote is escaped only if preceded by ODD number of backslashes
26+
// e.g., \" = escaped, \\" = not escaped (escaped backslash + quote)
27+
if (backslashCount % 2 === 0) {
28+
inString = !inString;
29+
}
1930
result += char;
2031
i++;
2132
continue;

0 commit comments

Comments
 (0)