universal-ast-mapper 0.8.6 → 1.0.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 +58 -0
- package/dist/callgraph.js +13 -0
- package/dist/cli.js +34 -0
- package/dist/extractors/common.js +2 -0
- package/dist/extractors/python.js +12 -1
- package/dist/index.js +47 -0
- package/dist/typeflow.js +124 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -96,6 +96,7 @@ ast-map cycles <dir>
|
|
|
96
96
|
ast-map duplicates <dir> [alias: dupes]
|
|
97
97
|
ast-map complexity <path> [alias: cx] [--min N]
|
|
98
98
|
ast-map unused-params <path> [alias: unused]
|
|
99
|
+
ast-map trace-type <type> [dir] [alias: flow]
|
|
99
100
|
ast-map search <pattern> [dir] [-m contains|exact|regex] [-k kind] [-e]
|
|
100
101
|
ast-map deps <file> [--scan <dir>]
|
|
101
102
|
ast-map top <dir> [-n 10]
|
|
@@ -309,6 +310,21 @@ Scan a file or directory for **named functions/methods with parameters that are
|
|
|
309
310
|
|
|
310
311
|
---
|
|
311
312
|
|
|
313
|
+
### `trace_type`
|
|
314
|
+
**Scoped type-flow tracing.** Find everywhere a named type flows through a directory — function **parameters** and **return types**, typed **variables**, and class **fields**. AST-based (no full type inference), so it tracks where a type is *named* in signatures; works best for TS/Python but resolves return/param types in any language that annotates them.
|
|
315
|
+
|
|
316
|
+
```json
|
|
317
|
+
{
|
|
318
|
+
"type": "Inventory",
|
|
319
|
+
"byRole": { "param": 3, "return": 2, "variable": 1, "field": 1 },
|
|
320
|
+
"refs": [ { "file": "src/svc.ts", "symbol": "make", "role": "return", "line": 4 } ]
|
|
321
|
+
}
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
**Params:** `type`, `path`
|
|
325
|
+
|
|
326
|
+
---
|
|
327
|
+
|
|
312
328
|
### `get_change_impact`
|
|
313
329
|
Given a file + symbol, reverse-traverse the import graph to compute **blast radius**.
|
|
314
330
|
|
|
@@ -527,10 +543,52 @@ src/
|
|
|
527
543
|
|
|
528
544
|
---
|
|
529
545
|
|
|
546
|
+
## GitHub Action — architecture gate in CI
|
|
547
|
+
|
|
548
|
+
Use AST-MCP as a CI check with the bundled composite action (`action.yml`):
|
|
549
|
+
|
|
550
|
+
```yaml
|
|
551
|
+
# .github/workflows/architecture.yml
|
|
552
|
+
name: Architecture
|
|
553
|
+
on: [pull_request]
|
|
554
|
+
jobs:
|
|
555
|
+
validate:
|
|
556
|
+
runs-on: ubuntu-latest
|
|
557
|
+
steps:
|
|
558
|
+
- uses: actions/checkout@v4
|
|
559
|
+
- uses: actions/setup-node@v4
|
|
560
|
+
with: { node-version: "20" }
|
|
561
|
+
- uses: 6ixthxense/AST-MCP@v1.0.0
|
|
562
|
+
with:
|
|
563
|
+
path: src
|
|
564
|
+
max-lines: "400"
|
|
565
|
+
max-imports: "20"
|
|
566
|
+
max-exports: "15"
|
|
567
|
+
```
|
|
568
|
+
|
|
569
|
+
The action runs `ast-map validate` and fails the job on threshold violations. You can also call any CLI command directly with `npx -p universal-ast-mapper ast-map <command>`.
|
|
570
|
+
|
|
571
|
+
---
|
|
572
|
+
|
|
573
|
+
## Stability (1.0)
|
|
574
|
+
|
|
575
|
+
As of **v1.0.0**, the public surface is stable across the `1.x` line:
|
|
576
|
+
|
|
577
|
+
- **MCP tool names and input schemas** — no breaking changes; new tools and new *optional* inputs may be added.
|
|
578
|
+
- **CLI commands and flags** — stable; new commands/flags may be added.
|
|
579
|
+
- **Skeleton JSON** — `schemaVersion` follows additive-compatible evolution; new *optional* fields (e.g. `props`, `decorators`) may appear without a major bump.
|
|
580
|
+
|
|
581
|
+
Not part of the public API: the internal `src/` module layout and the generated HTML markup.
|
|
582
|
+
|
|
583
|
+
---
|
|
584
|
+
|
|
530
585
|
## Changelog
|
|
531
586
|
|
|
532
587
|
| Version | What changed |
|
|
533
588
|
|---------|--------------|
|
|
589
|
+
| **1.0.0** | **Stable release.** Locks the public API (MCP tool names + schemas, CLI surface) for the 1.x line. Adds a **GitHub Action** (`action.yml`) to run `ast-map validate` as a CI architecture gate, plus a project CI workflow. Caps a 12-language engine with 18 MCP tools / 17 CLI commands spanning skeletons, dependency graphs, and deep analysis (dead code · cycles · impact · complexity · duplicates · unused params · decorators · type flow). |
|
|
590
|
+
| **0.9.0** | **Scoped type-flow tracing** — new `trace_type` MCP tool + `ast-map trace-type` (alias `flow`) CLI: follow a named type through function params, return types, typed variables, and class fields across a directory. Completes the deeper-analysis suite (dead code · cycles · impact · complexity · duplicates · unused params · type flow). **18 MCP tools**. |
|
|
591
|
+
| **0.8.7** | **Python decorators in the call graph** — function/method symbols now carry a `decorators` field (`@router.get("/x")` → `router.get("/x")`), surfaced in skeletons (outline + full) and in `get_call_graph`. Traces framework wiring like FastAPI/Flask routes and `@staticmethod`/`@property` stacks to their handler. |
|
|
534
592
|
| **0.8.6** | **Unused parameter detection** — new `find_unused_params` MCP tool + `ast-map unused-params` (alias `unused`) CLI: named functions whose params are never referenced. Skips `_`-prefixed/destructured/anonymous and treats object-shorthand as a use (low false-positive). Server now 17 tools. |
|
|
535
593
|
| **0.8.5** | **Cyclomatic complexity** — new `get_complexity` MCP tool + `ast-map complexity` (alias `cx`, `--min N`) CLI: per-function AST-based complexity score (`1 + if/for/while/case/catch/ternary/&&/\|\|`) with low/moderate/high/very-high ratings and directory hotspots. Server now 16 tools. |
|
|
536
594
|
| **0.8.4** | **Duplicate symbol detection** — new `find_duplicate_symbols` MCP tool + `ast-map duplicates` (alias `dupes`) CLI command: finds symbol names exported from more than one file, with every file/kind that declares each name. |
|
package/dist/callgraph.js
CHANGED
|
@@ -243,6 +243,17 @@ async function fileCallsSymbol(fileAbs, funcName) {
|
|
|
243
243
|
return false;
|
|
244
244
|
}
|
|
245
245
|
// ─── Public API ───────────────────────────────────────────────────────────────
|
|
246
|
+
/** Recursively find the first symbol with the given name and return its decorators. */
|
|
247
|
+
function findDecorators(symbols, name) {
|
|
248
|
+
for (const s of symbols) {
|
|
249
|
+
if (s.name === name && s.decorators && s.decorators.length > 0)
|
|
250
|
+
return s.decorators;
|
|
251
|
+
const nested = findDecorators(s.children, name);
|
|
252
|
+
if (nested)
|
|
253
|
+
return nested;
|
|
254
|
+
}
|
|
255
|
+
return undefined;
|
|
256
|
+
}
|
|
246
257
|
export async function buildCallGraph(filePath, funcName, root, allSkeletons) {
|
|
247
258
|
const langEntry = detectLanguage(filePath);
|
|
248
259
|
if (!langEntry)
|
|
@@ -427,6 +438,7 @@ export async function buildCallGraph(filePath, funcName, root, allSkeletons) {
|
|
|
427
438
|
}
|
|
428
439
|
}
|
|
429
440
|
}
|
|
441
|
+
const decorators = findDecorators(skel.symbols, funcName);
|
|
430
442
|
return {
|
|
431
443
|
file: relPath,
|
|
432
444
|
function: funcName,
|
|
@@ -434,6 +446,7 @@ export async function buildCallGraph(filePath, funcName, root, allSkeletons) {
|
|
|
434
446
|
startLine: funcNode.startPosition.row + 1,
|
|
435
447
|
endLine: funcNode.endPosition.row + 1,
|
|
436
448
|
},
|
|
449
|
+
...(decorators ? { decorators } : {}),
|
|
437
450
|
calls,
|
|
438
451
|
calledBy,
|
|
439
452
|
};
|
package/dist/cli.js
CHANGED
|
@@ -12,6 +12,7 @@ import { buildSymbolGraph } from "./graph.js";
|
|
|
12
12
|
import { findDeadExports, findCircularDeps, getChangeImpact, getFileDeps, getTopSymbols, findDuplicateSymbols } from "./graph-analysis.js";
|
|
13
13
|
import { computeFileComplexity } from "./complexity.js";
|
|
14
14
|
import { findUnusedParams } from "./unused-params.js";
|
|
15
|
+
import { traceTypeInFile } from "./typeflow.js";
|
|
15
16
|
import { buildCallGraph } from "./callgraph.js";
|
|
16
17
|
import { searchSymbols } from "./search.js";
|
|
17
18
|
const ROOT = path.resolve(process.env.AST_MAP_ROOT ?? process.cwd());
|
|
@@ -368,6 +369,39 @@ program
|
|
|
368
369
|
}
|
|
369
370
|
console.log();
|
|
370
371
|
});
|
|
372
|
+
// ─── Command: trace-type ──────────────────────────────────────────────────────
|
|
373
|
+
program
|
|
374
|
+
.command("trace-type <type> [dir]")
|
|
375
|
+
.alias("flow")
|
|
376
|
+
.description("Trace a type through params, returns, variables and fields")
|
|
377
|
+
.option("--json", "Output as JSON")
|
|
378
|
+
.action(async (typeName, dir, opts) => {
|
|
379
|
+
const { abs, rel } = resolveArg(dir ?? ".");
|
|
380
|
+
if (!fs.statSync(abs).isDirectory())
|
|
381
|
+
die(`"${rel}" is not a directory`);
|
|
382
|
+
const sopts = resolveOptions({ detail: "outline", emitHtml: false });
|
|
383
|
+
const refs = [];
|
|
384
|
+
for (const file of collectSourceFiles(abs, sopts)) {
|
|
385
|
+
const fileRel = path.relative(ROOT, file).split(path.sep).join("/");
|
|
386
|
+
refs.push(...(await traceTypeInFile(file, fileRel, typeName)));
|
|
387
|
+
}
|
|
388
|
+
if (opts.json)
|
|
389
|
+
return jsonOut({ type: typeName, dir: rel, refCount: refs.length, refs });
|
|
390
|
+
header(`Type Flow: ${bold(typeName)} — ${rel}/ ${dim(`(${refs.length} ref(s))`)}`);
|
|
391
|
+
if (refs.length === 0) {
|
|
392
|
+
console.log(indent(dim(`No references to type "${typeName}" found in signatures.`)));
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
const roleColor = (r) => (r === "return" ? green : r === "param" ? yellow : dim);
|
|
396
|
+
table(refs.map((r) => [
|
|
397
|
+
roleColor(r.role)(r.role),
|
|
398
|
+
r.symbol + (r.detail ? `(${r.detail})` : ""),
|
|
399
|
+
`:${r.line}`,
|
|
400
|
+
r.file,
|
|
401
|
+
]), [["Role", 9], ["Symbol", 24], ["Line", 6], ["File", 34]]);
|
|
402
|
+
}
|
|
403
|
+
console.log();
|
|
404
|
+
});
|
|
371
405
|
// ─── Command: unused-params ───────────────────────────────────────────────────
|
|
372
406
|
program
|
|
373
407
|
.command("unused-params <path>")
|
|
@@ -15,7 +15,18 @@ function collect(nodes, insideClass) {
|
|
|
15
15
|
function handle(node, insideClass) {
|
|
16
16
|
if (node.type === "decorated_definition") {
|
|
17
17
|
const inner = innerDefinition(node);
|
|
18
|
-
|
|
18
|
+
if (!inner)
|
|
19
|
+
return null;
|
|
20
|
+
const sym = handle(inner, insideClass);
|
|
21
|
+
if (sym) {
|
|
22
|
+
const decs = namedChildren(node)
|
|
23
|
+
.filter((c) => c.type === "decorator")
|
|
24
|
+
.map((d) => d.text.replace(/^@\s*/, "").replace(/\s+/g, " ").trim())
|
|
25
|
+
.filter((t) => t.length > 0);
|
|
26
|
+
if (decs.length > 0)
|
|
27
|
+
sym.decorators = decs;
|
|
28
|
+
}
|
|
29
|
+
return sym;
|
|
19
30
|
}
|
|
20
31
|
if (node.type === "class_definition") {
|
|
21
32
|
const name = nameOf(node) ?? "(class)";
|
package/dist/index.js
CHANGED
|
@@ -17,6 +17,7 @@ import { buildCallGraph } from "./callgraph.js";
|
|
|
17
17
|
import { searchSymbols } from "./search.js";
|
|
18
18
|
import { computeFileComplexity } from "./complexity.js";
|
|
19
19
|
import { findUnusedParams } from "./unused-params.js";
|
|
20
|
+
import { traceTypeInFile } from "./typeflow.js";
|
|
20
21
|
/** Files may only be read inside this root (override with AST_MAP_ROOT). */
|
|
21
22
|
const ROOT = path.resolve(process.env.AST_MAP_ROOT ?? process.cwd());
|
|
22
23
|
function resolveInRoot(input) {
|
|
@@ -667,6 +668,52 @@ server.registerTool("find_unused_params", {
|
|
|
667
668
|
return errorText(describeError(err));
|
|
668
669
|
}
|
|
669
670
|
});
|
|
671
|
+
/* ─────────────────── tool: trace_type ──────────────────────────────────── */
|
|
672
|
+
server.registerTool("trace_type", {
|
|
673
|
+
title: "Trace a type through the code",
|
|
674
|
+
description: "Find everywhere a named type flows through a directory: function parameters and return " +
|
|
675
|
+
"types, typed variables, and class fields. A scoped, AST-based type-flow view (best for " +
|
|
676
|
+
"TS/Python) \u2014 no full type inference, so it tracks where the type is *named* in signatures.",
|
|
677
|
+
inputSchema: {
|
|
678
|
+
type: z.string().describe('Type name to trace, e.g. "Inventory".'),
|
|
679
|
+
path: z.string().describe("Directory to scan, relative to project root or absolute within it."),
|
|
680
|
+
},
|
|
681
|
+
}, async ({ type: typeName, path: input }) => {
|
|
682
|
+
try {
|
|
683
|
+
const { abs, rel } = resolveInRoot(input);
|
|
684
|
+
if (!fs.statSync(abs).isDirectory()) {
|
|
685
|
+
return errorText(`"${input}" is not a directory. trace_type requires a directory.`);
|
|
686
|
+
}
|
|
687
|
+
const opts = resolveOptions({ detail: "outline", emitHtml: false });
|
|
688
|
+
const files = collectSourceFiles(abs, opts);
|
|
689
|
+
const refs = [];
|
|
690
|
+
const errors = [];
|
|
691
|
+
for (const file of files) {
|
|
692
|
+
const fileRel = path.relative(ROOT, file).split(path.sep).join("/");
|
|
693
|
+
try {
|
|
694
|
+
refs.push(...(await traceTypeInFile(file, fileRel, typeName)));
|
|
695
|
+
}
|
|
696
|
+
catch (err) {
|
|
697
|
+
errors.push({ file: fileRel, error: describeError(err) });
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
const byRole = { param: 0, return: 0, variable: 0, field: 0 };
|
|
701
|
+
for (const r of refs)
|
|
702
|
+
byRole[r.role]++;
|
|
703
|
+
return jsonText({
|
|
704
|
+
type: typeName,
|
|
705
|
+
directory: rel.split(path.sep).join("/"),
|
|
706
|
+
scanned: files.length,
|
|
707
|
+
refCount: refs.length,
|
|
708
|
+
byRole,
|
|
709
|
+
...(errors.length > 0 ? { errors } : {}),
|
|
710
|
+
refs,
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
catch (err) {
|
|
714
|
+
return errorText(describeError(err));
|
|
715
|
+
}
|
|
716
|
+
});
|
|
670
717
|
/* ─────────────────── tool: get_change_impact ───────────────────────────── */
|
|
671
718
|
server.registerTool("get_change_impact", {
|
|
672
719
|
title: "Get change impact (blast radius)",
|
package/dist/typeflow.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { parseSource } from "./parser.js";
|
|
3
|
+
import { detectLanguage } from "./registry.js";
|
|
4
|
+
const FN_TYPES = new Set([
|
|
5
|
+
"function_declaration", "generator_function_declaration", "function_definition",
|
|
6
|
+
"async_function_definition", "method_definition", "method_declaration",
|
|
7
|
+
"constructor_declaration", "function_item",
|
|
8
|
+
]);
|
|
9
|
+
const ID_TYPES = new Set(["identifier", "simple_identifier"]);
|
|
10
|
+
const TYPE_ID_TYPES = new Set(["type_identifier", "identifier"]);
|
|
11
|
+
const PARAM_CONTAINERS = new Set([
|
|
12
|
+
"formal_parameters", "parameters", "parameter_list", "function_value_parameters",
|
|
13
|
+
]);
|
|
14
|
+
function fnName(node) {
|
|
15
|
+
const nm = node.childForFieldName("name");
|
|
16
|
+
if (nm)
|
|
17
|
+
return nm.text;
|
|
18
|
+
for (let i = 0; i < node.namedChildCount; i++) {
|
|
19
|
+
const c = node.namedChild(i);
|
|
20
|
+
if (c && c.type === "simple_identifier")
|
|
21
|
+
return c.text;
|
|
22
|
+
}
|
|
23
|
+
return "(anonymous)";
|
|
24
|
+
}
|
|
25
|
+
/** Does this type-annotation subtree reference the bare type name `name`? */
|
|
26
|
+
function typeRefsName(node, name) {
|
|
27
|
+
if (!node)
|
|
28
|
+
return false;
|
|
29
|
+
let hit = false;
|
|
30
|
+
const walk = (n) => {
|
|
31
|
+
if (hit)
|
|
32
|
+
return;
|
|
33
|
+
if (TYPE_ID_TYPES.has(n.type) && n.text === name) {
|
|
34
|
+
hit = true;
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
for (let i = 0; i < n.namedChildCount; i++) {
|
|
38
|
+
const c = n.namedChild(i);
|
|
39
|
+
if (c)
|
|
40
|
+
walk(c);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
walk(node);
|
|
44
|
+
return hit;
|
|
45
|
+
}
|
|
46
|
+
function paramName(p) {
|
|
47
|
+
if (ID_TYPES.has(p.type))
|
|
48
|
+
return p.text;
|
|
49
|
+
const pat = p.childForFieldName("pattern");
|
|
50
|
+
if (pat && ID_TYPES.has(pat.type))
|
|
51
|
+
return pat.text;
|
|
52
|
+
const nm = p.childForFieldName("name");
|
|
53
|
+
if (nm && ID_TYPES.has(nm.type))
|
|
54
|
+
return nm.text;
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
function paramsNode(fn) {
|
|
58
|
+
const p = fn.childForFieldName("parameters");
|
|
59
|
+
if (p)
|
|
60
|
+
return p;
|
|
61
|
+
for (let i = 0; i < fn.namedChildCount; i++) {
|
|
62
|
+
const c = fn.namedChild(i);
|
|
63
|
+
if (c && PARAM_CONTAINERS.has(c.type))
|
|
64
|
+
return c;
|
|
65
|
+
}
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
export async function traceTypeInFile(absPath, relPath, typeName) {
|
|
69
|
+
const lang = detectLanguage(absPath);
|
|
70
|
+
if (!lang)
|
|
71
|
+
return [];
|
|
72
|
+
const source = fs.readFileSync(absPath, "utf8");
|
|
73
|
+
const root = await parseSource(lang.grammar, source);
|
|
74
|
+
const refs = [];
|
|
75
|
+
const walk = (node) => {
|
|
76
|
+
if (FN_TYPES.has(node.type)) {
|
|
77
|
+
const name = fnName(node);
|
|
78
|
+
// return type
|
|
79
|
+
const rt = node.childForFieldName("return_type");
|
|
80
|
+
if (typeRefsName(rt, typeName)) {
|
|
81
|
+
refs.push({ file: relPath, symbol: name, role: "return", line: node.startPosition.row + 1 });
|
|
82
|
+
}
|
|
83
|
+
// params
|
|
84
|
+
const pnode = paramsNode(node);
|
|
85
|
+
if (pnode) {
|
|
86
|
+
for (let i = 0; i < pnode.namedChildCount; i++) {
|
|
87
|
+
const p = pnode.namedChild(i);
|
|
88
|
+
if (!p)
|
|
89
|
+
continue;
|
|
90
|
+
const ty = p.childForFieldName("type");
|
|
91
|
+
if (typeRefsName(ty, typeName)) {
|
|
92
|
+
const pn = paramName(p);
|
|
93
|
+
refs.push({
|
|
94
|
+
file: relPath, symbol: name, role: "param",
|
|
95
|
+
...(pn ? { detail: pn } : {}),
|
|
96
|
+
line: p.startPosition.row + 1,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
else if (node.type === "variable_declarator") {
|
|
103
|
+
const ty = node.childForFieldName("type");
|
|
104
|
+
const nm = node.childForFieldName("name");
|
|
105
|
+
if (ty && nm && typeRefsName(ty, typeName)) {
|
|
106
|
+
refs.push({ file: relPath, symbol: nm.text, role: "variable", line: node.startPosition.row + 1 });
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
else if (node.type === "public_field_definition" || node.type === "field_definition") {
|
|
110
|
+
const ty = node.childForFieldName("type");
|
|
111
|
+
const nm = node.childForFieldName("name");
|
|
112
|
+
if (ty && nm && typeRefsName(ty, typeName)) {
|
|
113
|
+
refs.push({ file: relPath, symbol: nm.text, role: "field", line: node.startPosition.row + 1 });
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
for (let i = 0; i < node.namedChildCount; i++) {
|
|
117
|
+
const c = node.namedChild(i);
|
|
118
|
+
if (c)
|
|
119
|
+
walk(c);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
walk(root);
|
|
123
|
+
return refs;
|
|
124
|
+
}
|
package/package.json
CHANGED