Skip to content

Commit 29acc98

Browse files
committed
Fix: Enhance parsing logic to handle escaped characters and string boundaries
1 parent fc571a5 commit 29acc98

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

packages/core/src/utils/parse-pairs.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,36 @@ export default function parsePairs(
1313
): Pair | null {
1414
const stack: { start: number }[] = []
1515
let result: Pair | null = null
16+
let inString: '"' | '\'' | null = null
17+
1618
for (let i = 0; i < str.length; i++) {
17-
// 如果是被 escape 的括號就跳過
18-
if (str[i] === '\\') {
19-
i++
19+
const char = str[i]
20+
const prevChar = str[i - 1]
21+
22+
// 處理 escape 字元,例如 \"
23+
if (char === '\\') {
24+
i++ // skip next character
25+
continue
26+
}
27+
28+
// 處理字串區段進入與離開
29+
if ((char === '"' || char === '\'') && prevChar !== '\\') {
30+
if (inString === char) {
31+
inString = null // 離開字串
32+
} else if (!inString) {
33+
inString = char // 進入字串
34+
}
2035
continue
2136
}
22-
// 遇到開始符號
37+
38+
if (inString) continue // 若在字串中就忽略括號處理
39+
40+
// 開始符號
2341
if (str.startsWith(a, i)) {
2442
stack.push({ start: i })
2543
i += a.length - 1
2644
}
27-
// 遇到結束符號
45+
// 結束符號
2846
else if (str.startsWith(b, i)) {
2947
const last = stack.pop()
3048
if (last) {

0 commit comments

Comments
 (0)