flavor-code 1.2.13 → 1.2.15
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 +9 -1
- package/README.zh-CN.md +9 -1
- package/dist/agent/planner.d.ts +2 -0
- package/dist/{app-Z3YWFIBB.js → app-3KW2FTVJ.js} +2 -2
- package/dist/astgraph/db.mjs +237 -237
- package/dist/astgraph/extract.mjs +239 -239
- package/dist/astgraph/flavor-plugin.json +25 -25
- package/dist/astgraph/grammars.mjs +68 -68
- package/dist/astgraph/index.js +301 -301
- package/dist/astgraph/indexer.mjs +175 -175
- package/dist/astgraph/query.mjs +165 -165
- package/dist/astgraph/resolve.mjs +93 -93
- package/dist/astgraph/vendor/tree-sitter.js +3980 -3980
- package/dist/astgraph/vendor/zod/LICENSE +21 -21
- package/dist/astgraph/vendor/zod/index.js +4 -4
- package/dist/astgraph/vendor/zod/locales/index.js +1 -1
- package/dist/astgraph/vendor/zod/locales/package.json +7 -7
- package/dist/astgraph/vendor/zod/package.json +135 -135
- package/dist/astgraph/vendor/zod/v4/classic/checks.js +1 -1
- package/dist/astgraph/vendor/zod/v4/classic/coerce.js +17 -17
- package/dist/astgraph/vendor/zod/v4/classic/compat.js +31 -31
- package/dist/astgraph/vendor/zod/v4/classic/errors.js +48 -48
- package/dist/astgraph/vendor/zod/v4/classic/external.js +20 -20
- package/dist/astgraph/vendor/zod/v4/classic/from-json-schema.js +599 -599
- package/dist/astgraph/vendor/zod/v4/classic/index.js +4 -4
- package/dist/astgraph/vendor/zod/v4/classic/iso.js +30 -30
- package/dist/astgraph/vendor/zod/v4/classic/package.json +7 -7
- package/dist/astgraph/vendor/zod/v4/classic/parse.js +15 -15
- package/dist/astgraph/vendor/zod/v4/classic/schemas.js +1395 -1395
- package/dist/astgraph/vendor/zod/v4/core/api.js +1087 -1087
- package/dist/astgraph/vendor/zod/v4/core/checks.js +575 -575
- package/dist/astgraph/vendor/zod/v4/core/core.js +78 -78
- package/dist/astgraph/vendor/zod/v4/core/doc.js +35 -35
- package/dist/astgraph/vendor/zod/v4/core/errors.js +185 -185
- package/dist/astgraph/vendor/zod/v4/core/index.js +16 -16
- package/dist/astgraph/vendor/zod/v4/core/json-schema-generator.js +95 -95
- package/dist/astgraph/vendor/zod/v4/core/json-schema-processors.js +601 -601
- package/dist/astgraph/vendor/zod/v4/core/json-schema.js +1 -1
- package/dist/astgraph/vendor/zod/v4/core/package.json +7 -7
- package/dist/astgraph/vendor/zod/v4/core/parse.js +93 -93
- package/dist/astgraph/vendor/zod/v4/core/regexes.js +139 -139
- package/dist/astgraph/vendor/zod/v4/core/registries.js +51 -51
- package/dist/astgraph/vendor/zod/v4/core/schemas.js +2239 -2239
- package/dist/astgraph/vendor/zod/v4/core/standard-schema.js +1 -1
- package/dist/astgraph/vendor/zod/v4/core/to-json-schema.js +448 -448
- package/dist/astgraph/vendor/zod/v4/core/util.js +674 -674
- package/dist/astgraph/vendor/zod/v4/core/versions.js +5 -5
- package/dist/astgraph/vendor/zod/v4/index.js +3 -3
- package/dist/astgraph/vendor/zod/v4/locales/ar.js +106 -106
- package/dist/astgraph/vendor/zod/v4/locales/az.js +105 -105
- package/dist/astgraph/vendor/zod/v4/locales/be.js +156 -156
- package/dist/astgraph/vendor/zod/v4/locales/bg.js +120 -120
- package/dist/astgraph/vendor/zod/v4/locales/ca.js +107 -107
- package/dist/astgraph/vendor/zod/v4/locales/cs.js +111 -111
- package/dist/astgraph/vendor/zod/v4/locales/da.js +115 -115
- package/dist/astgraph/vendor/zod/v4/locales/de.js +108 -108
- package/dist/astgraph/vendor/zod/v4/locales/el.js +109 -109
- package/dist/astgraph/vendor/zod/v4/locales/en.js +113 -113
- package/dist/astgraph/vendor/zod/v4/locales/eo.js +109 -109
- package/dist/astgraph/vendor/zod/v4/locales/es.js +132 -132
- package/dist/astgraph/vendor/zod/v4/locales/fa.js +114 -114
- package/dist/astgraph/vendor/zod/v4/locales/fi.js +112 -112
- package/dist/astgraph/vendor/zod/v4/locales/fr-CA.js +107 -107
- package/dist/astgraph/vendor/zod/v4/locales/fr.js +125 -125
- package/dist/astgraph/vendor/zod/v4/locales/he.js +214 -214
- package/dist/astgraph/vendor/zod/v4/locales/hr.js +122 -122
- package/dist/astgraph/vendor/zod/v4/locales/hu.js +108 -108
- package/dist/astgraph/vendor/zod/v4/locales/hy.js +147 -147
- package/dist/astgraph/vendor/zod/v4/locales/id.js +106 -106
- package/dist/astgraph/vendor/zod/v4/locales/index.js +52 -52
- package/dist/astgraph/vendor/zod/v4/locales/is.js +109 -109
- package/dist/astgraph/vendor/zod/v4/locales/it.js +108 -108
- package/dist/astgraph/vendor/zod/v4/locales/ja.js +107 -107
- package/dist/astgraph/vendor/zod/v4/locales/ka.js +112 -112
- package/dist/astgraph/vendor/zod/v4/locales/kh.js +5 -5
- package/dist/astgraph/vendor/zod/v4/locales/km.js +110 -110
- package/dist/astgraph/vendor/zod/v4/locales/ko.js +111 -111
- package/dist/astgraph/vendor/zod/v4/locales/lt.js +203 -203
- package/dist/astgraph/vendor/zod/v4/locales/mk.js +109 -109
- package/dist/astgraph/vendor/zod/v4/locales/ms.js +107 -107
- package/dist/astgraph/vendor/zod/v4/locales/nl.js +110 -110
- package/dist/astgraph/vendor/zod/v4/locales/no.js +108 -108
- package/dist/astgraph/vendor/zod/v4/locales/ota.js +109 -109
- package/dist/astgraph/vendor/zod/v4/locales/package.json +7 -7
- package/dist/astgraph/vendor/zod/v4/locales/pl.js +109 -109
- package/dist/astgraph/vendor/zod/v4/locales/ps.js +114 -114
- package/dist/astgraph/vendor/zod/v4/locales/pt.js +108 -108
- package/dist/astgraph/vendor/zod/v4/locales/ro.js +119 -119
- package/dist/astgraph/vendor/zod/v4/locales/ru.js +156 -156
- package/dist/astgraph/vendor/zod/v4/locales/sl.js +109 -109
- package/dist/astgraph/vendor/zod/v4/locales/sv.js +110 -110
- package/dist/astgraph/vendor/zod/v4/locales/ta.js +110 -110
- package/dist/astgraph/vendor/zod/v4/locales/th.js +110 -110
- package/dist/astgraph/vendor/zod/v4/locales/tr.js +105 -105
- package/dist/astgraph/vendor/zod/v4/locales/ua.js +5 -5
- package/dist/astgraph/vendor/zod/v4/locales/uk.js +108 -108
- package/dist/astgraph/vendor/zod/v4/locales/ur.js +110 -110
- package/dist/astgraph/vendor/zod/v4/locales/uz.js +110 -110
- package/dist/astgraph/vendor/zod/v4/locales/vi.js +108 -108
- package/dist/astgraph/vendor/zod/v4/locales/yo.js +107 -107
- package/dist/astgraph/vendor/zod/v4/locales/zh-CN.js +109 -109
- package/dist/astgraph/vendor/zod/v4/locales/zh-TW.js +107 -107
- package/dist/astgraph/vendor/zod/v4/mini/checks.js +1 -1
- package/dist/astgraph/vendor/zod/v4/mini/coerce.js +22 -22
- package/dist/astgraph/vendor/zod/v4/mini/external.js +14 -14
- package/dist/astgraph/vendor/zod/v4/mini/index.js +3 -3
- package/dist/astgraph/vendor/zod/v4/mini/iso.js +34 -34
- package/dist/astgraph/vendor/zod/v4/mini/package.json +7 -7
- package/dist/astgraph/vendor/zod/v4/mini/parse.js +1 -1
- package/dist/astgraph/vendor/zod/v4/mini/schemas.js +961 -961
- package/dist/astgraph/vendor/zod/v4/package.json +7 -7
- package/dist/{chunk-Y5MZDG7I.js → chunk-5TG3E2EJ.js} +3253 -1909
- package/dist/{chunk-G6Q2Y2A7.js → chunk-BKARQJQZ.js} +1 -1
- package/dist/{chunk-XFCJXRJ2.js → chunk-IG4Z23CL.js} +6 -0
- package/dist/cli.js +80 -18
- package/dist/config/schema.d.ts +6 -0
- package/dist/desktop/main.js +6216 -4807
- package/dist/desktop-renderer/assets/{index-CCnSpff0.js → index-BCJjMvbX.js} +1 -1
- package/dist/desktop-renderer/index.html +1 -1
- package/dist/evolve/loader.d.ts +21 -0
- package/dist/evolve/service.d.ts +37 -0
- package/dist/evolve/store.d.ts +117 -0
- package/dist/git/insights.d.ts +52 -0
- package/dist/git/service.d.ts +43 -0
- package/dist/git/tools.d.ts +9 -0
- package/dist/hooks/types.d.ts +5 -1
- package/dist/{load-6ZMEBHSG.js → load-GKA7P6RJ.js} +1 -1
- package/dist/plugins/host.d.ts +9 -0
- package/dist/plugins/types.d.ts +1 -0
- package/dist/sdk/index.js +3 -3
- package/dist/session/store.d.ts +1 -0
- package/dist/tools/shell.d.ts +13 -0
- package/dist/ui/commands.d.ts +11 -2
- package/dist/ui/session.d.ts +3 -0
- package/package.json +1 -1
- package//346/212/200/346/234/257/346/226/271/346/241/210/346/212/245/345/221/212.md +303 -1
|
@@ -1,239 +1,239 @@
|
|
|
1
|
-
// astgraph TypeScript/JavaScript structural extractor.
|
|
2
|
-
// Walks the tree-sitter syntax tree and produces:
|
|
3
|
-
// nodes — functions, classes, methods, interfaces, type aliases, variables
|
|
4
|
-
// edges — intra-file calls / extends / implements
|
|
5
|
-
// refs — cross-file references (imports + unresolved calls/heritage) resolved later
|
|
6
|
-
//
|
|
7
|
-
// Lines are 1-based in all outputs so they match editor/Read tool coordinates.
|
|
8
|
-
|
|
9
|
-
import { parseSource } from "./grammars.mjs";
|
|
10
|
-
|
|
11
|
-
const DECLARATION_KINDS = new Map([
|
|
12
|
-
["function_declaration", "function"],
|
|
13
|
-
["class_declaration", "class"],
|
|
14
|
-
["interface_declaration", "interface"],
|
|
15
|
-
["type_alias_declaration", "type"],
|
|
16
|
-
]);
|
|
17
|
-
|
|
18
|
-
/** Extract the doc text from a `/** ... */` or `// ...` comment node. */
|
|
19
|
-
function docstringFrom(commentNode) {
|
|
20
|
-
if (commentNode === null || commentNode.type !== "comment") return undefined;
|
|
21
|
-
const text = commentNode.text;
|
|
22
|
-
if (text.startsWith("/**") || text.startsWith("/*!")) {
|
|
23
|
-
return text
|
|
24
|
-
.replace(/^\/\*+[!*]?/, "")
|
|
25
|
-
.replace(/\*+\/$/, "")
|
|
26
|
-
.split("\n")
|
|
27
|
-
.map((line) => line.replace(/^\s*\*+\s?/, "").trim())
|
|
28
|
-
.filter((line) => line.length > 0)
|
|
29
|
-
.join("\n") || undefined;
|
|
30
|
-
}
|
|
31
|
-
if (text.startsWith("//")) return text.replace(/^\/\/+\s?/, "").trim() || undefined;
|
|
32
|
-
return undefined;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function firstLine(text, limit = 240) {
|
|
36
|
-
const line = text.split("\n")[0].trim();
|
|
37
|
-
return line.length > limit ? `${line.slice(0, limit)}…` : line;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function signatureOf(node) {
|
|
41
|
-
const text = node.text;
|
|
42
|
-
const brace = text.indexOf("{");
|
|
43
|
-
const head = brace >= 0 ? text.slice(0, brace) : text.split("\n")[0];
|
|
44
|
-
return firstLine(head.replace(/\s+/g, " "));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function nameOf(node) {
|
|
48
|
-
const nameNode = node.childForFieldName("name");
|
|
49
|
-
return nameNode === null ? undefined : nameNode.text;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function isAsync(node) {
|
|
53
|
-
return node.children.some((child) => child.type === "async");
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export async function extract(source, filePath, language) {
|
|
57
|
-
const tree = await parseSource(source, language);
|
|
58
|
-
|
|
59
|
-
/** @type {Array<object>} */
|
|
60
|
-
const nodes = [];
|
|
61
|
-
/** @type {Array<object>} */
|
|
62
|
-
const edges = [];
|
|
63
|
-
/** @type {Array<object>} */
|
|
64
|
-
const refs = [];
|
|
65
|
-
/** Map local symbol name → node id (methods registered as name only for call matching). */
|
|
66
|
-
const localNames = new Map();
|
|
67
|
-
/** Map imported binding name → module specifier. */
|
|
68
|
-
const imports = new Map();
|
|
69
|
-
/** Map declaration AST node id → node record, for enclosing-scope lookup.
|
|
70
|
-
* Keyed by numeric node.id because web-tree-sitter returns fresh node object
|
|
71
|
-
* instances on every navigation (parent/childForFieldName), so object identity
|
|
72
|
-
* cannot be used as a Map key. */
|
|
73
|
-
const scopeByNodeId = new Map();
|
|
74
|
-
|
|
75
|
-
const makeNode = (astNode, kind, name, qualifiedName, exported, extra = {}) => {
|
|
76
|
-
// The docstring lives on the top-level statement's preceding sibling (comments
|
|
77
|
-
// attach to program/export_statement, not to the inner declaration node).
|
|
78
|
-
const docSource = extra.docSource ?? astNode;
|
|
79
|
-
const record = {
|
|
80
|
-
id: `${filePath}#${qualifiedName}`,
|
|
81
|
-
kind,
|
|
82
|
-
name,
|
|
83
|
-
qualifiedName,
|
|
84
|
-
filePath,
|
|
85
|
-
language,
|
|
86
|
-
startLine: astNode.startPosition.row + 1,
|
|
87
|
-
endLine: astNode.endPosition.row + 1,
|
|
88
|
-
signature: signatureOf(astNode),
|
|
89
|
-
docstring: docstringFrom(docSource.previousSibling),
|
|
90
|
-
isExported: exported,
|
|
91
|
-
isAsync: extra.isAsync ?? false,
|
|
92
|
-
...extra,
|
|
93
|
-
};
|
|
94
|
-
delete record.docSource;
|
|
95
|
-
nodes.push(record);
|
|
96
|
-
scopeByNodeId.set(astNode.id, record);
|
|
97
|
-
if (!localNames.has(name)) localNames.set(name, record.id);
|
|
98
|
-
return record;
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
const collectDeclaration = (statement, exported) => {
|
|
102
|
-
const inner = statement.type === "export_statement" ? statement.namedChildren.find((child) => DECLARATION_KINDS.has(child.type) || child.type === "lexical_declaration" || child.type === "abstract_class_declaration") : statement;
|
|
103
|
-
const target = inner ?? statement;
|
|
104
|
-
const kind = DECLARATION_KINDS.get(target.type);
|
|
105
|
-
if (kind !== undefined) {
|
|
106
|
-
const name = nameOf(target);
|
|
107
|
-
if (name !== undefined) {
|
|
108
|
-
// Comments attach to the top-level statement (export_statement / declaration),
|
|
109
|
-
// so the docstring is read from the statement's preceding sibling.
|
|
110
|
-
makeNode(target, kind, name, name, exported, {
|
|
111
|
-
...(kind === "function" ? { isAsync: isAsync(target) } : {}),
|
|
112
|
-
docSource: statement,
|
|
113
|
-
});
|
|
114
|
-
if (target.type === "class_declaration" || target.type === "abstract_class_declaration") collectClassMembers(target, exported);
|
|
115
|
-
collectHeritage(target, exported);
|
|
116
|
-
}
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
if (target.type === "lexical_declaration") {
|
|
120
|
-
for (const declarator of target.namedChildren.filter((child) => child.type === "variable_declarator")) {
|
|
121
|
-
const name = declarator.childForFieldName("name")?.text;
|
|
122
|
-
const value = declarator.childForFieldName("value");
|
|
123
|
-
if (name === undefined || value === null) continue;
|
|
124
|
-
const isFunction = value.type === "arrow_function" || value.type === "function_expression" || value.type === "function";
|
|
125
|
-
makeNode(declarator, isFunction ? "function" : "variable", name, name, exported, {
|
|
126
|
-
isAsync: isFunction && value.children.some((child) => child.type === "async"),
|
|
127
|
-
docSource: statement,
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
const collectClassMembers = (classNode, classExported) => {
|
|
134
|
-
const className = nameOf(classNode);
|
|
135
|
-
const body = classNode.childForFieldName("body");
|
|
136
|
-
if (body === null) return;
|
|
137
|
-
for (const member of body.namedChildren) {
|
|
138
|
-
if (member.type !== "method_definition") continue;
|
|
139
|
-
const name = member.childForFieldName("name")?.text;
|
|
140
|
-
if (name === undefined) continue;
|
|
141
|
-
const qualified = `${className}.${name}`;
|
|
142
|
-
makeNode(member, "method", name, qualified, classExported, { isAsync: isAsync(member) });
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
const collectHeritage = (classNode, _exported) => {
|
|
147
|
-
const heritage = classNode.namedChildren.find((child) => child.type === "class_heritage");
|
|
148
|
-
if (heritage === undefined) return;
|
|
149
|
-
const sourceName = nameOf(classNode);
|
|
150
|
-
if (sourceName === undefined) return;
|
|
151
|
-
const sourceId = `${filePath}#${sourceName}`;
|
|
152
|
-
for (const clause of heritage.namedChildren) {
|
|
153
|
-
const kind = clause.type === "extends_clause" ? "extends" : clause.type === "implements_clause" ? "implements" : undefined;
|
|
154
|
-
if (kind === undefined) continue;
|
|
155
|
-
for (const typeNode of clause.namedChildren.filter((child) => child.type === "type_identifier" || child.type === "identifier")) {
|
|
156
|
-
const typeName = typeNode.text;
|
|
157
|
-
if (localNames.has(typeName)) {
|
|
158
|
-
edges.push({ source: sourceId, target: localNames.get(typeName), kind, line: typeNode.startPosition.row + 1, col: typeNode.startPosition.column });
|
|
159
|
-
} else {
|
|
160
|
-
refs.push({
|
|
161
|
-
fromNodeId: sourceId, referenceName: typeName, referenceKind: kind,
|
|
162
|
-
line: typeNode.startPosition.row + 1, col: typeNode.startPosition.column,
|
|
163
|
-
filePath, moduleSpecifier: undefined,
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
// Pass 1 — top-level declarations and imports.
|
|
171
|
-
for (const statement of tree.rootNode.namedChildren) {
|
|
172
|
-
if (statement.type === "import_statement") {
|
|
173
|
-
const sourceNode = statement.childForFieldName("source");
|
|
174
|
-
const moduleSpecifier = sourceNode === null ? undefined : sourceNode.text.replace(/^["']|["']$/g, "");
|
|
175
|
-
for (const clause of statement.namedChildren.filter((child) => child.type === "import_clause")) {
|
|
176
|
-
for (const named of clause.namedChildren.filter((child) => child.type === "named_imports")) {
|
|
177
|
-
for (const specifier of named.namedChildren.filter((child) => child.type === "import_specifier")) {
|
|
178
|
-
const nameNode = specifier.childForFieldName("name");
|
|
179
|
-
if (nameNode === null) continue;
|
|
180
|
-
const importedName = nameNode.text;
|
|
181
|
-
imports.set(importedName, moduleSpecifier);
|
|
182
|
-
refs.push({
|
|
183
|
-
fromNodeId: null, referenceName: importedName, referenceKind: "import",
|
|
184
|
-
line: nameNode.startPosition.row + 1, col: nameNode.startPosition.column,
|
|
185
|
-
filePath, moduleSpecifier,
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
const defaultName = clause.namedChildren.find((child) => child.type === "identifier");
|
|
190
|
-
if (defaultName !== undefined) {
|
|
191
|
-
imports.set(defaultName.text, moduleSpecifier);
|
|
192
|
-
refs.push({
|
|
193
|
-
fromNodeId: null, referenceName: defaultName.text, referenceKind: "import",
|
|
194
|
-
line: defaultName.startPosition.row + 1, col: defaultName.startPosition.column,
|
|
195
|
-
filePath, moduleSpecifier,
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
continue;
|
|
200
|
-
}
|
|
201
|
-
collectDeclaration(statement, statement.type === "export_statement");
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// Pass 2 — call expressions anywhere in the file.
|
|
205
|
-
const enclosingScope = (node) => {
|
|
206
|
-
let cursor = node.parent;
|
|
207
|
-
while (cursor !== null) {
|
|
208
|
-
const record = scopeByNodeId.get(cursor.id);
|
|
209
|
-
if (record !== undefined) return record;
|
|
210
|
-
cursor = cursor.parent;
|
|
211
|
-
}
|
|
212
|
-
return undefined;
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
const visitCalls = (node) => {
|
|
216
|
-
if (node.type === "call_expression") {
|
|
217
|
-
const callee = node.childForFieldName("function");
|
|
218
|
-
if (callee !== null && callee.type === "identifier") {
|
|
219
|
-
const name = callee.text;
|
|
220
|
-
const scope = enclosingScope(node);
|
|
221
|
-
if (scope !== undefined) {
|
|
222
|
-
if (localNames.has(name) && localNames.get(name) !== scope.id) {
|
|
223
|
-
edges.push({ source: scope.id, target: localNames.get(name), kind: "calls", line: callee.startPosition.row + 1, col: callee.startPosition.column });
|
|
224
|
-
} else if (!localNames.has(name)) {
|
|
225
|
-
refs.push({
|
|
226
|
-
fromNodeId: scope.id, referenceName: name, referenceKind: "call",
|
|
227
|
-
line: callee.startPosition.row + 1, col: callee.startPosition.column,
|
|
228
|
-
filePath, moduleSpecifier: imports.get(name),
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
for (const child of node.namedChildren) visitCalls(child);
|
|
235
|
-
};
|
|
236
|
-
visitCalls(tree.rootNode);
|
|
237
|
-
|
|
238
|
-
return { nodes, edges, refs, imports: Object.fromEntries(imports) };
|
|
239
|
-
}
|
|
1
|
+
// astgraph TypeScript/JavaScript structural extractor.
|
|
2
|
+
// Walks the tree-sitter syntax tree and produces:
|
|
3
|
+
// nodes — functions, classes, methods, interfaces, type aliases, variables
|
|
4
|
+
// edges — intra-file calls / extends / implements
|
|
5
|
+
// refs — cross-file references (imports + unresolved calls/heritage) resolved later
|
|
6
|
+
//
|
|
7
|
+
// Lines are 1-based in all outputs so they match editor/Read tool coordinates.
|
|
8
|
+
|
|
9
|
+
import { parseSource } from "./grammars.mjs";
|
|
10
|
+
|
|
11
|
+
const DECLARATION_KINDS = new Map([
|
|
12
|
+
["function_declaration", "function"],
|
|
13
|
+
["class_declaration", "class"],
|
|
14
|
+
["interface_declaration", "interface"],
|
|
15
|
+
["type_alias_declaration", "type"],
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
/** Extract the doc text from a `/** ... */` or `// ...` comment node. */
|
|
19
|
+
function docstringFrom(commentNode) {
|
|
20
|
+
if (commentNode === null || commentNode.type !== "comment") return undefined;
|
|
21
|
+
const text = commentNode.text;
|
|
22
|
+
if (text.startsWith("/**") || text.startsWith("/*!")) {
|
|
23
|
+
return text
|
|
24
|
+
.replace(/^\/\*+[!*]?/, "")
|
|
25
|
+
.replace(/\*+\/$/, "")
|
|
26
|
+
.split("\n")
|
|
27
|
+
.map((line) => line.replace(/^\s*\*+\s?/, "").trim())
|
|
28
|
+
.filter((line) => line.length > 0)
|
|
29
|
+
.join("\n") || undefined;
|
|
30
|
+
}
|
|
31
|
+
if (text.startsWith("//")) return text.replace(/^\/\/+\s?/, "").trim() || undefined;
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function firstLine(text, limit = 240) {
|
|
36
|
+
const line = text.split("\n")[0].trim();
|
|
37
|
+
return line.length > limit ? `${line.slice(0, limit)}…` : line;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function signatureOf(node) {
|
|
41
|
+
const text = node.text;
|
|
42
|
+
const brace = text.indexOf("{");
|
|
43
|
+
const head = brace >= 0 ? text.slice(0, brace) : text.split("\n")[0];
|
|
44
|
+
return firstLine(head.replace(/\s+/g, " "));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function nameOf(node) {
|
|
48
|
+
const nameNode = node.childForFieldName("name");
|
|
49
|
+
return nameNode === null ? undefined : nameNode.text;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function isAsync(node) {
|
|
53
|
+
return node.children.some((child) => child.type === "async");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function extract(source, filePath, language) {
|
|
57
|
+
const tree = await parseSource(source, language);
|
|
58
|
+
|
|
59
|
+
/** @type {Array<object>} */
|
|
60
|
+
const nodes = [];
|
|
61
|
+
/** @type {Array<object>} */
|
|
62
|
+
const edges = [];
|
|
63
|
+
/** @type {Array<object>} */
|
|
64
|
+
const refs = [];
|
|
65
|
+
/** Map local symbol name → node id (methods registered as name only for call matching). */
|
|
66
|
+
const localNames = new Map();
|
|
67
|
+
/** Map imported binding name → module specifier. */
|
|
68
|
+
const imports = new Map();
|
|
69
|
+
/** Map declaration AST node id → node record, for enclosing-scope lookup.
|
|
70
|
+
* Keyed by numeric node.id because web-tree-sitter returns fresh node object
|
|
71
|
+
* instances on every navigation (parent/childForFieldName), so object identity
|
|
72
|
+
* cannot be used as a Map key. */
|
|
73
|
+
const scopeByNodeId = new Map();
|
|
74
|
+
|
|
75
|
+
const makeNode = (astNode, kind, name, qualifiedName, exported, extra = {}) => {
|
|
76
|
+
// The docstring lives on the top-level statement's preceding sibling (comments
|
|
77
|
+
// attach to program/export_statement, not to the inner declaration node).
|
|
78
|
+
const docSource = extra.docSource ?? astNode;
|
|
79
|
+
const record = {
|
|
80
|
+
id: `${filePath}#${qualifiedName}`,
|
|
81
|
+
kind,
|
|
82
|
+
name,
|
|
83
|
+
qualifiedName,
|
|
84
|
+
filePath,
|
|
85
|
+
language,
|
|
86
|
+
startLine: astNode.startPosition.row + 1,
|
|
87
|
+
endLine: astNode.endPosition.row + 1,
|
|
88
|
+
signature: signatureOf(astNode),
|
|
89
|
+
docstring: docstringFrom(docSource.previousSibling),
|
|
90
|
+
isExported: exported,
|
|
91
|
+
isAsync: extra.isAsync ?? false,
|
|
92
|
+
...extra,
|
|
93
|
+
};
|
|
94
|
+
delete record.docSource;
|
|
95
|
+
nodes.push(record);
|
|
96
|
+
scopeByNodeId.set(astNode.id, record);
|
|
97
|
+
if (!localNames.has(name)) localNames.set(name, record.id);
|
|
98
|
+
return record;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const collectDeclaration = (statement, exported) => {
|
|
102
|
+
const inner = statement.type === "export_statement" ? statement.namedChildren.find((child) => DECLARATION_KINDS.has(child.type) || child.type === "lexical_declaration" || child.type === "abstract_class_declaration") : statement;
|
|
103
|
+
const target = inner ?? statement;
|
|
104
|
+
const kind = DECLARATION_KINDS.get(target.type);
|
|
105
|
+
if (kind !== undefined) {
|
|
106
|
+
const name = nameOf(target);
|
|
107
|
+
if (name !== undefined) {
|
|
108
|
+
// Comments attach to the top-level statement (export_statement / declaration),
|
|
109
|
+
// so the docstring is read from the statement's preceding sibling.
|
|
110
|
+
makeNode(target, kind, name, name, exported, {
|
|
111
|
+
...(kind === "function" ? { isAsync: isAsync(target) } : {}),
|
|
112
|
+
docSource: statement,
|
|
113
|
+
});
|
|
114
|
+
if (target.type === "class_declaration" || target.type === "abstract_class_declaration") collectClassMembers(target, exported);
|
|
115
|
+
collectHeritage(target, exported);
|
|
116
|
+
}
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (target.type === "lexical_declaration") {
|
|
120
|
+
for (const declarator of target.namedChildren.filter((child) => child.type === "variable_declarator")) {
|
|
121
|
+
const name = declarator.childForFieldName("name")?.text;
|
|
122
|
+
const value = declarator.childForFieldName("value");
|
|
123
|
+
if (name === undefined || value === null) continue;
|
|
124
|
+
const isFunction = value.type === "arrow_function" || value.type === "function_expression" || value.type === "function";
|
|
125
|
+
makeNode(declarator, isFunction ? "function" : "variable", name, name, exported, {
|
|
126
|
+
isAsync: isFunction && value.children.some((child) => child.type === "async"),
|
|
127
|
+
docSource: statement,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const collectClassMembers = (classNode, classExported) => {
|
|
134
|
+
const className = nameOf(classNode);
|
|
135
|
+
const body = classNode.childForFieldName("body");
|
|
136
|
+
if (body === null) return;
|
|
137
|
+
for (const member of body.namedChildren) {
|
|
138
|
+
if (member.type !== "method_definition") continue;
|
|
139
|
+
const name = member.childForFieldName("name")?.text;
|
|
140
|
+
if (name === undefined) continue;
|
|
141
|
+
const qualified = `${className}.${name}`;
|
|
142
|
+
makeNode(member, "method", name, qualified, classExported, { isAsync: isAsync(member) });
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const collectHeritage = (classNode, _exported) => {
|
|
147
|
+
const heritage = classNode.namedChildren.find((child) => child.type === "class_heritage");
|
|
148
|
+
if (heritage === undefined) return;
|
|
149
|
+
const sourceName = nameOf(classNode);
|
|
150
|
+
if (sourceName === undefined) return;
|
|
151
|
+
const sourceId = `${filePath}#${sourceName}`;
|
|
152
|
+
for (const clause of heritage.namedChildren) {
|
|
153
|
+
const kind = clause.type === "extends_clause" ? "extends" : clause.type === "implements_clause" ? "implements" : undefined;
|
|
154
|
+
if (kind === undefined) continue;
|
|
155
|
+
for (const typeNode of clause.namedChildren.filter((child) => child.type === "type_identifier" || child.type === "identifier")) {
|
|
156
|
+
const typeName = typeNode.text;
|
|
157
|
+
if (localNames.has(typeName)) {
|
|
158
|
+
edges.push({ source: sourceId, target: localNames.get(typeName), kind, line: typeNode.startPosition.row + 1, col: typeNode.startPosition.column });
|
|
159
|
+
} else {
|
|
160
|
+
refs.push({
|
|
161
|
+
fromNodeId: sourceId, referenceName: typeName, referenceKind: kind,
|
|
162
|
+
line: typeNode.startPosition.row + 1, col: typeNode.startPosition.column,
|
|
163
|
+
filePath, moduleSpecifier: undefined,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
// Pass 1 — top-level declarations and imports.
|
|
171
|
+
for (const statement of tree.rootNode.namedChildren) {
|
|
172
|
+
if (statement.type === "import_statement") {
|
|
173
|
+
const sourceNode = statement.childForFieldName("source");
|
|
174
|
+
const moduleSpecifier = sourceNode === null ? undefined : sourceNode.text.replace(/^["']|["']$/g, "");
|
|
175
|
+
for (const clause of statement.namedChildren.filter((child) => child.type === "import_clause")) {
|
|
176
|
+
for (const named of clause.namedChildren.filter((child) => child.type === "named_imports")) {
|
|
177
|
+
for (const specifier of named.namedChildren.filter((child) => child.type === "import_specifier")) {
|
|
178
|
+
const nameNode = specifier.childForFieldName("name");
|
|
179
|
+
if (nameNode === null) continue;
|
|
180
|
+
const importedName = nameNode.text;
|
|
181
|
+
imports.set(importedName, moduleSpecifier);
|
|
182
|
+
refs.push({
|
|
183
|
+
fromNodeId: null, referenceName: importedName, referenceKind: "import",
|
|
184
|
+
line: nameNode.startPosition.row + 1, col: nameNode.startPosition.column,
|
|
185
|
+
filePath, moduleSpecifier,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
const defaultName = clause.namedChildren.find((child) => child.type === "identifier");
|
|
190
|
+
if (defaultName !== undefined) {
|
|
191
|
+
imports.set(defaultName.text, moduleSpecifier);
|
|
192
|
+
refs.push({
|
|
193
|
+
fromNodeId: null, referenceName: defaultName.text, referenceKind: "import",
|
|
194
|
+
line: defaultName.startPosition.row + 1, col: defaultName.startPosition.column,
|
|
195
|
+
filePath, moduleSpecifier,
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
collectDeclaration(statement, statement.type === "export_statement");
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Pass 2 — call expressions anywhere in the file.
|
|
205
|
+
const enclosingScope = (node) => {
|
|
206
|
+
let cursor = node.parent;
|
|
207
|
+
while (cursor !== null) {
|
|
208
|
+
const record = scopeByNodeId.get(cursor.id);
|
|
209
|
+
if (record !== undefined) return record;
|
|
210
|
+
cursor = cursor.parent;
|
|
211
|
+
}
|
|
212
|
+
return undefined;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const visitCalls = (node) => {
|
|
216
|
+
if (node.type === "call_expression") {
|
|
217
|
+
const callee = node.childForFieldName("function");
|
|
218
|
+
if (callee !== null && callee.type === "identifier") {
|
|
219
|
+
const name = callee.text;
|
|
220
|
+
const scope = enclosingScope(node);
|
|
221
|
+
if (scope !== undefined) {
|
|
222
|
+
if (localNames.has(name) && localNames.get(name) !== scope.id) {
|
|
223
|
+
edges.push({ source: scope.id, target: localNames.get(name), kind: "calls", line: callee.startPosition.row + 1, col: callee.startPosition.column });
|
|
224
|
+
} else if (!localNames.has(name)) {
|
|
225
|
+
refs.push({
|
|
226
|
+
fromNodeId: scope.id, referenceName: name, referenceKind: "call",
|
|
227
|
+
line: callee.startPosition.row + 1, col: callee.startPosition.column,
|
|
228
|
+
filePath, moduleSpecifier: imports.get(name),
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
for (const child of node.namedChildren) visitCalls(child);
|
|
235
|
+
};
|
|
236
|
+
visitCalls(tree.rootNode);
|
|
237
|
+
|
|
238
|
+
return { nodes, edges, refs, imports: Object.fromEntries(imports) };
|
|
239
|
+
}
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "astgraph",
|
|
3
|
-
"version": "0.1.0",
|
|
4
|
-
"apiVersion": "1",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"permissions": [],
|
|
7
|
-
"contributes": {
|
|
8
|
-
"commands": [
|
|
9
|
-
{ "name": "ast" }
|
|
10
|
-
],
|
|
11
|
-
"tools": [
|
|
12
|
-
{ "name": "ast_search" },
|
|
13
|
-
{ "name": "ast_callers" },
|
|
14
|
-
{ "name": "ast_callees" },
|
|
15
|
-
{ "name": "ast_impact" },
|
|
16
|
-
{ "name": "ast_context" }
|
|
17
|
-
],
|
|
18
|
-
"skillRoots": [],
|
|
19
|
-
"modelAdapters": [],
|
|
20
|
-
"hooks": [
|
|
21
|
-
{ "name": "SessionStart" },
|
|
22
|
-
{ "name": "PostToolUse" }
|
|
23
|
-
]
|
|
24
|
-
}
|
|
25
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "astgraph",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"apiVersion": "1",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"permissions": [],
|
|
7
|
+
"contributes": {
|
|
8
|
+
"commands": [
|
|
9
|
+
{ "name": "ast" }
|
|
10
|
+
],
|
|
11
|
+
"tools": [
|
|
12
|
+
{ "name": "ast_search" },
|
|
13
|
+
{ "name": "ast_callers" },
|
|
14
|
+
{ "name": "ast_callees" },
|
|
15
|
+
{ "name": "ast_impact" },
|
|
16
|
+
{ "name": "ast_context" }
|
|
17
|
+
],
|
|
18
|
+
"skillRoots": [],
|
|
19
|
+
"modelAdapters": [],
|
|
20
|
+
"hooks": [
|
|
21
|
+
{ "name": "SessionStart" },
|
|
22
|
+
{ "name": "PostToolUse" }
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
}
|