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.
Files changed (41) hide show
  1. package/dist/cli/index.js +15 -3
  2. package/dist/cli/index.js.map +1 -1
  3. package/dist/enrich/tasks.js +19 -0
  4. package/dist/enrich/tasks.js.map +1 -1
  5. package/dist/install/claude.d.ts +8 -0
  6. package/dist/install/claude.js +33 -0
  7. package/dist/install/claude.js.map +1 -1
  8. package/dist/scan/assemble.js +39 -108
  9. package/dist/scan/assemble.js.map +1 -1
  10. package/dist/scan/discover.js +13 -0
  11. package/dist/scan/discover.js.map +1 -1
  12. package/dist/scan/docs.js +20 -1
  13. package/dist/scan/docs.js.map +1 -1
  14. package/dist/scan/extract.js +12 -0
  15. package/dist/scan/extract.js.map +1 -1
  16. package/dist/scan/extractors/go.d.ts +4 -0
  17. package/dist/scan/extractors/go.js +743 -0
  18. package/dist/scan/extractors/go.js.map +1 -0
  19. package/dist/scan/extractors/java.d.ts +4 -0
  20. package/dist/scan/extractors/java.js +571 -0
  21. package/dist/scan/extractors/java.js.map +1 -0
  22. package/dist/scan/extractors/php.d.ts +4 -0
  23. package/dist/scan/extractors/php.js +775 -0
  24. package/dist/scan/extractors/php.js.map +1 -0
  25. package/dist/scan/extractors/rust.d.ts +4 -0
  26. package/dist/scan/extractors/rust.js +681 -0
  27. package/dist/scan/extractors/rust.js.map +1 -0
  28. package/dist/scan/extractors/typescript.js +13 -1
  29. package/dist/scan/extractors/typescript.js.map +1 -1
  30. package/dist/scan/parser.js +4 -0
  31. package/dist/scan/parser.js.map +1 -1
  32. package/dist/scan/resolve-named.d.ts +39 -0
  33. package/dist/scan/resolve-named.js +198 -0
  34. package/dist/scan/resolve-named.js.map +1 -0
  35. package/dist/scan/resolve.js +4 -1
  36. package/dist/scan/resolve.js.map +1 -1
  37. package/dist/scan/types.d.ts +1 -1
  38. package/dist/scan/types.js +1 -1
  39. package/dist/scan/types.js.map +1 -1
  40. package/package.json +1 -1
  41. package/skills/neuroplex/SKILL.md +3 -1
@@ -0,0 +1,775 @@
1
+ import { lineRange, namedChildren } from '../parser.js';
2
+ import { emptyExtraction } from '../types.js';
3
+ const TYPE_KINDS = new Map([
4
+ ['class_declaration', 'class'],
5
+ ['trait_declaration', 'class'],
6
+ ['interface_declaration', 'interface'],
7
+ ['enum_declaration', 'type'],
8
+ ]);
9
+ const CALL_TYPES = new Set([
10
+ 'function_call_expression',
11
+ 'member_call_expression',
12
+ 'nullsafe_member_call_expression',
13
+ 'scoped_call_expression',
14
+ ]);
15
+ const MEMBER_CALLS = new Set(['member_call_expression', 'nullsafe_member_call_expression']);
16
+ const MEMBER_ACCESS = new Set(['member_access_expression', 'nullsafe_member_access_expression']);
17
+ const CLOSURES = new Set(['anonymous_function', 'arrow_function']);
18
+ const REQUIRES = new Set(['require_expression', 'require_once_expression', 'include_expression', 'include_once_expression']);
19
+ /** Parents that continue a call chain (`Route::get(...)->name('x')`). */
20
+ const CHAIN_PARENTS = new Set([...CALL_TYPES, ...MEMBER_ACCESS, 'parenthesized_expression']);
21
+ /** Laravel `Route::verb(...)`. */
22
+ const ROUTE_VERBS = new Map([
23
+ ['get', 'GET'],
24
+ ['post', 'POST'],
25
+ ['put', 'PUT'],
26
+ ['patch', 'PATCH'],
27
+ ['delete', 'DELETE'],
28
+ ['options', 'OPTIONS'],
29
+ ['any', 'ANY'],
30
+ ]);
31
+ /** `Route::resource('photos', PhotoController::class)`: action, method, path suffix (`*` is the parameter). */
32
+ const RESOURCE_ACTIONS = [
33
+ ['index', 'GET', ''],
34
+ ['create', 'GET', '/create'],
35
+ ['store', 'POST', ''],
36
+ ['show', 'GET', '/*'],
37
+ ['edit', 'GET', '/*/edit'],
38
+ ['update', 'PUT', '/*'],
39
+ ['destroy', 'DELETE', '/*'],
40
+ ];
41
+ const API_RESOURCE_SKIP = new Set(['create', 'edit']);
42
+ /** Client methods of the Laravel `Http` facade and Guzzle. */
43
+ const CLIENT_VERBS = new Map([
44
+ ['get', 'GET'],
45
+ ['post', 'POST'],
46
+ ['put', 'PUT'],
47
+ ['patch', 'PATCH'],
48
+ ['delete', 'DELETE'],
49
+ ['head', 'HEAD'],
50
+ ['options', 'OPTIONS'],
51
+ ]);
52
+ const CLIENT_RECEIVER = /^(client|http|\w*Client)$/i;
53
+ const CLIENT_TYPES = new Set(['Client', 'ClientInterface', 'GuzzleClient']);
54
+ const MAGIC = /^__[a-zA-Z]/;
55
+ /** Extracts symbols, imports, calls, heritage, HTTP routes and requests from a PHP tree. */
56
+ export function extractPhp(root) {
57
+ return new PhpExtractor().run(root);
58
+ }
59
+ class PhpExtractor {
60
+ out = emptyExtraction();
61
+ names = new Set();
62
+ seenCalls = new Set();
63
+ run(root) {
64
+ this.walk(root, {
65
+ owner: undefined,
66
+ type: undefined,
67
+ parent: undefined,
68
+ routePrefix: '',
69
+ groupPrefix: '',
70
+ scopes: [],
71
+ });
72
+ return this.out;
73
+ }
74
+ walk(node, ctx) {
75
+ // ERROR nodes are walked: tree-sitter often wraps whole valid declarations in one (e.g. the root).
76
+ switch (node.type) {
77
+ case 'comment':
78
+ case 'text':
79
+ case 'attribute_list':
80
+ return;
81
+ case 'namespace_use_declaration':
82
+ this.addUse(node);
83
+ return;
84
+ case 'method_declaration':
85
+ this.visitMethod(node, ctx);
86
+ return;
87
+ case 'function_definition':
88
+ this.visitFunction(node, ctx);
89
+ return;
90
+ case 'const_declaration':
91
+ this.visitConstants(node, ctx);
92
+ return;
93
+ case 'anonymous_class':
94
+ // Its methods belong to no named type: keep their calls with the enclosing symbol.
95
+ this.walkChildren(node, { ...ctx, type: undefined });
96
+ return;
97
+ case 'object_creation_expression':
98
+ this.visitCreation(node, ctx);
99
+ break;
100
+ default:
101
+ if (TYPE_KINDS.has(node.type)) {
102
+ this.visitType(node, ctx);
103
+ return;
104
+ }
105
+ if (REQUIRES.has(node.type))
106
+ this.addRequire(node);
107
+ if (CALL_TYPES.has(node.type)) {
108
+ this.visitCall(node, ctx);
109
+ return;
110
+ }
111
+ if (CLOSURES.has(node.type))
112
+ ctx = withScope(ctx, memberScope(node));
113
+ }
114
+ this.walkChildren(node, ctx);
115
+ }
116
+ walkChildren(node, ctx) {
117
+ for (const child of namedChildren(node))
118
+ this.walk(child, ctx);
119
+ }
120
+ // ---- symbols ----
121
+ visitType(node, ctx) {
122
+ const name = node.childForFieldName('name')?.text;
123
+ const kind = TYPE_KINDS.get(node.type);
124
+ if (!name || !kind) {
125
+ this.walkChildren(node, ctx);
126
+ return;
127
+ }
128
+ this.addSymbol(name, kind, node, true);
129
+ const parent = this.addHeritage(name, node);
130
+ const prefix = routeAttribute(node)?.path ?? '';
131
+ this.walkChildren(node, {
132
+ owner: name,
133
+ type: name,
134
+ parent,
135
+ routePrefix: prefix,
136
+ groupPrefix: ctx.groupPrefix,
137
+ scopes: [{ member: false, types: typeScope(node) }],
138
+ });
139
+ }
140
+ visitMethod(node, ctx) {
141
+ const id = node.childForFieldName('name')?.text;
142
+ const scoped = withScope(ctx, memberScope(node));
143
+ if (!id || !ctx.type || MAGIC.test(id)) {
144
+ // Constructors and other magic methods: their calls go to the type.
145
+ this.walkChildren(node, scoped);
146
+ return;
147
+ }
148
+ const name = `${ctx.type}.${id}`;
149
+ const route = routeAttribute(node);
150
+ const http = route
151
+ ? { method: route.method, path: joinPath(ctx.routePrefix, route.path) }
152
+ : undefined;
153
+ this.addSymbol(name, http ? 'route' : 'method', node, isPublic(node), http);
154
+ this.walkChildren(node, { ...scoped, owner: name });
155
+ }
156
+ visitFunction(node, ctx) {
157
+ const name = node.childForFieldName('name')?.text;
158
+ const scoped = withScope(ctx, memberScope(node));
159
+ if (!name || ctx.owner) {
160
+ this.walkChildren(node, scoped); // nested functions are not reported
161
+ return;
162
+ }
163
+ this.addSymbol(name, 'function', node, true);
164
+ this.walkChildren(node, { ...scoped, owner: name });
165
+ }
166
+ /** Public class constants and top-level `const` become `variable` symbols whose doc carries the value. */
167
+ visitConstants(node, ctx) {
168
+ if (ctx.owner !== ctx.type || !isPublic(node))
169
+ return;
170
+ for (const element of namedChildren(node)) {
171
+ if (element.type !== 'const_element')
172
+ continue;
173
+ const [id, value] = namedChildren(element);
174
+ if (id?.type !== 'name')
175
+ continue;
176
+ this.addVariable(ctx.type ? `${ctx.type}.${id.text}` : id.text, node, value);
177
+ }
178
+ }
179
+ /** `define('NAME', 'value')` at top level. */
180
+ visitDefine(node, ctx) {
181
+ if (ctx.owner || ctx.type)
182
+ return;
183
+ const [nameArg, value] = callArgs(node);
184
+ const name = nameArg ? stringValue(nameArg) : undefined;
185
+ if (name && /^\w+$/.test(name))
186
+ this.addVariable(name, statementOf(node), value);
187
+ }
188
+ addVariable(name, node, value) {
189
+ if (this.names.has(name))
190
+ return;
191
+ this.names.add(name);
192
+ const literal = value && isLiteral(value) ? value.text : undefined;
193
+ this.out.symbols.push({
194
+ name,
195
+ kind: 'variable',
196
+ range: lineRange(node),
197
+ exported: true,
198
+ ...(literal !== undefined && { doc: `= ${literal.length > 120 ? `${literal.slice(0, 117)}...` : literal}` }),
199
+ });
200
+ }
201
+ addSymbol(name, kind, node, exported, http) {
202
+ if (this.names.has(name))
203
+ return;
204
+ this.names.add(name);
205
+ this.out.symbols.push({ name, kind, range: lineRange(node), exported, ...(http ? { http } : {}) });
206
+ }
207
+ /** `name`, or `name (2)`, `name (3)`... when already taken in this file. */
208
+ uniqueName(name) {
209
+ if (!this.names.has(name))
210
+ return name;
211
+ let n = 2;
212
+ while (this.names.has(`${name} (${n})`))
213
+ n++;
214
+ return `${name} (${n})`;
215
+ }
216
+ // ---- imports ----
217
+ /** `use A\B;`, `use A\B as C;`, `use A\{B, C as D};`, `use function A\f;`, `use const A\X;`. */
218
+ addUse(node) {
219
+ const group = node.childForFieldName('body');
220
+ const prefix = group ? namedChildren(node).find((c) => c.type === 'namespace_name')?.text : undefined;
221
+ const clauses = namedChildren(group ?? node).filter((c) => c.type === 'namespace_use_clause');
222
+ for (const clause of clauses) {
223
+ const alias = clause.childForFieldName('alias');
224
+ const target = namedChildren(clause).find((c) => c.type === 'qualified_name' || c.type === 'name');
225
+ if (!target)
226
+ continue;
227
+ const specifier = trimSlash(prefix ? `${prefix}\\${target.text}` : target.text);
228
+ const imported = lastSegment(specifier);
229
+ this.out.imports.push({
230
+ specifier,
231
+ bindings: [{ local: alias?.text ?? imported, imported }],
232
+ line: line(node),
233
+ });
234
+ }
235
+ }
236
+ /** `require 'x.php'`, `include_once __DIR__ . '/x.php'` (→ `./x.php`). */
237
+ addRequire(node) {
238
+ const arg = namedChildren(node)[0];
239
+ const specifier = arg ? requirePath(arg) : undefined;
240
+ if (specifier)
241
+ this.out.imports.push({ specifier, bindings: [], line: line(node) });
242
+ }
243
+ // ---- calls, routes and requests ----
244
+ visitCall(node, ctx) {
245
+ const target = callTarget(node, ctx);
246
+ if (target)
247
+ this.addCall(ctx, target, node);
248
+ if (node.type === 'function_call_expression' && target?.callee === 'define')
249
+ this.visitDefine(node, ctx);
250
+ this.addRequestOf(node, ctx, target);
251
+ const route = this.addLaravelRoute(node, ctx);
252
+ const group = route ? undefined : groupOf(node);
253
+ for (const child of namedChildren(node)) {
254
+ if (child.type !== 'arguments') {
255
+ this.walk(child, ctx);
256
+ continue;
257
+ }
258
+ for (const arg of namedChildren(child)) {
259
+ const value = argValue(arg);
260
+ if (route && value?.id === route.handler.id) {
261
+ this.walk(value, { ...ctx, owner: route.name });
262
+ }
263
+ else if (group && value?.id === group.closure.id) {
264
+ this.walk(value, { ...ctx, groupPrefix: joinPath(ctx.groupPrefix, group.prefix) });
265
+ }
266
+ else {
267
+ this.walk(arg, ctx);
268
+ }
269
+ }
270
+ }
271
+ }
272
+ visitCreation(node, ctx) {
273
+ const type = namedChildren(node)[0];
274
+ const callee = type ? typeName(type) : undefined;
275
+ if (callee && callee !== 'self' && callee !== 'static')
276
+ this.addCall(ctx, { callee }, node);
277
+ }
278
+ addCall(ctx, target, node, owner = ctx.owner) {
279
+ if (!owner || MAGIC.test(target.callee))
280
+ return;
281
+ const key = `${owner}\0${target.callee}\0${target.object ?? ''}`;
282
+ if (this.seenCalls.has(key))
283
+ return;
284
+ this.seenCalls.add(key);
285
+ this.out.calls.push({
286
+ caller: owner,
287
+ callee: target.callee,
288
+ ...(target.object !== undefined ? { object: target.object } : {}),
289
+ ...(target.objectType !== undefined ? { objectType: target.objectType } : {}),
290
+ line: line(node),
291
+ });
292
+ }
293
+ /** Laravel `Http::get(url)` / `Http::withToken(t)->post(url)`, Guzzle `$client->get(url)`, `file_get_contents('http…')`. */
294
+ addRequestOf(node, ctx, target) {
295
+ const callee = (target?.callee ?? node.childForFieldName('name')?.text ?? '').toLowerCase();
296
+ if (!ctx.owner || !callee)
297
+ return;
298
+ const args = callArgs(node);
299
+ let method;
300
+ let urlArg = args[0];
301
+ if (node.type === 'function_call_expression') {
302
+ if (callee !== 'file_get_contents')
303
+ return;
304
+ method = 'GET';
305
+ }
306
+ else if (rootScope(node) === 'Http') {
307
+ method = CLIENT_VERBS.get(callee);
308
+ if (callee === 'send')
309
+ [method, urlArg] = [args[0] && stringValue(args[0])?.toUpperCase(), args[1]];
310
+ }
311
+ else if (target && MEMBER_CALLS.has(node.type) && isClient(target)) {
312
+ const verb = callee.replace(/async$/, '');
313
+ method = CLIENT_VERBS.get(verb);
314
+ if (verb === 'request')
315
+ [method, urlArg] = [args[0] && stringValue(args[0])?.toUpperCase(), args[1]];
316
+ }
317
+ const url = urlArg ? urlValue(urlArg) : undefined;
318
+ if (!method || url === undefined)
319
+ return;
320
+ if (node.type === 'function_call_expression' && !/^https?:\/\//.test(url))
321
+ return;
322
+ this.out.requests.push({ caller: ctx.owner, method, url, line: line(node) });
323
+ }
324
+ /** `Route::get('/x', handler)` and friends → `GET /x` route symbols; returns the handler owning its calls. */
325
+ addLaravelRoute(node, ctx) {
326
+ if (node.type !== 'scoped_call_expression' || scopeName(node) !== 'Route')
327
+ return undefined;
328
+ const verb = node.childForFieldName('name')?.text.toLowerCase() ?? '';
329
+ const args = callArgs(node);
330
+ if (verb === 'resource' || verb === 'apiresource') {
331
+ this.addResource(node, ctx, args, verb === 'apiresource');
332
+ return undefined;
333
+ }
334
+ let method = ROUTE_VERBS.get(verb);
335
+ let rest = args;
336
+ if (verb === 'match') {
337
+ const first = args[0] && namedChildren(args[0]).map((e) => namedChildren(e)[0]).find((v) => v !== undefined);
338
+ method = (first && stringValue(first)?.toUpperCase()) || 'ANY';
339
+ rest = args.slice(1);
340
+ }
341
+ const [templateArg, handler] = rest;
342
+ const template = templateArg ? stringValue(templateArg) : undefined;
343
+ if (!method || template === undefined)
344
+ return undefined;
345
+ const path = joinPath(ctx.groupPrefix, template);
346
+ const name = this.uniqueName(`${method} ${path}`);
347
+ this.addSymbol(name, 'route', statementOf(node), false, { method, path });
348
+ if (!handler)
349
+ return undefined;
350
+ const target = handlerTarget(handler);
351
+ if (target)
352
+ this.addCall(ctx, target, handler, name);
353
+ return CLOSURES.has(handler.type) ? { name, handler } : undefined;
354
+ }
355
+ addResource(node, ctx, args, api) {
356
+ const resource = args[0] ? stringValue(args[0]) : undefined;
357
+ const controller = args[1] ? handlerTarget(args[1])?.callee : undefined;
358
+ if (!resource || !controller)
359
+ return;
360
+ const parts = resource.split('.').filter(Boolean);
361
+ const last = parts.pop() ?? '';
362
+ const base = joinPath(ctx.groupPrefix, [...parts.map((p) => `${p}/{${singular(p)}}`), last].join('/'));
363
+ for (const [action, method, suffix] of RESOURCE_ACTIONS) {
364
+ if (api && API_RESOURCE_SKIP.has(action))
365
+ continue;
366
+ const path = joinPath(base, suffix.replace('*', `{${singular(last)}}`));
367
+ const name = this.uniqueName(`${method} ${path}`);
368
+ this.addSymbol(name, 'route', statementOf(node), false, { method, path });
369
+ this.addCall(ctx, { callee: action, object: controller }, node, name);
370
+ }
371
+ }
372
+ // ---- heritage ----
373
+ /** Records `extends`, `implements` and trait `use`; returns the base class. */
374
+ addHeritage(symbol, node) {
375
+ let base;
376
+ const add = (relation, list) => {
377
+ for (const child of namedChildren(list)) {
378
+ const target = typeName(child);
379
+ if (!target)
380
+ continue;
381
+ this.out.heritage.push({ symbol, relation, target });
382
+ if (relation === 'extends' && list.type === 'base_clause')
383
+ base ??= target;
384
+ }
385
+ };
386
+ for (const child of namedChildren(node)) {
387
+ if (child.type === 'base_clause')
388
+ add('extends', child);
389
+ else if (child.type === 'class_interface_clause')
390
+ add('implements', child);
391
+ }
392
+ const body = node.childForFieldName('body');
393
+ for (const member of body ? namedChildren(body) : []) {
394
+ if (member.type === 'use_declaration')
395
+ add('extends', member);
396
+ }
397
+ return node.type === 'interface_declaration' ? undefined : base;
398
+ }
399
+ }
400
+ // ---- helpers ----
401
+ function line(node) {
402
+ return node.startPosition.row + 1;
403
+ }
404
+ function withScope(ctx, types) {
405
+ return { ...ctx, scopes: [{ member: true, types }, ...ctx.scopes] };
406
+ }
407
+ function trimSlash(name) {
408
+ return name.replace(/^\\/, '');
409
+ }
410
+ function lastSegment(name) {
411
+ return name.slice(name.lastIndexOf('\\') + 1);
412
+ }
413
+ /** Methods and constants are public unless marked `private` or `protected`. */
414
+ function isPublic(node) {
415
+ const visibility = namedChildren(node).find((c) => c.type === 'visibility_modifier')?.text;
416
+ return visibility !== 'private' && visibility !== 'protected';
417
+ }
418
+ function isLiteral(node) {
419
+ if (node.type === 'string' || node.type === 'integer' || node.type === 'float' || node.type === 'boolean') {
420
+ return true;
421
+ }
422
+ return node.type === 'encapsed_string' && namedChildren(node).every((c) => c.type === 'string_content');
423
+ }
424
+ /** Simple type name: namespaces, nullability and leading `\` dropped; undefined for primitives and unions. */
425
+ function typeName(node) {
426
+ switch (node.type) {
427
+ case 'name':
428
+ return node.text;
429
+ case 'qualified_name':
430
+ return lastSegment(node.text);
431
+ case 'named_type':
432
+ case 'optional_type': {
433
+ const inner = namedChildren(node)[0];
434
+ return inner ? typeName(inner) : undefined;
435
+ }
436
+ default:
437
+ return undefined;
438
+ }
439
+ }
440
+ function addTyped(types, typeNode, nameNode) {
441
+ const name = variableName(nameNode);
442
+ if (!typeNode || !name || types.has(name))
443
+ return;
444
+ const type = typeName(typeNode);
445
+ if (type)
446
+ types.set(name, type);
447
+ }
448
+ /** `$repo` → `repo`. */
449
+ function variableName(node) {
450
+ if (node?.type !== 'variable_name')
451
+ return undefined;
452
+ return namedChildren(node)[0]?.text;
453
+ }
454
+ /** `@var Foo $x` (or `@var Foo` with `fallback`) in a doc comment. */
455
+ function addDocVar(types, comment, fallback) {
456
+ const match = /@var\s+\??\\?([\w\\]+)(?:\s+\$(\w+))?/.exec(comment.text);
457
+ const type = match?.[1] ? lastSegment(match[1]) : undefined;
458
+ const name = match?.[2] ?? fallback;
459
+ if (type && name && !types.has(name) && /^[A-Z]/.test(type))
460
+ types.set(name, type);
461
+ }
462
+ /** Typed properties (`private Repo $repo;`, `@var Repo`) and constructor-promoted parameters of a type. */
463
+ function typeScope(node) {
464
+ const types = new Map();
465
+ const body = node.childForFieldName('body');
466
+ let previous;
467
+ for (const member of body ? namedChildren(body) : []) {
468
+ if (member.type === 'property_declaration') {
469
+ const type = member.childForFieldName('type');
470
+ for (const element of namedChildren(member)) {
471
+ if (element.type !== 'property_element')
472
+ continue;
473
+ const nameNode = element.childForFieldName('name') ?? namedChildren(element)[0] ?? null;
474
+ addTyped(types, type, nameNode);
475
+ if (!type && previous?.type === 'comment')
476
+ addDocVar(types, previous, variableName(nameNode));
477
+ }
478
+ }
479
+ else if (member.type === 'method_declaration' && member.childForFieldName('name')?.text === '__construct') {
480
+ const params = member.childForFieldName('parameters');
481
+ for (const param of params ? namedChildren(params) : []) {
482
+ if (param.type === 'property_promotion_parameter') {
483
+ addTyped(types, param.childForFieldName('type'), param.childForFieldName('name'));
484
+ }
485
+ }
486
+ }
487
+ previous = member;
488
+ }
489
+ return types;
490
+ }
491
+ /** Typed parameters, `$x = new Foo()` locals and `@var Foo $x` comments anywhere inside a function. */
492
+ function memberScope(node) {
493
+ const types = new Map();
494
+ const visit = (n) => {
495
+ if (n.type.endsWith('_parameter')) {
496
+ addTyped(types, n.childForFieldName('type'), n.childForFieldName('name'));
497
+ }
498
+ else if (n.type === 'assignment_expression') {
499
+ const right = n.childForFieldName('right');
500
+ const created = right?.type === 'object_creation_expression' ? namedChildren(right)[0] : undefined;
501
+ const name = variableName(n.childForFieldName('left'));
502
+ const type = created ? typeName(created) : undefined;
503
+ if (name && type && type !== 'self' && type !== 'static' && !types.has(name))
504
+ types.set(name, type);
505
+ }
506
+ else if (n.type === 'comment') {
507
+ addDocVar(types, n);
508
+ }
509
+ for (const child of namedChildren(n))
510
+ visit(child);
511
+ };
512
+ visit(node);
513
+ return types;
514
+ }
515
+ /** Declared type of `name`: locals and parameters for `$x`, properties for `$this->x`. */
516
+ function lookupType(scopes, name, property) {
517
+ for (const scope of scopes) {
518
+ if (scope.member === property)
519
+ continue;
520
+ const type = scope.types.get(name);
521
+ if (type)
522
+ return type;
523
+ }
524
+ return undefined;
525
+ }
526
+ /** `foo()`, `$x->foo()`, `$this->foo()`, `$this->x->foo()`, `Foo::bar()`, `self::bar()`; undefined otherwise. */
527
+ function callTarget(node, ctx) {
528
+ if (node.type === 'function_call_expression') {
529
+ const fn = node.childForFieldName('function');
530
+ const callee = fn ? typeName(fn) : undefined;
531
+ return callee ? { callee } : undefined;
532
+ }
533
+ const nameNode = node.childForFieldName('name');
534
+ if (nameNode?.type !== 'name')
535
+ return undefined;
536
+ const callee = nameNode.text;
537
+ if (node.type === 'scoped_call_expression') {
538
+ const scope = node.childForFieldName('scope');
539
+ if (scope?.type === 'relative_scope') {
540
+ const parent = scope.text.toLowerCase() === 'parent';
541
+ const type = parent ? ctx.parent : ctx.type;
542
+ return { callee, object: parent ? 'parent' : 'self', ...(type ? { objectType: type } : {}) };
543
+ }
544
+ const object = scope ? typeName(scope) : undefined;
545
+ return object ? { callee, object } : undefined;
546
+ }
547
+ const receiver = node.childForFieldName('object');
548
+ const variable = variableName(receiver);
549
+ if (variable === 'this')
550
+ return { callee, object: 'this' };
551
+ if (variable)
552
+ return withType({ callee, object: variable }, lookupType(ctx.scopes, variable, false));
553
+ if (receiver && MEMBER_ACCESS.has(receiver.type) && variableName(receiver.childForFieldName('object')) === 'this') {
554
+ const property = receiver.childForFieldName('name');
555
+ if (property?.type === 'name') {
556
+ return withType({ callee, object: property.text }, lookupType(ctx.scopes, property.text, true));
557
+ }
558
+ }
559
+ return undefined; // $a->b->c(), f()->g(), $$x()...
560
+ }
561
+ function withType(target, type) {
562
+ return type ? { ...target, objectType: type } : target;
563
+ }
564
+ /** Guzzle-like receivers: `$client`, `$this->http`, or anything typed `Client`. */
565
+ function isClient(target) {
566
+ if (target.objectType)
567
+ return CLIENT_TYPES.has(target.objectType) || /Client$/.test(target.objectType);
568
+ return target.object !== undefined && target.object !== 'this' && CLIENT_RECEIVER.test(target.object);
569
+ }
570
+ /** Class name of a `Foo::bar()` scope (namespace dropped). */
571
+ function scopeName(node) {
572
+ const scope = node.childForFieldName('scope');
573
+ return scope ? typeName(scope) : undefined;
574
+ }
575
+ /** Scope class at the root of a call chain: `Http` in `Http::withToken($t)->get(...)`. */
576
+ function rootScope(node) {
577
+ let current = node;
578
+ while (current && MEMBER_CALLS.has(current.type))
579
+ current = current.childForFieldName('object');
580
+ return current?.type === 'scoped_call_expression' ? scopeName(current) : undefined;
581
+ }
582
+ /**
583
+ * `Route::prefix('api')->group(fn)` or `Route::group(['prefix' => 'api'], fn)`:
584
+ * the closure and the prefix its routes get.
585
+ */
586
+ function groupOf(node) {
587
+ if (node.childForFieldName('name')?.text !== 'group')
588
+ return undefined;
589
+ const args = callArgs(node);
590
+ const closure = args.at(-1);
591
+ if (!closure || !CLOSURES.has(closure.type))
592
+ return undefined;
593
+ if (node.type === 'scoped_call_expression') {
594
+ if (scopeName(node) !== 'Route')
595
+ return undefined;
596
+ return { closure, prefix: (args[0] && arrayOption(args[0], 'prefix')) ?? '' };
597
+ }
598
+ if (rootScope(node) !== 'Route')
599
+ return undefined;
600
+ const prefixes = [];
601
+ let current = node.childForFieldName('object');
602
+ while (current && CALL_TYPES.has(current.type)) {
603
+ if (current.childForFieldName('name')?.text === 'prefix') {
604
+ const first = callArgs(current)[0];
605
+ const value = first ? stringValue(first) : undefined;
606
+ if (value !== undefined)
607
+ prefixes.unshift(value);
608
+ }
609
+ current = current.type === 'scoped_call_expression' ? null : current.childForFieldName('object');
610
+ }
611
+ return { closure, prefix: prefixes.join('/') };
612
+ }
613
+ /** `'prefix' => 'api'` in an array literal. */
614
+ function arrayOption(array, key) {
615
+ if (array.type !== 'array_creation_expression')
616
+ return undefined;
617
+ for (const element of namedChildren(array)) {
618
+ const [k, v] = namedChildren(element);
619
+ if (k && v && stringValue(k) === key)
620
+ return stringValue(v);
621
+ }
622
+ return undefined;
623
+ }
624
+ /** Controller call of a route handler: `[Foo::class, 'show']`, `'Foo@show'`, invokable `Foo::class`. */
625
+ function handlerTarget(handler) {
626
+ if (handler.type === 'array_creation_expression') {
627
+ const [cls, method] = namedChildren(handler).map((e) => namedChildren(e)[0]);
628
+ const object = cls ? classConstant(cls) : undefined;
629
+ const callee = method ? stringValue(method) : undefined;
630
+ return object && callee ? { callee, object } : undefined;
631
+ }
632
+ const cls = classConstant(handler);
633
+ if (cls)
634
+ return { callee: cls };
635
+ const text = stringValue(handler);
636
+ const match = text ? /^([\w\\]+)@(\w+)$/.exec(text) : null;
637
+ return match?.[1] && match[2] ? { callee: match[2], object: lastSegment(match[1]) } : undefined;
638
+ }
639
+ /** `Foo::class` → `Foo`. */
640
+ function classConstant(node) {
641
+ if (node.type !== 'class_constant_access_expression')
642
+ return undefined;
643
+ const [cls, member] = namedChildren(node);
644
+ return cls && member?.text === 'class' ? typeName(cls) : undefined;
645
+ }
646
+ function singular(word) {
647
+ if (word.endsWith('ies'))
648
+ return `${word.slice(0, -3)}y`;
649
+ return word.replace(/s$/, '');
650
+ }
651
+ function argValue(arg) {
652
+ return namedChildren(arg).at(-1); // skips `name:` labels
653
+ }
654
+ function callArgs(call) {
655
+ const list = call.childForFieldName('arguments');
656
+ if (!list)
657
+ return [];
658
+ return namedChildren(list)
659
+ .filter((a) => a.type === 'argument')
660
+ .map(argValue)
661
+ .filter((v) => v !== undefined);
662
+ }
663
+ /** Content of a string without interpolation; undefined otherwise. */
664
+ function stringValue(node) {
665
+ if (node.type !== 'string' && node.type !== 'encapsed_string')
666
+ return undefined;
667
+ const parts = namedChildren(node);
668
+ if (!parts.every((c) => c.type === 'string_content' || c.type === 'escape_sequence'))
669
+ return undefined;
670
+ return parts.map((c) => c.text).join('');
671
+ }
672
+ /** A URL as written: interpolations and concatenated expressions become `{}`; undefined without any literal. */
673
+ function urlValue(node) {
674
+ let literal = false;
675
+ const visit = (n) => {
676
+ if (n.type === 'string' || n.type === 'encapsed_string' || n.type === 'heredoc') {
677
+ literal = true;
678
+ const parts = n.type === 'heredoc' ? namedChildren(n.childForFieldName('value') ?? n) : namedChildren(n);
679
+ return parts
680
+ .map((c) => (c.type === 'string_content' || c.type === 'escape_sequence' ? c.text : '{}'))
681
+ .join('');
682
+ }
683
+ if (n.type === 'binary_expression' && n.child(1)?.type === '.') {
684
+ const left = n.childForFieldName('left');
685
+ const right = n.childForFieldName('right');
686
+ return `${left ? visit(left) : ''}${right ? visit(right) : ''}`;
687
+ }
688
+ if (n.type === 'parenthesized_expression') {
689
+ const inner = namedChildren(n)[0];
690
+ return inner ? visit(inner) : '{}';
691
+ }
692
+ return '{}';
693
+ };
694
+ const url = visit(node);
695
+ return literal ? url : undefined;
696
+ }
697
+ /** `'x.php'` as written; `__DIR__ . '/x.php'` → `./x.php`. */
698
+ function requirePath(node) {
699
+ const literal = stringValue(node);
700
+ if (literal !== undefined)
701
+ return literal || undefined;
702
+ if (node.type === 'parenthesized_expression') {
703
+ const inner = namedChildren(node)[0];
704
+ return inner ? requirePath(inner) : undefined;
705
+ }
706
+ if (node.type !== 'binary_expression')
707
+ return undefined;
708
+ const left = node.childForFieldName('left');
709
+ const right = node.childForFieldName('right');
710
+ const rest = right ? stringValue(right) : undefined;
711
+ if (left?.text !== '__DIR__' || !rest)
712
+ return undefined;
713
+ return `./${rest.replace(/^\/+/, '')}`;
714
+ }
715
+ /** The statement holding a call chain, or the call itself when it is not a statement. */
716
+ function statementOf(call) {
717
+ let top = call;
718
+ while (top.parent && CHAIN_PARENTS.has(top.parent.type))
719
+ top = top.parent;
720
+ return top.parent?.type === 'expression_statement' ? top.parent : call;
721
+ }
722
+ /** Joins route segments: leading `/`, collapsed slashes, no trailing slash (root stays `/`). */
723
+ function joinPath(prefix, template) {
724
+ const path = `/${prefix}/${template}`.replace(/\/+/g, '/');
725
+ return path.length > 1 ? path.replace(/\/$/, '') : path;
726
+ }
727
+ // ---- Symfony routing ----
728
+ /** `#[Route('/x', methods: ['GET'])]` or a `@Route("/x", methods={"GET"})` doc comment on a class or method. */
729
+ function routeAttribute(node) {
730
+ for (const list of namedChildren(node)) {
731
+ if (list.type !== 'attribute_list')
732
+ continue;
733
+ for (const group of namedChildren(list)) {
734
+ for (const attr of namedChildren(group)) {
735
+ if (attr.type !== 'attribute')
736
+ continue;
737
+ const nameNode = namedChildren(attr)[0];
738
+ if (!nameNode || typeName(nameNode) !== 'Route')
739
+ continue;
740
+ return attributeRoute(attr);
741
+ }
742
+ }
743
+ }
744
+ const comment = node.previousNamedSibling;
745
+ return comment?.type === 'comment' ? annotationRoute(comment.text) : undefined;
746
+ }
747
+ function attributeRoute(attr) {
748
+ const params = attr.childForFieldName('parameters');
749
+ let path = '';
750
+ let method = 'ANY';
751
+ for (const arg of params ? namedChildren(params) : []) {
752
+ if (arg.type !== 'argument')
753
+ continue;
754
+ const label = arg.childForFieldName('name')?.text;
755
+ const value = argValue(arg);
756
+ if (!value)
757
+ continue;
758
+ if (label === undefined || label === 'path') {
759
+ path = stringValue(value) ?? path;
760
+ }
761
+ else if (label === 'methods') {
762
+ const first = value.type === 'array_creation_expression' ? namedChildren(namedChildren(value)[0] ?? value)[0] : value;
763
+ method = (first && stringValue(first)?.toUpperCase()) || method;
764
+ }
765
+ }
766
+ return { method, path };
767
+ }
768
+ function annotationRoute(comment) {
769
+ const match = /@Route\(\s*(?:path\s*=\s*)?"([^"]*)"([^)]*)\)/.exec(comment);
770
+ if (!match)
771
+ return undefined;
772
+ const method = /methods\s*=\s*\{\s*"(\w+)"/.exec(match[2] ?? '')?.[1]?.toUpperCase() ?? 'ANY';
773
+ return { method, path: match[1] ?? '' };
774
+ }
775
+ //# sourceMappingURL=php.js.map