| 64 | } |
| 65 | |
| 66 | function stripTrailingCommas(content: string): string { |
| 67 | let result = ""; |
| 68 | let inString = false; |
| 69 | let escaped = false; |
| 70 | |
| 71 | for (let index = 0; index < content.length; index += 1) { |
| 72 | const char = content[index]; |
| 73 | |
| 74 | if (inString) { |
| 75 | result += char; |
| 76 | if (escaped) { |
| 77 | escaped = false; |
| 78 | } else if (char === "\\") { |
| 79 | escaped = true; |
| 80 | } else if (char === '"') { |
| 81 | inString = false; |
| 82 | } |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | if (char === '"') { |
| 87 | inString = true; |
| 88 | result += char; |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | if (char === ",") { |
| 93 | let lookahead = index + 1; |
| 94 | while (lookahead < content.length && /\s/.test(content[lookahead] ?? "")) { |
| 95 | lookahead += 1; |
| 96 | } |
| 97 | const next = content[lookahead]; |
| 98 | if (next === "}" || next === "]") { |
| 99 | continue; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | result += char; |
| 104 | } |
| 105 | |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | export function parseJsonc<T = unknown>(content: string): T { |
| 110 | const normalized = stripTrailingCommas(stripJsonComments(content)); |