depgraph-core 1.5.1 → 1.5.2

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/docs/README.md CHANGED
@@ -18,6 +18,7 @@ This folder explains how every moving part of the compiler works so you can cont
18
18
  | [stage-impact.md](./stage-impact.md) | Stage 5 — Impact simulation (BFS + risk scoring) |
19
19
  | [stage-output.md](./stage-output.md) | Stage 6 — JSON report generation |
20
20
  | [language-registry.md](./language-registry.md) | The language plugin system — how to add a new language |
21
+ | [parser-conventions.md](./parser-conventions.md) | Parser conventions — single-file vs modular packages and coding standards |
21
22
  | [data-types.md](./data-types.md) | All shared TypeScript interfaces, explained |
22
23
 
23
24
  ---
@@ -104,20 +104,21 @@ These are **side-effect imports** (no named export is consumed). They exist pure
104
104
 
105
105
  ## Currently Registered Languages
106
106
 
107
- | File | `lang` | Extensions |
108
- |---|---|---|
109
- | `javascript.ts` | `js` | `.js` `.jsx` `.mjs` `.cjs` `.ts` `.tsx` |
110
- | `python.ts` | `py` | `.py` |
111
-
112
- The following language files exist as stubs (imported but with minimal or no implementation yet):
113
-
114
- - `java.ts`
115
- - `csharp.ts`
116
- - `go.ts`
117
- - `kotlin.ts`
118
- - `php.ts`
119
- - `ruby.ts`
120
- - `swift.ts`
107
+ | Module / File | `lang` | Extensions | Organization Tier |
108
+ |---|---|---|---|
109
+ | `javascript.ts` | `js` | `.js` `.jsx` `.mjs` `.cjs` `.ts` `.tsx` | Tier 1 (Single-file) |
110
+ | `python.ts` | `py` | `.py` | Tier 1 (Single-file) |
111
+ | `go.ts` | `go` | `.go` | Tier 1 (Single-file) |
112
+ | `java.ts` | `java` | `.java` | Tier 1 (Single-file) |
113
+ | `csharp.ts` | `csharp` | `.cs` | Tier 1 (Single-file) |
114
+ | `kotlin.ts` | `kotlin` | `.kt` `.kts` | Tier 1 (Single-file) |
115
+ | `php.ts` | `php` | `.php` | Tier 1 (Single-file) |
116
+ | `ruby.ts` | `ruby` | `.rb` | Tier 1 (Single-file) |
117
+ | `swift.ts` | `swift` | `.swift` | Tier 1 (Single-file) |
118
+ | `dart/` | `dart` | `.dart` | Tier 2 (Modular package) |
119
+ | `rust/` | `rust` | `.rs` | Tier 2 (Modular package) |
120
+
121
+ > For architectural guidelines on choosing between Tier 1 and Tier 2, see [parser-conventions.md](./parser-conventions.md).
121
122
 
122
123
  ---
123
124
 
@@ -0,0 +1,194 @@
1
+ # Language Parser Conventions
2
+
3
+ > **Standardized architecture, directory conventions, and coding guidelines for DepGraph language parsers.**
4
+
5
+ ---
6
+
7
+ ## Overview
8
+
9
+ DepGraph relies on language plugins to extract code entities, imports, and exports from source files across various languages without heavy AST dependencies. As support for languages grows in sophistication—especially when incorporating deep graph extractors (e.g. Graphify engines) or rich syntax parsing—parsers must adhere to clear structural conventions to remain readable, maintainable, and testable.
10
+
11
+ ---
12
+
13
+ ## The Two-Tier Architecture
14
+
15
+ To balance simplicity for straightforward languages and scalability for feature-rich languages, DepGraph uses a two-tier organization model:
16
+
17
+ | Tier | Format | When to Use | Typical File Count | Target Size |
18
+ |---|---|---|---|---|
19
+ | **Tier 1: Single-File Plugin** | `src/languages/<lang>.ts` | Simple languages or basic regex extractors | 1 file | < 300 lines |
20
+ | **Tier 2: Modular Directory Module** | `src/languages/<lang>/` | Complex languages, >300 lines, or dual-purpose extractors (DepGraph + Graphify) | 4–6 modular files | < 250 lines per file |
21
+
22
+ ---
23
+
24
+ ## Tier 1: Single-File Plugin Convention
25
+
26
+ Used by languages like Python, Go, Java, Kotlin, PHP, Ruby, Swift, C#, and JavaScript.
27
+
28
+ ### Structure
29
+
30
+ A single file: `src/languages/<lang>.ts`
31
+
32
+ ```
33
+ src/languages/
34
+ ├── go.ts
35
+ ├── python.ts
36
+ └── java.ts
37
+ ```
38
+
39
+ ### Standard Section Order
40
+
41
+ Every single-file parser must organize code into well-demarcated sections in the following order:
42
+
43
+ ```ts
44
+ // 1. Imports & External Types
45
+ import { RawEntity, RawImport } from '../types';
46
+ import { EntityPattern, LanguageParser, registerParser } from './registry';
47
+ import { COMPLEXITY_THRESHOLDS } from '../constants';
48
+
49
+ // 2. Helpers & Complexity Estimation
50
+ function estimateComplexity(code: string, name: string): string { ... }
51
+
52
+ // 3. Entity Patterns (Exported for gitdiff reuse)
53
+ export const langEntityPatterns: EntityPattern[] = [ ... ];
54
+
55
+ // 4. Entity Extractor
56
+ function extractEntities(code: string, filePath: string): RawEntity[] { ... }
57
+
58
+ // 5. Import Extractor
59
+ function extractImports(code: string): RawImport[] { ... }
60
+
61
+ // 6. Export Extractor
62
+ function extractExports(code: string): string[] { ... }
63
+
64
+ // 7. Parser Registration
65
+ export const LangParser: LanguageParser = {
66
+ lang: 'lang',
67
+ extensions: ['.ext'],
68
+ extractEntities,
69
+ extractImports,
70
+ extractExports,
71
+ entityPatterns: langEntityPatterns,
72
+ };
73
+
74
+ registerParser(LangParser);
75
+ ```
76
+
77
+ ---
78
+
79
+ ## Tier 2: Modular Directory Module Convention
80
+
81
+ Used when a language parser exceeds ~300 lines or integrates secondary engines (such as standalone Graphify graph extractors). Languages using this tier include **Dart** and **Rust**.
82
+
83
+ ### Directory Layout
84
+
85
+ ```
86
+ src/languages/<lang>/
87
+ ├── index.ts # Public entrypoint, LanguageParser definition, side-effect registration
88
+ ├── types.ts # Specialized TypeScript interfaces (e.g., Graphify node/edge definitions)
89
+ ├── patterns.ts # EntityPattern definitions, keyword sets, and token constants
90
+ ├── helpers.ts # Lexing utilities, comment cleaners, brace depth counters, complexity
91
+ ├── extractor.ts # Core DepGraph contract: extractEntities, extractImports, extractExports
92
+ └── graphify.ts # Standalone deep AST/regex graph extraction engine (if applicable)
93
+ ```
94
+
95
+ ### File Responsibilities & Contract
96
+
97
+ #### 1. `index.ts` (Entry Point & Barrel)
98
+ - Constructs the `LanguageParser` object.
99
+ - Invokes `registerParser(...)` at module evaluation time.
100
+ - Re-exports public helpers and secondary functions (e.g., `cleanDartComments`, `estimateComplexity`, `extractDart`) to ensure seamless backward compatibility.
101
+ - Transparently resolves `import './languages/dart'` without altering caller syntax.
102
+
103
+ ```ts
104
+ import { LanguageParser, registerParser } from '../registry';
105
+ import { langEntityPatterns } from './patterns';
106
+ import { extractEntities, extractImports, extractExports } from './extractor';
107
+
108
+ export * from './types';
109
+ export * from './patterns';
110
+ export * from './helpers';
111
+ export * from './graphify';
112
+
113
+ export const LangParser: LanguageParser = {
114
+ lang: 'lang',
115
+ extensions: ['.ext'],
116
+ extractEntities,
117
+ extractImports,
118
+ extractExports,
119
+ entityPatterns: langEntityPatterns,
120
+ };
121
+
122
+ registerParser(LangParser);
123
+ ```
124
+
125
+ #### 2. `types.ts` (Domain Types)
126
+ - Houses language-specific data contracts that do not belong in global `src/types.ts`.
127
+ - Example: Graphify node representations, edge payloads, and extractor result contracts (`DartGraphNode`, `DartGraphEdge`, `DartGraphResult`).
128
+
129
+ #### 3. `patterns.ts` (Regex & Token Sets)
130
+ - Contains `EntityPattern[]` regex rules with named or indexed capture groups (Group 1 must always be the entity name).
131
+ - Houses language keywords and primitive type sets (e.g., `DART_KEYWORDS`, `RUST_PRIMITIVES`).
132
+
133
+ #### 4. `helpers.ts` (Lexical & Analysis Utilities)
134
+ - String manipulations, string literal escaping (`escapeRegex`).
135
+ - Comment cleaning functions that preserve line counts (`clean<Lang>Comments`).
136
+ - Balanced bracket/brace scanners (`_findMatchingBrace`, `_splitBalanced`).
137
+ - Cyclomatic complexity estimation (`estimateComplexity`).
138
+
139
+ #### 5. `extractor.ts` (DepGraph Stage 2 Implementation)
140
+ - Implements the 3 required methods of `LanguageParser`:
141
+ - `extractEntities(code: string, filePath: string): RawEntity[]`
142
+ - `extractImports(code: string): RawImport[]`
143
+ - `extractExports(code: string): string[]`
144
+ - Operates strictly on pre-cleaned code (single-line comments already stripped by the stage caller).
145
+
146
+ #### 6. `graphify.ts` (Deep Graph Engine)
147
+ - Isolated from core stage pipeline execution.
148
+ - Contains the self-contained graph extraction function (`extractDart`, `extractRust`).
149
+ - Allows comprehensive standalone graph analysis without bloating the pipeline parsing logic.
150
+
151
+ ---
152
+
153
+ ## Core Implementation Rules
154
+
155
+ Every parser, whether Tier 1 or Tier 2, must respect the following rules:
156
+
157
+ ### 1. Zero Heavy AST Dependencies
158
+ DepGraph is intentionally designed to be lightweight, ultrafast, and dependency-free.
159
+ - **Do not** introduce heavy AST parsers (such as Babel, tree-sitter, or Roslyn).
160
+ - Rely on regular expressions, state machines, and balanced delimiter traversal.
161
+
162
+ ### 2. Preserve Line Number Offsets During Comment Cleaning
163
+ When stripping comments or preprocessing files:
164
+ - Replace stripped blocks with the identical number of `\n` newline characters they spanned.
165
+ - This guarantees that entity line numbers reported in `RawEntity.line` accurately match original file source lines.
166
+
167
+ ### 3. Regex State Hygiene
168
+ Global regular expressions (`/pattern/g`) retain state in `regex.lastIndex`.
169
+ - Reset `regex.lastIndex = 0` prior to any `exec` loop or instantiate regular expressions within the function scope.
170
+ - Never let `lastIndex` leak across calls.
171
+
172
+ ### 4. Group 1 Entity Identifier Contract
173
+ Any pattern in `entityPatterns` must capture the entity identifier name in **Group 1**:
174
+ ```ts
175
+ {
176
+ regex: /^[ \t]*fn\s+([a-z_]\w*)/gm,
177
+ type: 'function',
178
+ }
179
+ ```
180
+ This enables `src/stages/gitdiff.ts` to deduce modified entity names directly from git hunk headers.
181
+
182
+ ### 5. Local vs. External Import Flagging
183
+ - Mark `isLocal: true` only for relative or internal project paths (e.g., `./utils`, `../services`).
184
+ - Mark `isLocal: false` for standard library packages or third-party package dependencies (e.g., `fmt`, `dart:async`, `std::collections`).
185
+ - Only local imports become edges in DepGraph's dependency graph.
186
+
187
+ ---
188
+
189
+ ## When to Migrate from Tier 1 to Tier 2
190
+
191
+ Migrate a language parser from `src/languages/<lang>.ts` to `src/languages/<lang>/` when:
192
+ 1. The single file exceeds **300 lines of code**.
193
+ 2. The language incorporates custom type systems or deep extractors (e.g., Graphify bindings).
194
+ 3. The language requires extensive grammar assistance (e.g., custom bracket matching, token splitting, or stateful tokenization).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "depgraph-core",
3
- "version": "1.5.1",
3
+ "version": "1.5.2",
4
4
  "description": "Dependency mapping and impact simulation for JS/TS projects",
5
5
  "main": "depgraph.js",
6
6
  "bin": {