depgraph-core 1.5.2 → 1.8.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/.vscode/depgraph-output.json +7197 -2405
- package/README.md +44 -2
- package/depgraph-mcp.js +24282 -0
- package/depgraph.js +2 -2
- package/package.json +10 -3
- package/docs/README.md +0 -57
- package/docs/architecture.md +0 -89
- package/docs/data-types.md +0 -218
- package/docs/language-registry.md +0 -206
- package/docs/parser-conventions.md +0 -194
- package/docs/stage-collector.md +0 -109
- package/docs/stage-graph.md +0 -157
- package/docs/stage-impact.md +0 -154
- package/docs/stage-metrics.md +0 -113
- package/docs/stage-output.md +0 -144
- package/docs/stage-parser.md +0 -144
package/docs/stage-metrics.md
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
# Stage 4 — Metrics
|
|
2
|
-
|
|
3
|
-
> **File**: `src/stages/metrics.ts`
|
|
4
|
-
> **Exports**: `computeMetrics`, `getEntryPoints`, `getLeafNodes`, `getIsolatedNodes`, `getCriticalNodes`
|
|
5
|
-
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
## What It Does
|
|
9
|
-
|
|
10
|
-
The metrics stage takes the raw graph from the builder and **enriches each node** with three numeric properties: `inDegree`, `outDegree`, and `centralityScore`. It also provides four helper functions for categorising nodes.
|
|
11
|
-
|
|
12
|
-
This stage **mutates** the nodes inside the graph in place and returns the same `DepGraph` object.
|
|
13
|
-
|
|
14
|
-
---
|
|
15
|
-
|
|
16
|
-
## `computeMetrics(graph: DepGraph): DepGraph`
|
|
17
|
-
|
|
18
|
-
### Step 1 — Count in-degree and out-degree
|
|
19
|
-
|
|
20
|
-
The function iterates over every edge and increments counters on the two endpoint nodes:
|
|
21
|
-
|
|
22
|
-
```ts
|
|
23
|
-
for (const edge of graph.edges) {
|
|
24
|
-
const fromNode = graph.nodes.get(edge.from);
|
|
25
|
-
const toNode = graph.nodes.get(edge.to);
|
|
26
|
-
|
|
27
|
-
if (fromNode) fromNode.outDegree += 1; // this node imports something
|
|
28
|
-
if (toNode) toNode.inDegree += 1; // this node is imported by something
|
|
29
|
-
}
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
| Property | Meaning |
|
|
33
|
-
|---|---|
|
|
34
|
-
| `inDegree` | Number of other nodes that import **this** node |
|
|
35
|
-
| `outDegree` | Number of nodes **this** node imports |
|
|
36
|
-
|
|
37
|
-
### Step 2 — Compute centrality score
|
|
38
|
-
|
|
39
|
-
```ts
|
|
40
|
-
node.centralityScore = node.inDegree * 2 + node.outDegree;
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
The formula weights **inDegree** more heavily (×2) because being imported by many things is riskier than importing many things. A node with a high inDegree is a "load-bearing" piece of code — changing it breaks many consumers.
|
|
44
|
-
|
|
45
|
-
---
|
|
46
|
-
|
|
47
|
-
## Node Categorisation Helpers
|
|
48
|
-
|
|
49
|
-
These four helper functions are used by both the **Output** stage and the **main** entry point to produce the summary section of the report.
|
|
50
|
-
|
|
51
|
-
### `getEntryPoints(graph)`
|
|
52
|
-
|
|
53
|
-
```ts
|
|
54
|
-
n.inDegree === 0 && n.outDegree > 0
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
**Nothing imports this node, but it imports others.**
|
|
58
|
-
These are application roots: `main.ts`, route files, top-level scripts. They start the dependency chain.
|
|
59
|
-
|
|
60
|
-
### `getLeafNodes(graph)`
|
|
61
|
-
|
|
62
|
-
```ts
|
|
63
|
-
n.outDegree === 0 && n.inDegree > 0
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
**This node imports nothing, but others import it.**
|
|
67
|
-
These are pure utilities and helpers: `hashPassword`, `formatDate`, shared constants. They're at the bottom of the dependency chain.
|
|
68
|
-
|
|
69
|
-
### `getIsolatedNodes(graph)`
|
|
70
|
-
|
|
71
|
-
```ts
|
|
72
|
-
n.inDegree === 0 && n.outDegree === 0
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
**This node has no connections at all.**
|
|
76
|
-
These are dead code candidates — entities defined but never imported or used by any other tracked entity in the project.
|
|
77
|
-
|
|
78
|
-
### `getCriticalNodes(graph)`
|
|
79
|
-
|
|
80
|
-
```ts
|
|
81
|
-
n.centralityScore > 20
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
**Nodes with a centrality score above 20.**
|
|
85
|
-
These are architectural hotspots. Modifying them carries high risk of unintended side-effects across the codebase. The impact simulator (see [stage-impact.md](./stage-impact.md)) was designed specifically for these.
|
|
86
|
-
|
|
87
|
-
---
|
|
88
|
-
|
|
89
|
-
## Understanding the Score Threshold
|
|
90
|
-
|
|
91
|
-
The threshold of `20` for critical nodes translates roughly to:
|
|
92
|
-
- A node imported by 10 other nodes (`10 * 2 = 20`), or
|
|
93
|
-
- A node imported by 7 nodes and importing 6 others (`7*2 + 6 = 20`)
|
|
94
|
-
|
|
95
|
-
Adjust `getCriticalNodes` if your project has a different risk tolerance.
|
|
96
|
-
|
|
97
|
-
---
|
|
98
|
-
|
|
99
|
-
## Example — Annotated Node After This Stage
|
|
100
|
-
|
|
101
|
-
```json
|
|
102
|
-
{
|
|
103
|
-
"id": "getUserById__userService",
|
|
104
|
-
"name": "getUserById",
|
|
105
|
-
"type": "function",
|
|
106
|
-
"inDegree": 8,
|
|
107
|
-
"outDegree": 2,
|
|
108
|
-
"centralityScore": 18,
|
|
109
|
-
"connections": ["usersRouter__routes", "adminRouter__routes", "..."]
|
|
110
|
-
}
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
This node is imported by 8 other nodes and imports 2. Its centrality score is 18 — just under the critical threshold.
|
package/docs/stage-output.md
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
# Stage 6 — Output
|
|
2
|
-
|
|
3
|
-
> **File**: `src/stages/output.ts`
|
|
4
|
-
> **Exports**: `writeOutput(graph, parsed, outputPath, impact?): void`
|
|
5
|
-
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
## What It Does
|
|
9
|
-
|
|
10
|
-
The output stage is the final step in the pipeline. It takes the fully-enriched graph, the parsed file list, and the optional impact report, **serialises everything into a structured JSON document**, and writes it to disk.
|
|
11
|
-
|
|
12
|
-
---
|
|
13
|
-
|
|
14
|
-
## The Four-Step Write Process
|
|
15
|
-
|
|
16
|
-
### Step 1 — Compute the summary
|
|
17
|
-
|
|
18
|
-
```ts
|
|
19
|
-
const summary = {
|
|
20
|
-
totalNodes: graph.nodes.size,
|
|
21
|
-
totalEdges: graph.edges.length,
|
|
22
|
-
entryPoints: getEntryPoints(graph),
|
|
23
|
-
leafNodes: getLeafNodes(graph),
|
|
24
|
-
isolatedNodes: getIsolatedNodes(graph),
|
|
25
|
-
criticalNodes: getCriticalNodes(graph),
|
|
26
|
-
};
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
This calls the four categorisation helpers from the metrics stage (see [stage-metrics.md](./stage-metrics.md)) to produce the summary section.
|
|
30
|
-
|
|
31
|
-
### Step 2 — Assemble the full output object
|
|
32
|
-
|
|
33
|
-
```ts
|
|
34
|
-
const output: OutputJSON = {
|
|
35
|
-
meta: {
|
|
36
|
-
version: '1.0.0',
|
|
37
|
-
timestamp: new Date().toISOString(),
|
|
38
|
-
totalFiles: parsed.length,
|
|
39
|
-
totalLines: totalLines,
|
|
40
|
-
},
|
|
41
|
-
summary,
|
|
42
|
-
nodes: [...graph.nodes.values()], // Map → Array
|
|
43
|
-
edges: graph.edges,
|
|
44
|
-
files: parsed,
|
|
45
|
-
impact, // undefined if --impact was not used
|
|
46
|
-
};
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
The `nodes` Map is converted to an Array here so it serialises correctly as JSON.
|
|
50
|
-
|
|
51
|
-
### Step 3 — Write to disk
|
|
52
|
-
|
|
53
|
-
```ts
|
|
54
|
-
fs.writeFileSync(
|
|
55
|
-
outputPath,
|
|
56
|
-
JSON.stringify(output, null, 2), // pretty-printed, 2-space indent
|
|
57
|
-
'utf-8'
|
|
58
|
-
);
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
If the output directory does not exist, it is created recursively:
|
|
62
|
-
|
|
63
|
-
```ts
|
|
64
|
-
const dir = path.dirname(outputPath);
|
|
65
|
-
if (!fs.existsSync(dir)) {
|
|
66
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
67
|
-
}
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
### Step 4 — Print confirmation
|
|
71
|
-
|
|
72
|
-
After a successful write, a summary is printed to stdout:
|
|
73
|
-
|
|
74
|
-
```
|
|
75
|
-
Output written to ./depgraph-output.json
|
|
76
|
-
12 files
|
|
77
|
-
87 nodes
|
|
78
|
-
143 edges
|
|
79
|
-
3240 total lines of code
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
If an impact report was included:
|
|
83
|
-
|
|
84
|
-
```
|
|
85
|
-
Impact Report included
|
|
86
|
-
Target : getUserById__userService
|
|
87
|
-
Risk Level : HIGH
|
|
88
|
-
Risk Score : 62
|
|
89
|
-
Affected : 9 nodes
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
---
|
|
93
|
-
|
|
94
|
-
## Output File Schema — `OutputJSON`
|
|
95
|
-
|
|
96
|
-
```ts
|
|
97
|
-
interface OutputJSON {
|
|
98
|
-
meta: {
|
|
99
|
-
version: string; // tool version, e.g. "1.0.0"
|
|
100
|
-
timestamp: string; // ISO 8601 datetime
|
|
101
|
-
totalFiles: number;
|
|
102
|
-
totalLines: number;
|
|
103
|
-
};
|
|
104
|
-
summary: {
|
|
105
|
-
totalNodes: number;
|
|
106
|
-
totalEdges: number;
|
|
107
|
-
entryPoints: string[]; // node IDs
|
|
108
|
-
leafNodes: string[]; // node IDs
|
|
109
|
-
isolatedNodes: string[]; // node IDs (dead code candidates)
|
|
110
|
-
criticalNodes: string[]; // node IDs (centrality > 20)
|
|
111
|
-
};
|
|
112
|
-
nodes: DepNode[];
|
|
113
|
-
edges: DepEdge[];
|
|
114
|
-
files: ParsedFile[];
|
|
115
|
-
impact?: ImpactReport; // only present if --impact was used
|
|
116
|
-
}
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
See [data-types.md](./data-types.md) for the full definitions of each nested type.
|
|
120
|
-
|
|
121
|
-
---
|
|
122
|
-
|
|
123
|
-
## Default Output Path
|
|
124
|
-
|
|
125
|
-
```
|
|
126
|
-
./depgraph-output.json
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
Override with `--output <path>`:
|
|
130
|
-
|
|
131
|
-
```bash
|
|
132
|
-
node depgraph.js ./src --output ./reports/my-graph.json
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
---
|
|
136
|
-
|
|
137
|
-
## Using the Output File
|
|
138
|
-
|
|
139
|
-
The JSON output is designed to be consumed by:
|
|
140
|
-
|
|
141
|
-
- **Visualisation tools** — feed `nodes` and `edges` into a graph renderer (e.g. D3, Cytoscape, Sigma.js)
|
|
142
|
-
- **CI checks** — inspect `summary.criticalNodes` or `impact.riskLevel` in a script
|
|
143
|
-
- **IDE plugins** — parse `files` to annotate code with entity metadata
|
|
144
|
-
- **AI tools** — pass the graph as context for code review or refactoring suggestions
|
package/docs/stage-parser.md
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
# Stage 2 — Parser
|
|
2
|
-
|
|
3
|
-
> **File**: `src/stages/parser.ts`
|
|
4
|
-
> **Exports**: `parseFile(filePath: string): ParsedFile | null`, `parseFiles(filePaths: string[]): ParsedFile[]`
|
|
5
|
-
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
## What It Does
|
|
9
|
-
|
|
10
|
-
The parser takes each file path from the collector and:
|
|
11
|
-
|
|
12
|
-
1. Reads the raw source code from disk.
|
|
13
|
-
2. Looks up the correct **language parser** from the registry based on the file extension.
|
|
14
|
-
3. Strips single-line `//` comments from the code before analysis.
|
|
15
|
-
4. Calls the language parser to extract **entities**, **imports**, and **exports**.
|
|
16
|
-
5. Returns a structured `ParsedFile` object.
|
|
17
|
-
|
|
18
|
-
---
|
|
19
|
-
|
|
20
|
-
## The `parseFile` Function — Step by Step
|
|
21
|
-
|
|
22
|
-
### Step 1: Read the file
|
|
23
|
-
|
|
24
|
-
```ts
|
|
25
|
-
const code = fs.readFileSync(filePath, 'utf-8');
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
If the file can't be read, it logs a warning and returns `null`.
|
|
29
|
-
|
|
30
|
-
### Step 2: Resolve the language parser
|
|
31
|
-
|
|
32
|
-
```ts
|
|
33
|
-
const ext = path.extname(filePath).toLowerCase();
|
|
34
|
-
const parser = getLanguageParser(ext);
|
|
35
|
-
if (!parser) return null;
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
`getLanguageParser` queries the language registry (see [language-registry.md](./language-registry.md)). If no parser is registered for the extension, the file is silently skipped.
|
|
39
|
-
|
|
40
|
-
### Step 3: Strip single-line comments
|
|
41
|
-
|
|
42
|
-
```ts
|
|
43
|
-
const cleanCode = code
|
|
44
|
-
.split('\n')
|
|
45
|
-
.map(line => {
|
|
46
|
-
const commentIndex = line.indexOf('//');
|
|
47
|
-
if (commentIndex === -1) return line;
|
|
48
|
-
// make sure // is not inside a string
|
|
49
|
-
const before = line.slice(0, commentIndex);
|
|
50
|
-
const inString = (before.match(/"/g) || []).length % 2 !== 0
|
|
51
|
-
|| (before.match(/'/g) || []).length % 2 !== 0;
|
|
52
|
-
return inString ? line : line.slice(0, commentIndex);
|
|
53
|
-
})
|
|
54
|
-
.join('\n');
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
This is done before passing code to any parser to prevent `//` inside comments from confusing regex patterns. The string-detection guard prevents false positives on lines like:
|
|
58
|
-
|
|
59
|
-
```ts
|
|
60
|
-
const url = "https://example.com"; // this would break without the guard
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
> **Limitation**: The guard only checks for unbalanced `"` or `'` quotes on the *same line*. Multi-line strings or template literals are not fully handled.
|
|
64
|
-
|
|
65
|
-
### Step 4: Extract data via the language parser
|
|
66
|
-
|
|
67
|
-
```ts
|
|
68
|
-
const lines = code.split('\n').length; // count from original code
|
|
69
|
-
const entities = parser.extractEntities(cleanCode, filePath);
|
|
70
|
-
const imports = parser.extractImports(cleanCode);
|
|
71
|
-
const exports = parser.extractExports(cleanCode);
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
Note that line counting uses the **original** code (before stripping) to get accurate line numbers. Extraction uses the **clean** code.
|
|
75
|
-
|
|
76
|
-
### Step 5: Return the result
|
|
77
|
-
|
|
78
|
-
```ts
|
|
79
|
-
return { filePath, lang: parser.lang, lines, entities, imports, exports };
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
---
|
|
83
|
-
|
|
84
|
-
## The `parseFiles` Function
|
|
85
|
-
|
|
86
|
-
A simple iterator over `parseFile`. Files that return `null` are silently dropped:
|
|
87
|
-
|
|
88
|
-
```ts
|
|
89
|
-
for (const filePath of filePaths) {
|
|
90
|
-
const parsed = parseFile(filePath);
|
|
91
|
-
if (parsed) results.push(parsed);
|
|
92
|
-
}
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
---
|
|
96
|
-
|
|
97
|
-
## Output Shape — `ParsedFile`
|
|
98
|
-
|
|
99
|
-
```ts
|
|
100
|
-
interface ParsedFile {
|
|
101
|
-
filePath: string; // e.g. "/project/src/auth.ts"
|
|
102
|
-
lang: string; // e.g. "js" — from the parser's lang field
|
|
103
|
-
lines: number; // total line count
|
|
104
|
-
entities: RawEntity[];
|
|
105
|
-
imports: RawImport[];
|
|
106
|
-
exports: string[];
|
|
107
|
-
}
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
See [data-types.md](./data-types.md) for the full definitions of `RawEntity` and `RawImport`.
|
|
111
|
-
|
|
112
|
-
---
|
|
113
|
-
|
|
114
|
-
## What "entities" means
|
|
115
|
-
|
|
116
|
-
An entity is any named code construct the parser can recognise:
|
|
117
|
-
|
|
118
|
-
- Functions, async functions, arrow functions
|
|
119
|
-
- Classes, interfaces, types (TypeScript)
|
|
120
|
-
- React components and hooks
|
|
121
|
-
- Express/Fastify API route declarations
|
|
122
|
-
- Python classes and `def` functions
|
|
123
|
-
|
|
124
|
-
Every entity has a `name`, `type`, `line`, and `complexity` rating.
|
|
125
|
-
|
|
126
|
-
---
|
|
127
|
-
|
|
128
|
-
## Complexity Estimation
|
|
129
|
-
|
|
130
|
-
Each language parser estimates complexity by counting branching keywords in the entity's body:
|
|
131
|
-
|
|
132
|
-
| Branch count | Rating |
|
|
133
|
-
|---|---|
|
|
134
|
-
| 0–3 | `"low"` |
|
|
135
|
-
| 4–8 | `"medium"` |
|
|
136
|
-
| 9+ | `"high"` |
|
|
137
|
-
|
|
138
|
-
Thresholds are set in `COMPLEXITY_THRESHOLDS` in [`src/constants.ts`](../src/constants.ts).
|
|
139
|
-
|
|
140
|
-
---
|
|
141
|
-
|
|
142
|
-
## Adding Support for a New Language
|
|
143
|
-
|
|
144
|
-
You don't touch the parser stage at all. Instead, register a new language parser — see [language-registry.md](./language-registry.md).
|