code-inventory 0.1.0 → 0.2.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 CHANGED
@@ -4,7 +4,14 @@ Find where JavaScript and TypeScript declarations are used. The CLI and library
4
4
 
5
5
  ## Run your first scan
6
6
 
7
- Build from a checkout with Node.js 26 or newer. CI uses the version in [.node-version](.node-version).
7
+ In a JavaScript or TypeScript project with Node.js 26 or newer:
8
+
9
+ ```sh
10
+ npm install --save-dev code-inventory
11
+ npx code-inventory .
12
+ ```
13
+
14
+ For a checkout of this repository, use the fixture to check how calls are counted. CI uses the version in [.node-version](.node-version).
8
15
 
9
16
  ```sh
10
17
  npm ci --ignore-scripts --no-audit
@@ -30,18 +37,23 @@ The lines that follow identify each reference by file, line, column, kind, and s
30
37
  | How do I save the full snapshot? | `node dist/cli.js test/fixtures/basic --format json --output inventory.json` |
31
38
  | How do I scan one TypeScript project? | `node dist/cli.js . --project tsconfig.build.json` |
32
39
 
40
+ 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
+
42
+ 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.
43
+
33
44
  Repeat `--entry` to combine entry points or `--project` to select multiple configs. Output files must be new. Use `--strict` when a partial result should set exit code 2. The CLI still produces the report.
34
45
 
35
46
  Check diagnostics before interpreting counts. Call sites measure source occurrences. Runtime dispatch, callbacks, and external consumers can introduce paths the scanner cannot see. Review declarations with no known path before considering removal.
36
47
 
37
48
  ## Use the library
38
49
 
39
- From an ES module in the repository root:
50
+ From an ES module in a project with the package installed:
40
51
 
41
52
  ```js
42
- import { scan, usages, reachability } from './dist/index.js';
53
+ import { scan, usages, reachability } from 'code-inventory';
43
54
 
44
- const inventory = scan('test/fixtures/basic');
55
+ // For a project with an app.ts entry and a normalize declaration:
56
+ const inventory = scan('.');
45
57
  const usage = usages(inventory, 'normalize');
46
58
  const paths = reachability(inventory, ['app.ts']);
47
59
  ```
package/dist/cli.js CHANGED
@@ -1,13 +1,16 @@
1
1
  #!/usr/bin/env node
2
2
  import { parseArgs } from 'node:util';
3
3
  import { writeFileSync } from 'node:fs';
4
- import { reachability, scan, markdown, summary, usages, usageText } from "./index.js";
4
+ import { packageEntries, reachability, scan, markdown, summary, usages, usageText } from "./index.js";
5
5
  import { reachabilityText } from "./report.js";
6
6
  /** Run a read-only scan; output files must be new paths to avoid overwriting user work. */
7
7
  function main() {
8
8
  const { values, positionals } = parseArgs({
9
9
  allowPositionals: true,
10
10
  options: {
11
+ 'package-entries': { type: 'boolean' },
12
+ 'entry-glob': { type: 'string', multiple: true },
13
+ 'entry-symbol': { type: 'string', multiple: true },
11
14
  entry: { type: 'string', multiple: true },
12
15
  project: { type: 'string', multiple: true },
13
16
  exclude: { type: 'string', multiple: true },
@@ -22,23 +25,31 @@ function main() {
22
25
  },
23
26
  });
24
27
  if (values.help) {
25
- process.stdout.write('Usage: code-inventory [directory] [--entry source-file] [--project config] [--exclude glob] [--test glob] [--tooling glob] [--production glob] [--format summary|json|markdown] [--symbol name-or-id] [--output new-file] [--strict]\n');
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');
26
29
  return;
27
30
  }
28
31
  if (positionals.length > 1)
29
32
  throw new Error('Provide at most one directory.');
30
33
  if (!['summary', 'json', 'markdown'].includes(values.format))
31
34
  throw new Error(`Unknown format: ${values.format}`);
32
- if (values.symbol && values.entry)
33
- throw new Error('Select either --entry or --symbol.');
35
+ const directory = positionals[0] ?? '.';
36
+ const entryQuery = values.entry || values['entry-glob'] || values['entry-symbol'] || values['package-entries'];
37
+ if (values.symbol && entryQuery)
38
+ throw new Error('Select either entry points or --symbol.');
34
39
  if (values.symbol && values.format === 'markdown')
35
40
  throw new Error('--symbol supports summary and json formats.');
36
- const inventory = scan(positionals[0] ?? '.', {
41
+ const inventory = scan(directory, {
37
42
  projects: values.project ?? [],
38
43
  exclude: values.exclude ?? [],
39
44
  scopes: { test: values.test ?? [], tooling: values.tooling ?? [], production: values.production ?? [] },
40
45
  });
41
- const paths = values.entry ? reachability(inventory, values.entry) : null;
46
+ const entries = [...(values.entry ?? []), ...(values['package-entries'] ? packageEntries(directory, inventory) : [])];
47
+ const paths = entryQuery
48
+ ? reachability(inventory, entries, {
49
+ entryGlobs: values['entry-glob'] ?? [],
50
+ entrySymbols: values['entry-symbol'] ?? [],
51
+ })
52
+ : null;
42
53
  const selected = values.symbol ? usages(inventory, values.symbol) : null;
43
54
  const output = values.format === 'json'
44
55
  ? JSON.stringify(selected
package/dist/compiler.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import ts from 'typescript';
2
2
  import { isBuiltin } from 'node:module';
3
+ import manifest from '../package.json' with { type: 'json' };
3
4
  import { realpathSync } from 'node:fs';
4
5
  import { resolve } from 'node:path';
5
6
  import { loadProjects, projectOwners, fileRules } from "./projects.js";
@@ -26,7 +27,7 @@ export function scan(rootDirectory, settings = {}) {
26
27
  const analyzed = new Set();
27
28
  const inventory = {
28
29
  schemaVersion: 1,
29
- analyzer: { name: 'code-inventory', version: '0.1.0', compilerVersion: ts.version },
30
+ analyzer: { name: 'code-inventory', version: manifest.version, compilerVersion: ts.version },
30
31
  snapshotId: '',
31
32
  config: {
32
33
  path: configPath ? portablePath(root, configPath) : null,
package/dist/index.d.ts CHANGED
@@ -3,5 +3,6 @@ export { usages, findings } from './query.ts';
3
3
  export { markdown, summary, usageText } from './report.ts';
4
4
  export type { Inventory, CodeSymbol, Reference, ModuleReference, Diagnostic, Usage, Finding, Location, } from './model.ts';
5
5
  export type { ScanOptions } from './projects.ts';
6
- export { reachability } from './reachability.ts';
6
+ export { entryPath, reachability } from './reachability.ts';
7
7
  export type { Reachability } from './reachability.ts';
8
+ export { packageEntries } from './package-entries.ts';
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export { scan } from "./compiler.js";
2
2
  export { usages, findings } from "./query.js";
3
3
  export { markdown, summary, usageText } from "./report.js";
4
- export { reachability } from "./reachability.js";
4
+ export { entryPath, reachability } from "./reachability.js";
5
+ export { packageEntries } from "./package-entries.js";
package/dist/model.d.ts CHANGED
@@ -58,7 +58,7 @@ export interface Inventory {
58
58
  schemaVersion: 1;
59
59
  analyzer: {
60
60
  name: 'code-inventory';
61
- version: '0.1.0';
61
+ version: string;
62
62
  compilerVersion: string;
63
63
  };
64
64
  snapshotId: string;
@@ -0,0 +1,3 @@
1
+ import type { Inventory } from './model.ts';
2
+ /** Collect explicit runtime entry files from one package, across its declared conditions. */
3
+ export declare function packageEntries(directory: string, inventory: Inventory): string[];
@@ -0,0 +1,147 @@
1
+ import ts from 'typescript';
2
+ import { loadProjects } from "./projects.js";
3
+ import { portablePath } from "./discovery.js";
4
+ import { readFileSync } from 'node:fs';
5
+ import { resolve, posix } from 'node:path';
6
+ /** Node substitutes the same nonempty subpath for every star in a target, including across directories. */
7
+ function capture(parts, path) {
8
+ if (!path.startsWith(parts[0]))
9
+ return null;
10
+ const literals = parts.reduce((length, part) => length + part.length, 0);
11
+ const length = (path.length - literals) / (parts.length - 1);
12
+ if (length < 1 || !Number.isInteger(length))
13
+ return null;
14
+ const value = path.slice(parts[0].length, parts[0].length + length);
15
+ return parts.join(value) === path ? value : null;
16
+ }
17
+ function* expandPatterns(patterns, subpaths, candidates) {
18
+ const exact = new Set(subpaths.filter((key) => !key.includes('*')));
19
+ // Node selects the longest prefix first, then the longest complete pattern.
20
+ const wildcardKeys = subpaths
21
+ .filter((key) => key.includes('*'))
22
+ .sort((a, b) => b.indexOf('*') - a.indexOf('*') || b.length - a.length);
23
+ const keys = wildcardKeys.map((key) => ({ key, parts: key.split('*') }));
24
+ if (keys.some(({ parts }) => parts.length !== 2))
25
+ throw new Error('An export subpath pattern must contain one star.');
26
+ for (const { key, target } of patterns) {
27
+ const parts = target.split('*');
28
+ let matched = false;
29
+ for (const path of candidates) {
30
+ const value = capture(parts, path);
31
+ if (value === null)
32
+ continue;
33
+ matched = true;
34
+ const subpath = key.replaceAll('*', () => value);
35
+ if (exact.has(subpath))
36
+ continue;
37
+ const winner = keys.find(({ parts }) => capture(parts, subpath) !== null);
38
+ if (winner?.key === key)
39
+ yield path;
40
+ }
41
+ if (!matched)
42
+ throw new Error('No analyzed source or compiler output matches package target: ' +
43
+ target +
44
+ '. Select the build config with --project or use --entry.');
45
+ }
46
+ }
47
+ /** Collect explicit runtime entry files from one package, across its declared conditions. */
48
+ export function packageEntries(directory, inventory) {
49
+ const manifest = JSON.parse(readFileSync(resolve(directory, 'package.json'), 'utf8'));
50
+ const files = new Set();
51
+ const patterns = [];
52
+ const exports = manifest.exports;
53
+ const subpaths = exports && typeof exports === 'object' && Object.keys(exports).some((key) => key.startsWith('.'))
54
+ ? Object.keys(exports)
55
+ : [];
56
+ function add(target, key = '') {
57
+ if (typeof target !== 'string' ||
58
+ !target ||
59
+ (target.includes('*') && !key.includes('*')) ||
60
+ posix.isAbsolute(target) ||
61
+ target.includes('\\') ||
62
+ target.split('/').includes('..')) {
63
+ throw new Error('Package entry requires an explicit local source path. Use --entry to select it.');
64
+ }
65
+ if (/\.json$|\.d\.[cm]?ts$/.test(target))
66
+ return;
67
+ const path = posix.normalize(target);
68
+ if (path.includes('*'))
69
+ patterns.push({ key, target: path });
70
+ else
71
+ files.add(path);
72
+ }
73
+ function collect(target, key = '') {
74
+ if (target === null)
75
+ return;
76
+ if (Array.isArray(target)) {
77
+ for (const item of target)
78
+ collect(item, key);
79
+ }
80
+ else if (typeof target === 'object') {
81
+ for (const [condition, value] of Object.entries(target)) {
82
+ if (condition !== 'types' && !condition.startsWith('types@'))
83
+ collect(value, key);
84
+ }
85
+ }
86
+ else
87
+ add(target, key);
88
+ }
89
+ if (subpaths.length) {
90
+ for (const key of subpaths)
91
+ collect(exports[key], key);
92
+ }
93
+ else if ('exports' in manifest)
94
+ collect(exports);
95
+ else if ('main' in manifest)
96
+ add(manifest.main);
97
+ if (typeof manifest.bin === 'string')
98
+ add(manifest.bin);
99
+ else if (manifest.bin)
100
+ Object.values(manifest.bin).forEach((target) => add(target));
101
+ const analyzed = new Set(inventory.files.filter((file) => file.status === 'analyzed').map((file) => file.path));
102
+ if (!patterns.length) {
103
+ if (!files.size)
104
+ throw new Error('No explicit package entries. Select a source file with --entry.');
105
+ if ([...files].every((file) => analyzed.has(file)))
106
+ return [...files].sort();
107
+ }
108
+ const sources = new Map();
109
+ for (const project of loadProjects(directory, inventory.config.entries).ordered) {
110
+ if (!project.path || project.options.noEmit || project.options.emitDeclarationOnly)
111
+ continue;
112
+ const config = { options: project.options, fileNames: [...project.files], errors: [] };
113
+ for (const input of project.files) {
114
+ const source = portablePath(directory, input);
115
+ if (!analyzed.has(source))
116
+ continue;
117
+ for (const output of ts.getOutputFileNames(config, input, !ts.sys.useCaseSensitiveFileNames)) {
118
+ const path = portablePath(directory, output);
119
+ if (!files.has(path) && !(patterns.length && /\.(?:[cm]?js|jsx)$/.test(path)))
120
+ continue;
121
+ const candidates = sources.get(path) ?? new Set();
122
+ candidates.add(source);
123
+ sources.set(path, candidates);
124
+ }
125
+ }
126
+ }
127
+ if (patterns.length) {
128
+ for (const file of expandPatterns(patterns, subpaths, new Set([...analyzed, ...sources.keys()])))
129
+ files.add(file);
130
+ if (!files.size)
131
+ throw new Error('No analyzed files are exposed by these package entries.');
132
+ }
133
+ const roots = new Set();
134
+ for (const file of files) {
135
+ if (analyzed.has(file))
136
+ roots.add(file);
137
+ else {
138
+ const candidates = [...(sources.get(file) ?? [])];
139
+ if (candidates.length !== 1)
140
+ throw new Error('Cannot map package entry to one analyzed source: ' +
141
+ file +
142
+ '. Select the build config with --project or use --entry.');
143
+ roots.add(candidates[0]);
144
+ }
145
+ }
146
+ return [...roots].sort();
147
+ }
package/dist/query.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- import type { Finding, Inventory, Usage } from './model.ts';
1
+ import type { CodeSymbol, Finding, Inventory, Usage } from './model.ts';
2
2
  /** Select by exact snapshot ID or by unambiguous declaration name. */
3
+ export declare function selectSymbol(inventory: Inventory, selector: string): CodeSymbol;
3
4
  export declare function usages(inventory: Inventory, selector: string): Usage;
4
5
  /** Describe absent references without asserting dead code or runtime reachability. */
5
6
  export declare function findings(inventory: Inventory): Finding[];
package/dist/query.js CHANGED
@@ -1,12 +1,15 @@
1
1
  /** Select by exact snapshot ID or by unambiguous declaration name. */
2
- export function usages(inventory, selector) {
2
+ export function selectSymbol(inventory, selector) {
3
3
  const matches = inventory.symbols.filter((symbol) => symbol.id === selector || symbol.name === selector);
4
4
  if (matches.length !== 1) {
5
5
  throw new Error(matches.length === 0
6
6
  ? `Symbol not found: ${selector}`
7
7
  : `Ambiguous symbol ${selector}; use one of these IDs:\n${matches.map((symbol) => symbol.id).join('\n')}`);
8
8
  }
9
- const symbol = matches[0];
9
+ return matches[0];
10
+ }
11
+ export function usages(inventory, selector) {
12
+ const symbol = selectSymbol(inventory, selector);
10
13
  const references = inventory.references.filter((reference) => reference.targetId === symbol.id);
11
14
  const calls = references.filter((reference) => reference.kind === 'call' || reference.kind === 'construct');
12
15
  return {
@@ -7,6 +7,7 @@ type Step = {
7
7
  export interface Reachability {
8
8
  snapshotId: string;
9
9
  entries: string[];
10
+ entrySymbols?: string[];
10
11
  completeness: Inventory['completeness'];
11
12
  diagnostics: Inventory['diagnostics'];
12
13
  /** One shortest-path predecessor per reached module or declaration. Roots have no predecessor. */
@@ -22,6 +23,11 @@ export interface Reachability {
22
23
  }[];
23
24
  limitations: string[];
24
25
  }
25
- /** Trace resolved direct calls, construction and literal module edges from explicit source files. */
26
- export declare function reachability(inventory: Inventory, files: readonly string[]): Reachability;
26
+ /** Trace resolved direct calls, construction and literal module edges from selected source files. */
27
+ export declare function reachability(inventory: Inventory, files: readonly string[], options?: {
28
+ entryGlobs?: readonly string[];
29
+ entrySymbols?: readonly string[];
30
+ }): Reachability;
31
+ /** Return one shortest path in entry-to-target order, or null for an unreached ID. */
32
+ export declare function entryPath(report: Reachability, id: string): Reachability['reached'] | null;
27
33
  export {};
@@ -1,11 +1,30 @@
1
1
  import { posix } from 'node:path';
2
+ import { selectSymbol } from "./query.js";
2
3
  const moduleId = (file) => 'module:' + file;
3
- /** Trace resolved direct calls, construction and literal module edges from explicit source files. */
4
- export function reachability(inventory, files) {
5
- const entries = [...new Set(files.map((file) => posix.normalize(file)))].sort();
6
- if (!entries.length)
7
- throw new Error('Select at least one entry file.');
4
+ /** Trace resolved direct calls, construction and literal module edges from selected source files. */
5
+ export function reachability(inventory, files, options = {}) {
6
+ const selected = new Set(files.map((file) => posix.normalize(file)));
8
7
  const analyzed = new Set(inventory.files.filter((file) => file.status === 'analyzed').map((file) => file.path));
8
+ for (const glob of options.entryGlobs ?? []) {
9
+ const pattern = glob.replace(/^\.\//, '');
10
+ const matches = [...analyzed].filter((file) => posix.matchesGlob(file, pattern));
11
+ if (!matches.length)
12
+ throw new Error('No analyzed entry files match: ' + glob);
13
+ for (const file of matches)
14
+ selected.add(file);
15
+ }
16
+ const entries = [...selected].sort();
17
+ const invoked = new Set();
18
+ for (const selector of options.entrySymbols ?? []) {
19
+ const symbol = selectSymbol(inventory, selector);
20
+ if (!['function', 'method', 'constructor'].includes(symbol.kind)) {
21
+ throw new Error('Entry symbol must be a function, method or constructor: ' + selector);
22
+ }
23
+ invoked.add(symbol.id);
24
+ }
25
+ const entrySymbols = [...invoked].sort();
26
+ if (!entries.length && !entrySymbols.length)
27
+ throw new Error('Select at least one entry file or symbol.');
9
28
  for (const file of entries) {
10
29
  if (!analyzed.has(file))
11
30
  throw new Error('Entry file was not analyzed: ' + file);
@@ -49,7 +68,7 @@ export function reachability(inventory, files) {
49
68
  location: dep.location,
50
69
  });
51
70
  }
52
- const queue = entries.map(moduleId);
71
+ const queue = [...entries.map(moduleId), ...entrySymbols];
53
72
  const paths = new Map(queue.map((id) => [id, null]));
54
73
  for (let index = 0; index < queue.length; index++) {
55
74
  for (const { to, via } of edges.get(queue[index]) ?? []) {
@@ -62,6 +81,7 @@ export function reachability(inventory, files) {
62
81
  return {
63
82
  snapshotId: inventory.snapshotId,
64
83
  entries,
84
+ ...(entrySymbols.length ? { entrySymbols } : {}),
65
85
  completeness: inventory.completeness,
66
86
  diagnostics: inventory.diagnostics,
67
87
  reached: [...paths].map(([id, via]) => ({ id, via })),
@@ -80,3 +100,19 @@ export function reachability(inventory, files) {
80
100
  ],
81
101
  };
82
102
  }
103
+ /** Return one shortest path in entry-to-target order, or null for an unreached ID. */
104
+ export function entryPath(report, id) {
105
+ const nodes = new Map(report.reached.map((node) => [node.id, node]));
106
+ let node = nodes.get(id);
107
+ if (!node)
108
+ return null;
109
+ const path = [node];
110
+ while (node.via) {
111
+ const parent = nodes.get(node.via.from);
112
+ if (!parent || path.length >= nodes.size)
113
+ throw new Error('Invalid reachability predecessor chain.');
114
+ path.push(parent);
115
+ node = parent;
116
+ }
117
+ return path.reverse();
118
+ }
package/dist/report.js CHANGED
@@ -1,15 +1,22 @@
1
+ import { compare } from "./discovery.js";
1
2
  import { findings, usages } from "./query.js";
2
3
  /** Render a concise scan summary, including limitations affecting its findings. */
3
4
  export function summary(inventory) {
4
5
  const analyzed = inventory.files.filter((file) => file.status === 'analyzed').length;
5
6
  const calls = inventory.references.filter((reference) => reference.kind === 'call').length;
6
7
  const candidates = findings(inventory);
8
+ const causes = new Map();
9
+ for (const { code } of inventory.diagnostics)
10
+ causes.set(code, (causes.get(code) ?? 0) + 1);
7
11
  return ([
8
12
  `Code inventory: ${analyzed} analyzed files, ${inventory.symbols.length} symbols`,
9
13
  `${inventory.references.length} references, ${calls} direct call sites`,
10
14
  `${candidates.filter((finding) => finding.kind === 'no-references-found').length} symbols with no references found`,
11
15
  `${candidates.filter((finding) => finding.kind === 'exported-without-local-usage').length} exports without local usage`,
12
16
  `Analysis: ${inventory.completeness}; ${inventory.diagnostics.length} diagnostics`,
17
+ ...Array.from(causes)
18
+ .sort(([a], [b]) => compare(a, b))
19
+ .map(([code, count]) => ` ${code}: ${count}`),
13
20
  'Static evidence only; no-reference findings are not proof of dead code.',
14
21
  ].join('\n') + '\n');
15
22
  }
@@ -76,7 +83,8 @@ export function markdown(inventory) {
76
83
  export function reachabilityText(report, detailed) {
77
84
  const missing = report.declarations.filter((symbol) => symbol.status === 'no-known-path');
78
85
  const lines = [
79
- 'Entry files: ' + report.entries.join(', '),
86
+ ...(report.entries.length ? ['Entry files: ' + report.entries.join(', ')] : []),
87
+ ...(report.entrySymbols?.length ? ['Assumed invocations: ' + report.entrySymbols.join(', ')] : []),
80
88
  report.declarations.length - missing.length + ' reachable declarations, ' + missing.length + ' with no known path',
81
89
  'Analysis: ' + report.completeness + '; ' + report.diagnostics.length + ' diagnostics',
82
90
  ];
@@ -58,6 +58,32 @@
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>--package-entries</code></dt>
62
+ <dd>
63
+ Trace module initialization from the root package's exports (or main) and bin targets. All runtime
64
+ conditions and array alternatives contribute roots. Type-only branches, declaration files and JSON targets
65
+ are skipped. Combine with --entry for framework or implicit roots. For compiled packages select the emitting
66
+ --project so the compiler can map outputs to analyzed inputs. Subpath patterns such as
67
+ <code>"./features/*": "./dist/*.js"</code> expand against analyzed files and their predicted JavaScript
68
+ outputs. An exact subpath or a more specific pattern takes precedence, including private paths blocked with
69
+ <code>null</code>. If a target pattern has no matching source or predicted output, select its source
70
+ explicitly with <code>--entry</code>. External invocations of exported functions are outside this trace.
71
+ </dd>
72
+ <dt><code>--entry-symbol name-or-id</code></dt>
73
+ <dd>
74
+ Assume a function, method or constructor is invoked and trace calls from its body. For example, select
75
+ <code>GET</code> when reviewing a route handler. A repeated name requires the snapshot ID shown in the
76
+ ambiguity message. Repeat the option for multiple invocations. Add file entries to include module
77
+ initialization. The library accepts <code>reachability(inventory, [], { entrySymbols: ['GET'] })</code>. The
78
+ report records these assumptions as snapshot IDs in the optional <code>entrySymbols</code> array.
79
+ </dd>
80
+ <dt><code>--entry-glob glob</code></dt>
81
+ <dd>
82
+ Trace analyzed files matching a quoted, root-relative pattern such as <code>'src/routes/**/+page.ts'</code>.
83
+ Repeat the option or combine it with --entry and --package-entries. Each pattern must match an analyzed
84
+ file. Library callers can pass
85
+ <code>reachability(inventory, [], { entryGlobs: ['src/routes/**/+page.ts'] })</code>.
86
+ </dd>
61
87
  <dt><code>--entry source-file</code></dt>
62
88
  <dd>
63
89
  Trace paths from an analyzed file relative to the scan root. Repeat for multiple entries. Use a separate
@@ -148,6 +174,30 @@
148
174
  <code>reachable</code> or <code>no-known-path</code>, along with snapshot ID, completeness, diagnostics, and
149
175
  limitations.
150
176
  </p>
177
+ <h2>Consuming the 0.x API</h2>
178
+ <p>
179
+ The package root exports scan, usages, findings, reachability, packageEntries and entryPath. scan reads a
180
+ directory synchronously. The other queries operate on its inventory or reachability report, except
181
+ packageEntries(directory, inventory), which also reads the matching manifest and build configuration. Pass its
182
+ result to reachability. entryPath(report, symbolId) returns the ordered nodes of one shortest path, including
183
+ the selected file or invocation root and target, or null for an unreached ID. Select the ID with usages. Each
184
+ non-root node carries the edge kind and source location. summary, usageText and markdown render display text.
185
+ </p>
186
+ <p>
187
+ The <a href="report-schema.json">JSON Schema</a> describes all three CLI report shapes. For persisted data
188
+ check schemaVersion before reading a full inventory or symbol response. The entry response has the
189
+ Reachability shape in the shipped TypeScript declarations. In 0.1.0 it has no schemaVersion. Keep analyzer and
190
+ compiler versions from the full snapshot alongside query results. Use snapshot-local IDs for joins. Accept
191
+ additional fields and unknown diagnostic codes. Compare counts across upgrades against source examples;
192
+ changes in resolution can change a result even when its shape stays compatible. Keep the generating version
193
+ for older snapshots. During 0.x the format and API can change.
194
+ </p>
195
+ <p>
196
+ TS-prefixed codes are compiler diagnostics. unresolved-module and dynamic-module identify missing module
197
+ edges. unresolved-call, mutable-binding and accessor-dispatch identify uncertainty about invocation targets.
198
+ Inspect the diagnostic location alongside the usage or path. Scope is snapshot-wide, so a partial scan can
199
+ still contain individually verifiable calls. The inventory summary groups diagnostics by code.
200
+ </p>
151
201
  <h2 id="boundaries">Analysis limits</h2>
152
202
  <p>
153
203
  Scanning reads source and configuration without executing modules, tests, or configuration scripts. Parse
@@ -0,0 +1,513 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "title": "code-inventory snapshot and query reports",
4
+ "oneOf": [
5
+ {
6
+ "$ref": "#/definitions/inventory"
7
+ },
8
+ {
9
+ "$ref": "#/definitions/symbolReport"
10
+ },
11
+ {
12
+ "$ref": "#/definitions/entryReport"
13
+ }
14
+ ],
15
+ "definitions": {
16
+ "location": {
17
+ "type": "object",
18
+ "required": ["file", "start", "end", "line", "column"],
19
+ "properties": {
20
+ "file": {
21
+ "type": "string"
22
+ },
23
+ "start": {
24
+ "type": "integer",
25
+ "minimum": 0
26
+ },
27
+ "end": {
28
+ "type": "integer",
29
+ "minimum": 0
30
+ },
31
+ "line": {
32
+ "type": "integer",
33
+ "minimum": 1
34
+ },
35
+ "column": {
36
+ "type": "integer",
37
+ "minimum": 1
38
+ }
39
+ }
40
+ },
41
+ "symbol": {
42
+ "type": "object",
43
+ "required": ["id", "name", "kind", "location", "containerId", "scope", "exportedAs"],
44
+ "properties": {
45
+ "id": {
46
+ "type": "string"
47
+ },
48
+ "name": {
49
+ "type": "string"
50
+ },
51
+ "kind": {
52
+ "enum": ["class", "constructor", "function", "method", "enum", "type", "interface"]
53
+ },
54
+ "location": {
55
+ "$ref": "#/definitions/location"
56
+ },
57
+ "containerId": {
58
+ "anyOf": [
59
+ {
60
+ "type": "string"
61
+ },
62
+ {
63
+ "type": "null"
64
+ }
65
+ ]
66
+ },
67
+ "scope": {
68
+ "enum": ["production", "test", "tooling"]
69
+ },
70
+ "exportedAs": {
71
+ "type": "array",
72
+ "items": {
73
+ "type": "string"
74
+ }
75
+ }
76
+ }
77
+ },
78
+ "reference": {
79
+ "type": "object",
80
+ "required": ["targetId", "kind", "location", "callerId", "scope"],
81
+ "properties": {
82
+ "targetId": {
83
+ "type": "string"
84
+ },
85
+ "kind": {
86
+ "enum": ["call", "construct", "read", "type", "import", "reexport", "jsx"]
87
+ },
88
+ "location": {
89
+ "$ref": "#/definitions/location"
90
+ },
91
+ "callerId": {
92
+ "anyOf": [
93
+ {
94
+ "type": "string"
95
+ },
96
+ {
97
+ "type": "null"
98
+ }
99
+ ]
100
+ },
101
+ "scope": {
102
+ "enum": ["production", "test", "tooling"]
103
+ }
104
+ }
105
+ },
106
+ "diagnostic": {
107
+ "type": "object",
108
+ "required": ["code", "message", "location"],
109
+ "properties": {
110
+ "code": {
111
+ "type": "string"
112
+ },
113
+ "message": {
114
+ "type": "string"
115
+ },
116
+ "location": {
117
+ "anyOf": [
118
+ {
119
+ "$ref": "#/definitions/location"
120
+ },
121
+ {
122
+ "type": "null"
123
+ }
124
+ ]
125
+ }
126
+ }
127
+ },
128
+ "inventory": {
129
+ "type": "object",
130
+ "required": [
131
+ "schemaVersion",
132
+ "snapshotId",
133
+ "completeness",
134
+ "diagnostics",
135
+ "analyzer",
136
+ "config",
137
+ "files",
138
+ "symbols",
139
+ "references",
140
+ "modules",
141
+ "exports",
142
+ "limitations"
143
+ ],
144
+ "properties": {
145
+ "schemaVersion": {
146
+ "const": 1
147
+ },
148
+ "snapshotId": {
149
+ "type": "string"
150
+ },
151
+ "completeness": {
152
+ "enum": ["complete", "partial"]
153
+ },
154
+ "diagnostics": {
155
+ "type": "array",
156
+ "items": {
157
+ "$ref": "#/definitions/diagnostic"
158
+ }
159
+ },
160
+ "analyzer": {
161
+ "type": "object",
162
+ "required": ["name", "version", "compilerVersion"],
163
+ "properties": {
164
+ "name": {
165
+ "const": "code-inventory"
166
+ },
167
+ "version": {
168
+ "type": "string"
169
+ },
170
+ "compilerVersion": {
171
+ "type": "string"
172
+ }
173
+ }
174
+ },
175
+ "config": {
176
+ "type": "object",
177
+ "required": ["path", "hash"],
178
+ "properties": {
179
+ "path": {
180
+ "anyOf": [
181
+ {
182
+ "type": "string"
183
+ },
184
+ {
185
+ "type": "null"
186
+ }
187
+ ]
188
+ },
189
+ "hash": {
190
+ "anyOf": [
191
+ {
192
+ "type": "string"
193
+ },
194
+ {
195
+ "type": "null"
196
+ }
197
+ ]
198
+ },
199
+ "entries": {
200
+ "type": "array",
201
+ "items": {
202
+ "type": "string"
203
+ }
204
+ },
205
+ "projects": {
206
+ "type": "array",
207
+ "items": {
208
+ "type": "string"
209
+ }
210
+ },
211
+ "inputs": {
212
+ "type": "array",
213
+ "items": {
214
+ "type": "object",
215
+ "required": ["path", "hash"],
216
+ "properties": {
217
+ "path": {
218
+ "type": "string"
219
+ },
220
+ "hash": {
221
+ "type": "string"
222
+ }
223
+ }
224
+ }
225
+ }
226
+ }
227
+ },
228
+ "files": {
229
+ "type": "array",
230
+ "items": {
231
+ "type": "object",
232
+ "required": ["path", "hash", "status", "reason"],
233
+ "properties": {
234
+ "path": {
235
+ "type": "string"
236
+ },
237
+ "hash": {
238
+ "type": "string"
239
+ },
240
+ "status": {
241
+ "enum": ["excluded", "analyzed"]
242
+ },
243
+ "reason": {
244
+ "anyOf": [
245
+ {
246
+ "type": "string"
247
+ },
248
+ {
249
+ "type": "null"
250
+ }
251
+ ]
252
+ },
253
+ "project": {
254
+ "type": "string"
255
+ }
256
+ }
257
+ }
258
+ },
259
+ "symbols": {
260
+ "type": "array",
261
+ "items": {
262
+ "$ref": "#/definitions/symbol"
263
+ }
264
+ },
265
+ "references": {
266
+ "type": "array",
267
+ "items": {
268
+ "$ref": "#/definitions/reference"
269
+ }
270
+ },
271
+ "modules": {
272
+ "type": "array",
273
+ "items": {
274
+ "type": "object",
275
+ "required": ["specifier", "kind", "location", "typeOnly", "resolvedFile"],
276
+ "properties": {
277
+ "callerId": {
278
+ "anyOf": [
279
+ {
280
+ "type": "string"
281
+ },
282
+ {
283
+ "type": "null"
284
+ }
285
+ ]
286
+ },
287
+ "specifier": {
288
+ "type": "string"
289
+ },
290
+ "kind": {
291
+ "enum": ["import", "export", "dynamic-import", "require"]
292
+ },
293
+ "location": {
294
+ "$ref": "#/definitions/location"
295
+ },
296
+ "typeOnly": {
297
+ "type": "boolean"
298
+ },
299
+ "resolvedFile": {
300
+ "anyOf": [
301
+ {
302
+ "type": "string"
303
+ },
304
+ {
305
+ "type": "null"
306
+ }
307
+ ]
308
+ }
309
+ }
310
+ }
311
+ },
312
+ "exports": {
313
+ "type": "array",
314
+ "items": {
315
+ "type": "object",
316
+ "required": ["file", "name", "targetId"],
317
+ "properties": {
318
+ "file": {
319
+ "type": "string"
320
+ },
321
+ "name": {
322
+ "type": "string"
323
+ },
324
+ "targetId": {
325
+ "anyOf": [
326
+ {
327
+ "type": "string"
328
+ },
329
+ {
330
+ "type": "null"
331
+ }
332
+ ]
333
+ }
334
+ }
335
+ }
336
+ },
337
+ "limitations": {
338
+ "type": "array",
339
+ "items": {
340
+ "type": "string"
341
+ }
342
+ }
343
+ }
344
+ },
345
+ "symbolReport": {
346
+ "type": "object",
347
+ "required": ["schemaVersion", "snapshotId", "completeness", "diagnostics", "usage"],
348
+ "properties": {
349
+ "schemaVersion": {
350
+ "const": 1
351
+ },
352
+ "snapshotId": {
353
+ "type": "string"
354
+ },
355
+ "completeness": {
356
+ "enum": ["complete", "partial"]
357
+ },
358
+ "diagnostics": {
359
+ "type": "array",
360
+ "items": {
361
+ "$ref": "#/definitions/diagnostic"
362
+ }
363
+ },
364
+ "usage": {
365
+ "type": "object",
366
+ "required": ["symbol", "references", "counts"],
367
+ "properties": {
368
+ "symbol": {
369
+ "$ref": "#/definitions/symbol"
370
+ },
371
+ "references": {
372
+ "type": "array",
373
+ "items": {
374
+ "$ref": "#/definitions/reference"
375
+ }
376
+ },
377
+ "counts": {
378
+ "type": "object",
379
+ "required": [
380
+ "references",
381
+ "directCallSites",
382
+ "constructorSites",
383
+ "distinctCallers",
384
+ "production",
385
+ "test",
386
+ "tooling"
387
+ ],
388
+ "properties": {
389
+ "references": {
390
+ "type": "integer",
391
+ "minimum": 0
392
+ },
393
+ "directCallSites": {
394
+ "type": "integer",
395
+ "minimum": 0
396
+ },
397
+ "constructorSites": {
398
+ "type": "integer",
399
+ "minimum": 0
400
+ },
401
+ "distinctCallers": {
402
+ "type": "integer",
403
+ "minimum": 0
404
+ },
405
+ "production": {
406
+ "type": "integer",
407
+ "minimum": 0
408
+ },
409
+ "test": {
410
+ "type": "integer",
411
+ "minimum": 0
412
+ },
413
+ "tooling": {
414
+ "type": "integer",
415
+ "minimum": 0
416
+ }
417
+ }
418
+ }
419
+ }
420
+ }
421
+ }
422
+ },
423
+ "entryReport": {
424
+ "type": "object",
425
+ "required": ["snapshotId", "completeness", "diagnostics", "entries", "reached", "declarations", "limitations"],
426
+ "properties": {
427
+ "snapshotId": {
428
+ "type": "string"
429
+ },
430
+ "completeness": {
431
+ "enum": ["complete", "partial"]
432
+ },
433
+ "diagnostics": {
434
+ "type": "array",
435
+ "items": {
436
+ "$ref": "#/definitions/diagnostic"
437
+ }
438
+ },
439
+ "entries": {
440
+ "type": "array",
441
+ "items": {
442
+ "type": "string"
443
+ }
444
+ },
445
+ "entrySymbols": {
446
+ "type": "array",
447
+ "items": { "type": "string" }
448
+ },
449
+ "reached": {
450
+ "type": "array",
451
+ "items": {
452
+ "type": "object",
453
+ "required": ["id", "via"],
454
+ "properties": {
455
+ "id": {
456
+ "type": "string"
457
+ },
458
+ "via": {
459
+ "anyOf": [
460
+ {
461
+ "type": "object",
462
+ "required": ["from", "kind", "location"],
463
+ "properties": {
464
+ "from": {
465
+ "type": "string"
466
+ },
467
+ "kind": {
468
+ "enum": ["import", "export", "dynamic-import", "require", "call", "construct"]
469
+ },
470
+ "location": {
471
+ "$ref": "#/definitions/location"
472
+ }
473
+ }
474
+ },
475
+ {
476
+ "type": "null"
477
+ }
478
+ ]
479
+ }
480
+ }
481
+ }
482
+ },
483
+ "declarations": {
484
+ "type": "array",
485
+ "items": {
486
+ "type": "object",
487
+ "required": ["id", "name", "location", "status"],
488
+ "properties": {
489
+ "id": {
490
+ "type": "string"
491
+ },
492
+ "name": {
493
+ "type": "string"
494
+ },
495
+ "location": {
496
+ "$ref": "#/definitions/location"
497
+ },
498
+ "status": {
499
+ "enum": ["reachable", "no-known-path"]
500
+ }
501
+ }
502
+ }
503
+ },
504
+ "limitations": {
505
+ "type": "array",
506
+ "items": {
507
+ "type": "string"
508
+ }
509
+ }
510
+ }
511
+ }
512
+ }
513
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "code-inventory",
3
- "version": "0.1.0",
3
+ "version": "0.2.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.",