// JSONC is used by VS Code configuration. Keep strings intact while removing // comments and trailing commas; JSON.parse still validates everything else. export function jsonc(text) { let out = '', quoted = false, escaped = false; for (let i = 0; i < text.length; i++) { const c = text[i], next = text[i + 1]; if (quoted) { out += c; if (escaped) escaped = false; else if (c === '\\') escaped = true; else if (c === '"') quoted = false; } else if (c === '"') { quoted = true; out += c; } else if (c === '/' && next === '/') { while (i < text.length && text[i] !== '\n') i++; out += '\n'; } else if (c === '/' && next === '*') { i += 2; while (i < text.length && !(text[i] === '*' && text[i + 1] === '/')) { out += text[i] === '\n' ? '\n' : ' '; i++; } if (i >= text.length) throw new SyntaxError('Unterminated JSONC comment'); i++; out += ' '; } else out += c; } let clean = ''; quoted = false; escaped = false; for (let i = 0; i < out.length; i++) { const c = out[i]; if (quoted) { clean += c; if (escaped) escaped = false; else if (c === '\\') escaped = true; else if (c === '"') quoted = false; } else if (c === '"') { quoted = true; clean += c; } else if (c !== ',' || !/^\s*[}\]]/.test(out.slice(i + 1))) clean += c; } return JSON.parse(clean); }