neuroplex 0.5.0 → 0.7.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/dist/cli/index.js +15 -3
- package/dist/cli/index.js.map +1 -1
- package/dist/enrich/tasks.js +19 -0
- package/dist/enrich/tasks.js.map +1 -1
- package/dist/install/claude.d.ts +8 -0
- package/dist/install/claude.js +33 -0
- package/dist/install/claude.js.map +1 -1
- package/dist/scan/assemble.js +39 -108
- package/dist/scan/assemble.js.map +1 -1
- package/dist/scan/discover.js +13 -0
- package/dist/scan/discover.js.map +1 -1
- package/dist/scan/docs.js +20 -1
- package/dist/scan/docs.js.map +1 -1
- package/dist/scan/extract.js +12 -0
- package/dist/scan/extract.js.map +1 -1
- package/dist/scan/extractors/go.d.ts +4 -0
- package/dist/scan/extractors/go.js +743 -0
- package/dist/scan/extractors/go.js.map +1 -0
- package/dist/scan/extractors/java.d.ts +4 -0
- package/dist/scan/extractors/java.js +571 -0
- package/dist/scan/extractors/java.js.map +1 -0
- package/dist/scan/extractors/php.d.ts +4 -0
- package/dist/scan/extractors/php.js +775 -0
- package/dist/scan/extractors/php.js.map +1 -0
- package/dist/scan/extractors/rust.d.ts +4 -0
- package/dist/scan/extractors/rust.js +681 -0
- package/dist/scan/extractors/rust.js.map +1 -0
- package/dist/scan/extractors/typescript.js +13 -1
- package/dist/scan/extractors/typescript.js.map +1 -1
- package/dist/scan/parser.js +4 -0
- package/dist/scan/parser.js.map +1 -1
- package/dist/scan/resolve-named.d.ts +39 -0
- package/dist/scan/resolve-named.js +198 -0
- package/dist/scan/resolve-named.js.map +1 -0
- package/dist/scan/resolve.js +4 -1
- package/dist/scan/resolve.js.map +1 -1
- package/dist/scan/types.d.ts +1 -1
- package/dist/scan/types.js +1 -1
- package/dist/scan/types.js.map +1 -1
- package/package.json +1 -1
- package/skills/neuroplex/SKILL.md +3 -1
|
@@ -0,0 +1,743 @@
|
|
|
1
|
+
import { lineRange, namedChildren } from '../parser.js';
|
|
2
|
+
import { emptyExtraction } from '../types.js';
|
|
3
|
+
/** Route registration methods (gin/echo upper case, chi/fiber title case) and their HTTP method. */
|
|
4
|
+
const ROUTE_VERBS = new Map([
|
|
5
|
+
['GET', 'GET'],
|
|
6
|
+
['POST', 'POST'],
|
|
7
|
+
['PUT', 'PUT'],
|
|
8
|
+
['PATCH', 'PATCH'],
|
|
9
|
+
['DELETE', 'DELETE'],
|
|
10
|
+
['HEAD', 'HEAD'],
|
|
11
|
+
['OPTIONS', 'OPTIONS'],
|
|
12
|
+
['Get', 'GET'],
|
|
13
|
+
['Post', 'POST'],
|
|
14
|
+
['Put', 'PUT'],
|
|
15
|
+
['Patch', 'PATCH'],
|
|
16
|
+
['Delete', 'DELETE'],
|
|
17
|
+
['Head', 'HEAD'],
|
|
18
|
+
['Options', 'OPTIONS'],
|
|
19
|
+
['Any', 'ANY'],
|
|
20
|
+
['All', 'ANY'],
|
|
21
|
+
]);
|
|
22
|
+
/** net/http and chi registrations whose pattern may carry a method (`"GET /users/{id}"`). */
|
|
23
|
+
const MUX_METHODS = new Set(['HandleFunc', 'Handle']);
|
|
24
|
+
/** `gin.Handle("GET", "/x", h)`, chi `Method("GET", "/x", h)`: the method comes first. */
|
|
25
|
+
const METHOD_FIRST = new Set(['Handle', 'Method', 'MethodFunc']);
|
|
26
|
+
const HTTP_METHODS = new Set(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS', 'CONNECT', 'TRACE']);
|
|
27
|
+
/** `http.Get(url)`, `client.Post(url, ...)`: the URL is the first argument. */
|
|
28
|
+
const CLIENT_METHODS = new Map([
|
|
29
|
+
['Get', 'GET'],
|
|
30
|
+
['Head', 'HEAD'],
|
|
31
|
+
['Post', 'POST'],
|
|
32
|
+
['PostForm', 'POST'],
|
|
33
|
+
]);
|
|
34
|
+
/** resty `client.R().Get(url)`. */
|
|
35
|
+
const RESTY_METHODS = new Map([...CLIENT_METHODS, ['Put', 'PUT'], ['Patch', 'PATCH'], ['Delete', 'DELETE'], ['Options', 'OPTIONS']]);
|
|
36
|
+
const CLIENT_NAMES = new Set(['client', 'httpClient']);
|
|
37
|
+
/** Builtins and basic-type conversions are not worth an edge. */
|
|
38
|
+
const BUILTINS = new Set([
|
|
39
|
+
'append', 'cap', 'clear', 'close', 'complex', 'copy', 'delete', 'imag', 'len', 'make', 'max', 'min', 'new',
|
|
40
|
+
'panic', 'print', 'println', 'real', 'recover', 'string', 'bool', 'byte', 'rune', 'int', 'int8', 'int16',
|
|
41
|
+
'int32', 'int64', 'uint', 'uint8', 'uint16', 'uint32', 'uint64', 'uintptr', 'float32', 'float64',
|
|
42
|
+
'complex64', 'complex128', 'error', 'any',
|
|
43
|
+
]);
|
|
44
|
+
const LITERALS = new Set([
|
|
45
|
+
'int_literal',
|
|
46
|
+
'float_literal',
|
|
47
|
+
'imaginary_literal',
|
|
48
|
+
'rune_literal',
|
|
49
|
+
'interpreted_string_literal',
|
|
50
|
+
'raw_string_literal',
|
|
51
|
+
'true',
|
|
52
|
+
'false',
|
|
53
|
+
]);
|
|
54
|
+
/** Parents that continue a call chain up to its statement. */
|
|
55
|
+
const CHAIN_PARENTS = new Set(['call_expression', 'selector_expression', 'parenthesized_expression']);
|
|
56
|
+
/** Extracts symbols, imports, calls, heritage, HTTP routes and requests from a Go tree. */
|
|
57
|
+
export function extractGo(root) {
|
|
58
|
+
return new GoExtractor().run(root);
|
|
59
|
+
}
|
|
60
|
+
class GoExtractor {
|
|
61
|
+
out = emptyExtraction();
|
|
62
|
+
names = new Set();
|
|
63
|
+
seenCalls = new Set();
|
|
64
|
+
seenRequests = new Set();
|
|
65
|
+
/** Struct name → field name → field type, for `s.repo.Find()`. */
|
|
66
|
+
fields = new Map();
|
|
67
|
+
/** Local name of the `net/http` import. */
|
|
68
|
+
httpName = 'http';
|
|
69
|
+
run(root) {
|
|
70
|
+
const pkgScope = new Map();
|
|
71
|
+
for (const decl of namedChildren(root)) {
|
|
72
|
+
if (decl.type === 'type_declaration')
|
|
73
|
+
this.collectFields(decl);
|
|
74
|
+
if (decl.type === 'var_declaration')
|
|
75
|
+
collectScope(decl, pkgScope);
|
|
76
|
+
if (decl.type === 'import_declaration')
|
|
77
|
+
this.findHttpImport(decl);
|
|
78
|
+
}
|
|
79
|
+
const ctx = { owner: undefined, receiver: undefined, scopes: [pkgScope], prefixes: new Map() };
|
|
80
|
+
for (const child of namedChildren(root))
|
|
81
|
+
this.walk(child, ctx);
|
|
82
|
+
return this.out;
|
|
83
|
+
}
|
|
84
|
+
walk(node, ctx) {
|
|
85
|
+
switch (node.type) {
|
|
86
|
+
case 'ERROR':
|
|
87
|
+
return;
|
|
88
|
+
case 'import_declaration':
|
|
89
|
+
this.addImports(node);
|
|
90
|
+
return;
|
|
91
|
+
case 'function_declaration':
|
|
92
|
+
case 'method_declaration':
|
|
93
|
+
this.visitFunction(node, ctx);
|
|
94
|
+
return;
|
|
95
|
+
case 'type_declaration':
|
|
96
|
+
this.visitTypes(node);
|
|
97
|
+
return;
|
|
98
|
+
case 'const_declaration':
|
|
99
|
+
case 'var_declaration':
|
|
100
|
+
if (!ctx.owner)
|
|
101
|
+
this.visitValues(node);
|
|
102
|
+
break;
|
|
103
|
+
case 'call_expression':
|
|
104
|
+
this.visitCall(node, ctx);
|
|
105
|
+
return;
|
|
106
|
+
case 'type_conversion_expression':
|
|
107
|
+
this.visitConversion(node, ctx);
|
|
108
|
+
break;
|
|
109
|
+
case 'composite_literal':
|
|
110
|
+
this.visitComposite(node, ctx);
|
|
111
|
+
break;
|
|
112
|
+
case 'short_var_declaration':
|
|
113
|
+
case 'var_spec':
|
|
114
|
+
case 'assignment_statement':
|
|
115
|
+
this.trackGroups(node, ctx);
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
this.walkChildren(node, ctx);
|
|
119
|
+
}
|
|
120
|
+
walkChildren(node, ctx) {
|
|
121
|
+
for (const child of namedChildren(node))
|
|
122
|
+
this.walk(child, ctx);
|
|
123
|
+
}
|
|
124
|
+
// ---- symbols ----
|
|
125
|
+
visitFunction(node, ctx) {
|
|
126
|
+
const id = node.childForFieldName('name')?.text;
|
|
127
|
+
const body = node.childForFieldName('body');
|
|
128
|
+
if (!id)
|
|
129
|
+
return;
|
|
130
|
+
const receiver = node.type === 'method_declaration' ? receiverOf(node) : undefined;
|
|
131
|
+
if (node.type === 'method_declaration' && !receiver)
|
|
132
|
+
return;
|
|
133
|
+
const name = receiver ? `${receiver.type}.${id}` : id;
|
|
134
|
+
const exported = isExported(id) && (!receiver || isExported(receiver.type));
|
|
135
|
+
this.addSymbol(name, receiver ? 'method' : 'function', node, exported);
|
|
136
|
+
if (!body)
|
|
137
|
+
return;
|
|
138
|
+
const scope = new Map();
|
|
139
|
+
if (receiver?.name)
|
|
140
|
+
scope.set(receiver.name, receiver.type);
|
|
141
|
+
collectScope(node, scope);
|
|
142
|
+
const inner = {
|
|
143
|
+
owner: name,
|
|
144
|
+
receiver: receiver?.name ? receiver : undefined,
|
|
145
|
+
scopes: [scope, ...ctx.scopes],
|
|
146
|
+
prefixes: new Map(ctx.prefixes),
|
|
147
|
+
};
|
|
148
|
+
this.walk(body, inner);
|
|
149
|
+
}
|
|
150
|
+
visitTypes(decl) {
|
|
151
|
+
const specs = namedChildren(decl).filter((c) => c.type === 'type_spec' || c.type === 'type_alias');
|
|
152
|
+
const grouped = isGrouped(decl);
|
|
153
|
+
for (const spec of specs) {
|
|
154
|
+
const name = spec.childForFieldName('name')?.text;
|
|
155
|
+
const type = spec.childForFieldName('type');
|
|
156
|
+
if (!name || !type)
|
|
157
|
+
continue;
|
|
158
|
+
const kind = spec.type === 'type_alias'
|
|
159
|
+
? 'type'
|
|
160
|
+
: type.type === 'struct_type'
|
|
161
|
+
? 'class'
|
|
162
|
+
: type.type === 'interface_type'
|
|
163
|
+
? 'interface'
|
|
164
|
+
: 'type';
|
|
165
|
+
const exported = isExported(name);
|
|
166
|
+
this.addSymbol(name, kind, grouped ? spec : decl, exported);
|
|
167
|
+
if (kind === 'class')
|
|
168
|
+
this.addEmbedding(name, type);
|
|
169
|
+
if (kind === 'interface')
|
|
170
|
+
this.visitInterface(name, type, exported);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/** Interface embedding → `extends`; method elements → `method` symbols. */
|
|
174
|
+
visitInterface(name, type, exported) {
|
|
175
|
+
for (const elem of namedChildren(type)) {
|
|
176
|
+
if (elem.type === 'method_elem') {
|
|
177
|
+
const id = elem.childForFieldName('name')?.text;
|
|
178
|
+
if (id)
|
|
179
|
+
this.addSymbol(`${name}.${id}`, 'method', elem, exported && isExported(id));
|
|
180
|
+
}
|
|
181
|
+
else if (elem.type === 'type_elem') {
|
|
182
|
+
const parts = namedChildren(elem);
|
|
183
|
+
const target = parts.length === 1 && parts[0] ? writtenType(parts[0]) : undefined;
|
|
184
|
+
if (target)
|
|
185
|
+
this.out.heritage.push({ symbol: name, relation: 'extends', target });
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/** Embedded struct fields (`Base`, `*Other`, `pkg.T`) → `extends`. */
|
|
190
|
+
addEmbedding(name, struct) {
|
|
191
|
+
for (const field of fieldDeclarations(struct)) {
|
|
192
|
+
if (field.childForFieldName('name'))
|
|
193
|
+
continue;
|
|
194
|
+
const type = field.childForFieldName('type');
|
|
195
|
+
const target = type ? writtenType(type) : undefined;
|
|
196
|
+
if (target)
|
|
197
|
+
this.out.heritage.push({ symbol: name, relation: 'extends', target });
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/** Exported package-level `const`/`var` names; constants with a literal value carry it as doc. */
|
|
201
|
+
visitValues(decl) {
|
|
202
|
+
const grouped = isGrouped(decl);
|
|
203
|
+
for (const spec of namedChildren(decl)) {
|
|
204
|
+
if (spec.type !== 'const_spec' && spec.type !== 'var_spec')
|
|
205
|
+
continue;
|
|
206
|
+
const values = exprList(spec.childForFieldName('value'));
|
|
207
|
+
namedChildren(spec)
|
|
208
|
+
.filter((c) => c.type === 'identifier')
|
|
209
|
+
.forEach((id, i) => {
|
|
210
|
+
if (!isExported(id.text) || this.names.has(id.text))
|
|
211
|
+
return;
|
|
212
|
+
const value = spec.type === 'const_spec' ? values[i] : undefined;
|
|
213
|
+
const literal = value && isLiteral(value) ? value.text : undefined;
|
|
214
|
+
this.names.add(id.text);
|
|
215
|
+
this.out.symbols.push({
|
|
216
|
+
name: id.text,
|
|
217
|
+
kind: 'variable',
|
|
218
|
+
range: lineRange(grouped ? spec : decl),
|
|
219
|
+
exported: true,
|
|
220
|
+
...(literal !== undefined && { doc: `= ${literal.length > 120 ? `${literal.slice(0, 117)}...` : literal}` }),
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
addSymbol(name, kind, node, exported, http) {
|
|
226
|
+
if (this.names.has(name))
|
|
227
|
+
return;
|
|
228
|
+
this.names.add(name);
|
|
229
|
+
this.out.symbols.push({ name, kind, range: lineRange(node), exported, ...(http ? { http } : {}) });
|
|
230
|
+
}
|
|
231
|
+
/** `name`, or `name (2)`, `name (3)`... when already taken in this file. */
|
|
232
|
+
uniqueName(name) {
|
|
233
|
+
if (!this.names.has(name))
|
|
234
|
+
return name;
|
|
235
|
+
let n = 2;
|
|
236
|
+
while (this.names.has(`${name} (${n})`))
|
|
237
|
+
n++;
|
|
238
|
+
return `${name} (${n})`;
|
|
239
|
+
}
|
|
240
|
+
collectFields(decl) {
|
|
241
|
+
for (const spec of namedChildren(decl)) {
|
|
242
|
+
const name = spec.childForFieldName('name')?.text;
|
|
243
|
+
const type = spec.childForFieldName('type');
|
|
244
|
+
if (spec.type !== 'type_spec' || !name || type?.type !== 'struct_type')
|
|
245
|
+
continue;
|
|
246
|
+
const types = new Map();
|
|
247
|
+
for (const field of fieldDeclarations(type)) {
|
|
248
|
+
const fieldType = field.childForFieldName('type');
|
|
249
|
+
const simple = fieldType ? typeName(fieldType) : undefined;
|
|
250
|
+
if (!simple)
|
|
251
|
+
continue;
|
|
252
|
+
const ids = namedChildren(field).filter((c) => c.type === 'field_identifier');
|
|
253
|
+
for (const id of ids)
|
|
254
|
+
types.set(id.text, simple);
|
|
255
|
+
if (ids.length === 0)
|
|
256
|
+
types.set(simple, simple); // embedded: `s.Base.X()`
|
|
257
|
+
}
|
|
258
|
+
this.fields.set(name, types);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
// ---- imports ----
|
|
262
|
+
addImports(decl) {
|
|
263
|
+
for (const spec of importSpecs(decl)) {
|
|
264
|
+
const specifier = importPath(spec);
|
|
265
|
+
if (specifier === undefined)
|
|
266
|
+
continue;
|
|
267
|
+
const alias = spec.childForFieldName('name');
|
|
268
|
+
const local = alias ? (alias.type === 'dot' ? '.' : alias.text) : packageName(specifier);
|
|
269
|
+
const bindings = alias?.type === 'blank_identifier' || !local ? [] : [{ local, imported: '*' }];
|
|
270
|
+
this.out.imports.push({ specifier, bindings, line: line(spec) });
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
findHttpImport(decl) {
|
|
274
|
+
for (const spec of importSpecs(decl)) {
|
|
275
|
+
const alias = spec.childForFieldName('name');
|
|
276
|
+
if (importPath(spec) === 'net/http' && alias?.type === 'package_identifier')
|
|
277
|
+
this.httpName = alias.text;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
// ---- calls, routes and requests ----
|
|
281
|
+
visitCall(node, ctx) {
|
|
282
|
+
const fn = node.childForFieldName('function');
|
|
283
|
+
const args = callArgs(node);
|
|
284
|
+
const target = fn ? this.callTarget(fn, ctx) : undefined;
|
|
285
|
+
if (target && !(target.object === undefined && BUILTINS.has(target.callee)))
|
|
286
|
+
this.addCall(ctx.owner, target, node);
|
|
287
|
+
const request = fn ? this.visitRequest(fn, args, ctx, node) : false;
|
|
288
|
+
// Arguments that get their own context: route handlers and router closures.
|
|
289
|
+
const special = new Map();
|
|
290
|
+
const route = fn && !request ? this.addRoute(fn, args, ctx, node) : undefined;
|
|
291
|
+
if (route) {
|
|
292
|
+
const handlerCtx = { ...ctx, owner: route.name, prefixes: new Map(ctx.prefixes) };
|
|
293
|
+
special.set(route.handler.id, handlerCtx);
|
|
294
|
+
const ref = this.handlerReference(route.handler, ctx);
|
|
295
|
+
if (ref)
|
|
296
|
+
this.addCall(route.name, ref, route.handler);
|
|
297
|
+
}
|
|
298
|
+
else if (fn?.type === 'selector_expression') {
|
|
299
|
+
this.routerClosure(fn, args, ctx, special);
|
|
300
|
+
}
|
|
301
|
+
if (fn)
|
|
302
|
+
this.walk(fn, ctx);
|
|
303
|
+
for (const arg of args)
|
|
304
|
+
this.walk(arg, special.get(arg.id) ?? ctx);
|
|
305
|
+
}
|
|
306
|
+
/** `Gen[int](x)` parses as a conversion to a generic type: a call to `Gen`. */
|
|
307
|
+
visitConversion(node, ctx) {
|
|
308
|
+
const type = node.childForFieldName('type');
|
|
309
|
+
if (type?.type !== 'generic_type')
|
|
310
|
+
return;
|
|
311
|
+
const target = typeTarget(type);
|
|
312
|
+
if (target)
|
|
313
|
+
this.addCall(ctx.owner, target, node);
|
|
314
|
+
}
|
|
315
|
+
/** `T{}` / `&T{}` / `pkg.T{}` instantiate `T`. */
|
|
316
|
+
visitComposite(node, ctx) {
|
|
317
|
+
const type = node.childForFieldName('type');
|
|
318
|
+
const target = type ? typeTarget(type) : undefined;
|
|
319
|
+
if (target)
|
|
320
|
+
this.addCall(ctx.owner, target, node);
|
|
321
|
+
}
|
|
322
|
+
callTarget(fn, ctx) {
|
|
323
|
+
if (fn.type === 'identifier')
|
|
324
|
+
return { callee: fn.text };
|
|
325
|
+
if (fn.type === 'index_expression' || fn.type === 'generic_type') {
|
|
326
|
+
const inner = fn.childForFieldName('operand') ?? fn.childForFieldName('type');
|
|
327
|
+
return inner ? this.callTarget(inner, ctx) : undefined;
|
|
328
|
+
}
|
|
329
|
+
if (fn.type === 'qualified_type')
|
|
330
|
+
return typeTarget(fn);
|
|
331
|
+
if (fn.type !== 'selector_expression')
|
|
332
|
+
return undefined;
|
|
333
|
+
const callee = fn.childForFieldName('field')?.text;
|
|
334
|
+
const operand = fn.childForFieldName('operand');
|
|
335
|
+
if (!callee || !operand)
|
|
336
|
+
return undefined;
|
|
337
|
+
if (operand.type === 'identifier') {
|
|
338
|
+
const objectType = lookupType(ctx.scopes, operand.text);
|
|
339
|
+
return { callee, object: operand.text, ...(objectType !== undefined && { objectType }) };
|
|
340
|
+
}
|
|
341
|
+
// `s.repo.Find()` inside a method on `s`: the receiver's field is the object.
|
|
342
|
+
const inner = operand.type === 'selector_expression' ? operand.childForFieldName('operand') : null;
|
|
343
|
+
const field = operand.childForFieldName('field')?.text;
|
|
344
|
+
if (inner?.type === 'identifier' && ctx.receiver && inner.text === ctx.receiver.name && field) {
|
|
345
|
+
const objectType = this.fields.get(ctx.receiver.type)?.get(field);
|
|
346
|
+
return { callee, object: field, ...(objectType !== undefined && { objectType }) };
|
|
347
|
+
}
|
|
348
|
+
return undefined;
|
|
349
|
+
}
|
|
350
|
+
addCall(caller, target, node) {
|
|
351
|
+
if (!caller)
|
|
352
|
+
return;
|
|
353
|
+
const key = `${caller}\0${target.callee}\0${target.object ?? ''}`;
|
|
354
|
+
if (this.seenCalls.has(key))
|
|
355
|
+
return;
|
|
356
|
+
this.seenCalls.add(key);
|
|
357
|
+
this.out.calls.push({ caller, ...target, line: line(node) });
|
|
358
|
+
}
|
|
359
|
+
/** `r.GET("/x", h)`, `mux.HandleFunc("GET /x", h)`... → a route symbol owning the handler. */
|
|
360
|
+
addRoute(fn, args, ctx, call) {
|
|
361
|
+
if (fn.type !== 'selector_expression' || args.length < 2)
|
|
362
|
+
return undefined;
|
|
363
|
+
const member = fn.childForFieldName('field')?.text ?? '';
|
|
364
|
+
const receiver = fn.childForFieldName('operand');
|
|
365
|
+
const handler = args.at(-1);
|
|
366
|
+
if (!receiver || !handler || this.isClient(receiver, ctx))
|
|
367
|
+
return undefined;
|
|
368
|
+
const first = args[0] ? stringValue(args[0]) : undefined;
|
|
369
|
+
const second = args[1] ? stringValue(args[1]) : undefined;
|
|
370
|
+
let endpoint;
|
|
371
|
+
if (METHOD_FIRST.has(member) && first && HTTP_METHODS.has(first) && second !== undefined && args.length >= 3) {
|
|
372
|
+
endpoint = { method: first, path: second };
|
|
373
|
+
}
|
|
374
|
+
else if (MUX_METHODS.has(member) && first !== undefined) {
|
|
375
|
+
const match = /^([A-Z]+)\s+(\S+)$/.exec(first);
|
|
376
|
+
endpoint = match?.[1] && match[2] ? { method: match[1], path: match[2] } : { method: 'ANY', path: first };
|
|
377
|
+
}
|
|
378
|
+
else if (ROUTE_VERBS.has(member) && first !== undefined) {
|
|
379
|
+
endpoint = { method: ROUTE_VERBS.get(member) ?? 'ANY', path: first };
|
|
380
|
+
}
|
|
381
|
+
if (!endpoint || (endpoint.path !== '' && !endpoint.path.startsWith('/')))
|
|
382
|
+
return undefined;
|
|
383
|
+
const path = joinPath(this.prefixOf(receiver, ctx), endpoint.path);
|
|
384
|
+
const name = this.uniqueName(`${endpoint.method} ${path}`);
|
|
385
|
+
this.addSymbol(name, 'route', statementOf(call), false, { method: endpoint.method, path });
|
|
386
|
+
return { name, handler };
|
|
387
|
+
}
|
|
388
|
+
/** A named handler (`h`, `s.handle`, `http.HandlerFunc(h)`): the route calls it. */
|
|
389
|
+
handlerReference(handler, ctx) {
|
|
390
|
+
if (handler.type === 'identifier' || handler.type === 'selector_expression')
|
|
391
|
+
return this.callTarget(handler, ctx);
|
|
392
|
+
if (handler.type !== 'call_expression')
|
|
393
|
+
return undefined;
|
|
394
|
+
const fn = handler.childForFieldName('function');
|
|
395
|
+
const args = callArgs(handler);
|
|
396
|
+
if (fn?.type === 'selector_expression' && fn.childForFieldName('field')?.text === 'HandlerFunc' && args[0]) {
|
|
397
|
+
return this.handlerReference(args[0], ctx);
|
|
398
|
+
}
|
|
399
|
+
return undefined;
|
|
400
|
+
}
|
|
401
|
+
/** `r.Route("/api", func(r chi.Router) {...})`, `r.Group(func(r chi.Router) {...})`: the closure's router gets the prefix. */
|
|
402
|
+
routerClosure(fn, args, ctx, special) {
|
|
403
|
+
const member = fn.childForFieldName('field')?.text;
|
|
404
|
+
const receiver = fn.childForFieldName('operand');
|
|
405
|
+
const closure = args.find((a) => a.type === 'func_literal');
|
|
406
|
+
if ((member !== 'Route' && member !== 'Group') || !receiver || !closure)
|
|
407
|
+
return;
|
|
408
|
+
const first = args[0] ? stringValue(args[0]) : undefined;
|
|
409
|
+
const prefix = joinPath(this.prefixOf(receiver, ctx), first ?? '');
|
|
410
|
+
const param = firstParamName(closure);
|
|
411
|
+
if (!param)
|
|
412
|
+
return;
|
|
413
|
+
const prefixes = new Map(ctx.prefixes);
|
|
414
|
+
prefixes.set(param, prefix);
|
|
415
|
+
special.set(closure.id, { ...ctx, prefixes });
|
|
416
|
+
}
|
|
417
|
+
/** `api := r.Group("/api")` → `api` routes start with `/api`. */
|
|
418
|
+
trackGroups(node, ctx) {
|
|
419
|
+
const left = node.type === 'var_spec' ? namedChildren(node).filter((c) => c.type === 'identifier') : exprList(node.childForFieldName('left'));
|
|
420
|
+
const right = exprList(node.childForFieldName(node.type === 'var_spec' ? 'value' : 'right'));
|
|
421
|
+
left.forEach((id, i) => {
|
|
422
|
+
const value = right[i];
|
|
423
|
+
if (id.type !== 'identifier' || value?.type !== 'call_expression')
|
|
424
|
+
return;
|
|
425
|
+
const prefix = this.groupPrefix(value, ctx);
|
|
426
|
+
if (prefix !== undefined)
|
|
427
|
+
ctx.prefixes.set(id.text, prefix);
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
/** Prefix of a router expression: a tracked variable or a `x.Group("/p")` call. */
|
|
431
|
+
prefixOf(node, ctx) {
|
|
432
|
+
if (node.type === 'identifier')
|
|
433
|
+
return ctx.prefixes.get(node.text) ?? '';
|
|
434
|
+
return node.type === 'call_expression' ? (this.groupPrefix(node, ctx) ?? '') : '';
|
|
435
|
+
}
|
|
436
|
+
groupPrefix(call, ctx) {
|
|
437
|
+
const fn = call.childForFieldName('function');
|
|
438
|
+
const receiver = fn?.childForFieldName('operand');
|
|
439
|
+
const first = callArgs(call)[0];
|
|
440
|
+
const prefix = first ? stringValue(first) : undefined;
|
|
441
|
+
if (fn?.type !== 'selector_expression' || fn.childForFieldName('field')?.text !== 'Group')
|
|
442
|
+
return undefined;
|
|
443
|
+
if (!receiver || prefix === undefined)
|
|
444
|
+
return undefined;
|
|
445
|
+
return joinPath(this.prefixOf(receiver, ctx), prefix);
|
|
446
|
+
}
|
|
447
|
+
/** Reports an outgoing request; true when the call is a client call (even without a literal URL). */
|
|
448
|
+
visitRequest(fn, args, ctx, call) {
|
|
449
|
+
if (fn.type !== 'selector_expression')
|
|
450
|
+
return false;
|
|
451
|
+
const member = fn.childForFieldName('field')?.text ?? '';
|
|
452
|
+
const receiver = fn.childForFieldName('operand');
|
|
453
|
+
if (!receiver)
|
|
454
|
+
return false;
|
|
455
|
+
let method;
|
|
456
|
+
let urlArg;
|
|
457
|
+
if (receiver.type === 'identifier' && receiver.text === this.httpName && member.startsWith('NewRequest')) {
|
|
458
|
+
const offset = member === 'NewRequestWithContext' ? 1 : 0;
|
|
459
|
+
const methodArg = args[offset];
|
|
460
|
+
method = methodArg ? requestMethod(methodArg) : 'GET';
|
|
461
|
+
urlArg = args[offset + 1];
|
|
462
|
+
}
|
|
463
|
+
else if (CLIENT_METHODS.has(member) && (isPackage(receiver, this.httpName) || this.isClient(receiver, ctx))) {
|
|
464
|
+
method = CLIENT_METHODS.get(member);
|
|
465
|
+
urlArg = args[0];
|
|
466
|
+
}
|
|
467
|
+
else if (RESTY_METHODS.has(member) && isRestyRequest(receiver)) {
|
|
468
|
+
method = RESTY_METHODS.get(member);
|
|
469
|
+
urlArg = args[0];
|
|
470
|
+
}
|
|
471
|
+
if (!method)
|
|
472
|
+
return false;
|
|
473
|
+
const url = urlArg ? urlText(urlArg) : undefined;
|
|
474
|
+
const key = `${ctx.owner}\0${method}\0${url}`;
|
|
475
|
+
if (url === undefined || !ctx.owner || this.seenRequests.has(key))
|
|
476
|
+
return true;
|
|
477
|
+
this.seenRequests.add(key);
|
|
478
|
+
this.out.requests.push({ caller: ctx.owner, method, url, line: line(call) });
|
|
479
|
+
return true;
|
|
480
|
+
}
|
|
481
|
+
/** `*http.Client` values, `http.DefaultClient`, and receivers named `client`/`httpClient`. */
|
|
482
|
+
isClient(receiver, ctx) {
|
|
483
|
+
if (receiver.type === 'selector_expression') {
|
|
484
|
+
return isPackage(receiver.childForFieldName('operand'), this.httpName) && receiver.childForFieldName('field')?.text === 'DefaultClient';
|
|
485
|
+
}
|
|
486
|
+
if (receiver.type !== 'identifier')
|
|
487
|
+
return false;
|
|
488
|
+
return CLIENT_NAMES.has(receiver.text) || lookupType(ctx.scopes, receiver.text) === 'Client';
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
// ---- helpers ----
|
|
492
|
+
function line(node) {
|
|
493
|
+
return node.startPosition.row + 1;
|
|
494
|
+
}
|
|
495
|
+
function isExported(name) {
|
|
496
|
+
return /^\p{Lu}/u.test(name);
|
|
497
|
+
}
|
|
498
|
+
/** `const ( ... )` / `type ( ... )`: ranges then cover one spec, not the whole block. */
|
|
499
|
+
function isGrouped(decl) {
|
|
500
|
+
return decl.children.some((c) => c?.type === '(');
|
|
501
|
+
}
|
|
502
|
+
function isLiteral(node) {
|
|
503
|
+
if (LITERALS.has(node.type))
|
|
504
|
+
return true;
|
|
505
|
+
const operand = node.type === 'unary_expression' ? node.childForFieldName('operand') : null;
|
|
506
|
+
return operand !== null && /^(int|float|imaginary)_literal$/.test(operand.type);
|
|
507
|
+
}
|
|
508
|
+
function receiverOf(method) {
|
|
509
|
+
const param = namedChildren(method.childForFieldName('receiver') ?? method).find((c) => c.type === 'parameter_declaration');
|
|
510
|
+
const type = param?.childForFieldName('type');
|
|
511
|
+
const name = type ? typeName(type) : undefined;
|
|
512
|
+
if (!name)
|
|
513
|
+
return undefined;
|
|
514
|
+
return { name: param?.childForFieldName('name')?.text ?? '', type: name };
|
|
515
|
+
}
|
|
516
|
+
/** Simple type name: pointers, packages and type arguments dropped (`*pkg.T[int]` → `T`). */
|
|
517
|
+
function typeName(node) {
|
|
518
|
+
switch (node.type) {
|
|
519
|
+
case 'type_identifier':
|
|
520
|
+
case 'identifier':
|
|
521
|
+
return node.text;
|
|
522
|
+
case 'pointer_type':
|
|
523
|
+
case 'parenthesized_type': {
|
|
524
|
+
const inner = namedChildren(node)[0];
|
|
525
|
+
return inner ? typeName(inner) : undefined;
|
|
526
|
+
}
|
|
527
|
+
case 'qualified_type':
|
|
528
|
+
return node.childForFieldName('name')?.text;
|
|
529
|
+
case 'generic_type': {
|
|
530
|
+
const inner = node.childForFieldName('type');
|
|
531
|
+
return inner ? typeName(inner) : undefined;
|
|
532
|
+
}
|
|
533
|
+
default:
|
|
534
|
+
return undefined;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
/** Type as written for heritage, type arguments and pointers dropped: `Base`, `pkg.T`. */
|
|
538
|
+
function writtenType(node) {
|
|
539
|
+
if (node.type === 'qualified_type')
|
|
540
|
+
return node.text.replace(/\s+/g, '');
|
|
541
|
+
if (node.type === 'pointer_type' || node.type === 'generic_type') {
|
|
542
|
+
const inner = node.childForFieldName('type') ?? namedChildren(node)[0];
|
|
543
|
+
return inner ? writtenType(inner) : undefined;
|
|
544
|
+
}
|
|
545
|
+
return node.type === 'type_identifier' ? node.text : undefined;
|
|
546
|
+
}
|
|
547
|
+
/** Callee of an instantiation or generic call: `T`, or `T` on object `pkg` for `pkg.T`. */
|
|
548
|
+
function typeTarget(type) {
|
|
549
|
+
if (type.type === 'type_identifier')
|
|
550
|
+
return { callee: type.text };
|
|
551
|
+
if (type.type === 'generic_type') {
|
|
552
|
+
const inner = type.childForFieldName('type');
|
|
553
|
+
return inner ? typeTarget(inner) : undefined;
|
|
554
|
+
}
|
|
555
|
+
if (type.type === 'qualified_type') {
|
|
556
|
+
const callee = type.childForFieldName('name')?.text;
|
|
557
|
+
const object = type.childForFieldName('package')?.text;
|
|
558
|
+
return callee && object ? { callee, object } : undefined;
|
|
559
|
+
}
|
|
560
|
+
return undefined;
|
|
561
|
+
}
|
|
562
|
+
function fieldDeclarations(struct) {
|
|
563
|
+
const list = namedChildren(struct).find((c) => c.type === 'field_declaration_list');
|
|
564
|
+
return list ? namedChildren(list).filter((c) => c.type === 'field_declaration') : [];
|
|
565
|
+
}
|
|
566
|
+
function exprList(node) {
|
|
567
|
+
if (!node)
|
|
568
|
+
return [];
|
|
569
|
+
return node.type === 'expression_list' ? namedChildren(node) : [node];
|
|
570
|
+
}
|
|
571
|
+
/** Type of a value expression: `T{}`, `&T{}`, `NewT(...)`, `pkg.NewT(...)`. */
|
|
572
|
+
function valueType(node) {
|
|
573
|
+
if (node.type === 'composite_literal') {
|
|
574
|
+
const type = node.childForFieldName('type');
|
|
575
|
+
return type ? typeName(type) : undefined;
|
|
576
|
+
}
|
|
577
|
+
if (node.type === 'unary_expression') {
|
|
578
|
+
const operand = node.childForFieldName('operand');
|
|
579
|
+
return operand ? valueType(operand) : undefined;
|
|
580
|
+
}
|
|
581
|
+
if (node.type !== 'call_expression')
|
|
582
|
+
return undefined;
|
|
583
|
+
const fn = node.childForFieldName('function');
|
|
584
|
+
const name = fn?.type === 'selector_expression' ? fn.childForFieldName('field')?.text : fn?.text;
|
|
585
|
+
const match = name ? /^New(\p{Lu}\w*)$/u.exec(name) : null;
|
|
586
|
+
return match?.[1];
|
|
587
|
+
}
|
|
588
|
+
/** Parameters, `var` and `:=` declarations anywhere inside `node` (func literals included). */
|
|
589
|
+
function collectScope(node, types) {
|
|
590
|
+
const set = (name, type) => {
|
|
591
|
+
if (type && name !== '_' && !types.has(name))
|
|
592
|
+
types.set(name, type);
|
|
593
|
+
};
|
|
594
|
+
const visit = (n) => {
|
|
595
|
+
if (n.type === 'ERROR')
|
|
596
|
+
return;
|
|
597
|
+
if (n.type === 'parameter_declaration' ||
|
|
598
|
+
n.type === 'variadic_parameter_declaration' ||
|
|
599
|
+
n.type === 'var_spec') {
|
|
600
|
+
const typeNode = n.childForFieldName('type');
|
|
601
|
+
const declared = typeNode && n.type !== 'variadic_parameter_declaration' ? typeName(typeNode) : undefined;
|
|
602
|
+
const values = exprList(n.childForFieldName('value'));
|
|
603
|
+
namedChildren(n)
|
|
604
|
+
.filter((c) => c.type === 'identifier')
|
|
605
|
+
.forEach((id, i) => {
|
|
606
|
+
const value = values[i];
|
|
607
|
+
set(id.text, declared ?? (value ? valueType(value) : undefined));
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
else if (n.type === 'short_var_declaration') {
|
|
611
|
+
const left = exprList(n.childForFieldName('left'));
|
|
612
|
+
const right = exprList(n.childForFieldName('right'));
|
|
613
|
+
left.forEach((id, i) => {
|
|
614
|
+
// `x, err := NewX()`: one call, several results; the first is the value.
|
|
615
|
+
const value = right.length === 1 ? (i === 0 ? right[0] : undefined) : right[i];
|
|
616
|
+
if (id.type === 'identifier' && value)
|
|
617
|
+
set(id.text, valueType(value));
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
for (const child of namedChildren(n))
|
|
621
|
+
visit(child);
|
|
622
|
+
};
|
|
623
|
+
visit(node);
|
|
624
|
+
}
|
|
625
|
+
function lookupType(scopes, name) {
|
|
626
|
+
for (const scope of scopes) {
|
|
627
|
+
const type = scope.get(name);
|
|
628
|
+
if (type)
|
|
629
|
+
return type;
|
|
630
|
+
}
|
|
631
|
+
return undefined;
|
|
632
|
+
}
|
|
633
|
+
function importSpecs(decl) {
|
|
634
|
+
const list = namedChildren(decl).find((c) => c.type === 'import_spec_list');
|
|
635
|
+
return namedChildren(list ?? decl).filter((c) => c.type === 'import_spec');
|
|
636
|
+
}
|
|
637
|
+
function importPath(spec) {
|
|
638
|
+
const path = spec.childForFieldName('path');
|
|
639
|
+
return path ? stringValue(path) : undefined;
|
|
640
|
+
}
|
|
641
|
+
/** Package name implied by an import path: last segment, skipping `/v2` and `.v3` version suffixes. */
|
|
642
|
+
function packageName(path) {
|
|
643
|
+
const segments = path.split('/').filter(Boolean);
|
|
644
|
+
let last = segments.at(-1) ?? path;
|
|
645
|
+
if (/^v\d+$/.test(last) && segments.length > 1)
|
|
646
|
+
last = segments.at(-2) ?? last;
|
|
647
|
+
return last.replace(/\.v\d+$/, '');
|
|
648
|
+
}
|
|
649
|
+
function callArgs(call) {
|
|
650
|
+
const list = call.childForFieldName('arguments');
|
|
651
|
+
return list ? namedChildren(list) : [];
|
|
652
|
+
}
|
|
653
|
+
function firstParamName(fn) {
|
|
654
|
+
const params = fn.childForFieldName('parameters');
|
|
655
|
+
const first = params ? namedChildren(params).find((c) => c.type === 'parameter_declaration') : undefined;
|
|
656
|
+
return first?.childForFieldName('name')?.text;
|
|
657
|
+
}
|
|
658
|
+
function isPackage(node, name) {
|
|
659
|
+
return node?.type === 'identifier' && node.text === name;
|
|
660
|
+
}
|
|
661
|
+
/** Receiver chain containing `.R()` (resty: `client.R().SetBody(b).Post(url)`). */
|
|
662
|
+
function isRestyRequest(node) {
|
|
663
|
+
let current = node;
|
|
664
|
+
while (current?.type === 'call_expression') {
|
|
665
|
+
const fn = current.childForFieldName('function');
|
|
666
|
+
if (fn?.type !== 'selector_expression')
|
|
667
|
+
return false;
|
|
668
|
+
if (fn.childForFieldName('field')?.text === 'R')
|
|
669
|
+
return true;
|
|
670
|
+
current = fn.childForFieldName('operand');
|
|
671
|
+
}
|
|
672
|
+
return false;
|
|
673
|
+
}
|
|
674
|
+
/** Content of a string literal; undefined for anything else. */
|
|
675
|
+
function stringValue(node) {
|
|
676
|
+
if (node.type !== 'interpreted_string_literal' && node.type !== 'raw_string_literal')
|
|
677
|
+
return undefined;
|
|
678
|
+
return node.text.slice(1, -1);
|
|
679
|
+
}
|
|
680
|
+
/** `"GET"` or `http.MethodPost` → uppercase method; `GET` otherwise. */
|
|
681
|
+
function requestMethod(node) {
|
|
682
|
+
const literal = stringValue(node);
|
|
683
|
+
if (literal)
|
|
684
|
+
return literal.toUpperCase();
|
|
685
|
+
const field = node.type === 'selector_expression' ? node.childForFieldName('field')?.text : undefined;
|
|
686
|
+
return field?.startsWith('Method') && field.length > 6 ? field.slice(6).toUpperCase() : 'GET';
|
|
687
|
+
}
|
|
688
|
+
/** URL with dynamic parts as `{}`: literals, `"/a/" + id`, `fmt.Sprintf("/u/%d", id)`. */
|
|
689
|
+
function urlText(node) {
|
|
690
|
+
const parts = [];
|
|
691
|
+
const state = { literal: false };
|
|
692
|
+
const visit = (n) => {
|
|
693
|
+
const value = stringValue(n);
|
|
694
|
+
if (value !== undefined) {
|
|
695
|
+
state.literal = true;
|
|
696
|
+
parts.push(value);
|
|
697
|
+
}
|
|
698
|
+
else if (n.type === 'binary_expression' && n.childForFieldName('operator')?.text === '+') {
|
|
699
|
+
const left = n.childForFieldName('left');
|
|
700
|
+
const right = n.childForFieldName('right');
|
|
701
|
+
if (left)
|
|
702
|
+
visit(left);
|
|
703
|
+
if (right)
|
|
704
|
+
visit(right);
|
|
705
|
+
}
|
|
706
|
+
else if (n.type === 'parenthesized_expression' && namedChildren(n)[0]) {
|
|
707
|
+
visit(namedChildren(n)[0]);
|
|
708
|
+
}
|
|
709
|
+
else {
|
|
710
|
+
const format = sprintfFormat(n);
|
|
711
|
+
if (format !== undefined)
|
|
712
|
+
state.literal = true;
|
|
713
|
+
if (parts.at(-1) !== '{}' || format !== undefined)
|
|
714
|
+
parts.push(format ?? '{}');
|
|
715
|
+
}
|
|
716
|
+
};
|
|
717
|
+
visit(node);
|
|
718
|
+
return state.literal ? parts.join('') : undefined;
|
|
719
|
+
}
|
|
720
|
+
/** `fmt.Sprintf("/users/%d", id)` → `/users/{}`. */
|
|
721
|
+
function sprintfFormat(node) {
|
|
722
|
+
if (node.type !== 'call_expression')
|
|
723
|
+
return undefined;
|
|
724
|
+
const fn = node.childForFieldName('function');
|
|
725
|
+
if (fn?.type !== 'selector_expression' || fn.childForFieldName('field')?.text !== 'Sprintf')
|
|
726
|
+
return undefined;
|
|
727
|
+
const first = callArgs(node)[0];
|
|
728
|
+
const format = first ? stringValue(first) : undefined;
|
|
729
|
+
return format?.replace(/%(%|[-+# 0-9.*[\]]*[a-zA-Z])/g, (_match, verb) => (verb === '%' ? '%' : '{}'));
|
|
730
|
+
}
|
|
731
|
+
/** The statement holding a call chain, or the call itself when it is not a statement. */
|
|
732
|
+
function statementOf(call) {
|
|
733
|
+
let top = call;
|
|
734
|
+
while (top.parent && CHAIN_PARENTS.has(top.parent.type))
|
|
735
|
+
top = top.parent;
|
|
736
|
+
return top.parent?.type === 'expression_statement' ? top.parent : call;
|
|
737
|
+
}
|
|
738
|
+
/** Joins route segments: leading `/`, collapsed slashes, no trailing slash (root stays `/`). */
|
|
739
|
+
function joinPath(prefix, template) {
|
|
740
|
+
const path = `/${prefix}/${template}`.replace(/\/+/g, '/');
|
|
741
|
+
return path.length > 1 ? path.replace(/\/$/, '') : path;
|
|
742
|
+
}
|
|
743
|
+
//# sourceMappingURL=go.js.map
|