Graphs that teach > graphs that impress. Turn any code into an interactive knowledge graph you can explore, search, and ask questions about. Works with Claude Code, Codex, Cursor, Copilot, Gemini CLI, and more.
TypeScript antigravity-skillsbusiness-knowledgeclaude-codeclaude-skillscodebase-analysiscodexcodex-skillsdeveloper-tools-ai-agentgemini-cli-skillskarpathy-llm-wikiknowledge-baseknowledge-graphmemoryopencode-skillspi-agentunderstandcodevibe-coding
fix(import-map): parse tsconfig JSONC safely
Lum1104c65580f7e6e1
diff --git a/tests/skill/understand/test_extract_import_map.test.mjs b/tests/skill/understand/test_extract_import_map.test.mjsindex 30b17a3d..4d8c95b8 100644--- a/tests/skill/understand/test_extract_import_map.test.mjs+++ b/tests/skill/understand/test_extract_import_map.test.mjs@@ -1962,6 +1962,36 @@ describe('extract-import-map.mjs — tsconfig parse resilience', () => {]);});+ it('accepts JSONC comments and trailing commas without reporting a failure', () => {+ projectRoot = setupTree({+ 'tsconfig.json': `{+ // aliases may point at URL-like strings without losing // in quotes+ "compilerOptions": {+ "baseUrl": ".",+ "paths": {+ "@/*": ["src/*",],+ "@url/*": ["https://example.test/*",],+ },+ },+ }`,+ 'src/index.ts': `import { value } from '@/value';\n`,+ 'src/value.ts': 'export const value = 1;\n',+ });++ const result = runScript(projectRoot, {+ projectRoot,+ files: [+ { path: 'tsconfig.json', language: 'json', fileCategory: 'config' },+ { path: 'src/index.ts', language: 'typescript', fileCategory: 'code' },+ { path: 'src/value.ts', language: 'typescript', fileCategory: 'code' },+ ],+ });++ expect(result.status, result.stderr).toBe(0);+ expect(result.output.failures).toEqual([]);+ expect(result.output.importMap['src/index.ts']).toEqual(['src/value.ts']);+ });+it('falls back to raw-text parse when a paths value contains "//" that the stripper would damage', () => {// tsconfig with NO comments but a string literal containing "//". The// naive stripper would chew the second `//` away and break the JSON;diff --git a/understand-anything-plugin/skills/understand/extract-import-map.mjs b/understand-anything-plugin/skills/understand/extract-import-map.mjsindex ea2a9414..1af9698b 100644--- a/understand-anything-plugin/skills/understand/extract-import-map.mjs+++ b/understand-anything-plugin/skills/understand/extract-import-map.mjs@@ -204,16 +204,74 @@ function dirOf(p) {* with the exact tsconfig path that failed; bubbling the error would* conceal which file was at fault when many tsconfigs are loaded.*/+function normalizeJsonc(raw) {+ let withoutComments = '';+ let inString = false;+ let escaped = false;+ for (let i = 0; i < raw.length; i++) {+ const ch = raw[i];+ const next = raw[i + 1];+ if (inString) {+ withoutComments += ch;+ if (escaped) escaped = false;+ else if (ch === '\\') escaped = true;+ else if (ch === '"') inString = false;+ continue;+ }+ if (ch === '"') {+ inString = true;+ withoutComments += ch;+ continue;+ }+ if (ch === '/' && next === '/') {+ while (i < raw.length && raw[i] !== '\n') i++;+ if (i < raw.length) withoutComments += '\n';+ continue;+ }+ if (ch === '/' && next === '*') {+ i += 2;+ while (i < raw.length && !(raw[i] === '*' && raw[i + 1] === '/')) {+ if (raw[i] === '\n') withoutComments += '\n';+ i++;+ }+ i++;+ continue;+ }+ withoutComments += ch;+ }++ let normalized = '';+ inString = false;+ escaped = false;+ for (let i = 0; i < withoutComments.length; i++) {+ const ch = withoutComments[i];+ if (inString) {+ normalized += ch;+ if (escaped) escaped = false;+ else if (ch === '\\') escaped = true;+ else if (ch === '"') inString = false;+ continue;+ }+ if (ch === '"') {+ inString = true;+ normalized += ch;+ continue;+ }+ if (ch === ',') {+ let nextIndex = i + 1;+ while (/\s/.test(withoutComments[nextIndex] ?? '')) nextIndex++;+ if (withoutComments[nextIndex] === '}' || withoutComments[nextIndex] === ']') continue;+ }+ normalized += ch;+ }+ return normalized.replace(/^\uFEFF/, '');+}+function parseTsConfigText(raw) {- // tsconfig.json often contains JSONC-style comments; strip line and block- // comments before parsing. The strip is naive (it doesn't honor string- // contents), so we fall back to the raw text on failure.- const stripped = raw- .replace(/\/\*[\s\S]*?\*\//g, '')- .replace(/(^|[^:])\/\/.*$/gm, '$1');+ const normalized = normalizeJsonc(raw);let parsed;try {- parsed = JSON.parse(stripped);+ parsed = JSON.parse(normalized);} catch {try {parsed = JSON.parse(raw);