Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ The lines that follow identify each reference by file, line, column, kind, and s
| How do I save the full snapshot? | `node dist/cli.js test/fixtures/basic --format json --output inventory.json` |
| How do I scan one TypeScript project? | `node dist/cli.js . --project tsconfig.build.json` |

In a Git checkout, run `npx code-inventory . --changed-since main` to see the declarations touched by tracked
source changes, their exports, and references grouped by caller or test file. Deleted files and removed base lines
appear separately. The query uses the current checkout, including staged and unstaged edits.

For convention-based entry points, use a quoted pattern such as `--entry-glob 'src/routes/**/+page.ts'`. Combine it with `--package-entries` to include a package’s public entry files.

To review what an external caller can reach by invoking a handler, use `--entry-symbol GET`. If several declarations share that name, the CLI lists their snapshot IDs. File entries trace module initialization. Combining both options includes both starting points.
Expand Down
10 changes: 10 additions & 0 deletions docs/reference.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ <h2 id="cli">CLI options</h2>
</dd>
<dt><code>--symbol name-or-id</code></dt>
<dd>Query one declaration by exact unique name or snapshot ID. Supports summary and JSON.</dd>
<dt><code>--changed-since revision</code></dt>
<dd>
Compare a local Git revision directly with the tracked working tree under the scan root, including staged
and unstaged changes. Untracked files enter the diff once staged. The summary groups current references by
caller or test file and shows module exports, including re-exports. Changed lines select overlapping
declarations, including an enclosing declaration when the line also contains its own code. Reads include
callback arguments. Test references identify source sites and do not establish runtime coverage. Removed
lines use base coordinates. Deleted files and the old side of renames have no current declaration to query.
Use this option on its own with summary output.
</dd>
<dt><code>--package-entries</code></dt>
<dd>
Trace module initialization from the root package's exports (or main) and bin targets. All runtime
Expand Down
76 changes: 38 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "code-inventory",
"version": "0.2.1",
"version": "0.3.0",
"license": "MIT",
"type": "module",
"description": "Static code inventory for JavaScript and TypeScript. Find symbol usages and trace paths from entry points.",
Expand Down Expand Up @@ -53,9 +53,9 @@
},
"devDependencies": {
"@types/node": "26.6.6",
"vitest": "5.1.0",
"prettier": "3.10.1",
"eslint": "10.15.0",
"typescript-eslint": "8.73.0"
"prettier": "3.10.1",
"typescript-eslint": "8.73.0",
"vitest": "5.1.1"
}
}
152 changes: 152 additions & 0 deletions src/change-impact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import { execFileSync } from 'node:child_process';
import { resolve } from 'node:path';
import ts from 'typescript';
import { sourceExtension } from './discovery.ts';
import type { CodeSymbol, Inventory, Reference } from './model.ts';

function git(directory: string, ...args: string[]): string {
return execFileSync('git', ['--literal-pathspecs', ...args], {
cwd: directory,
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
timeout: 30_000,
maxBuffer: 64 * 1024 * 1024,
});
}

function range(first: number, last: number): string {
return first === last ? String(first) : `${first}-${last}`;
}

/** Check the part of a changed line belonging to the parent between nested declaration spans. */
function ownsText(source: string, parent: CodeSymbol, children: CodeSymbol[], start: number, end: number): boolean {
let cursor = Math.max(start, parent.location.start);
const limit = Math.min(end, parent.location.end);
for (const child of children) {
if (source.slice(cursor, Math.min(limit, child.location.start)).trim()) return true;
cursor = Math.max(cursor, child.location.end);
}
return Boolean(source.slice(cursor, limit).trim());
}

/** Intersect Git's LF-delimited lines with the compiler's end-exclusive UTF-16 spans. */
function mapHunks(
patch: string,
file: string,
source: string,
declarations: CodeSymbol[],
changed: Set<CodeSymbol>,
notes: string[],
): void {
const starts = [0];
for (const newline of source.matchAll(/\n/g)) starts.push(newline.index + 1);
declarations.sort((a, b) => a.location.start - b.location.start);
let next = 0;
let active: CodeSymbol[] = [];
let hunks = 0;
for (const hunk of patch.matchAll(/(?:^|\n)@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/g)) {
hunks++;
const oldStart = Number(hunk[1]);
const oldCount = Number(hunk[2] ?? 1);
const first = Number(hunk[3]);
const count = Number(hunk[4] ?? 1);
if (oldCount) notes.push(`Removed base lines: ${file}:${range(oldStart, oldStart + oldCount - 1)}`);
let outsideStart: number | undefined;
// A pure deletion has a zero-width position between two surviving lines.
for (let line = first; line < first + Math.max(1, count); line++) {
const start = starts[count ? line - 1 : line] ?? source.length;
const end = count ? (starts[line] ?? source.length) : start;
while (next < declarations.length && declarations[next]!.location.start < end) {
active.push(declarations[next++]!);
}
active = active.filter((symbol) => symbol.location.end > start);
const children = groupBy(active, (symbol) => symbol.containerId);
for (const symbol of active) {
const nested = children.get(symbol.id);
if (!nested || ownsText(source, symbol, nested, start, end)) changed.add(symbol);
}
if (count && !active.length) outsideStart ??= line;
if (outsideStart !== undefined && (active.length || line === first + count - 1)) {
notes.push(`Outside a declaration: ${file}:${range(outsideStart, active.length ? line - 1 : line)}`);
outsideStart = undefined;
}
}
}
if (!hunks) notes.push(`No text hunks: ${file}`);
}

function groupBy<T, K>(items: T[], keyOf: (item: T) => K): Map<K, T[]> {
const groups = new Map<K, T[]>();
for (const item of items) {
const key = keyOf(item);
const group = groups.get(key);
if (group) group.push(item);
else groups.set(key, [item]);
}
return groups;
}

function referenceGroups(references: Reference[], byId: Map<string, CodeSymbol>): string[] {
const groups = new Map<string, { label: string; sites: string[] }>();
for (const ref of references) {
const { file, line, column } = ref.location;
const caller = ref.callerId ? byId.get(ref.callerId) : undefined;
const container = caller?.containerId ? byId.get(caller.containerId) : undefined;
const label =
ref.scope === 'test' ? file : caller ? [container?.name, caller.name].filter(Boolean).join('.') : file;
const key = ref.scope === 'test' ? file : (ref.callerId ?? file);
let group = groups.get(key);
if (!group) {
group = { label: `[${ref.scope}] ${label}`, sites: [] };
groups.set(key, group);
}
group.sites.push(`${ref.kind} ${file}:${line}:${column}`);
}
return Array.from(groups.values(), ({ label, sites }) => ` ${label}: ${sites.join(', ')}`);
}

/** Compare a tree to the tracked working tree, resolving references only in the current inventory. */
export function changeImpactText(directory: string, base: string, inventory: Inventory): string {
if (!base) throw new Error('Provide a Git revision for --changed-since.');
const tree = git(directory, 'rev-parse', '--verify', '--end-of-options', base + '^{tree}').trim();
const diff = ['diff', '--relative', '--no-ext-diff', '--no-textconv', '--no-renames', '--no-color'];
const fields = git(directory, ...diff, '--name-status', '-z', tree, '--', '.').split('\0');
const files = new Map(inventory.files.map((file) => [file.path, file]));
const symbolsByFile = groupBy(inventory.symbols, (symbol) => symbol.location.file);
const changed = new Set<CodeSymbol>();
const notes: string[] = [];
for (let i = 0; i + 1 < fields.length; i += 2) {
const status = fields[i];
const path = fields[i + 1]!;
if (!sourceExtension.test(path)) continue;
if (status === 'D') {
notes.push(`Deleted file: ${path}`);
continue;
}
const file = files.get(path);
if (file?.status !== 'analyzed') {
notes.push(`Outside scan: ${path} (${file?.reason ?? 'not inventoried'})`);
continue;
}
const patch = git(directory, ...diff, '--unified=0', '--inter-hunk-context=0', tree, '--', path);
const source = ts.sys.readFile(resolve(directory, path));
if (source === undefined) throw new Error(`Cannot read changed file: ${path}`);
mapHunks(patch, path, source, symbolsByFile.get(path) ?? [], changed, notes);
}
const byId = new Map(inventory.symbols.map((symbol) => [symbol.id, symbol]));
const byTarget = groupBy(inventory.references, (ref) => ref.targetId);
const lines = [`Change impact since ${base}`];
for (const symbol of changed) {
lines.push('', `${symbol.kind} ${symbol.name} (${symbol.location.file}:${symbol.location.line})`);
if (symbol.exportedAs.length) lines.push(` Exports: ${symbol.exportedAs.join(', ')}`);
const groups = referenceGroups(byTarget.get(symbol.id) ?? [], byId);
lines.push(...(groups.length ? groups : [' No references found in this scan.']));
}
if (!changed.size && !notes.length) lines.push('No tracked source changes.');
return [
...lines,
...notes,
`Analysis: ${inventory.completeness}; ${inventory.diagnostics.length} diagnostics`,
'',
].join('\n');
}
Loading