code-inventory 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/dist/change-impact.d.ts +3 -0
- package/dist/change-impact.js +149 -0
- package/dist/cli.js +41 -26
- package/dist/discovery.d.ts +1 -0
- package/dist/discovery.js +1 -1
- package/docs/reference.html +10 -0
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -37,6 +37,10 @@ The lines that follow identify each reference by file, line, column, kind, and s
|
|
|
37
37
|
| How do I save the full snapshot? | `node dist/cli.js test/fixtures/basic --format json --output inventory.json` |
|
|
38
38
|
| How do I scan one TypeScript project? | `node dist/cli.js . --project tsconfig.build.json` |
|
|
39
39
|
|
|
40
|
+
In a Git checkout, run `npx code-inventory . --changed-since main` to see the declarations touched by tracked
|
|
41
|
+
source changes, their exports, and references grouped by caller or test file. Deleted files and removed base lines
|
|
42
|
+
appear separately. The query uses the current checkout, including staged and unstaged edits.
|
|
43
|
+
|
|
40
44
|
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.
|
|
41
45
|
|
|
42
46
|
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.
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import ts from 'typescript';
|
|
4
|
+
import { sourceExtension } from "./discovery.js";
|
|
5
|
+
function git(directory, ...args) {
|
|
6
|
+
return execFileSync('git', ['--literal-pathspecs', ...args], {
|
|
7
|
+
cwd: directory,
|
|
8
|
+
encoding: 'utf8',
|
|
9
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
10
|
+
timeout: 30_000,
|
|
11
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
function range(first, last) {
|
|
15
|
+
return first === last ? String(first) : `${first}-${last}`;
|
|
16
|
+
}
|
|
17
|
+
/** Check the part of a changed line belonging to the parent between nested declaration spans. */
|
|
18
|
+
function ownsText(source, parent, children, start, end) {
|
|
19
|
+
let cursor = Math.max(start, parent.location.start);
|
|
20
|
+
const limit = Math.min(end, parent.location.end);
|
|
21
|
+
for (const child of children) {
|
|
22
|
+
if (source.slice(cursor, Math.min(limit, child.location.start)).trim())
|
|
23
|
+
return true;
|
|
24
|
+
cursor = Math.max(cursor, child.location.end);
|
|
25
|
+
}
|
|
26
|
+
return Boolean(source.slice(cursor, limit).trim());
|
|
27
|
+
}
|
|
28
|
+
/** Intersect Git's LF-delimited lines with the compiler's end-exclusive UTF-16 spans. */
|
|
29
|
+
function mapHunks(patch, file, source, declarations, changed, notes) {
|
|
30
|
+
const starts = [0];
|
|
31
|
+
for (const newline of source.matchAll(/\n/g))
|
|
32
|
+
starts.push(newline.index + 1);
|
|
33
|
+
declarations.sort((a, b) => a.location.start - b.location.start);
|
|
34
|
+
let next = 0;
|
|
35
|
+
let active = [];
|
|
36
|
+
let hunks = 0;
|
|
37
|
+
for (const hunk of patch.matchAll(/(?:^|\n)@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/g)) {
|
|
38
|
+
hunks++;
|
|
39
|
+
const oldStart = Number(hunk[1]);
|
|
40
|
+
const oldCount = Number(hunk[2] ?? 1);
|
|
41
|
+
const first = Number(hunk[3]);
|
|
42
|
+
const count = Number(hunk[4] ?? 1);
|
|
43
|
+
if (oldCount)
|
|
44
|
+
notes.push(`Removed base lines: ${file}:${range(oldStart, oldStart + oldCount - 1)}`);
|
|
45
|
+
let outsideStart;
|
|
46
|
+
// A pure deletion has a zero-width position between two surviving lines.
|
|
47
|
+
for (let line = first; line < first + Math.max(1, count); line++) {
|
|
48
|
+
const start = starts[count ? line - 1 : line] ?? source.length;
|
|
49
|
+
const end = count ? (starts[line] ?? source.length) : start;
|
|
50
|
+
while (next < declarations.length && declarations[next].location.start < end) {
|
|
51
|
+
active.push(declarations[next++]);
|
|
52
|
+
}
|
|
53
|
+
active = active.filter((symbol) => symbol.location.end > start);
|
|
54
|
+
const children = groupBy(active, (symbol) => symbol.containerId);
|
|
55
|
+
for (const symbol of active) {
|
|
56
|
+
const nested = children.get(symbol.id);
|
|
57
|
+
if (!nested || ownsText(source, symbol, nested, start, end))
|
|
58
|
+
changed.add(symbol);
|
|
59
|
+
}
|
|
60
|
+
if (count && !active.length)
|
|
61
|
+
outsideStart ??= line;
|
|
62
|
+
if (outsideStart !== undefined && (active.length || line === first + count - 1)) {
|
|
63
|
+
notes.push(`Outside a declaration: ${file}:${range(outsideStart, active.length ? line - 1 : line)}`);
|
|
64
|
+
outsideStart = undefined;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (!hunks)
|
|
69
|
+
notes.push(`No text hunks: ${file}`);
|
|
70
|
+
}
|
|
71
|
+
function groupBy(items, keyOf) {
|
|
72
|
+
const groups = new Map();
|
|
73
|
+
for (const item of items) {
|
|
74
|
+
const key = keyOf(item);
|
|
75
|
+
const group = groups.get(key);
|
|
76
|
+
if (group)
|
|
77
|
+
group.push(item);
|
|
78
|
+
else
|
|
79
|
+
groups.set(key, [item]);
|
|
80
|
+
}
|
|
81
|
+
return groups;
|
|
82
|
+
}
|
|
83
|
+
function referenceGroups(references, byId) {
|
|
84
|
+
const groups = new Map();
|
|
85
|
+
for (const ref of references) {
|
|
86
|
+
const { file, line, column } = ref.location;
|
|
87
|
+
const caller = ref.callerId ? byId.get(ref.callerId) : undefined;
|
|
88
|
+
const container = caller?.containerId ? byId.get(caller.containerId) : undefined;
|
|
89
|
+
const label = ref.scope === 'test' ? file : caller ? [container?.name, caller.name].filter(Boolean).join('.') : file;
|
|
90
|
+
const key = ref.scope === 'test' ? file : (ref.callerId ?? file);
|
|
91
|
+
let group = groups.get(key);
|
|
92
|
+
if (!group) {
|
|
93
|
+
group = { label: `[${ref.scope}] ${label}`, sites: [] };
|
|
94
|
+
groups.set(key, group);
|
|
95
|
+
}
|
|
96
|
+
group.sites.push(`${ref.kind} ${file}:${line}:${column}`);
|
|
97
|
+
}
|
|
98
|
+
return Array.from(groups.values(), ({ label, sites }) => ` ${label}: ${sites.join(', ')}`);
|
|
99
|
+
}
|
|
100
|
+
/** Compare a tree to the tracked working tree, resolving references only in the current inventory. */
|
|
101
|
+
export function changeImpactText(directory, base, inventory) {
|
|
102
|
+
if (!base)
|
|
103
|
+
throw new Error('Provide a Git revision for --changed-since.');
|
|
104
|
+
const tree = git(directory, 'rev-parse', '--verify', '--end-of-options', base + '^{tree}').trim();
|
|
105
|
+
const diff = ['diff', '--relative', '--no-ext-diff', '--no-textconv', '--no-renames', '--no-color'];
|
|
106
|
+
const fields = git(directory, ...diff, '--name-status', '-z', tree, '--', '.').split('\0');
|
|
107
|
+
const files = new Map(inventory.files.map((file) => [file.path, file]));
|
|
108
|
+
const symbolsByFile = groupBy(inventory.symbols, (symbol) => symbol.location.file);
|
|
109
|
+
const changed = new Set();
|
|
110
|
+
const notes = [];
|
|
111
|
+
for (let i = 0; i + 1 < fields.length; i += 2) {
|
|
112
|
+
const status = fields[i];
|
|
113
|
+
const path = fields[i + 1];
|
|
114
|
+
if (!sourceExtension.test(path))
|
|
115
|
+
continue;
|
|
116
|
+
if (status === 'D') {
|
|
117
|
+
notes.push(`Deleted file: ${path}`);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const file = files.get(path);
|
|
121
|
+
if (file?.status !== 'analyzed') {
|
|
122
|
+
notes.push(`Outside scan: ${path} (${file?.reason ?? 'not inventoried'})`);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const patch = git(directory, ...diff, '--unified=0', '--inter-hunk-context=0', tree, '--', path);
|
|
126
|
+
const source = ts.sys.readFile(resolve(directory, path));
|
|
127
|
+
if (source === undefined)
|
|
128
|
+
throw new Error(`Cannot read changed file: ${path}`);
|
|
129
|
+
mapHunks(patch, path, source, symbolsByFile.get(path) ?? [], changed, notes);
|
|
130
|
+
}
|
|
131
|
+
const byId = new Map(inventory.symbols.map((symbol) => [symbol.id, symbol]));
|
|
132
|
+
const byTarget = groupBy(inventory.references, (ref) => ref.targetId);
|
|
133
|
+
const lines = [`Change impact since ${base}`];
|
|
134
|
+
for (const symbol of changed) {
|
|
135
|
+
lines.push('', `${symbol.kind} ${symbol.name} (${symbol.location.file}:${symbol.location.line})`);
|
|
136
|
+
if (symbol.exportedAs.length)
|
|
137
|
+
lines.push(` Exports: ${symbol.exportedAs.join(', ')}`);
|
|
138
|
+
const groups = referenceGroups(byTarget.get(symbol.id) ?? [], byId);
|
|
139
|
+
lines.push(...(groups.length ? groups : [' No references found in this scan.']));
|
|
140
|
+
}
|
|
141
|
+
if (!changed.size && !notes.length)
|
|
142
|
+
lines.push('No tracked source changes.');
|
|
143
|
+
return [
|
|
144
|
+
...lines,
|
|
145
|
+
...notes,
|
|
146
|
+
`Analysis: ${inventory.completeness}; ${inventory.diagnostics.length} diagnostics`,
|
|
147
|
+
'',
|
|
148
|
+
].join('\n');
|
|
149
|
+
}
|
package/dist/cli.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { parseArgs } from 'node:util';
|
|
3
3
|
import { writeFileSync } from 'node:fs';
|
|
4
4
|
import { packageEntries, reachability, scan, markdown, summary, usages, usageText } from "./index.js";
|
|
5
|
+
import { changeImpactText } from "./change-impact.js";
|
|
5
6
|
import { reachabilityText } from "./report.js";
|
|
6
7
|
/** Run a read-only scan; output files must be new paths to avoid overwriting user work. */
|
|
7
8
|
function main() {
|
|
@@ -18,6 +19,7 @@ function main() {
|
|
|
18
19
|
tooling: { type: 'string', multiple: true },
|
|
19
20
|
production: { type: 'string', multiple: true },
|
|
20
21
|
format: { type: 'string', default: 'summary' },
|
|
22
|
+
'changed-since': { type: 'string' },
|
|
21
23
|
symbol: { type: 'string' },
|
|
22
24
|
output: { type: 'string' },
|
|
23
25
|
strict: { type: 'boolean', default: false },
|
|
@@ -25,7 +27,7 @@ function main() {
|
|
|
25
27
|
},
|
|
26
28
|
});
|
|
27
29
|
if (values.help) {
|
|
28
|
-
process.stdout.write('Usage: code-inventory [directory] [--entry source-file] [--entry-glob glob] [--entry-symbol name-or-id] [--package-entries] [--project config] [--exclude glob] [--test glob] [--tooling glob] [--production glob] [--format summary|json|markdown] [--symbol name-or-id] [--output new-file] [--strict]\n');
|
|
30
|
+
process.stdout.write('Usage: code-inventory [directory] [--entry source-file] [--entry-glob glob] [--entry-symbol name-or-id] [--package-entries] [--project config] [--exclude glob] [--test glob] [--tooling glob] [--production glob] [--format summary|json|markdown] [--symbol name-or-id] [--changed-since revision] [--output new-file] [--strict]\n');
|
|
29
31
|
return;
|
|
30
32
|
}
|
|
31
33
|
if (positionals.length > 1)
|
|
@@ -38,36 +40,49 @@ function main() {
|
|
|
38
40
|
throw new Error('Select either entry points or --symbol.');
|
|
39
41
|
if (values.symbol && values.format === 'markdown')
|
|
40
42
|
throw new Error('--symbol supports summary and json formats.');
|
|
43
|
+
const base = values['changed-since'];
|
|
44
|
+
if (base !== undefined && (entryQuery || values.symbol !== undefined || values.format !== 'summary'))
|
|
45
|
+
throw new Error('--changed-since is a standalone summary query.');
|
|
41
46
|
const inventory = scan(directory, {
|
|
42
47
|
projects: values.project ?? [],
|
|
43
48
|
exclude: values.exclude ?? [],
|
|
44
49
|
scopes: { test: values.test ?? [], tooling: values.tooling ?? [], production: values.production ?? [] },
|
|
45
50
|
});
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
? {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
51
|
+
let output;
|
|
52
|
+
if (base !== undefined) {
|
|
53
|
+
output = changeImpactText(directory, base, inventory);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
const entries = [
|
|
57
|
+
...(values.entry ?? []),
|
|
58
|
+
...(values['package-entries'] ? packageEntries(directory, inventory) : []),
|
|
59
|
+
];
|
|
60
|
+
const paths = entryQuery
|
|
61
|
+
? reachability(inventory, entries, {
|
|
62
|
+
entryGlobs: values['entry-glob'] ?? [],
|
|
63
|
+
entrySymbols: values['entry-symbol'] ?? [],
|
|
64
|
+
})
|
|
65
|
+
: null;
|
|
66
|
+
const selected = values.symbol ? usages(inventory, values.symbol) : null;
|
|
67
|
+
output =
|
|
68
|
+
values.format === 'json'
|
|
69
|
+
? JSON.stringify(selected
|
|
70
|
+
? {
|
|
71
|
+
schemaVersion: inventory.schemaVersion,
|
|
72
|
+
snapshotId: inventory.snapshotId,
|
|
73
|
+
completeness: inventory.completeness,
|
|
74
|
+
diagnostics: inventory.diagnostics,
|
|
75
|
+
usage: selected,
|
|
76
|
+
}
|
|
77
|
+
: (paths ?? inventory), null, 2) + '\n'
|
|
78
|
+
: paths
|
|
79
|
+
? reachabilityText(paths, values.format === 'markdown')
|
|
80
|
+
: selected
|
|
81
|
+
? usageText(selected) + `Analysis: ${inventory.completeness}; ${inventory.diagnostics.length} diagnostics\n`
|
|
82
|
+
: values.format === 'markdown'
|
|
83
|
+
? markdown(inventory)
|
|
84
|
+
: summary(inventory);
|
|
85
|
+
}
|
|
71
86
|
if (values.output)
|
|
72
87
|
writeFileSync(values.output, output, { flag: 'wx' });
|
|
73
88
|
else
|
package/dist/discovery.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Scope } from './model.ts';
|
|
2
|
+
export declare const sourceExtension: RegExp;
|
|
2
3
|
/** Return sorted source paths without following symlinks or traversing build/dependency folders. */
|
|
3
4
|
export declare function discover(root: string): string[];
|
|
4
5
|
/** Normalize output paths independently of the host's directory separator. */
|
package/dist/discovery.js
CHANGED
|
@@ -2,7 +2,7 @@ import { createHash } from 'node:crypto';
|
|
|
2
2
|
import { readdirSync, readFileSync } from 'node:fs';
|
|
3
3
|
import { relative, resolve, sep } from 'node:path';
|
|
4
4
|
const excludedDirectories = new Set(['node_modules', '.git', 'dist', 'build', 'coverage', '.next', '.nuxt', '.output']);
|
|
5
|
-
const sourceExtension = /\.(?:[cm]?[jt]s|[jt]sx)$/;
|
|
5
|
+
export const sourceExtension = /\.(?:[cm]?[jt]s|[jt]sx)$/;
|
|
6
6
|
/** Return sorted source paths without following symlinks or traversing build/dependency folders. */
|
|
7
7
|
export function discover(root) {
|
|
8
8
|
const files = [];
|
package/docs/reference.html
CHANGED
|
@@ -58,6 +58,16 @@
|
|
|
58
58
|
</dd>
|
|
59
59
|
<dt><code>--symbol name-or-id</code></dt>
|
|
60
60
|
<dd>Query one declaration by exact unique name or snapshot ID. Supports summary and JSON.</dd>
|
|
61
|
+
<dt><code>--changed-since revision</code></dt>
|
|
62
|
+
<dd>
|
|
63
|
+
Compare a local Git revision directly with the tracked working tree under the scan root, including staged
|
|
64
|
+
and unstaged changes. Untracked files enter the diff once staged. The summary groups current references by
|
|
65
|
+
caller or test file and shows module exports, including re-exports. Changed lines select overlapping
|
|
66
|
+
declarations, including an enclosing declaration when the line also contains its own code. Reads include
|
|
67
|
+
callback arguments. Test references identify source sites and do not establish runtime coverage. Removed
|
|
68
|
+
lines use base coordinates. Deleted files and the old side of renames have no current declaration to query.
|
|
69
|
+
Use this option on its own with summary output.
|
|
70
|
+
</dd>
|
|
61
71
|
<dt><code>--package-entries</code></dt>
|
|
62
72
|
<dd>
|
|
63
73
|
Trace module initialization from the root package's exports (or main) and bin targets. All runtime
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "code-inventory",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Static code inventory for JavaScript and TypeScript. Find symbol usages and trace paths from entry points.",
|
|
@@ -53,9 +53,9 @@
|
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@types/node": "26.6.6",
|
|
56
|
-
"vitest": "5.1.0",
|
|
57
|
-
"prettier": "3.10.1",
|
|
58
56
|
"eslint": "10.15.0",
|
|
59
|
-
"
|
|
57
|
+
"prettier": "3.10.1",
|
|
58
|
+
"typescript-eslint": "8.73.0",
|
|
59
|
+
"vitest": "5.1.1"
|
|
60
60
|
}
|
|
61
61
|
}
|