Skip to content

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.

feat(import-map): support selective incremental extraction

Lum11043fa05ed9bdd7
diff --git a/tests/skill/understand/test_extract_import_map.test.mjs b/tests/skill/understand/test_extract_import_map.test.mjs
index fb5994cd..ad9d5489 100644
--- a/tests/skill/understand/test_extract_import_map.test.mjs
+++ b/tests/skill/understand/test_extract_import_map.test.mjs
@@ -97,6 +97,69 @@ describe('extract-import-map.mjs — TypeScript / JavaScript resolver', () => {
expect(result.output.stats.totalEdges).toBe(2);
});
+ it('uses the full inventory for resolution while emitting only analysisPaths', () => {
+ projectRoot = setupTree({
+ 'src/changed.ts': `import { stable } from './stable';\nexport const changed = stable;\n`,
+ 'src/stable.ts': `export const stable = 1;\n`,
+ 'src/untouched.ts': `import './stable';\n`,
+ });
+
+ const result = runScript(projectRoot, {
+ projectRoot,
+ files: [
+ { path: 'src/changed.ts', language: 'typescript', fileCategory: 'code' },
+ { path: 'src/stable.ts', language: 'typescript', fileCategory: 'code' },
+ { path: 'src/untouched.ts', language: 'typescript', fileCategory: 'code' },
+ ],
+ analysisPaths: ['src\\changed.ts'],
+ });
+
+ expect(result.status, result.stderr).toBe(0);
+ expect(result.output.importMap).toEqual({
+ 'src/changed.ts': ['src/stable.ts'],
+ });
+ expect(result.output.stats).toEqual({
+ filesScanned: 1,
+ filesWithImports: 1,
+ totalEdges: 1,
+ });
+ });
+
+ it('rejects analysisPaths that are absent from the current inventory', () => {
+ projectRoot = setupTree({
+ 'src/index.ts': 'export const value = 1;\n',
+ });
+
+ const result = runScript(projectRoot, {
+ projectRoot,
+ files: [
+ { path: 'src/index.ts', language: 'typescript', fileCategory: 'code' },
+ ],
+ analysisPaths: ['src/missing.ts'],
+ });
+
+ expect(result.status).not.toBe(0);
+ expect(result.stderr).toContain('analysisPaths entry is not present in files');
+ });
+
+ it('supports an empty analysisPaths list without emitting full-scan entries', () => {
+ projectRoot = setupTree({
+ 'src/index.ts': 'export const value = 1;\n',
+ });
+
+ const result = runScript(projectRoot, {
+ projectRoot,
+ files: [
+ { path: 'src/index.ts', language: 'typescript', fileCategory: 'code' },
+ ],
+ analysisPaths: [],
+ });
+
+ expect(result.status, result.stderr).toBe(0);
+ expect(result.output.importMap).toEqual({});
+ expect(result.output.stats.filesScanned).toBe(0);
+ });
+
it('orders Unicode import targets by locale-independent UTF-16 code units', () => {
projectRoot = setupTree({
'src/index.ts': `import './ä';\nimport './Z';\nimport './a';\n`,
diff --git a/understand-anything-plugin/skills/understand/extract-import-map.mjs b/understand-anything-plugin/skills/understand/extract-import-map.mjs
index ef1034b4..27d02398 100644
--- a/understand-anything-plugin/skills/understand/extract-import-map.mjs
+++ b/understand-anything-plugin/skills/understand/extract-import-map.mjs
@@ -18,9 +18,15 @@
* Input JSON:
* {
* projectRoot: <abs-path>,
- * files: [{ path, language, fileCategory }, ...]
+ * files: [{ path, language, fileCategory }, ...],
+ * analysisPaths?: [<project-relative-path>, ...]
* }
*
+ * `files` is always the complete current inventory because import resolution
+ * needs it for path/module probes. When `analysisPaths` is present, only those
+ * files are read and emitted in `importMap`; omitting it preserves the original
+ * full-scan behaviour.
+ *
* Output JSON:
* {
* scriptCompleted: true,
@@ -96,6 +102,50 @@ function toPosix(p) {
return p.split(/[\\/]/).filter(Boolean).join('/');
}
+/**
+ * Validate and normalize the optional selective-analysis path list. Keeping
+ * this strict prevents an incremental caller from accidentally asking the
+ * extractor to read outside projectRoot or silently miss a typo.
+ */
+function selectAnalysisFiles(files, analysisPaths) {
+ if (analysisPaths === undefined) return files;
+ if (!Array.isArray(analysisPaths)) {
+ throw new Error('Invalid input: analysisPaths must be an array when provided');
+ }
+
+ const filesByPath = new Map();
+ for (const file of files) {
+ if (!file || typeof file.path !== 'string' || file.path.length === 0) {
+ throw new Error('Invalid input: every files entry must contain a non-empty path');
+ }
+ filesByPath.set(toPosix(file.path), file);
+ }
+
+ const selected = [];
+ const seen = new Set();
+ for (const rawPath of analysisPaths) {
+ if (typeof rawPath !== 'string' || rawPath.length === 0) {
+ throw new Error('Invalid input: every analysisPaths entry must be a non-empty string');
+ }
+ if (/^(?:[A-Za-z]:[\\/]|[\\/])/.test(rawPath)) {
+ throw new Error(`Invalid input: analysisPaths entry must be project-relative: ${rawPath}`);
+ }
+ const path = toPosix(rawPath);
+ if (!path || path.split('/').some(part => part === '..')) {
+ throw new Error(`Invalid input: analysisPaths entry escapes projectRoot: ${rawPath}`);
+ }
+ const file = filesByPath.get(path);
+ if (!file) {
+ throw new Error(`Invalid input: analysisPaths entry is not present in files: ${rawPath}`);
+ }
+ if (!seen.has(path)) {
+ seen.add(path);
+ selected.push(file);
+ }
+ }
+ return selected;
+}
+
// ECMAScript relational string comparison is lexicographic over UTF-16 code
// units, so path ordering is stable across ICU versions, locales, and hosts.
function comparePaths(a, b) {
@@ -1806,12 +1856,27 @@ async function main() {
const inputRaw = readFileSync(inputPath, 'utf-8');
const input = JSON.parse(inputRaw);
- const { projectRoot, files } = input;
+ const { projectRoot, files, analysisPaths } = input;
if (!projectRoot || !Array.isArray(files)) {
throw new Error('Invalid input: must contain projectRoot and files array');
}
+ const analysisFiles = selectAnalysisFiles(files, analysisPaths);
+
+ if (analysisFiles.length === 0) {
+ const output = {
+ scriptCompleted: true,
+ stats: { filesScanned: 0, filesWithImports: 0, totalEdges: 0 },
+ importMap: {},
+ };
+ writeFileSync(outputPath, JSON.stringify(output, null, 2), 'utf-8');
+ process.stderr.write(
+ 'extract-import-map: filesScanned=0 filesWithImports=0 totalEdges=0\n',
+ );
+ return;
+ }
+
// Create tree-sitter plugin with all configs that have WASM grammars.
//
// WHY graceful init: the most likely real-world failure mode is the WASM
@@ -1848,7 +1913,7 @@ async function main() {
let filesWithImports = 0;
let totalEdges = 0;
- for (const file of files) {
+ for (const file of analysisFiles) {
const path = toPosix(file.path);
// Non-code files always get an empty array
@@ -1941,7 +2006,7 @@ async function main() {
const output = {
scriptCompleted: true,
stats: {
- filesScanned: files.length,
+ filesScanned: analysisFiles.length,
filesWithImports,
totalEdges,
},
@@ -1955,7 +2020,7 @@ async function main() {
}
process.stderr.write(
- `extract-import-map: filesScanned=${files.length} ` +
+ `extract-import-map: filesScanned=${analysisFiles.length} ` +
`filesWithImports=${filesWithImports} totalEdges=${totalEdges}\n`,
);
}