@optave/codegraph 3.11.2 → 3.12.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/README.md +8 -8
- package/dist/db/migrations.d.ts.map +1 -1
- package/dist/db/migrations.js +7 -0
- package/dist/db/migrations.js.map +1 -1
- package/dist/domain/analysis/module-map.d.ts +2 -0
- package/dist/domain/analysis/module-map.d.ts.map +1 -1
- package/dist/domain/analysis/module-map.js +24 -2
- package/dist/domain/analysis/module-map.js.map +1 -1
- package/dist/domain/graph/builder/call-resolver.d.ts +4 -2
- package/dist/domain/graph/builder/call-resolver.d.ts.map +1 -1
- package/dist/domain/graph/builder/call-resolver.js +170 -8
- package/dist/domain/graph/builder/call-resolver.js.map +1 -1
- package/dist/domain/graph/builder/cha.d.ts +61 -0
- package/dist/domain/graph/builder/cha.d.ts.map +1 -0
- package/dist/domain/graph/builder/cha.js +143 -0
- package/dist/domain/graph/builder/cha.js.map +1 -0
- package/dist/domain/graph/builder/context.d.ts +3 -0
- package/dist/domain/graph/builder/context.d.ts.map +1 -1
- package/dist/domain/graph/builder/context.js +2 -0
- package/dist/domain/graph/builder/context.js.map +1 -1
- package/dist/domain/graph/builder/helpers.d.ts +17 -1
- package/dist/domain/graph/builder/helpers.d.ts.map +1 -1
- package/dist/domain/graph/builder/helpers.js +159 -5
- package/dist/domain/graph/builder/helpers.js.map +1 -1
- package/dist/domain/graph/builder/incremental.d.ts.map +1 -1
- package/dist/domain/graph/builder/incremental.js +73 -1
- package/dist/domain/graph/builder/incremental.js.map +1 -1
- package/dist/domain/graph/builder/stages/build-edges.d.ts +2 -0
- package/dist/domain/graph/builder/stages/build-edges.d.ts.map +1 -1
- package/dist/domain/graph/builder/stages/build-edges.js +926 -26
- package/dist/domain/graph/builder/stages/build-edges.js.map +1 -1
- package/dist/domain/graph/builder/stages/detect-changes.d.ts.map +1 -1
- package/dist/domain/graph/builder/stages/detect-changes.js +2 -1
- package/dist/domain/graph/builder/stages/detect-changes.js.map +1 -1
- package/dist/domain/graph/builder/stages/native-orchestrator.d.ts.map +1 -1
- package/dist/domain/graph/builder/stages/native-orchestrator.js +501 -14
- package/dist/domain/graph/builder/stages/native-orchestrator.js.map +1 -1
- package/dist/domain/graph/builder/stages/resolve-imports.d.ts +1 -0
- package/dist/domain/graph/builder/stages/resolve-imports.d.ts.map +1 -1
- package/dist/domain/graph/builder/stages/resolve-imports.js +9 -0
- package/dist/domain/graph/builder/stages/resolve-imports.js.map +1 -1
- package/dist/domain/graph/journal.js +1 -1
- package/dist/domain/graph/journal.js.map +1 -1
- package/dist/domain/graph/resolver/points-to.d.ts +53 -0
- package/dist/domain/graph/resolver/points-to.d.ts.map +1 -0
- package/dist/domain/graph/resolver/points-to.js +213 -0
- package/dist/domain/graph/resolver/points-to.js.map +1 -0
- package/dist/domain/graph/resolver/ts-resolver.d.ts +9 -0
- package/dist/domain/graph/resolver/ts-resolver.d.ts.map +1 -0
- package/dist/domain/graph/resolver/ts-resolver.js +476 -0
- package/dist/domain/graph/resolver/ts-resolver.js.map +1 -0
- package/dist/domain/parser.d.ts +10 -1
- package/dist/domain/parser.d.ts.map +1 -1
- package/dist/domain/parser.js +39 -7
- package/dist/domain/parser.js.map +1 -1
- package/dist/domain/wasm-worker-entry.js +25 -0
- package/dist/domain/wasm-worker-entry.js.map +1 -1
- package/dist/domain/wasm-worker-pool.d.ts.map +1 -1
- package/dist/domain/wasm-worker-pool.js +32 -0
- package/dist/domain/wasm-worker-pool.js.map +1 -1
- package/dist/domain/wasm-worker-protocol.d.ts +14 -1
- package/dist/domain/wasm-worker-protocol.d.ts.map +1 -1
- package/dist/extractors/c.js +3 -3
- package/dist/extractors/c.js.map +1 -1
- package/dist/extractors/clojure.js +1 -1
- package/dist/extractors/clojure.js.map +1 -1
- package/dist/extractors/cpp.js +3 -3
- package/dist/extractors/cpp.js.map +1 -1
- package/dist/extractors/csharp.d.ts.map +1 -1
- package/dist/extractors/csharp.js +37 -8
- package/dist/extractors/csharp.js.map +1 -1
- package/dist/extractors/cuda.js +3 -3
- package/dist/extractors/cuda.js.map +1 -1
- package/dist/extractors/elixir.js +6 -6
- package/dist/extractors/elixir.js.map +1 -1
- package/dist/extractors/fsharp.js +1 -1
- package/dist/extractors/fsharp.js.map +1 -1
- package/dist/extractors/go.js +5 -5
- package/dist/extractors/go.js.map +1 -1
- package/dist/extractors/haskell.js +1 -1
- package/dist/extractors/haskell.js.map +1 -1
- package/dist/extractors/java.js +2 -2
- package/dist/extractors/java.js.map +1 -1
- package/dist/extractors/javascript.d.ts +2 -0
- package/dist/extractors/javascript.d.ts.map +1 -1
- package/dist/extractors/javascript.js +1674 -64
- package/dist/extractors/javascript.js.map +1 -1
- package/dist/extractors/kotlin.js +5 -5
- package/dist/extractors/kotlin.js.map +1 -1
- package/dist/extractors/lua.js +1 -1
- package/dist/extractors/lua.js.map +1 -1
- package/dist/extractors/objc.js +3 -3
- package/dist/extractors/objc.js.map +1 -1
- package/dist/extractors/ocaml.js +1 -1
- package/dist/extractors/ocaml.js.map +1 -1
- package/dist/extractors/php.js +2 -2
- package/dist/extractors/php.js.map +1 -1
- package/dist/extractors/python.js +7 -7
- package/dist/extractors/python.js.map +1 -1
- package/dist/extractors/ruby.js +2 -2
- package/dist/extractors/ruby.js.map +1 -1
- package/dist/extractors/scala.js +1 -1
- package/dist/extractors/scala.js.map +1 -1
- package/dist/extractors/solidity.js +1 -1
- package/dist/extractors/solidity.js.map +1 -1
- package/dist/extractors/swift.js +4 -4
- package/dist/extractors/swift.js.map +1 -1
- package/dist/extractors/zig.js +4 -4
- package/dist/extractors/zig.js.map +1 -1
- package/dist/infrastructure/config.d.ts +10 -0
- package/dist/infrastructure/config.d.ts.map +1 -1
- package/dist/infrastructure/config.js +15 -0
- package/dist/infrastructure/config.js.map +1 -1
- package/dist/infrastructure/native.d.ts +11 -0
- package/dist/infrastructure/native.d.ts.map +1 -1
- package/dist/infrastructure/native.js +78 -5
- package/dist/infrastructure/native.js.map +1 -1
- package/dist/presentation/queries-cli/overview.d.ts.map +1 -1
- package/dist/presentation/queries-cli/overview.js +5 -0
- package/dist/presentation/queries-cli/overview.js.map +1 -1
- package/dist/types.d.ts +184 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +7 -7
- package/src/db/migrations.ts +7 -0
- package/src/domain/analysis/module-map.ts +29 -1
- package/src/domain/graph/builder/call-resolver.ts +177 -7
- package/src/domain/graph/builder/cha.ts +175 -0
- package/src/domain/graph/builder/context.ts +3 -0
- package/src/domain/graph/builder/helpers.ts +175 -5
- package/src/domain/graph/builder/incremental.ts +79 -1
- package/src/domain/graph/builder/stages/build-edges.ts +1128 -24
- package/src/domain/graph/builder/stages/detect-changes.ts +3 -1
- package/src/domain/graph/builder/stages/native-orchestrator.ts +583 -20
- package/src/domain/graph/builder/stages/resolve-imports.ts +14 -0
- package/src/domain/graph/journal.ts +1 -1
- package/src/domain/graph/resolver/points-to.ts +254 -0
- package/src/domain/graph/resolver/ts-resolver.ts +536 -0
- package/src/domain/parser.ts +43 -5
- package/src/domain/wasm-worker-entry.ts +25 -0
- package/src/domain/wasm-worker-pool.ts +21 -0
- package/src/domain/wasm-worker-protocol.ts +14 -0
- package/src/extractors/c.ts +3 -3
- package/src/extractors/clojure.ts +1 -1
- package/src/extractors/cpp.ts +3 -3
- package/src/extractors/csharp.ts +33 -9
- package/src/extractors/cuda.ts +3 -3
- package/src/extractors/elixir.ts +6 -6
- package/src/extractors/fsharp.ts +1 -1
- package/src/extractors/go.ts +5 -5
- package/src/extractors/haskell.ts +1 -1
- package/src/extractors/java.ts +2 -2
- package/src/extractors/javascript.ts +1802 -66
- package/src/extractors/kotlin.ts +5 -5
- package/src/extractors/lua.ts +1 -1
- package/src/extractors/objc.ts +3 -3
- package/src/extractors/ocaml.ts +1 -1
- package/src/extractors/php.ts +2 -2
- package/src/extractors/python.ts +7 -7
- package/src/extractors/ruby.ts +2 -2
- package/src/extractors/scala.ts +1 -1
- package/src/extractors/solidity.ts +1 -1
- package/src/extractors/swift.ts +4 -4
- package/src/extractors/zig.ts +4 -4
- package/src/infrastructure/config.ts +15 -0
- package/src/infrastructure/native.ts +87 -5
- package/src/presentation/queries-cli/overview.ts +15 -1
- package/src/types.ts +194 -0
|
@@ -55,7 +55,15 @@ const BUILTIN_GLOBALS = new Set([
|
|
|
55
55
|
'Buffer',
|
|
56
56
|
'EventEmitter',
|
|
57
57
|
'Stream',
|
|
58
|
+
'process',
|
|
59
|
+
'window',
|
|
60
|
+
'document',
|
|
61
|
+
'globalThis',
|
|
58
62
|
]);
|
|
63
|
+
/** Maximum chain depth for inter-procedural return-type propagation (Phase 8.2). */
|
|
64
|
+
const MAX_PROPAGATION_DEPTH = 3;
|
|
65
|
+
/** Confidence penalty applied per propagation hop (1.0 → 0.9 → 0.8 → 0.7). */
|
|
66
|
+
export const PROPAGATION_HOP_PENALTY = 0.1;
|
|
59
67
|
/**
|
|
60
68
|
* Extract symbols from a JS/TS parsed AST.
|
|
61
69
|
* When a compiled tree-sitter Query is provided (from parser.js),
|
|
@@ -138,7 +146,9 @@ function handleExportCapture(c, exps, imports) {
|
|
|
138
146
|
const declType = decl.type;
|
|
139
147
|
const kindMap = {
|
|
140
148
|
function_declaration: 'function',
|
|
149
|
+
generator_function_declaration: 'function',
|
|
141
150
|
class_declaration: 'class',
|
|
151
|
+
abstract_class_declaration: 'class',
|
|
142
152
|
interface_declaration: 'interface',
|
|
143
153
|
type_alias_declaration: 'type',
|
|
144
154
|
};
|
|
@@ -261,6 +271,7 @@ function dispatchQueryMatch(c, definitions, calls, imports, classes, exps) {
|
|
|
261
271
|
}
|
|
262
272
|
else if (c.assign_node) {
|
|
263
273
|
handleCommonJSAssignment(c.assign_left, c.assign_right, c.assign_node, imports);
|
|
274
|
+
handleFuncPropAssignment(c.assign_left, c.assign_right, definitions);
|
|
264
275
|
}
|
|
265
276
|
}
|
|
266
277
|
function extractSymbolsQuery(tree, query) {
|
|
@@ -270,6 +281,17 @@ function extractSymbolsQuery(tree, query) {
|
|
|
270
281
|
const classes = [];
|
|
271
282
|
const exps = [];
|
|
272
283
|
const typeMap = new Map();
|
|
284
|
+
const returnTypeMap = new Map();
|
|
285
|
+
const callAssignments = [];
|
|
286
|
+
const fnRefBindings = [];
|
|
287
|
+
const paramBindings = [];
|
|
288
|
+
const arrayElemBindings = [];
|
|
289
|
+
const spreadArgBindings = [];
|
|
290
|
+
const forOfBindings = [];
|
|
291
|
+
const arrayCallbackBindings = [];
|
|
292
|
+
const objectRestParamBindings = [];
|
|
293
|
+
const objectPropBindings = [];
|
|
294
|
+
const thisCallBindings = [];
|
|
273
295
|
const matches = query.matches(tree.rootNode);
|
|
274
296
|
for (const match of matches) {
|
|
275
297
|
// Build capture lookup for this match (1-3 captures each, very fast)
|
|
@@ -280,13 +302,65 @@ function extractSymbolsQuery(tree, query) {
|
|
|
280
302
|
}
|
|
281
303
|
// Extract top-level constants via targeted walk (query patterns don't cover these)
|
|
282
304
|
extractConstantsWalk(tree.rootNode, definitions);
|
|
283
|
-
//
|
|
284
|
-
|
|
285
|
-
//
|
|
286
|
-
|
|
305
|
+
// Phase 8.2: Extract function return types first — runContextCollectorWalk's
|
|
306
|
+
// declarator handler reads the *complete* per-file map for inter-procedural
|
|
307
|
+
// propagation, so this cannot be folded into that pass.
|
|
308
|
+
extractReturnTypeMapWalk(tree.rootNode, returnTypeMap);
|
|
309
|
+
// Context-tracking collector pass: typeMap (with return-type propagation),
|
|
310
|
+
// object-rest param bindings, and spread/for-of/Array.from bindings.
|
|
311
|
+
runContextCollectorWalk(tree.rootNode, {
|
|
312
|
+
typeMap,
|
|
313
|
+
returnTypeMap,
|
|
314
|
+
callAssignments,
|
|
315
|
+
fnRefBindings,
|
|
316
|
+
objectRestParamBindings,
|
|
317
|
+
spreadArgBindings,
|
|
318
|
+
forOfBindings,
|
|
319
|
+
arrayCallbackBindings,
|
|
320
|
+
});
|
|
287
321
|
// Extract definitions from destructured bindings (query patterns don't match object_pattern)
|
|
288
322
|
extractDestructuredBindingsWalk(tree.rootNode, definitions);
|
|
289
|
-
|
|
323
|
+
// Everything without bespoke traversal semantics is collected in ONE pass:
|
|
324
|
+
// dynamic import() calls, prototype-method definitions, param bindings,
|
|
325
|
+
// array-element bindings, object-prop bindings, `new X()` names,
|
|
326
|
+
// Object.defineProperty receivers, class members (fields/static blocks,
|
|
327
|
+
// which query patterns don't capture), and this()/call/apply bindings.
|
|
328
|
+
const newExpressions = [];
|
|
329
|
+
const definePropertyReceivers = new Map();
|
|
330
|
+
runCollectorWalk(tree.rootNode, {
|
|
331
|
+
definitions,
|
|
332
|
+
typeMap,
|
|
333
|
+
paramBindings,
|
|
334
|
+
arrayElemBindings,
|
|
335
|
+
objectPropBindings,
|
|
336
|
+
newExpressions,
|
|
337
|
+
definePropertyReceivers,
|
|
338
|
+
imports,
|
|
339
|
+
calls,
|
|
340
|
+
thisCallBindings,
|
|
341
|
+
classMemberDefs: definitions,
|
|
342
|
+
});
|
|
343
|
+
return {
|
|
344
|
+
definitions,
|
|
345
|
+
calls,
|
|
346
|
+
imports,
|
|
347
|
+
classes,
|
|
348
|
+
exports: exps,
|
|
349
|
+
typeMap,
|
|
350
|
+
returnTypeMap,
|
|
351
|
+
callAssignments,
|
|
352
|
+
fnRefBindings,
|
|
353
|
+
paramBindings,
|
|
354
|
+
arrayElemBindings,
|
|
355
|
+
spreadArgBindings,
|
|
356
|
+
forOfBindings,
|
|
357
|
+
arrayCallbackBindings,
|
|
358
|
+
objectRestParamBindings,
|
|
359
|
+
objectPropBindings,
|
|
360
|
+
thisCallBindings,
|
|
361
|
+
newExpressions,
|
|
362
|
+
...(definePropertyReceivers.size > 0 ? { definePropertyReceivers } : {}),
|
|
363
|
+
};
|
|
290
364
|
}
|
|
291
365
|
/** Node types that define a function scope — constants inside these are skipped. */
|
|
292
366
|
const FUNCTION_SCOPE_TYPES = new Set([
|
|
@@ -338,6 +412,10 @@ function extractConstantsWalk(node, definitions) {
|
|
|
338
412
|
}
|
|
339
413
|
}
|
|
340
414
|
}
|
|
415
|
+
// Class field definitions and static initializer blocks (which query patterns
|
|
416
|
+
// don't capture) are collected inline in runCollectorWalk's field_definition /
|
|
417
|
+
// class_static_block cases when `classMemberDefs` is set. The walk-based path
|
|
418
|
+
// (extractSymbolsWalk) handles these node types via walkJavaScriptNode instead.
|
|
341
419
|
/**
|
|
342
420
|
* Walk the AST to find destructured const bindings (query patterns don't match object_pattern).
|
|
343
421
|
* e.g. `const { handleToken, checkPermissions } = initAuth(config)`
|
|
@@ -360,7 +438,7 @@ function extractDestructuredBindingsWalk(node, definitions) {
|
|
|
360
438
|
declNode.text.startsWith('const ')) {
|
|
361
439
|
for (let j = 0; j < declNode.childCount; j++) {
|
|
362
440
|
const declarator = declNode.child(j);
|
|
363
|
-
if (
|
|
441
|
+
if (declarator?.type !== 'variable_declarator')
|
|
364
442
|
continue;
|
|
365
443
|
const nameN = declarator.childForFieldName('name');
|
|
366
444
|
if (nameN && nameN.type === 'object_pattern') {
|
|
@@ -382,15 +460,18 @@ function extractConstDeclarators(declNode, definitions) {
|
|
|
382
460
|
return;
|
|
383
461
|
for (let j = 0; j < declNode.childCount; j++) {
|
|
384
462
|
const declarator = declNode.child(j);
|
|
385
|
-
if (
|
|
463
|
+
if (declarator?.type !== 'variable_declarator')
|
|
386
464
|
continue;
|
|
387
465
|
const nameN = declarator.childForFieldName('name');
|
|
388
466
|
const valueN = declarator.childForFieldName('value');
|
|
389
|
-
if (
|
|
467
|
+
if (nameN?.type !== 'identifier' || !valueN)
|
|
390
468
|
continue;
|
|
391
469
|
// Skip functions — already captured by query patterns
|
|
392
470
|
const valType = valueN.type;
|
|
393
|
-
if (valType === 'arrow_function' ||
|
|
471
|
+
if (valType === 'arrow_function' ||
|
|
472
|
+
valType === 'function_expression' ||
|
|
473
|
+
valType === 'function' ||
|
|
474
|
+
valType === 'generator_function')
|
|
394
475
|
continue;
|
|
395
476
|
if (isConstantValue(valueN)) {
|
|
396
477
|
definitions.push({
|
|
@@ -399,6 +480,14 @@ function extractConstDeclarators(declNode, definitions) {
|
|
|
399
480
|
line: nodeStartLine(declNode),
|
|
400
481
|
endLine: nodeEndLine(declNode),
|
|
401
482
|
});
|
|
483
|
+
// Phase 8.3f: extract function/arrow properties from object literals.
|
|
484
|
+
// Scope guard: extractConstDeclarators is only called from extractConstantsWalk, which
|
|
485
|
+
// already skips const declarations inside function scopes (line ~412). So these definitions
|
|
486
|
+
// are always top-level. Any new call site must add a hasFunctionScopeAncestor guard
|
|
487
|
+
// (the walk path at handleVariableDecl does this).
|
|
488
|
+
if (valueN.type === 'object') {
|
|
489
|
+
extractObjectLiteralFunctions(valueN, nameN.text, definitions);
|
|
490
|
+
}
|
|
402
491
|
}
|
|
403
492
|
}
|
|
404
493
|
}
|
|
@@ -407,33 +496,36 @@ function extractConstDeclarators(declNode, definitions) {
|
|
|
407
496
|
* Query patterns match call_expression with identifier/member_expression/subscript_expression
|
|
408
497
|
* functions, but import() has function type `import` which none of those patterns cover.
|
|
409
498
|
*/
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
499
|
+
/**
|
|
500
|
+
* Collect a dynamic `import()` call at `node` (a call_expression).
|
|
501
|
+
* Returns true when the node *is* an import() call — the collector walk uses
|
|
502
|
+
* this to suppress dynamic-import collection inside the import's own argument
|
|
503
|
+
* subtree, preserving the former standalone walk's "don't recurse into
|
|
504
|
+
* import() children" behaviour without hiding those children from the other
|
|
505
|
+
* collectors.
|
|
506
|
+
*/
|
|
507
|
+
function collectDynamicImport(node, imports) {
|
|
508
|
+
const fn = node.childForFieldName('function');
|
|
509
|
+
if (fn?.type !== 'import')
|
|
510
|
+
return false;
|
|
511
|
+
const args = node.childForFieldName('arguments') || findChild(node, 'arguments');
|
|
512
|
+
if (args) {
|
|
513
|
+
const strArg = findChild(args, 'string');
|
|
514
|
+
if (strArg) {
|
|
515
|
+
const modPath = strArg.text.replace(/['"]/g, '');
|
|
516
|
+
const names = extractDynamicImportNames(node);
|
|
517
|
+
imports.push({
|
|
518
|
+
source: modPath,
|
|
519
|
+
names,
|
|
520
|
+
line: nodeStartLine(node),
|
|
521
|
+
dynamicImport: true,
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
else {
|
|
525
|
+
debug(`Skipping non-static dynamic import() at line ${nodeStartLine(node)} (template literal or variable)`);
|
|
432
526
|
}
|
|
433
527
|
}
|
|
434
|
-
|
|
435
|
-
extractDynamicImportsWalk(node.child(i), imports);
|
|
436
|
-
}
|
|
528
|
+
return true;
|
|
437
529
|
}
|
|
438
530
|
function handleCommonJSAssignment(left, right, node, imports) {
|
|
439
531
|
if (!left || !right)
|
|
@@ -489,20 +581,76 @@ function extractSymbolsWalk(tree) {
|
|
|
489
581
|
classes: [],
|
|
490
582
|
exports: [],
|
|
491
583
|
typeMap: new Map(),
|
|
584
|
+
returnTypeMap: new Map(),
|
|
585
|
+
callAssignments: [],
|
|
586
|
+
fnRefBindings: [],
|
|
587
|
+
paramBindings: [],
|
|
588
|
+
arrayElemBindings: [],
|
|
589
|
+
spreadArgBindings: [],
|
|
590
|
+
forOfBindings: [],
|
|
591
|
+
arrayCallbackBindings: [],
|
|
592
|
+
objectRestParamBindings: [],
|
|
593
|
+
objectPropBindings: [],
|
|
594
|
+
thisCallBindings: [],
|
|
492
595
|
};
|
|
493
596
|
walkJavaScriptNode(tree.rootNode, ctx);
|
|
494
|
-
//
|
|
495
|
-
|
|
597
|
+
// Phase 8.2: Extract function return types first — runContextCollectorWalk's
|
|
598
|
+
// declarator handler reads the *complete* per-file map for inter-procedural
|
|
599
|
+
// propagation, so this cannot be folded into that pass.
|
|
600
|
+
extractReturnTypeMapWalk(tree.rootNode, ctx.returnTypeMap);
|
|
601
|
+
// Context-tracking collector pass: typeMap (with return-type propagation),
|
|
602
|
+
// object-rest param bindings, and spread/for-of/Array.from bindings.
|
|
603
|
+
runContextCollectorWalk(tree.rootNode, {
|
|
604
|
+
typeMap: ctx.typeMap,
|
|
605
|
+
returnTypeMap: ctx.returnTypeMap,
|
|
606
|
+
callAssignments: ctx.callAssignments,
|
|
607
|
+
fnRefBindings: ctx.fnRefBindings,
|
|
608
|
+
objectRestParamBindings: ctx.objectRestParamBindings,
|
|
609
|
+
spreadArgBindings: ctx.spreadArgBindings,
|
|
610
|
+
forOfBindings: ctx.forOfBindings,
|
|
611
|
+
arrayCallbackBindings: ctx.arrayCallbackBindings,
|
|
612
|
+
});
|
|
613
|
+
// Single collector pass for everything else: prototype-method and func-prop
|
|
614
|
+
// definitions, param bindings, array-element bindings, object-prop bindings,
|
|
615
|
+
// `new X()` names, and Object.defineProperty receivers. Dynamic imports,
|
|
616
|
+
// this()/call/apply bindings, and class members are omitted here —
|
|
617
|
+
// walkJavaScriptNode already covers those node types on this path.
|
|
618
|
+
const newExpressions = [];
|
|
619
|
+
const definePropertyReceivers = new Map();
|
|
620
|
+
runCollectorWalk(tree.rootNode, {
|
|
621
|
+
definitions: ctx.definitions,
|
|
622
|
+
typeMap: ctx.typeMap,
|
|
623
|
+
paramBindings: ctx.paramBindings,
|
|
624
|
+
arrayElemBindings: ctx.arrayElemBindings,
|
|
625
|
+
objectPropBindings: ctx.objectPropBindings,
|
|
626
|
+
newExpressions,
|
|
627
|
+
definePropertyReceivers,
|
|
628
|
+
funcPropDefs: ctx.definitions,
|
|
629
|
+
});
|
|
630
|
+
ctx.newExpressions = newExpressions;
|
|
631
|
+
if (definePropertyReceivers.size > 0)
|
|
632
|
+
ctx.definePropertyReceivers = definePropertyReceivers;
|
|
496
633
|
return ctx;
|
|
497
634
|
}
|
|
498
635
|
function walkJavaScriptNode(node, ctx) {
|
|
499
636
|
switch (node.type) {
|
|
500
637
|
case 'function_declaration':
|
|
638
|
+
case 'generator_function_declaration':
|
|
501
639
|
handleFunctionDecl(node, ctx);
|
|
502
640
|
break;
|
|
503
641
|
case 'class_declaration':
|
|
642
|
+
case 'abstract_class_declaration':
|
|
643
|
+
// class expressions: `return class Foo extends Bar { ... }` or `const X = class Foo { ... }`
|
|
644
|
+
case 'class':
|
|
504
645
|
handleClassDecl(node, ctx);
|
|
505
646
|
break;
|
|
647
|
+
case 'class_static_block':
|
|
648
|
+
handleStaticBlock(node, ctx.definitions);
|
|
649
|
+
break;
|
|
650
|
+
case 'field_definition':
|
|
651
|
+
case 'public_field_definition':
|
|
652
|
+
handleFieldDef(node, ctx.definitions);
|
|
653
|
+
break;
|
|
506
654
|
case 'method_definition':
|
|
507
655
|
handleMethodDef(node, ctx);
|
|
508
656
|
break;
|
|
@@ -596,6 +744,71 @@ function handleMethodDef(node, ctx) {
|
|
|
596
744
|
});
|
|
597
745
|
}
|
|
598
746
|
}
|
|
747
|
+
/**
|
|
748
|
+
* Create a synthetic `ClassName.<static:L:C>` definition for a class static block
|
|
749
|
+
* so that calls inside the block can be attributed to a method-kind node and
|
|
750
|
+
* `resolveThisDispatch` can walk up to the parent class for `super.method()`.
|
|
751
|
+
*
|
|
752
|
+
* The start line and column are appended to the name to ensure uniqueness when a
|
|
753
|
+
* class has multiple `static { }` blocks (each has a distinct start position even
|
|
754
|
+
* if on the same line).
|
|
755
|
+
*
|
|
756
|
+
* Tree-sitter uses `class_static_block` (not `static_block`) for `static { ... }`.
|
|
757
|
+
*/
|
|
758
|
+
function handleStaticBlock(node, definitions) {
|
|
759
|
+
const parentClass = findParentClass(node);
|
|
760
|
+
if (!parentClass)
|
|
761
|
+
return;
|
|
762
|
+
const line = nodeStartLine(node);
|
|
763
|
+
const col = node.startPosition.column;
|
|
764
|
+
definitions.push({
|
|
765
|
+
name: `${parentClass}.<static:${line}:${col}>`,
|
|
766
|
+
kind: 'method',
|
|
767
|
+
line,
|
|
768
|
+
endLine: nodeEndLine(node),
|
|
769
|
+
});
|
|
770
|
+
}
|
|
771
|
+
/**
|
|
772
|
+
* Emit a `ClassName.fieldName` definition for class fields that have an initializer.
|
|
773
|
+
* This lets `findCaller` attribute calls inside field initializers (e.g. static field
|
|
774
|
+
* side-effects) to the field rather than the enclosing class.
|
|
775
|
+
*
|
|
776
|
+
* JS `field_definition` uses the `'property'` field name; TS
|
|
777
|
+
* `public_field_definition` uses `'name'`. As a third fallback (Rust/TS parity) we
|
|
778
|
+
* also check for a positional `property_identifier` child.
|
|
779
|
+
*/
|
|
780
|
+
const CALLABLE_FIELD_TYPES = new Set([
|
|
781
|
+
'arrow_function',
|
|
782
|
+
'function_expression',
|
|
783
|
+
'generator_function',
|
|
784
|
+
]);
|
|
785
|
+
function handleFieldDef(node, definitions) {
|
|
786
|
+
// JS field_definition uses 'property' field; TS public_field_definition uses 'name' field
|
|
787
|
+
const nameNode = node.childForFieldName('name') ||
|
|
788
|
+
node.childForFieldName('property') ||
|
|
789
|
+
findChild(node, 'property_identifier');
|
|
790
|
+
const valueNode = node.childForFieldName('value');
|
|
791
|
+
if (!nameNode || !valueNode)
|
|
792
|
+
return;
|
|
793
|
+
if (nameNode.type === 'computed_property_name')
|
|
794
|
+
return;
|
|
795
|
+
// Only emit a callable definition when the initializer is a function/arrow expression.
|
|
796
|
+
// Scalar fields like `static x = 42` should not appear as method-kind nodes.
|
|
797
|
+
if (!CALLABLE_FIELD_TYPES.has(valueNode.type))
|
|
798
|
+
return;
|
|
799
|
+
const fieldName = nameNode.text;
|
|
800
|
+
if (!fieldName)
|
|
801
|
+
return;
|
|
802
|
+
const parentClass = findParentClass(node);
|
|
803
|
+
if (!parentClass)
|
|
804
|
+
return;
|
|
805
|
+
definitions.push({
|
|
806
|
+
name: `${parentClass}.${fieldName}`,
|
|
807
|
+
kind: 'method',
|
|
808
|
+
line: nodeStartLine(node),
|
|
809
|
+
endLine: nodeEndLine(node),
|
|
810
|
+
});
|
|
811
|
+
}
|
|
599
812
|
function handleInterfaceDecl(node, ctx) {
|
|
600
813
|
const nameNode = node.childForFieldName('name');
|
|
601
814
|
if (!nameNode)
|
|
@@ -660,7 +873,8 @@ function handleVariableDecl(node, ctx) {
|
|
|
660
873
|
const valType = valueN.type;
|
|
661
874
|
if (valType === 'arrow_function' ||
|
|
662
875
|
valType === 'function_expression' ||
|
|
663
|
-
valType === 'function'
|
|
876
|
+
valType === 'function' ||
|
|
877
|
+
valType === 'generator_function') {
|
|
664
878
|
const varFnChildren = extractParameters(valueN);
|
|
665
879
|
ctx.definitions.push({
|
|
666
880
|
name: nameN.text,
|
|
@@ -670,13 +884,26 @@ function handleVariableDecl(node, ctx) {
|
|
|
670
884
|
children: varFnChildren.length > 0 ? varFnChildren : undefined,
|
|
671
885
|
});
|
|
672
886
|
}
|
|
673
|
-
else if (isConst &&
|
|
887
|
+
else if (isConst &&
|
|
888
|
+
nameN.type === 'identifier' &&
|
|
889
|
+
isConstantValue(valueN) &&
|
|
890
|
+
!hasFunctionScopeAncestor(node)) {
|
|
674
891
|
ctx.definitions.push({
|
|
675
892
|
name: nameN.text,
|
|
676
893
|
kind: 'constant',
|
|
677
894
|
line: nodeStartLine(node),
|
|
678
895
|
endLine: nodeEndLine(node),
|
|
679
896
|
});
|
|
897
|
+
// Phase 8.3f: extract function/arrow properties from object literals so that
|
|
898
|
+
// this.method() calls inside Object.defineProperty accessors can resolve them.
|
|
899
|
+
// Scope guard: hasFunctionScopeAncestor mirrors the Rust path's find_parent_of_types
|
|
900
|
+
// check and the sibling destructured-binding branch below — skips object literals
|
|
901
|
+
// inside function bodies to avoid polluting the global definition index with
|
|
902
|
+
// local variable properties (e.g. `localObj.fn` from `const localObj = { fn: ... }`
|
|
903
|
+
// inside a function).
|
|
904
|
+
if (valueN.type === 'object') {
|
|
905
|
+
extractObjectLiteralFunctions(valueN, nameN.text, ctx.definitions);
|
|
906
|
+
}
|
|
680
907
|
}
|
|
681
908
|
else if (isConst && nameN.type === 'object_pattern' && !hasFunctionScopeAncestor(node)) {
|
|
682
909
|
// Destructured bindings: const { handleToken, checkPermissions } = initAuth(...)
|
|
@@ -692,6 +919,55 @@ function handleVariableDecl(node, ctx) {
|
|
|
692
919
|
}
|
|
693
920
|
}
|
|
694
921
|
}
|
|
922
|
+
/**
|
|
923
|
+
* Phase 8.3f: extract function/arrow function properties from an object literal as standalone
|
|
924
|
+
* definitions so that `this.method()` calls inside Object.defineProperty accessor functions can
|
|
925
|
+
* resolve them via the same-file definition lookup.
|
|
926
|
+
*
|
|
927
|
+
* Definitions are emitted as qualified names (`obj.baz` rather than bare `baz`) to avoid
|
|
928
|
+
* polluting the global definition index with common property names like `init`, `run`, or
|
|
929
|
+
* `render`. The typeMap value stored by the caller also uses the qualified name so the resolver
|
|
930
|
+
* looks up `lookup.byName('obj.baz')` rather than `lookup.byName('baz')`.
|
|
931
|
+
*
|
|
932
|
+
* `const obj = { baz: () => {} }` → emits Definition { name: 'obj.baz', kind: 'function' }
|
|
933
|
+
*/
|
|
934
|
+
function extractObjectLiteralFunctions(objNode, varName, definitions) {
|
|
935
|
+
for (let i = 0; i < objNode.childCount; i++) {
|
|
936
|
+
const child = objNode.child(i);
|
|
937
|
+
if (!child)
|
|
938
|
+
continue;
|
|
939
|
+
if (child.type === 'pair') {
|
|
940
|
+
const keyNode = child.childForFieldName('key');
|
|
941
|
+
const valueNode = child.childForFieldName('value');
|
|
942
|
+
if (!keyNode || !valueNode)
|
|
943
|
+
continue;
|
|
944
|
+
const keyName = keyNode.type === 'string' ? keyNode.text.replace(/^['"]|['"]$/g, '') : keyNode.text;
|
|
945
|
+
if (!keyName)
|
|
946
|
+
continue;
|
|
947
|
+
if (valueNode.type === 'arrow_function' ||
|
|
948
|
+
valueNode.type === 'function_expression' ||
|
|
949
|
+
valueNode.type === 'function') {
|
|
950
|
+
definitions.push({
|
|
951
|
+
name: `${varName}.${keyName}`,
|
|
952
|
+
kind: 'function',
|
|
953
|
+
line: nodeStartLine(child),
|
|
954
|
+
endLine: nodeEndLine(valueNode),
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
else if (child.type === 'method_definition') {
|
|
959
|
+
const nameNode = child.childForFieldName('name');
|
|
960
|
+
if (nameNode) {
|
|
961
|
+
definitions.push({
|
|
962
|
+
name: `${varName}.${nameNode.text}`,
|
|
963
|
+
kind: 'function',
|
|
964
|
+
line: nodeStartLine(child),
|
|
965
|
+
endLine: nodeEndLine(child),
|
|
966
|
+
});
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
}
|
|
695
971
|
function handleEnumDecl(node, ctx) {
|
|
696
972
|
const nameNode = node.childForFieldName('name');
|
|
697
973
|
if (!nameNode)
|
|
@@ -731,6 +1007,11 @@ function handleCallExpr(node, ctx) {
|
|
|
731
1007
|
handleDynamicImportCall(node, ctx.imports);
|
|
732
1008
|
}
|
|
733
1009
|
else {
|
|
1010
|
+
// this() calls: `this` used as a function (not as a receiver).
|
|
1011
|
+
if (fn.type === 'this') {
|
|
1012
|
+
ctx.calls.push({ name: 'this', line: nodeStartLine(node) });
|
|
1013
|
+
return; // no further processing needed for this()-style calls
|
|
1014
|
+
}
|
|
734
1015
|
const callInfo = extractCallInfo(fn, node);
|
|
735
1016
|
if (callInfo)
|
|
736
1017
|
ctx.calls.push(callInfo);
|
|
@@ -738,6 +1019,32 @@ function handleCallExpr(node, ctx) {
|
|
|
738
1019
|
const cbDef = extractCallbackDefinition(node, fn);
|
|
739
1020
|
if (cbDef)
|
|
740
1021
|
ctx.definitions.push(cbDef);
|
|
1022
|
+
// this-call bindings: `fn.call(namedCtx, ...)` / `fn.apply(namedCtx, ...)`
|
|
1023
|
+
const obj = fn.childForFieldName('object');
|
|
1024
|
+
const prop = fn.childForFieldName('property');
|
|
1025
|
+
if (obj?.type === 'identifier' &&
|
|
1026
|
+
prop &&
|
|
1027
|
+
(prop.text === 'call' || prop.text === 'apply') &&
|
|
1028
|
+
!BUILTIN_GLOBALS.has(obj.text)) {
|
|
1029
|
+
const args = node.childForFieldName('arguments') || findChild(node, 'arguments');
|
|
1030
|
+
if (args) {
|
|
1031
|
+
for (let i = 0; i < args.childCount; i++) {
|
|
1032
|
+
const child = args.child(i);
|
|
1033
|
+
if (!child)
|
|
1034
|
+
continue;
|
|
1035
|
+
const t = child.type;
|
|
1036
|
+
if (t === '(' || t === ')' || t === ',')
|
|
1037
|
+
continue;
|
|
1038
|
+
if (t === 'identifier' &&
|
|
1039
|
+
!BUILTIN_GLOBALS.has(child.text) &&
|
|
1040
|
+
child.text !== 'undefined' &&
|
|
1041
|
+
child.text !== 'null') {
|
|
1042
|
+
ctx.thisCallBindings.push({ callee: obj.text, thisArg: child.text });
|
|
1043
|
+
}
|
|
1044
|
+
break;
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
741
1048
|
}
|
|
742
1049
|
ctx.calls.push(...extractCallbackReferenceCalls(node));
|
|
743
1050
|
}
|
|
@@ -791,7 +1098,9 @@ function handleExportStmt(node, ctx) {
|
|
|
791
1098
|
const declType = decl.type;
|
|
792
1099
|
const kindMap = {
|
|
793
1100
|
function_declaration: 'function',
|
|
1101
|
+
generator_function_declaration: 'function',
|
|
794
1102
|
class_declaration: 'class',
|
|
1103
|
+
abstract_class_declaration: 'class',
|
|
795
1104
|
interface_declaration: 'interface',
|
|
796
1105
|
type_alias_declaration: 'type',
|
|
797
1106
|
};
|
|
@@ -1010,7 +1319,7 @@ function extractSimpleTypeName(typeAnnotationNode) {
|
|
|
1010
1319
|
return null;
|
|
1011
1320
|
}
|
|
1012
1321
|
function extractNewExprTypeName(newExprNode) {
|
|
1013
|
-
if (
|
|
1322
|
+
if (newExprNode?.type !== 'new_expression')
|
|
1014
1323
|
return null;
|
|
1015
1324
|
const ctor = newExprNode.childForFieldName('constructor') || newExprNode.child(1);
|
|
1016
1325
|
if (!ctor)
|
|
@@ -1023,40 +1332,458 @@ function extractNewExprTypeName(newExprNode) {
|
|
|
1023
1332
|
}
|
|
1024
1333
|
return null;
|
|
1025
1334
|
}
|
|
1335
|
+
// ── Phase 8.2: Inter-Procedural Return Type Propagation ─────────────────────
|
|
1026
1336
|
/**
|
|
1027
|
-
*
|
|
1337
|
+
* Walk the AST and record the return type of every function/method definition.
|
|
1028
1338
|
*
|
|
1029
|
-
*
|
|
1030
|
-
*
|
|
1031
|
-
* - 0
|
|
1032
|
-
* - 0.
|
|
1339
|
+
* Keys: plain name (e.g. "createUser") or "ClassName.methodName" for methods.
|
|
1340
|
+
* Confidence:
|
|
1341
|
+
* - 1.0: explicit TypeScript return type annotation
|
|
1342
|
+
* - 0.85: inferred from the first `return new Constructor()` in the body
|
|
1343
|
+
*/
|
|
1344
|
+
function extractReturnTypeMapWalk(rootNode, returnTypeMap) {
|
|
1345
|
+
function walk(node, depth, currentClass) {
|
|
1346
|
+
if (depth >= MAX_WALK_DEPTH)
|
|
1347
|
+
return;
|
|
1348
|
+
const t = node.type;
|
|
1349
|
+
if (t === 'class_declaration' || t === 'abstract_class_declaration' || t === 'class') {
|
|
1350
|
+
const nameNode = node.childForFieldName('name');
|
|
1351
|
+
const className = nameNode?.text ?? null;
|
|
1352
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
1353
|
+
walk(node.child(i), depth + 1, className);
|
|
1354
|
+
}
|
|
1355
|
+
return;
|
|
1356
|
+
}
|
|
1357
|
+
if (t === 'function_declaration' || t === 'generator_function_declaration') {
|
|
1358
|
+
const nameNode = node.childForFieldName('name');
|
|
1359
|
+
if (nameNode?.type === 'identifier' && nameNode.text !== 'constructor') {
|
|
1360
|
+
const fnName = currentClass ? `${currentClass}.${nameNode.text}` : nameNode.text;
|
|
1361
|
+
storeReturnType(node, fnName, returnTypeMap);
|
|
1362
|
+
}
|
|
1363
|
+
// Recurse into the function body with null currentClass so nested
|
|
1364
|
+
// function declarations are not stored under the enclosing class name.
|
|
1365
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
1366
|
+
walk(node.child(i), depth + 1, null);
|
|
1367
|
+
}
|
|
1368
|
+
return;
|
|
1369
|
+
}
|
|
1370
|
+
else if (t === 'method_definition') {
|
|
1371
|
+
const nameNode = node.childForFieldName('name');
|
|
1372
|
+
if (nameNode && currentClass && nameNode.text !== 'constructor') {
|
|
1373
|
+
storeReturnType(node, `${currentClass}.${nameNode.text}`, returnTypeMap);
|
|
1374
|
+
}
|
|
1375
|
+
// Recurse into the method body with null currentClass so nested
|
|
1376
|
+
// function declarations are not stored under the enclosing class name.
|
|
1377
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
1378
|
+
walk(node.child(i), depth + 1, null);
|
|
1379
|
+
}
|
|
1380
|
+
return;
|
|
1381
|
+
}
|
|
1382
|
+
else if (t === 'variable_declarator') {
|
|
1383
|
+
// const foo = (): ReturnType => … or const foo = function(): ReturnType { … }
|
|
1384
|
+
const nameN = node.childForFieldName('name');
|
|
1385
|
+
const valueN = node.childForFieldName('value');
|
|
1386
|
+
if (nameN?.type === 'identifier' && valueN) {
|
|
1387
|
+
const vt = valueN.type;
|
|
1388
|
+
if (vt === 'arrow_function' ||
|
|
1389
|
+
vt === 'function_expression' ||
|
|
1390
|
+
vt === 'generator_function') {
|
|
1391
|
+
const fnName = currentClass ? `${currentClass}.${nameN.text}` : nameN.text;
|
|
1392
|
+
storeReturnType(valueN, fnName, returnTypeMap);
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
1397
|
+
walk(node.child(i), depth + 1, currentClass);
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
walk(rootNode, 0, null);
|
|
1401
|
+
}
|
|
1402
|
+
/** Extract the return type of a function node and store it in the returnTypeMap. */
|
|
1403
|
+
function storeReturnType(fnNode, fnName, returnTypeMap) {
|
|
1404
|
+
const returnTypeNode = fnNode.childForFieldName('return_type');
|
|
1405
|
+
if (returnTypeNode) {
|
|
1406
|
+
const typeName = extractSimpleTypeName(returnTypeNode);
|
|
1407
|
+
if (typeName) {
|
|
1408
|
+
const existing = returnTypeMap.get(fnName);
|
|
1409
|
+
if (!existing || existing.confidence < 1.0)
|
|
1410
|
+
returnTypeMap.set(fnName, { type: typeName, confidence: 1.0 });
|
|
1411
|
+
return;
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
// Infer from first `return new Constructor()` in the function body
|
|
1415
|
+
const body = fnNode.childForFieldName('body');
|
|
1416
|
+
if (body) {
|
|
1417
|
+
const inferred = findReturnNewExprType(body);
|
|
1418
|
+
if (inferred) {
|
|
1419
|
+
const existing = returnTypeMap.get(fnName);
|
|
1420
|
+
if (!existing || 0.85 > existing.confidence)
|
|
1421
|
+
returnTypeMap.set(fnName, { type: inferred, confidence: 0.85 });
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1425
|
+
/** Return the constructor name from the first `return new Constructor()` in a body, or null. */
|
|
1426
|
+
function findReturnNewExprType(bodyNode) {
|
|
1427
|
+
for (let i = 0; i < bodyNode.childCount; i++) {
|
|
1428
|
+
const child = bodyNode.child(i);
|
|
1429
|
+
if (child?.type !== 'return_statement')
|
|
1430
|
+
continue;
|
|
1431
|
+
for (let j = 0; j < child.childCount; j++) {
|
|
1432
|
+
const expr = child.child(j);
|
|
1433
|
+
if (expr?.type === 'new_expression')
|
|
1434
|
+
return extractNewExprTypeName(expr);
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
return null;
|
|
1438
|
+
}
|
|
1439
|
+
/**
|
|
1440
|
+
* Resolve the return type of a call_expression node using returnTypeMap.
|
|
1441
|
+
* Handles: createUser() (identifier), service.getRepo() (member), and
|
|
1442
|
+
* getService().getRepo() (chained call) up to MAX_PROPAGATION_DEPTH hops.
|
|
1443
|
+
*
|
|
1444
|
+
* `depth` tracks total chain hops consumed so far. Each call boundary — both
|
|
1445
|
+
* resolving the receiver and resolving the final return type — costs one hop.
|
|
1446
|
+
* Confidence = annotated return type confidence − 0.1 × (depth + 1).
|
|
1447
|
+
*
|
|
1448
|
+
* Examples (annotated sources → confidence 1.0):
|
|
1449
|
+
* createUser() depth=0 → 1.0 − 0.1 = 0.9 (1 hop)
|
|
1450
|
+
* svc.getUser() depth=0 → 1.0 − 0.1 = 0.9 (1 hop; receiver from typeMap)
|
|
1451
|
+
* getService().getRepo() depth=0 → inner resolved at depth=1, outer at depth+1 → 0.8 (2 hops)
|
|
1452
|
+
*/
|
|
1453
|
+
function resolveCallExprReturnType(callNode, typeMap, returnTypeMap, depth) {
|
|
1454
|
+
if (depth >= MAX_PROPAGATION_DEPTH)
|
|
1455
|
+
return null;
|
|
1456
|
+
const fn = callNode.childForFieldName('function');
|
|
1457
|
+
if (!fn)
|
|
1458
|
+
return null;
|
|
1459
|
+
if (fn.type === 'identifier') {
|
|
1460
|
+
const entry = returnTypeMap.get(fn.text);
|
|
1461
|
+
if (!entry)
|
|
1462
|
+
return null;
|
|
1463
|
+
const confidence = entry.confidence - PROPAGATION_HOP_PENALTY * (depth + 1);
|
|
1464
|
+
return confidence > 0 ? { type: entry.type, confidence } : null;
|
|
1465
|
+
}
|
|
1466
|
+
if (fn.type === 'member_expression') {
|
|
1467
|
+
const obj = fn.childForFieldName('object');
|
|
1468
|
+
const prop = fn.childForFieldName('property');
|
|
1469
|
+
if (!obj || !prop)
|
|
1470
|
+
return null;
|
|
1471
|
+
let receiverType = null;
|
|
1472
|
+
// effectiveDepth tracks the depth at which THIS call's return type is charged.
|
|
1473
|
+
// When the receiver is itself a call expression (chain), we've already consumed
|
|
1474
|
+
// a hop resolving it, so charge this call at depth+1.
|
|
1475
|
+
let effectiveDepth = depth;
|
|
1476
|
+
if (obj.type === 'identifier') {
|
|
1477
|
+
const typeEntry = typeMap.get(obj.text);
|
|
1478
|
+
receiverType = typeEntry ? typeEntry.type : null;
|
|
1479
|
+
}
|
|
1480
|
+
else if (obj.type === 'call_expression') {
|
|
1481
|
+
// Each link in a call chain costs an extra hop.
|
|
1482
|
+
const innerResult = resolveCallExprReturnType(obj, typeMap, returnTypeMap, depth + 1);
|
|
1483
|
+
receiverType = innerResult ? innerResult.type : null;
|
|
1484
|
+
effectiveDepth = depth + 1;
|
|
1485
|
+
}
|
|
1486
|
+
if (receiverType) {
|
|
1487
|
+
const entry = returnTypeMap.get(`${receiverType}.${prop.text}`);
|
|
1488
|
+
if (entry) {
|
|
1489
|
+
const confidence = entry.confidence - PROPAGATION_HOP_PENALTY * (effectiveDepth + 1);
|
|
1490
|
+
return confidence > 0 ? { type: entry.type, confidence } : null;
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
return null;
|
|
1495
|
+
}
|
|
1496
|
+
/**
|
|
1497
|
+
* Record a call assignment into callAssignments for cross-file propagation.
|
|
1498
|
+
* Only records cases where the callee is a simple identifier or a method call
|
|
1499
|
+
* on a known-typed variable — chain expressions are skipped (handled locally).
|
|
1500
|
+
*/
|
|
1501
|
+
function recordCallAssignment(callNode, varName, typeMap, callAssignments) {
|
|
1502
|
+
const fn = callNode.childForFieldName('function');
|
|
1503
|
+
if (!fn)
|
|
1504
|
+
return;
|
|
1505
|
+
if (fn.type === 'identifier') {
|
|
1506
|
+
callAssignments.push({ varName, calleeName: fn.text });
|
|
1507
|
+
}
|
|
1508
|
+
else if (fn.type === 'member_expression') {
|
|
1509
|
+
const obj = fn.childForFieldName('object');
|
|
1510
|
+
const prop = fn.childForFieldName('property');
|
|
1511
|
+
if (obj?.type === 'identifier' && prop) {
|
|
1512
|
+
const receiverEntry = typeMap.get(obj.text);
|
|
1513
|
+
callAssignments.push({
|
|
1514
|
+
varName,
|
|
1515
|
+
calleeName: prop.text,
|
|
1516
|
+
receiverTypeName: receiverEntry?.type,
|
|
1517
|
+
});
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
}
|
|
1521
|
+
/**
|
|
1522
|
+
* Phase 8.5 (RTA): collect all constructor names from `new X()` expressions
|
|
1523
|
+
* in the file. Captures both assigned (`const x = new Foo()`) and unassigned
|
|
1524
|
+
* (`doSomething(new Foo())`) usages that the typeMap-based approach would miss.
|
|
1525
|
+
*/
|
|
1526
|
+
// `new X()` constructor-name collection (Phase 8.5 RTA instantiation tracking)
|
|
1527
|
+
// happens inline in runCollectorWalk's new_expression case.
|
|
1528
|
+
/**
|
|
1529
|
+
* Walk the AST to find `Object.defineProperty(obj, "bar", { get: getter })` patterns
|
|
1530
|
+
* and record which functions are used as getter/setter accessors for which objects.
|
|
1531
|
+
*
|
|
1532
|
+
* Result is stored in the provided map as `funcName → receiverVarName`.
|
|
1533
|
+
*/
|
|
1534
|
+
function collectDefinePropertyReceiver(node, out) {
|
|
1535
|
+
const fn = node.childForFieldName('function');
|
|
1536
|
+
// Match `Object.defineProperty`
|
|
1537
|
+
if (fn?.type !== 'member_expression')
|
|
1538
|
+
return;
|
|
1539
|
+
const obj = fn.childForFieldName('object');
|
|
1540
|
+
const prop = fn.childForFieldName('property');
|
|
1541
|
+
if (obj?.type !== 'identifier' || obj.text !== 'Object' || prop?.text !== 'defineProperty') {
|
|
1542
|
+
return;
|
|
1543
|
+
}
|
|
1544
|
+
const argsNode = node.childForFieldName('arguments') ?? findChild(node, 'arguments');
|
|
1545
|
+
if (!argsNode)
|
|
1546
|
+
return;
|
|
1547
|
+
// Collect non-punctuation children: arg0 (target obj), arg1 (prop name string), arg2 (descriptor)
|
|
1548
|
+
const argChildren = [];
|
|
1549
|
+
for (let i = 0; i < argsNode.childCount; i++) {
|
|
1550
|
+
const c = argsNode.child(i);
|
|
1551
|
+
if (!c)
|
|
1552
|
+
continue;
|
|
1553
|
+
if (c.type === ',' || c.type === '(' || c.type === ')')
|
|
1554
|
+
continue;
|
|
1555
|
+
argChildren.push(c);
|
|
1556
|
+
}
|
|
1557
|
+
if (argChildren.length < 3)
|
|
1558
|
+
return;
|
|
1559
|
+
const targetObj = argChildren[0];
|
|
1560
|
+
const descriptor = argChildren[2];
|
|
1561
|
+
if (targetObj?.type !== 'identifier' || descriptor?.type !== 'object')
|
|
1562
|
+
return;
|
|
1563
|
+
const targetName = targetObj.text;
|
|
1564
|
+
// Walk the descriptor object's pair children looking for get/set
|
|
1565
|
+
for (let i = 0; i < descriptor.childCount; i++) {
|
|
1566
|
+
const pair = descriptor.child(i);
|
|
1567
|
+
if (pair?.type !== 'pair')
|
|
1568
|
+
continue;
|
|
1569
|
+
const key = pair.childForFieldName('key');
|
|
1570
|
+
const val = pair.childForFieldName('value');
|
|
1571
|
+
if (key &&
|
|
1572
|
+
(key.text === 'get' || key.text === 'set') &&
|
|
1573
|
+
val?.type === 'identifier' &&
|
|
1574
|
+
!BUILTIN_GLOBALS.has(val.text)) {
|
|
1575
|
+
// Known limitation: if the same function is registered as an
|
|
1576
|
+
// accessor on multiple objects, last-write-wins — only the
|
|
1577
|
+
// last target object is retained. This is an unusual pattern
|
|
1578
|
+
// (sharing one function across multiple defineProperty calls)
|
|
1579
|
+
// and covering it would require Map<string, string[]> which
|
|
1580
|
+
// changes the consumer API. Tracked as a known edge case.
|
|
1581
|
+
out.set(val.text, targetName);
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
/**
|
|
1586
|
+
* Single context-tracking pass combining what were three separate full-tree
|
|
1587
|
+
* walks (typeMap, object-rest params, spread/for-of) — see runCollectorWalk
|
|
1588
|
+
* for why traversal count dominates extraction cost on WASM trees.
|
|
1589
|
+
*
|
|
1590
|
+
* Each concern keeps its own enclosing-class register because their reset
|
|
1591
|
+
* rules intentionally differ:
|
|
1592
|
+
*
|
|
1593
|
+
* - typeMap (`typeMapClass`): extracts variable-to-type assignments.
|
|
1594
|
+
* Values are `{ type: string, confidence: number }`:
|
|
1595
|
+
* - 1.0: explicit constructor (`new Foo()`)
|
|
1596
|
+
* - 0.9: type annotation (`: Foo`) or typed parameter
|
|
1597
|
+
* - 0.85: property write (`obj.prop = fn` — Phase 8.3d pts tracking)
|
|
1598
|
+
* - 0.7–0.9: inter-procedural propagation from return-type map (Phase 8.2)
|
|
1599
|
+
* - 0.7: factory method call (`Foo.create()` — uppercase-first heuristic)
|
|
1600
|
+
* Higher-confidence entries take priority when the same variable is seen
|
|
1601
|
+
* twice. Class declarations propagate their name into the subtree; class
|
|
1602
|
+
* *expressions* (`const Foo = class Bar { … }`) propagate null because the
|
|
1603
|
+
* expression-internal name is never visible to the resolver, preserving the
|
|
1604
|
+
* `this.prop` fallback in resolveByMethodOrGlobal. No reset at function
|
|
1605
|
+
* boundaries.
|
|
1606
|
+
*
|
|
1607
|
+
* - object-rest params (`objectRestClass`, Phase 8.3f): context flows only
|
|
1608
|
+
* class_declaration/class → class_body → method_definition so methods are
|
|
1609
|
+
* keyed "ClassName.method"; every other node type resets to null, and
|
|
1610
|
+
* function/method bodies recurse with null so nested declarations don't
|
|
1611
|
+
* inherit the class context.
|
|
1033
1612
|
*
|
|
1034
|
-
*
|
|
1613
|
+
* - spread/for-of (`funcStack`/`classStack`, Phase 8.3e): tracks the
|
|
1614
|
+
* enclosing *function* (not just class) via push/pop so for-of bindings
|
|
1615
|
+
* record the qualified enclosing callable (e.g. 'Foo.bar', 'obj.method',
|
|
1616
|
+
* or '<module>' at top level).
|
|
1617
|
+
*
|
|
1618
|
+
* NOTE: returnTypeMap population stays a separate, earlier pass
|
|
1619
|
+
* (extractReturnTypeMapWalk) — handleVarDeclaratorTypeMap reads it for
|
|
1620
|
+
* inter-procedural propagation, so it must be complete for the whole file
|
|
1621
|
+
* before any declarator is processed (a function declared *after* its first
|
|
1622
|
+
* use would otherwise be missed).
|
|
1035
1623
|
*/
|
|
1036
|
-
function
|
|
1037
|
-
|
|
1624
|
+
function runContextCollectorWalk(rootNode, out) {
|
|
1625
|
+
const funcStack = [];
|
|
1626
|
+
const classStack = [];
|
|
1627
|
+
const walk = (node, depth, typeMapClass, objectRestClass) => {
|
|
1038
1628
|
if (depth >= MAX_WALK_DEPTH)
|
|
1039
1629
|
return;
|
|
1040
1630
|
const t = node.type;
|
|
1631
|
+
const isClassDecl = t === 'class_declaration' || t === 'abstract_class_declaration';
|
|
1632
|
+
const isClassExpr = t === 'class';
|
|
1633
|
+
const isFnDecl = t === 'function_declaration' || t === 'generator_function_declaration';
|
|
1634
|
+
// Class name read once, shared by every concern that needs it below.
|
|
1635
|
+
let className = null;
|
|
1636
|
+
let classNameIsIdentifier = false;
|
|
1637
|
+
if (isClassDecl || isClassExpr) {
|
|
1638
|
+
const nameNode = node.childForFieldName('name');
|
|
1639
|
+
className = nameNode?.text ?? null;
|
|
1640
|
+
classNameIsIdentifier = nameNode?.type === 'identifier';
|
|
1641
|
+
}
|
|
1642
|
+
// ── spread/for-of enclosing-context stacks (push on enter, pop after children) ──
|
|
1643
|
+
let pushedFunc = false;
|
|
1644
|
+
let pushedClass = false;
|
|
1645
|
+
if (isClassDecl || isClassExpr) {
|
|
1646
|
+
// The stack push keeps the original walk's `identifier`-only check (TS
|
|
1647
|
+
// class names parse as type_identifier and were never pushed), while
|
|
1648
|
+
// typeMapClass/objectRestClass below use the bare text like their
|
|
1649
|
+
// original walks did.
|
|
1650
|
+
if (className && classNameIsIdentifier) {
|
|
1651
|
+
classStack.push(className);
|
|
1652
|
+
pushedClass = true;
|
|
1653
|
+
}
|
|
1654
|
+
}
|
|
1655
|
+
else if (isFnDecl) {
|
|
1656
|
+
const nameNode = node.childForFieldName('name');
|
|
1657
|
+
if (nameNode?.type === 'identifier') {
|
|
1658
|
+
funcStack.push(nameNode.text);
|
|
1659
|
+
pushedFunc = true;
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
else if (t === 'method_definition') {
|
|
1663
|
+
const nameNode = node.childForFieldName('name');
|
|
1664
|
+
if (nameNode) {
|
|
1665
|
+
// Qualify with the enclosing class name so the PTS key matches
|
|
1666
|
+
// callerName from findCaller (which uses def.name = 'ClassName.method').
|
|
1667
|
+
const enclosingClass = classStack.length > 0 ? classStack[classStack.length - 1] : null;
|
|
1668
|
+
const qualifiedName = enclosingClass ? `${enclosingClass}.${nameNode.text}` : nameNode.text;
|
|
1669
|
+
funcStack.push(qualifiedName);
|
|
1670
|
+
pushedFunc = true;
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1673
|
+
else if (t === 'variable_declarator') {
|
|
1674
|
+
// `const process = (arr) => { ... }` — arrow/expression functions assigned
|
|
1675
|
+
// to a variable have no `name` field on the function node itself.
|
|
1676
|
+
const nameNode = node.childForFieldName('name');
|
|
1677
|
+
const valueNode = node.childForFieldName('value');
|
|
1678
|
+
if (nameNode?.type === 'identifier' &&
|
|
1679
|
+
(valueNode?.type === 'arrow_function' || valueNode?.type === 'function_expression')) {
|
|
1680
|
+
funcStack.push(nameNode.text);
|
|
1681
|
+
pushedFunc = true;
|
|
1682
|
+
}
|
|
1683
|
+
}
|
|
1684
|
+
else if (t === 'assignment_expression') {
|
|
1685
|
+
// `obj.method = function() { ... }` — func-prop assignment.
|
|
1686
|
+
// Mirror handleFuncPropAssignment's logic so for-of loops inside the
|
|
1687
|
+
// body get the correct enclosingFunc (e.g. 'obj.method') instead of
|
|
1688
|
+
// '<module>' or the wrong outer function name.
|
|
1689
|
+
const lhs = node.childForFieldName('left');
|
|
1690
|
+
const rhs = node.childForFieldName('right');
|
|
1691
|
+
if (lhs?.type === 'member_expression' &&
|
|
1692
|
+
(rhs?.type === 'function_expression' || rhs?.type === 'arrow_function')) {
|
|
1693
|
+
const obj = lhs.childForFieldName('object');
|
|
1694
|
+
const prop = lhs.childForFieldName('property');
|
|
1695
|
+
if (obj?.type === 'identifier' &&
|
|
1696
|
+
(prop?.type === 'property_identifier' || prop?.type === 'identifier') &&
|
|
1697
|
+
!BUILTIN_GLOBALS.has(obj.text) &&
|
|
1698
|
+
prop.text !== 'prototype') {
|
|
1699
|
+
funcStack.push(`${obj.text}.${prop.text}`);
|
|
1700
|
+
pushedFunc = true;
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
}
|
|
1704
|
+
// ── per-node collectors (class nodes match none of these types) ──
|
|
1041
1705
|
if (t === 'variable_declarator') {
|
|
1042
|
-
handleVarDeclaratorTypeMap(node, typeMap);
|
|
1706
|
+
handleVarDeclaratorTypeMap(node, out.typeMap, out.returnTypeMap, out.callAssignments, out.fnRefBindings);
|
|
1707
|
+
collectCollectionWrapBinding(node, out.fnRefBindings);
|
|
1043
1708
|
}
|
|
1044
1709
|
else if (t === 'required_parameter' || t === 'optional_parameter') {
|
|
1045
|
-
handleParamTypeMap(node, typeMap);
|
|
1710
|
+
handleParamTypeMap(node, out.typeMap);
|
|
1711
|
+
}
|
|
1712
|
+
else if (t === 'assignment_expression') {
|
|
1713
|
+
handlePropWriteTypeMap(node, out.typeMap, typeMapClass);
|
|
1714
|
+
}
|
|
1715
|
+
else if (t === 'call_expression') {
|
|
1716
|
+
handleDefinePropertyTypeMap(node, out.typeMap);
|
|
1717
|
+
collectSpreadAndArrayFromBindings(node, out.spreadArgBindings, out.arrayCallbackBindings);
|
|
1718
|
+
}
|
|
1719
|
+
else if (t === 'for_in_statement') {
|
|
1720
|
+
const enclosingFunc = funcStack.length > 0 ? funcStack[funcStack.length - 1] : '<module>';
|
|
1721
|
+
collectForOfBinding(node, enclosingFunc, out.forOfBindings);
|
|
1722
|
+
}
|
|
1723
|
+
collectObjectRestParams(node, t, objectRestClass, out.objectRestParamBindings);
|
|
1724
|
+
// ── child context per concern ──
|
|
1725
|
+
const childTypeMapClass = isClassDecl ? className : isClassExpr ? null : typeMapClass;
|
|
1726
|
+
let childObjectRestClass = null;
|
|
1727
|
+
if (t === 'class_declaration' || t === 'class') {
|
|
1728
|
+
childObjectRestClass = className;
|
|
1729
|
+
}
|
|
1730
|
+
else if (t === 'class_body') {
|
|
1731
|
+
childObjectRestClass = objectRestClass;
|
|
1046
1732
|
}
|
|
1047
1733
|
for (let i = 0; i < node.childCount; i++) {
|
|
1048
|
-
walk(node.child(i), depth + 1);
|
|
1734
|
+
walk(node.child(i), depth + 1, childTypeMapClass, childObjectRestClass);
|
|
1049
1735
|
}
|
|
1050
|
-
|
|
1051
|
-
|
|
1736
|
+
if (pushedFunc)
|
|
1737
|
+
funcStack.pop();
|
|
1738
|
+
if (pushedClass)
|
|
1739
|
+
classStack.pop();
|
|
1740
|
+
};
|
|
1741
|
+
walk(rootNode, 0, null, null);
|
|
1052
1742
|
}
|
|
1053
1743
|
/** Extract type info from a variable_declarator: type annotation, constructor, or factory. */
|
|
1054
|
-
function handleVarDeclaratorTypeMap(node, typeMap) {
|
|
1744
|
+
function handleVarDeclaratorTypeMap(node, typeMap, returnTypeMap, callAssignments, fnRefBindings) {
|
|
1055
1745
|
const nameN = node.childForFieldName('name');
|
|
1056
|
-
if (
|
|
1746
|
+
if (nameN?.type !== 'identifier')
|
|
1057
1747
|
return;
|
|
1058
1748
|
const typeAnno = findChild(node, 'type_annotation');
|
|
1059
1749
|
const valueN = node.childForFieldName('value');
|
|
1750
|
+
// Phase 8.3: record function-reference bindings before any type-analysis early returns.
|
|
1751
|
+
// Captures `const fn = handler` (identifier) and `const fn = obj.method` (member_expression).
|
|
1752
|
+
// Also handles `const f = fn.bind(ctx)` — bind returns a new function aliasing fn.
|
|
1753
|
+
if (fnRefBindings && valueN) {
|
|
1754
|
+
if (valueN.type === 'identifier' && !BUILTIN_GLOBALS.has(valueN.text)) {
|
|
1755
|
+
fnRefBindings.push({ lhs: nameN.text, rhs: valueN.text });
|
|
1756
|
+
}
|
|
1757
|
+
else if (valueN.type === 'member_expression') {
|
|
1758
|
+
const prop = valueN.childForFieldName('property');
|
|
1759
|
+
const obj = valueN.childForFieldName('object');
|
|
1760
|
+
// Guard: only static property access (property_identifier or identifier), not
|
|
1761
|
+
// computed subscript expressions like obj[expr] where prop.text would be the
|
|
1762
|
+
// full expression rather than a simple name — those can never match pts keys.
|
|
1763
|
+
if (prop &&
|
|
1764
|
+
(prop.type === 'property_identifier' || prop.type === 'identifier') &&
|
|
1765
|
+
obj?.type === 'identifier' &&
|
|
1766
|
+
!BUILTIN_GLOBALS.has(obj.text)) {
|
|
1767
|
+
fnRefBindings.push({ lhs: nameN.text, rhs: prop.text, rhsReceiver: obj.text });
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1770
|
+
else if (valueN.type === 'call_expression') {
|
|
1771
|
+
// `const f = fn.bind(ctx)` — bind returns a bound copy of fn; track f → fn so
|
|
1772
|
+
// pts(f) ⊇ pts(fn) and subsequent `f(args)` calls resolve to fn.
|
|
1773
|
+
// Note: only flat-identifier binds (fn.bind) are tracked here; method-receiver
|
|
1774
|
+
// binds like `obj.method.bind(ctx)` are not captured (boundFn must be an identifier).
|
|
1775
|
+
const callFn = valueN.childForFieldName('function');
|
|
1776
|
+
if (callFn?.type === 'member_expression') {
|
|
1777
|
+
const bindProp = callFn.childForFieldName('property');
|
|
1778
|
+
if (bindProp?.text === 'bind') {
|
|
1779
|
+
const boundFn = callFn.childForFieldName('object');
|
|
1780
|
+
if (boundFn?.type === 'identifier' && !BUILTIN_GLOBALS.has(boundFn.text)) {
|
|
1781
|
+
fnRefBindings.push({ lhs: nameN.text, rhs: boundFn.text });
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
}
|
|
1785
|
+
}
|
|
1786
|
+
}
|
|
1060
1787
|
// Constructor on the same declaration wins over annotation: the runtime type is
|
|
1061
1788
|
// what matters for call resolution (e.g. `const x: Base = new Derived()` should
|
|
1062
1789
|
// resolve `x.render()` to `Derived.render`, not `Base.render`).
|
|
@@ -1078,15 +1805,50 @@ function handleVarDeclaratorTypeMap(node, typeMap) {
|
|
|
1078
1805
|
}
|
|
1079
1806
|
if (!valueN)
|
|
1080
1807
|
return;
|
|
1081
|
-
// Constructor already handled above — only factory path remains.
|
|
1082
1808
|
if (valueN.type === 'new_expression')
|
|
1083
1809
|
return;
|
|
1084
|
-
|
|
1085
|
-
|
|
1810
|
+
if (valueN.type === 'call_expression') {
|
|
1811
|
+
// Phase 8.3e: Object.create({ f1, f2 }) — seed composite pts keys obj.f1 → f1, etc.
|
|
1812
|
+
const createFn = valueN.childForFieldName('function');
|
|
1813
|
+
if (createFn?.type === 'member_expression') {
|
|
1814
|
+
const createObj = createFn.childForFieldName('object');
|
|
1815
|
+
const createProp = createFn.childForFieldName('property');
|
|
1816
|
+
if (createObj?.text === 'Object' && createProp?.text === 'create') {
|
|
1817
|
+
const createArgs = valueN.childForFieldName('arguments') || findChild(valueN, 'arguments');
|
|
1818
|
+
if (createArgs) {
|
|
1819
|
+
let proto = null;
|
|
1820
|
+
for (let i = 0; i < createArgs.childCount; i++) {
|
|
1821
|
+
const n = createArgs.child(i);
|
|
1822
|
+
if (n && n.type !== '(' && n.type !== ')' && n.type !== ',') {
|
|
1823
|
+
proto = n;
|
|
1824
|
+
break;
|
|
1825
|
+
}
|
|
1826
|
+
}
|
|
1827
|
+
if (proto?.type === 'object') {
|
|
1828
|
+
seedProtoProperties(nameN.text, proto, typeMap);
|
|
1829
|
+
}
|
|
1830
|
+
}
|
|
1831
|
+
return;
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
// Phase 8.2: inter-procedural propagation — try to resolve return type from
|
|
1835
|
+
// the local returnTypeMap before falling back to factory heuristics.
|
|
1836
|
+
if (returnTypeMap) {
|
|
1837
|
+
const result = resolveCallExprReturnType(valueN, typeMap, returnTypeMap, 0);
|
|
1838
|
+
if (result) {
|
|
1839
|
+
setTypeMapEntry(typeMap, nameN.text, result.type, result.confidence);
|
|
1840
|
+
return;
|
|
1841
|
+
}
|
|
1842
|
+
}
|
|
1843
|
+
// Record for cross-file resolution in build-edges.ts (imported functions)
|
|
1844
|
+
if (callAssignments) {
|
|
1845
|
+
recordCallAssignment(valueN, nameN.text, typeMap, callAssignments);
|
|
1846
|
+
}
|
|
1847
|
+
// Factory method heuristic: const x = Foo.create() → type Foo, confidence 0.7
|
|
1086
1848
|
const fn = valueN.childForFieldName('function');
|
|
1087
|
-
if (fn
|
|
1849
|
+
if (fn?.type === 'member_expression') {
|
|
1088
1850
|
const obj = fn.childForFieldName('object');
|
|
1089
|
-
if (obj
|
|
1851
|
+
if (obj?.type === 'identifier') {
|
|
1090
1852
|
const objName = obj.text;
|
|
1091
1853
|
if (objName[0] &&
|
|
1092
1854
|
objName[0] !== objName[0].toLowerCase() &&
|
|
@@ -1096,11 +1858,64 @@ function handleVarDeclaratorTypeMap(node, typeMap) {
|
|
|
1096
1858
|
}
|
|
1097
1859
|
}
|
|
1098
1860
|
}
|
|
1861
|
+
// Phase 8.3f: seed composite pts keys for object literal properties.
|
|
1862
|
+
// `const obj = { baz: () => {} }` → typeMap['obj.baz'] = 'obj.baz'
|
|
1863
|
+
// `const obj = { baz }` (shorthand) → typeMap['obj.baz'] = 'baz' (bare identifier target)
|
|
1864
|
+
// `const obj = { baz: otherFn }` → typeMap['obj.baz'] = 'otherFn' (identifier alias)
|
|
1865
|
+
//
|
|
1866
|
+
// For function/arrow values, the value is the qualified name ('obj.baz') because
|
|
1867
|
+
// extractObjectLiteralFunctions now registers definitions under that qualified name to avoid
|
|
1868
|
+
// polluting the global index with bare property names like 'init', 'run', or 'render'.
|
|
1869
|
+
// Enables accessor this-dispatch: when typeMap['getter:this'] = 'obj',
|
|
1870
|
+
// resolving this.baz() inside getter → typeMap['obj.baz'] → 'obj.baz' → lookup.byName('obj.baz').
|
|
1871
|
+
//
|
|
1872
|
+
// Scope guard: mirrors Rust handle_var_decl's find_parent_of_types check — skip object literals
|
|
1873
|
+
// inside function bodies so function-scoped `const localObj = { fn: ... }` never seeds
|
|
1874
|
+
// the typeMap (which would shadow a module-level `const obj` with the same property names).
|
|
1875
|
+
if (valueN.type === 'object' && !hasFunctionScopeAncestor(node)) {
|
|
1876
|
+
for (let i = 0; i < valueN.childCount; i++) {
|
|
1877
|
+
const child = valueN.child(i);
|
|
1878
|
+
if (!child)
|
|
1879
|
+
continue;
|
|
1880
|
+
if (child.type === 'shorthand_property_identifier') {
|
|
1881
|
+
setTypeMapEntry(typeMap, `${nameN.text}.${child.text}`, child.text, 0.85);
|
|
1882
|
+
}
|
|
1883
|
+
else if (child.type === 'pair') {
|
|
1884
|
+
const keyNode = child.childForFieldName('key');
|
|
1885
|
+
const valNode = child.childForFieldName('value');
|
|
1886
|
+
if (!keyNode || !valNode)
|
|
1887
|
+
continue;
|
|
1888
|
+
const keyName = keyNode.type === 'string' ? keyNode.text.replace(/^['"]|['"]$/g, '') : keyNode.text;
|
|
1889
|
+
if (!keyName)
|
|
1890
|
+
continue;
|
|
1891
|
+
const qualifiedKey = `${nameN.text}.${keyName}`;
|
|
1892
|
+
if (valNode.type === 'arrow_function' ||
|
|
1893
|
+
valNode.type === 'function_expression' ||
|
|
1894
|
+
valNode.type === 'function') {
|
|
1895
|
+
// Store the qualified name so the resolver finds the qualified definition.
|
|
1896
|
+
setTypeMapEntry(typeMap, qualifiedKey, qualifiedKey, 0.85);
|
|
1897
|
+
}
|
|
1898
|
+
else if (valNode.type === 'identifier') {
|
|
1899
|
+
setTypeMapEntry(typeMap, qualifiedKey, valNode.text, 0.85);
|
|
1900
|
+
}
|
|
1901
|
+
}
|
|
1902
|
+
else if (child.type === 'method_definition') {
|
|
1903
|
+
// Method shorthand: `const obj = { baz() {} }` → typeMap['obj.baz'] = 'obj.baz'
|
|
1904
|
+
// extractObjectLiteralFunctions registers a definition under the qualified name;
|
|
1905
|
+
// seed the matching typeMap entry so the two-step accessor dispatch finds it.
|
|
1906
|
+
const nameNode = child.childForFieldName('name');
|
|
1907
|
+
if (!nameNode)
|
|
1908
|
+
continue;
|
|
1909
|
+
const qualifiedKey = `${nameN.text}.${nameNode.text}`;
|
|
1910
|
+
setTypeMapEntry(typeMap, qualifiedKey, qualifiedKey, 0.85);
|
|
1911
|
+
}
|
|
1912
|
+
}
|
|
1913
|
+
}
|
|
1099
1914
|
}
|
|
1100
1915
|
/** Extract type info from a required_parameter or optional_parameter. */
|
|
1101
1916
|
function handleParamTypeMap(node, typeMap) {
|
|
1102
1917
|
const nameNode = node.childForFieldName('pattern') || node.childForFieldName('left') || node.child(0);
|
|
1103
|
-
if (
|
|
1918
|
+
if (nameNode?.type !== 'identifier')
|
|
1104
1919
|
return;
|
|
1105
1920
|
const typeAnno = findChild(node, 'type_annotation');
|
|
1106
1921
|
if (typeAnno) {
|
|
@@ -1109,12 +1924,527 @@ function handleParamTypeMap(node, typeMap) {
|
|
|
1109
1924
|
setTypeMapEntry(typeMap, nameNode.text, typeName, 0.9);
|
|
1110
1925
|
}
|
|
1111
1926
|
}
|
|
1927
|
+
/**
|
|
1928
|
+
* Phase 8.3d: seed the pts map from object property writes.
|
|
1929
|
+
*
|
|
1930
|
+
* `handlers.auth = authMiddleware` → typeMap.set('handlers.auth', { type: 'authMiddleware', confidence: 0.85 })
|
|
1931
|
+
* `this.logger = new Logger(...)` → typeMap.set('UserService.logger', { type: 'Logger', confidence: 1.0 })
|
|
1932
|
+
* (keyed as ClassName.prop when currentClass is known, to avoid collisions across classes)
|
|
1933
|
+
*
|
|
1934
|
+
* Only simple `obj.prop = identifier` and `this.prop = new Ctor()` writes are tracked
|
|
1935
|
+
* (not chained `a.b.c = x`). BUILTIN_GLOBALS are skipped (e.g. `console.log = fn`).
|
|
1936
|
+
*/
|
|
1937
|
+
function handlePropWriteTypeMap(node, typeMap, currentClass) {
|
|
1938
|
+
const lhsN = node.childForFieldName('left');
|
|
1939
|
+
const rhsN = node.childForFieldName('right');
|
|
1940
|
+
if (!lhsN || !rhsN)
|
|
1941
|
+
return;
|
|
1942
|
+
if (lhsN.type !== 'member_expression')
|
|
1943
|
+
return;
|
|
1944
|
+
const obj = lhsN.childForFieldName('object');
|
|
1945
|
+
const prop = lhsN.childForFieldName('property');
|
|
1946
|
+
if (!obj || !prop)
|
|
1947
|
+
return;
|
|
1948
|
+
// Guard: only static property access (property_identifier or identifier), not
|
|
1949
|
+
// computed subscript expressions — consistent with the adjacent fnRefBindings block.
|
|
1950
|
+
if (prop.type !== 'property_identifier' && prop.type !== 'identifier')
|
|
1951
|
+
return;
|
|
1952
|
+
// this.prop = new ClassName(...) — constructor-assigned property type.
|
|
1953
|
+
// Key as ClassName.prop (class-scoped) so two classes with identically-named
|
|
1954
|
+
// properties don't overwrite each other's typeMap entry.
|
|
1955
|
+
if (obj.type === 'this' && rhsN.type === 'new_expression') {
|
|
1956
|
+
const ctorType = extractNewExprTypeName(rhsN);
|
|
1957
|
+
if (ctorType) {
|
|
1958
|
+
const key = currentClass ? `${currentClass}.${prop.text}` : `this.${prop.text}`;
|
|
1959
|
+
setTypeMapEntry(typeMap, key, ctorType, 1.0);
|
|
1960
|
+
}
|
|
1961
|
+
return;
|
|
1962
|
+
}
|
|
1963
|
+
// obj.prop = identifier — existing behaviour (skip chained a.b.c = x and builtins)
|
|
1964
|
+
if (rhsN.type !== 'identifier')
|
|
1965
|
+
return;
|
|
1966
|
+
if (obj.type !== 'identifier')
|
|
1967
|
+
return;
|
|
1968
|
+
const objName = obj.text;
|
|
1969
|
+
if (BUILTIN_GLOBALS.has(objName))
|
|
1970
|
+
return;
|
|
1971
|
+
setTypeMapEntry(typeMap, `${objName}.${prop.text}`, rhsN.text, 0.85);
|
|
1972
|
+
}
|
|
1973
|
+
/**
|
|
1974
|
+
* Phase 8.3e/8.3f: seed composite pts keys from Object.defineProperty / defineProperties.
|
|
1975
|
+
*
|
|
1976
|
+
* `Object.defineProperty(obj, "key", { value: fn })` → typeMap.set('obj.key', fn, 0.85)
|
|
1977
|
+
* `Object.defineProperties(obj, { "k1": { value: v1 } })` → typeMap.set('obj.k1', v1, 0.85)
|
|
1978
|
+
* `Object.defineProperty(obj, "key", { get: getter })` → typeMap.set('getter:this', obj, 0.85)
|
|
1979
|
+
*/
|
|
1980
|
+
function handleDefinePropertyTypeMap(node, typeMap) {
|
|
1981
|
+
const fn = node.childForFieldName('function');
|
|
1982
|
+
if (fn?.type !== 'member_expression')
|
|
1983
|
+
return;
|
|
1984
|
+
const fnObj = fn.childForFieldName('object');
|
|
1985
|
+
const fnProp = fn.childForFieldName('property');
|
|
1986
|
+
if (fnObj?.text !== 'Object')
|
|
1987
|
+
return;
|
|
1988
|
+
const method = fnProp?.text;
|
|
1989
|
+
if (method !== 'defineProperty' && method !== 'defineProperties')
|
|
1990
|
+
return;
|
|
1991
|
+
const argsNode = node.childForFieldName('arguments') || findChild(node, 'arguments');
|
|
1992
|
+
if (!argsNode)
|
|
1993
|
+
return;
|
|
1994
|
+
const args = [];
|
|
1995
|
+
for (let i = 0; i < argsNode.childCount; i++) {
|
|
1996
|
+
const n = argsNode.child(i);
|
|
1997
|
+
if (n && n.type !== '(' && n.type !== ')' && n.type !== ',')
|
|
1998
|
+
args.push(n);
|
|
1999
|
+
}
|
|
2000
|
+
if (method === 'defineProperty') {
|
|
2001
|
+
if (args.length < 3)
|
|
2002
|
+
return;
|
|
2003
|
+
const arg0 = args[0], arg1 = args[1], arg2 = args[2];
|
|
2004
|
+
if (arg0.type !== 'identifier')
|
|
2005
|
+
return;
|
|
2006
|
+
if (arg1.type !== 'string')
|
|
2007
|
+
return;
|
|
2008
|
+
const key = arg1.text.replace(/^['"]|['"]$/g, '');
|
|
2009
|
+
if (!key)
|
|
2010
|
+
return;
|
|
2011
|
+
// Phase 8.3e: { value: fn } → obj.key pts to fn
|
|
2012
|
+
const target = findDescriptorValue(arg2);
|
|
2013
|
+
if (target) {
|
|
2014
|
+
setTypeMapEntry(typeMap, `${arg0.text}.${key}`, target, 0.85);
|
|
2015
|
+
}
|
|
2016
|
+
// Phase 8.3f: { get: getter } and/or { set: setter } → this inside each accessor is arg0 (obj)
|
|
2017
|
+
// Key format: '<accessorName>:this' — colon is a reserved separator used only by this phase.
|
|
2018
|
+
// JS identifiers cannot contain ':', so this key never collides with real variable names.
|
|
2019
|
+
for (const accessor of findDescriptorAccessors(arg2)) {
|
|
2020
|
+
setTypeMapEntry(typeMap, `${accessor}:this`, arg0.text, 0.85);
|
|
2021
|
+
}
|
|
2022
|
+
}
|
|
2023
|
+
else {
|
|
2024
|
+
// defineProperties
|
|
2025
|
+
if (args.length < 2)
|
|
2026
|
+
return;
|
|
2027
|
+
const arg0 = args[0], arg1 = args[1];
|
|
2028
|
+
if (arg0.type !== 'identifier')
|
|
2029
|
+
return;
|
|
2030
|
+
if (arg1.type !== 'object')
|
|
2031
|
+
return;
|
|
2032
|
+
for (let i = 0; i < arg1.childCount; i++) {
|
|
2033
|
+
const pair = arg1.child(i);
|
|
2034
|
+
if (pair?.type !== 'pair')
|
|
2035
|
+
continue;
|
|
2036
|
+
const keyN = pair.childForFieldName('key');
|
|
2037
|
+
const valN = pair.childForFieldName('value');
|
|
2038
|
+
if (!keyN || !valN)
|
|
2039
|
+
continue;
|
|
2040
|
+
const key = keyN.type === 'string' ? keyN.text.replace(/^['"]|['"]$/g, '') : keyN.text;
|
|
2041
|
+
const target = findDescriptorValue(valN);
|
|
2042
|
+
if (!target)
|
|
2043
|
+
continue;
|
|
2044
|
+
setTypeMapEntry(typeMap, `${arg0.text}.${key}`, target, 0.85);
|
|
2045
|
+
}
|
|
2046
|
+
}
|
|
2047
|
+
}
|
|
2048
|
+
/** Return the identifier text of the `value` field in a property descriptor object. */
|
|
2049
|
+
function findDescriptorValue(desc) {
|
|
2050
|
+
if (desc.type !== 'object')
|
|
2051
|
+
return undefined;
|
|
2052
|
+
for (let i = 0; i < desc.childCount; i++) {
|
|
2053
|
+
const pair = desc.child(i);
|
|
2054
|
+
if (pair?.type !== 'pair')
|
|
2055
|
+
continue;
|
|
2056
|
+
const key = pair.childForFieldName('key');
|
|
2057
|
+
const val = pair.childForFieldName('value');
|
|
2058
|
+
if (key?.text === 'value' && val?.type === 'identifier')
|
|
2059
|
+
return val.text;
|
|
2060
|
+
}
|
|
2061
|
+
return undefined;
|
|
2062
|
+
}
|
|
2063
|
+
/**
|
|
2064
|
+
* Phase 8.3f: return the identifier texts of all `get` and `set` accessors in a property
|
|
2065
|
+
* descriptor. `{ get: getter, set: setter }` → ['getter', 'setter'].
|
|
2066
|
+
* Returns all accessors so that each one gets a `callerName:this = obj` typeMap entry.
|
|
2067
|
+
*/
|
|
2068
|
+
function findDescriptorAccessors(desc) {
|
|
2069
|
+
if (desc.type !== 'object')
|
|
2070
|
+
return [];
|
|
2071
|
+
const result = [];
|
|
2072
|
+
for (let i = 0; i < desc.childCount; i++) {
|
|
2073
|
+
const pair = desc.child(i);
|
|
2074
|
+
if (pair?.type !== 'pair')
|
|
2075
|
+
continue;
|
|
2076
|
+
const key = pair.childForFieldName('key');
|
|
2077
|
+
const val = pair.childForFieldName('value');
|
|
2078
|
+
if ((key?.text === 'get' || key?.text === 'set') && val?.type === 'identifier') {
|
|
2079
|
+
result.push(val.text);
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
2082
|
+
return result;
|
|
2083
|
+
}
|
|
2084
|
+
/** Seed composite pts keys for each property in a prototype object literal. */
|
|
2085
|
+
function seedProtoProperties(varName, proto, typeMap) {
|
|
2086
|
+
for (let i = 0; i < proto.childCount; i++) {
|
|
2087
|
+
const child = proto.child(i);
|
|
2088
|
+
if (!child)
|
|
2089
|
+
continue;
|
|
2090
|
+
if (child.type === 'shorthand_property_identifier') {
|
|
2091
|
+
setTypeMapEntry(typeMap, `${varName}.${child.text}`, child.text, 0.85);
|
|
2092
|
+
}
|
|
2093
|
+
else if (child.type === 'pair') {
|
|
2094
|
+
const keyN = child.childForFieldName('key');
|
|
2095
|
+
const valN = child.childForFieldName('value');
|
|
2096
|
+
if (!keyN || !valN || valN.type !== 'identifier')
|
|
2097
|
+
continue;
|
|
2098
|
+
const key = keyN.type === 'string' ? keyN.text.replace(/^['"]|['"]$/g, '') : keyN.text;
|
|
2099
|
+
setTypeMapEntry(typeMap, `${varName}.${key}`, valN.text, 0.85);
|
|
2100
|
+
}
|
|
2101
|
+
}
|
|
2102
|
+
}
|
|
2103
|
+
/**
|
|
2104
|
+
* Phase 8.3c: record argument-to-parameter bindings at call sites.
|
|
2105
|
+
*
|
|
2106
|
+
* For each `f(x, y)` where the callee is a simple identifier and an argument
|
|
2107
|
+
* is a simple identifier, emits a ParamBinding so the pts solver can add
|
|
2108
|
+
* constraint: pts(param_i_of_f) ⊇ pts(arg_i). The solver uses the
|
|
2109
|
+
* definitionParams map to resolve the actual parameter names.
|
|
2110
|
+
*
|
|
2111
|
+
* Scope: intra-module only (the solver only materialises constraints for
|
|
2112
|
+
* locally-defined callees, so cross-module calls produce no spurious flow).
|
|
2113
|
+
*/
|
|
2114
|
+
function collectParamBindings(node, paramBindings) {
|
|
2115
|
+
const fn = node.childForFieldName('function');
|
|
2116
|
+
const args = node.childForFieldName('arguments') ?? findChild(node, 'arguments');
|
|
2117
|
+
if (fn?.type === 'identifier' && !BUILTIN_GLOBALS.has(fn.text) && args) {
|
|
2118
|
+
let argIdx = 0;
|
|
2119
|
+
for (let i = 0; i < args.childCount; i++) {
|
|
2120
|
+
const child = args.child(i);
|
|
2121
|
+
if (!child)
|
|
2122
|
+
continue;
|
|
2123
|
+
const ct = child.type;
|
|
2124
|
+
if (ct === ',' || ct === '(' || ct === ')')
|
|
2125
|
+
continue;
|
|
2126
|
+
if (ct === 'identifier' && !BUILTIN_GLOBALS.has(child.text)) {
|
|
2127
|
+
paramBindings.push({ callee: fn.text, argIndex: argIdx, argName: child.text });
|
|
2128
|
+
}
|
|
2129
|
+
else if (ct === 'spread_element') {
|
|
2130
|
+
// f(...[a, b]) — inline array literal: expand each element as a direct param binding.
|
|
2131
|
+
const inner = child.childForFieldName('argument') ?? (child.childCount > 1 ? child.child(1) : null);
|
|
2132
|
+
if (inner?.type === 'array') {
|
|
2133
|
+
let elemCount = 0;
|
|
2134
|
+
for (let j = 0; j < inner.childCount; j++) {
|
|
2135
|
+
const elem = inner.child(j);
|
|
2136
|
+
if (!elem)
|
|
2137
|
+
continue;
|
|
2138
|
+
if (elem.type === ',' || elem.type === '[' || elem.type === ']')
|
|
2139
|
+
continue;
|
|
2140
|
+
if (elem.type === 'identifier' && !BUILTIN_GLOBALS.has(elem.text)) {
|
|
2141
|
+
paramBindings.push({
|
|
2142
|
+
callee: fn.text,
|
|
2143
|
+
argIndex: argIdx + elemCount,
|
|
2144
|
+
argName: elem.text,
|
|
2145
|
+
});
|
|
2146
|
+
}
|
|
2147
|
+
elemCount++;
|
|
2148
|
+
}
|
|
2149
|
+
// Advance by the exact number of slots this spread occupies and skip
|
|
2150
|
+
// the unconditional argIdx++ below so that zero-element spreads (...[])
|
|
2151
|
+
// do not shift subsequent argument indices.
|
|
2152
|
+
argIdx += elemCount;
|
|
2153
|
+
continue;
|
|
2154
|
+
}
|
|
2155
|
+
}
|
|
2156
|
+
argIdx++;
|
|
2157
|
+
}
|
|
2158
|
+
}
|
|
2159
|
+
}
|
|
2160
|
+
/** Collection constructors whose argument is treated as an element source. */
|
|
2161
|
+
const COLLECTION_CTOR_SET = new Set(['Set', 'Map']);
|
|
2162
|
+
/**
|
|
2163
|
+
* Phase 8.3e: Extract array-element bindings from `const arr = [fn1, fn2]` patterns.
|
|
2164
|
+
* Emits an ArrayElemBinding for each identifier element in an array literal assigned
|
|
2165
|
+
* to a variable.
|
|
2166
|
+
*/
|
|
2167
|
+
function collectArrayElemBindings(node, arrayElemBindings) {
|
|
2168
|
+
const nameN = node.childForFieldName('name');
|
|
2169
|
+
const valueN = node.childForFieldName('value');
|
|
2170
|
+
if (nameN?.type === 'identifier' && valueN?.type === 'array') {
|
|
2171
|
+
let idx = 0;
|
|
2172
|
+
for (let i = 0; i < valueN.childCount; i++) {
|
|
2173
|
+
const elem = valueN.child(i);
|
|
2174
|
+
if (!elem)
|
|
2175
|
+
continue;
|
|
2176
|
+
if (elem.type === ',' || elem.type === '[' || elem.type === ']')
|
|
2177
|
+
continue;
|
|
2178
|
+
if (elem.type === 'identifier' && !BUILTIN_GLOBALS.has(elem.text)) {
|
|
2179
|
+
arrayElemBindings.push({ arrayName: nameN.text, index: idx, elemName: elem.text });
|
|
2180
|
+
}
|
|
2181
|
+
idx++;
|
|
2182
|
+
}
|
|
2183
|
+
}
|
|
2184
|
+
}
|
|
2185
|
+
/**
|
|
2186
|
+
* Phase 8.3e collectors (spread-argument, Array.from, collection-wrap, for-of
|
|
2187
|
+
* bindings), invoked from runContextCollectorWalk:
|
|
2188
|
+
*
|
|
2189
|
+
* - Spread: `f(...arr)` → SpreadArgBinding
|
|
2190
|
+
* - Array.from: `Array.from(src, cb)` → ArrayCallbackBinding
|
|
2191
|
+
* - Collection wrap: `new Set(arr)` / `new Map(arr)` → FnRefBinding lhs=s[*] rhs=arr[*]
|
|
2192
|
+
* - For-of: `for (const x of arr)` → ForOfBinding
|
|
2193
|
+
*/
|
|
2194
|
+
function collectSpreadAndArrayFromBindings(node, spreadArgBindings, arrayCallbackBindings) {
|
|
2195
|
+
const fn = node.childForFieldName('function');
|
|
2196
|
+
const argsNode = node.childForFieldName('arguments') ?? findChild(node, 'arguments');
|
|
2197
|
+
// Spread: f(...arr)
|
|
2198
|
+
if (fn?.type === 'identifier' && !BUILTIN_GLOBALS.has(fn.text) && argsNode) {
|
|
2199
|
+
let argIdx = 0;
|
|
2200
|
+
for (let i = 0; i < argsNode.childCount; i++) {
|
|
2201
|
+
const child = argsNode.child(i);
|
|
2202
|
+
if (!child)
|
|
2203
|
+
continue;
|
|
2204
|
+
if (child.type === ',' || child.type === '(' || child.type === ')')
|
|
2205
|
+
continue;
|
|
2206
|
+
if (child.type === 'spread_element') {
|
|
2207
|
+
const spreadTarget = child.childForFieldName('argument') ?? (child.childCount > 1 ? child.child(1) : null);
|
|
2208
|
+
if (spreadTarget?.type === 'identifier' && !BUILTIN_GLOBALS.has(spreadTarget.text)) {
|
|
2209
|
+
spreadArgBindings.push({
|
|
2210
|
+
callee: fn.text,
|
|
2211
|
+
arrayName: spreadTarget.text,
|
|
2212
|
+
startIndex: argIdx,
|
|
2213
|
+
});
|
|
2214
|
+
}
|
|
2215
|
+
}
|
|
2216
|
+
argIdx++;
|
|
2217
|
+
}
|
|
2218
|
+
}
|
|
2219
|
+
// Array.from(source, cb)
|
|
2220
|
+
if (fn?.type === 'member_expression' && argsNode) {
|
|
2221
|
+
const obj = fn.childForFieldName('object');
|
|
2222
|
+
const prop = fn.childForFieldName('property');
|
|
2223
|
+
if (obj?.text === 'Array' && prop?.text === 'from') {
|
|
2224
|
+
const fnArgs = [];
|
|
2225
|
+
for (let i = 0; i < argsNode.childCount; i++) {
|
|
2226
|
+
const child = argsNode.child(i);
|
|
2227
|
+
if (!child)
|
|
2228
|
+
continue;
|
|
2229
|
+
if (child.type === ',' || child.type === '(' || child.type === ')')
|
|
2230
|
+
continue;
|
|
2231
|
+
fnArgs.push(child);
|
|
2232
|
+
}
|
|
2233
|
+
if (fnArgs.length >= 2) {
|
|
2234
|
+
const srcArg = fnArgs[0];
|
|
2235
|
+
const cbArg = fnArgs[1];
|
|
2236
|
+
if (srcArg.type === 'identifier' &&
|
|
2237
|
+
!BUILTIN_GLOBALS.has(srcArg.text) &&
|
|
2238
|
+
cbArg.type === 'identifier' &&
|
|
2239
|
+
!BUILTIN_GLOBALS.has(cbArg.text)) {
|
|
2240
|
+
arrayCallbackBindings.push({ sourceName: srcArg.text, calleeName: cbArg.text });
|
|
2241
|
+
}
|
|
2242
|
+
}
|
|
2243
|
+
}
|
|
2244
|
+
}
|
|
2245
|
+
}
|
|
2246
|
+
/** Collection wrap: `const s = new Set(arr)` or `new Map(arr)` (variable_declarator). */
|
|
2247
|
+
function collectCollectionWrapBinding(node, fnRefBindings) {
|
|
2248
|
+
const nameN = node.childForFieldName('name');
|
|
2249
|
+
const valueN = node.childForFieldName('value');
|
|
2250
|
+
if (nameN?.type === 'identifier' && valueN?.type === 'new_expression') {
|
|
2251
|
+
const ctor = valueN.childForFieldName('constructor');
|
|
2252
|
+
const args = valueN.childForFieldName('arguments');
|
|
2253
|
+
if (ctor && COLLECTION_CTOR_SET.has(ctor.text) && args) {
|
|
2254
|
+
for (let i = 0; i < args.childCount; i++) {
|
|
2255
|
+
const arg = args.child(i);
|
|
2256
|
+
if (!arg || arg.type === '(' || arg.type === ')')
|
|
2257
|
+
continue;
|
|
2258
|
+
if (arg.type === 'identifier' && !BUILTIN_GLOBALS.has(arg.text)) {
|
|
2259
|
+
fnRefBindings.push({ lhs: `${nameN.text}[*]`, rhs: `${arg.text}[*]` });
|
|
2260
|
+
break;
|
|
2261
|
+
}
|
|
2262
|
+
}
|
|
2263
|
+
}
|
|
2264
|
+
}
|
|
2265
|
+
}
|
|
2266
|
+
/** For-of: `for (const x of arr)` (for_in_statement with an `of` keyword). */
|
|
2267
|
+
function collectForOfBinding(node, enclosingFunc, forOfBindings) {
|
|
2268
|
+
let isForOf = false;
|
|
2269
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
2270
|
+
if (node.child(i)?.text === 'of') {
|
|
2271
|
+
isForOf = true;
|
|
2272
|
+
break;
|
|
2273
|
+
}
|
|
2274
|
+
}
|
|
2275
|
+
if (!isForOf)
|
|
2276
|
+
return;
|
|
2277
|
+
const right = node.childForFieldName('right');
|
|
2278
|
+
if (right?.type !== 'identifier' || BUILTIN_GLOBALS.has(right.text))
|
|
2279
|
+
return;
|
|
2280
|
+
const left = node.childForFieldName('left');
|
|
2281
|
+
let varName = null;
|
|
2282
|
+
if (left?.type === 'identifier') {
|
|
2283
|
+
varName = left.text;
|
|
2284
|
+
}
|
|
2285
|
+
else if (left) {
|
|
2286
|
+
for (let i = 0; i < left.childCount; i++) {
|
|
2287
|
+
const lc = left.child(i);
|
|
2288
|
+
if (lc?.type === 'variable_declarator') {
|
|
2289
|
+
const nc = lc.childForFieldName('name');
|
|
2290
|
+
if (nc?.type === 'identifier') {
|
|
2291
|
+
varName = nc.text;
|
|
2292
|
+
break;
|
|
2293
|
+
}
|
|
2294
|
+
}
|
|
2295
|
+
else if (lc?.type === 'identifier' &&
|
|
2296
|
+
lc.text !== 'const' &&
|
|
2297
|
+
lc.text !== 'let' &&
|
|
2298
|
+
lc.text !== 'var') {
|
|
2299
|
+
varName = lc.text;
|
|
2300
|
+
break;
|
|
2301
|
+
}
|
|
2302
|
+
}
|
|
2303
|
+
}
|
|
2304
|
+
if (varName && !BUILTIN_GLOBALS.has(varName)) {
|
|
2305
|
+
forOfBindings.push({ varName, sourceName: right.text, enclosingFunc });
|
|
2306
|
+
}
|
|
2307
|
+
}
|
|
2308
|
+
/**
|
|
2309
|
+
* Phase 8.3f: record object-destructuring rest-parameter bindings from function definitions.
|
|
2310
|
+
*
|
|
2311
|
+
* For each `function f({ a, ...rest })` (or arrow/function-expression equivalent),
|
|
2312
|
+
* records { callee: 'f', restName: 'rest', argIndex: N }. Also covers class methods
|
|
2313
|
+
* (`callee: 'ClassName.method'`) and object-literal methods (`callee: 'method'`).
|
|
2314
|
+
* The edge builder uses these to seed typeMap[rest] = { type: argName } when f(obj)
|
|
2315
|
+
* is called with an identifier, enabling `rest.method()` calls to resolve.
|
|
2316
|
+
*/
|
|
2317
|
+
function collectObjectRestParams(node, t, currentClass, bindings) {
|
|
2318
|
+
let fnName = null;
|
|
2319
|
+
let paramsNode = null;
|
|
2320
|
+
if (t === 'function_declaration' || t === 'generator_function_declaration') {
|
|
2321
|
+
const nameN = node.childForFieldName('name');
|
|
2322
|
+
if (nameN?.type === 'identifier')
|
|
2323
|
+
fnName = nameN.text;
|
|
2324
|
+
paramsNode = node.childForFieldName('parameters') ?? findChild(node, 'formal_parameters');
|
|
2325
|
+
}
|
|
2326
|
+
else if (t === 'variable_declarator') {
|
|
2327
|
+
const nameN = node.childForFieldName('name');
|
|
2328
|
+
const valueN = node.childForFieldName('value');
|
|
2329
|
+
if (nameN?.type === 'identifier' && valueN) {
|
|
2330
|
+
const vt = valueN.type;
|
|
2331
|
+
if (vt === 'arrow_function' || vt === 'function_expression' || vt === 'generator_function') {
|
|
2332
|
+
fnName = nameN.text;
|
|
2333
|
+
paramsNode =
|
|
2334
|
+
valueN.childForFieldName('parameters') ?? findChild(valueN, 'formal_parameters');
|
|
2335
|
+
}
|
|
2336
|
+
}
|
|
2337
|
+
}
|
|
2338
|
+
else if (t === 'method_definition') {
|
|
2339
|
+
// class method: `class Foo { bar({ a, ...rest }) {} }`
|
|
2340
|
+
// object-literal shorthand method: `{ bar({ a, ...rest }) {} }`
|
|
2341
|
+
const nameN = node.childForFieldName('name');
|
|
2342
|
+
if (nameN) {
|
|
2343
|
+
fnName = currentClass ? `${currentClass}.${nameN.text}` : nameN.text;
|
|
2344
|
+
paramsNode = node.childForFieldName('parameters') ?? findChild(node, 'formal_parameters');
|
|
2345
|
+
}
|
|
2346
|
+
}
|
|
2347
|
+
else if (t === 'pair') {
|
|
2348
|
+
// object-literal method: `{ bar: function({ a, ...rest }) {} }`
|
|
2349
|
+
// Skip computed property keys (e.g. `{ [Symbol.iterator]: function({ ...rest }) {} }`)
|
|
2350
|
+
// because `callee: '[Symbol.iterator]'` can never match a paramBinding callee.
|
|
2351
|
+
const keyN = node.childForFieldName('key');
|
|
2352
|
+
const valueN = node.childForFieldName('value');
|
|
2353
|
+
if (keyN && valueN && keyN.type !== 'computed_property_name') {
|
|
2354
|
+
const vt = valueN.type;
|
|
2355
|
+
if (vt === 'arrow_function' || vt === 'function_expression' || vt === 'generator_function') {
|
|
2356
|
+
fnName = keyN.type === 'string' ? keyN.text.slice(1, -1) : keyN.text;
|
|
2357
|
+
paramsNode =
|
|
2358
|
+
valueN.childForFieldName('parameters') ?? findChild(valueN, 'formal_parameters');
|
|
2359
|
+
}
|
|
2360
|
+
}
|
|
2361
|
+
}
|
|
2362
|
+
if (fnName && paramsNode) {
|
|
2363
|
+
let paramIdx = 0;
|
|
2364
|
+
for (let i = 0; i < paramsNode.childCount; i++) {
|
|
2365
|
+
const child = paramsNode.child(i);
|
|
2366
|
+
if (!child)
|
|
2367
|
+
continue;
|
|
2368
|
+
const ct = child.type;
|
|
2369
|
+
if (ct === ',' || ct === '(' || ct === ')')
|
|
2370
|
+
continue;
|
|
2371
|
+
if (ct === 'object_pattern') {
|
|
2372
|
+
for (let j = 0; j < child.childCount; j++) {
|
|
2373
|
+
const inner = child.child(j);
|
|
2374
|
+
if (!inner)
|
|
2375
|
+
continue;
|
|
2376
|
+
if (inner.type === 'rest_pattern' || inner.type === 'rest_element') {
|
|
2377
|
+
// rest_pattern node: `...identifier` — the identifier is at child index 1
|
|
2378
|
+
const restId = inner.child(1) ?? inner.childForFieldName('name');
|
|
2379
|
+
if (restId?.type === 'identifier') {
|
|
2380
|
+
bindings.push({ callee: fnName, restName: restId.text, argIndex: paramIdx });
|
|
2381
|
+
}
|
|
2382
|
+
}
|
|
2383
|
+
}
|
|
2384
|
+
}
|
|
2385
|
+
paramIdx++;
|
|
2386
|
+
}
|
|
2387
|
+
}
|
|
2388
|
+
}
|
|
2389
|
+
/**
|
|
2390
|
+
* Phase 8.3f: collect object-property bindings from object literals.
|
|
2391
|
+
*
|
|
2392
|
+
* `const obj = { e4 }` → `{ objectName: "obj", propName: "e4", valueName: "e4" }`
|
|
2393
|
+
* `const obj = { e1: fn }` → `{ objectName: "obj", propName: "e1", valueName: "fn" }`
|
|
2394
|
+
*
|
|
2395
|
+
* Only tracks shorthand and `key: identifier` pairs; skips function literals.
|
|
2396
|
+
*/
|
|
2397
|
+
function collectObjectPropBindings(node, bindings) {
|
|
2398
|
+
const nameN = node.childForFieldName('name');
|
|
2399
|
+
const valueN = node.childForFieldName('value');
|
|
2400
|
+
if (nameN?.type === 'identifier' && valueN?.type === 'object') {
|
|
2401
|
+
const objectName = nameN.text;
|
|
2402
|
+
for (let i = 0; i < valueN.childCount; i++) {
|
|
2403
|
+
const child = valueN.child(i);
|
|
2404
|
+
if (!child)
|
|
2405
|
+
continue;
|
|
2406
|
+
if (child.type === 'shorthand_property_identifier') {
|
|
2407
|
+
bindings.push({ objectName, propName: child.text, valueName: child.text });
|
|
2408
|
+
}
|
|
2409
|
+
else if (child.type === 'pair') {
|
|
2410
|
+
const keyN = child.childForFieldName('key');
|
|
2411
|
+
const valN = child.childForFieldName('value');
|
|
2412
|
+
if (keyN?.type === 'property_identifier' &&
|
|
2413
|
+
valN?.type === 'identifier' &&
|
|
2414
|
+
!BUILTIN_GLOBALS.has(valN.text)) {
|
|
2415
|
+
bindings.push({ objectName, propName: keyN.text, valueName: valN.text });
|
|
2416
|
+
}
|
|
2417
|
+
}
|
|
2418
|
+
}
|
|
2419
|
+
}
|
|
2420
|
+
}
|
|
1112
2421
|
function extractReceiverName(objNode) {
|
|
1113
2422
|
if (!objNode)
|
|
1114
2423
|
return undefined;
|
|
1115
2424
|
const t = objNode.type;
|
|
1116
2425
|
if (t === 'identifier' || t === 'this' || t === 'super')
|
|
1117
2426
|
return objNode.text;
|
|
2427
|
+
// `(new Foo(...)).method()` — extract the constructor name so the resolver can
|
|
2428
|
+
// look up `Foo.method` directly without relying on a text-based regex heuristic.
|
|
2429
|
+
if (t === 'new_expression') {
|
|
2430
|
+
const name = extractNewExprTypeName(objNode);
|
|
2431
|
+
if (name)
|
|
2432
|
+
return name;
|
|
2433
|
+
}
|
|
2434
|
+
if (t === 'parenthesized_expression') {
|
|
2435
|
+
// Only one level of parentheses is unwrapped here. Doubly-nested parens
|
|
2436
|
+
// (e.g. `((new Dog())).bark()`) and cast expressions inside parens
|
|
2437
|
+
// (e.g. `(new Dog() as Animal).bark()`) fall through to raw-text handling
|
|
2438
|
+
// below and are caught by the regex fallback in call-resolver.ts.
|
|
2439
|
+
for (let i = 0; i < objNode.childCount; i++) {
|
|
2440
|
+
const child = objNode.child(i);
|
|
2441
|
+
if (child?.type === 'new_expression') {
|
|
2442
|
+
const name = extractNewExprTypeName(child);
|
|
2443
|
+
if (name)
|
|
2444
|
+
return name;
|
|
2445
|
+
}
|
|
2446
|
+
}
|
|
2447
|
+
}
|
|
1118
2448
|
return objNode.text;
|
|
1119
2449
|
}
|
|
1120
2450
|
function extractCallInfo(fn, callNode) {
|
|
@@ -1320,12 +2650,23 @@ function firstArgIsStringLiteral(argsNode) {
|
|
|
1320
2650
|
* member-expr args are only emitted when the first argument is a string
|
|
1321
2651
|
* literal route path — matching Express/router shape and skipping
|
|
1322
2652
|
* `cache.get(user.id)`-style calls.
|
|
2653
|
+
*
|
|
2654
|
+
* `.call()` / `.apply()` / `.bind()` — the first arg is the `this` context (not a callback of
|
|
2655
|
+
* the enclosing function) and subsequent args flow into the delegated function's parameters.
|
|
2656
|
+
* Emitting them here would produce false-positive edges from the *calling* function.
|
|
2657
|
+
* This-rebinding (fn::this → ctx) is handled separately by extractThisCallBindingsWalk.
|
|
1323
2658
|
*/
|
|
1324
2659
|
function extractCallbackReferenceCalls(callNode) {
|
|
1325
2660
|
const args = callNode.childForFieldName('arguments') || findChild(callNode, 'arguments');
|
|
1326
2661
|
if (!args)
|
|
1327
2662
|
return [];
|
|
1328
2663
|
const calleeName = extractCalleeName(callNode);
|
|
2664
|
+
// .call() / .apply() / .bind() — the first arg is the `this` context (not a callback of
|
|
2665
|
+
// the enclosing function) and subsequent args flow into the delegated function's parameters.
|
|
2666
|
+
// Emitting them here would produce false-positive edges from the *calling* function.
|
|
2667
|
+
// This-rebinding (fn::this → ctx) is handled separately by extractThisCallBindingsWalk.
|
|
2668
|
+
if (calleeName === 'call' || calleeName === 'apply' || calleeName === 'bind')
|
|
2669
|
+
return [];
|
|
1329
2670
|
let memberExprArgsAllowed = calleeName !== null && CALLBACK_ACCEPTING_CALLEES.has(calleeName);
|
|
1330
2671
|
if (memberExprArgsAllowed && calleeName !== null && HTTP_VERB_CALLEES.has(calleeName)) {
|
|
1331
2672
|
// HTTP verbs require a string-literal route path to be treated as a
|
|
@@ -1353,6 +2694,126 @@ function extractCallbackReferenceCalls(callNode) {
|
|
|
1353
2694
|
}
|
|
1354
2695
|
return result;
|
|
1355
2696
|
}
|
|
2697
|
+
/**
|
|
2698
|
+
* Collect, from a call_expression node:
|
|
2699
|
+
* - `this(args)` call expressions → `{name: 'this', ...}` entries in `calls`
|
|
2700
|
+
* (where `this` is used as a function, not as a receiver)
|
|
2701
|
+
* - `fn.call(namedCtx, ...)` / `fn.apply(namedCtx, ...)` bindings →
|
|
2702
|
+
* `{ callee: 'fn', thisArg: 'namedCtx' }` entries in `thisCallBindings`
|
|
2703
|
+
*/
|
|
2704
|
+
function collectThisCallAndBindings(node, calls, thisCallBindings) {
|
|
2705
|
+
const fn = node.childForFieldName('function');
|
|
2706
|
+
if (fn?.type === 'this') {
|
|
2707
|
+
calls.push({ name: 'this', line: nodeStartLine(node) });
|
|
2708
|
+
}
|
|
2709
|
+
else if (fn?.type === 'member_expression') {
|
|
2710
|
+
const obj = fn.childForFieldName('object');
|
|
2711
|
+
const prop = fn.childForFieldName('property');
|
|
2712
|
+
if (obj?.type === 'identifier' &&
|
|
2713
|
+
prop &&
|
|
2714
|
+
(prop.text === 'call' || prop.text === 'apply') &&
|
|
2715
|
+
!BUILTIN_GLOBALS.has(obj.text)) {
|
|
2716
|
+
const args = node.childForFieldName('arguments') || findChild(node, 'arguments');
|
|
2717
|
+
if (args) {
|
|
2718
|
+
for (let i = 0; i < args.childCount; i++) {
|
|
2719
|
+
const child = args.child(i);
|
|
2720
|
+
if (!child)
|
|
2721
|
+
continue;
|
|
2722
|
+
const t = child.type;
|
|
2723
|
+
if (t === '(' || t === ')' || t === ',')
|
|
2724
|
+
continue;
|
|
2725
|
+
// First real argument: only bind if it's a plain identifier
|
|
2726
|
+
if (t === 'identifier' &&
|
|
2727
|
+
!BUILTIN_GLOBALS.has(child.text) &&
|
|
2728
|
+
child.text !== 'undefined' &&
|
|
2729
|
+
child.text !== 'null') {
|
|
2730
|
+
thisCallBindings.push({ callee: obj.text, thisArg: child.text });
|
|
2731
|
+
}
|
|
2732
|
+
break;
|
|
2733
|
+
}
|
|
2734
|
+
}
|
|
2735
|
+
}
|
|
2736
|
+
}
|
|
2737
|
+
}
|
|
2738
|
+
/**
|
|
2739
|
+
* Single-pass collector walk: one DFS that dispatches each node to every
|
|
2740
|
+
* collector interested in its type.
|
|
2741
|
+
*
|
|
2742
|
+
* This replaces what had grown to ten independent full-tree traversals (one
|
|
2743
|
+
* per collector). On WASM trees every node access (`child(i)`, `.type`,
|
|
2744
|
+
* `childForFieldName`) marshals through the JS↔WASM boundary, so traversal
|
|
2745
|
+
* count — not collector work — dominated extraction cost: the accumulated
|
|
2746
|
+
* per-collector walks made extraction ~2.4× slower between v3.11.2 and
|
|
2747
|
+
* v3.12.0 (7.5 → 17.7 ms/file on codegraph's own corpus).
|
|
2748
|
+
*
|
|
2749
|
+
* Collectors with bespoke traversal semantics stay separate:
|
|
2750
|
+
* - extractConstantsWalk / extractDestructuredBindingsWalk prune function
|
|
2751
|
+
* scopes and unwrap export statements on the way down;
|
|
2752
|
+
* - extractReturnTypeMapWalk / extractTypeMapWalk / extractSpreadForOfWalk /
|
|
2753
|
+
* extractObjectRestParamBindingsWalk thread enclosing-class context with
|
|
2754
|
+
* per-walk reset rules that intentionally differ (see each walk's comments).
|
|
2755
|
+
*/
|
|
2756
|
+
function runCollectorWalk(rootNode, targets) {
|
|
2757
|
+
const walk = (node, depth, inDynamicImport) => {
|
|
2758
|
+
if (depth >= MAX_WALK_DEPTH)
|
|
2759
|
+
return;
|
|
2760
|
+
let childInDynamicImport = inDynamicImport;
|
|
2761
|
+
switch (node.type) {
|
|
2762
|
+
case 'call_expression': {
|
|
2763
|
+
// Matched import() calls suppress *dynamic-import* collection in their
|
|
2764
|
+
// argument subtree (mirrors the old walk's early return) while leaving
|
|
2765
|
+
// the subtree visible to every other collector. The !inDynamicImport
|
|
2766
|
+
// check runs first so nested import() calls are neither collected nor
|
|
2767
|
+
// re-matched.
|
|
2768
|
+
if (targets.imports && !inDynamicImport && collectDynamicImport(node, targets.imports)) {
|
|
2769
|
+
childInDynamicImport = true;
|
|
2770
|
+
}
|
|
2771
|
+
if (targets.calls && targets.thisCallBindings) {
|
|
2772
|
+
collectThisCallAndBindings(node, targets.calls, targets.thisCallBindings);
|
|
2773
|
+
}
|
|
2774
|
+
collectParamBindings(node, targets.paramBindings);
|
|
2775
|
+
collectDefinePropertyReceiver(node, targets.definePropertyReceivers);
|
|
2776
|
+
break;
|
|
2777
|
+
}
|
|
2778
|
+
case 'variable_declarator':
|
|
2779
|
+
collectArrayElemBindings(node, targets.arrayElemBindings);
|
|
2780
|
+
collectObjectPropBindings(node, targets.objectPropBindings);
|
|
2781
|
+
break;
|
|
2782
|
+
case 'expression_statement': {
|
|
2783
|
+
const expr = node.child(0);
|
|
2784
|
+
if (expr?.type === 'assignment_expression') {
|
|
2785
|
+
const lhs = expr.childForFieldName('left');
|
|
2786
|
+
const rhs = expr.childForFieldName('right');
|
|
2787
|
+
if (lhs && rhs) {
|
|
2788
|
+
handlePrototypeAssignment(lhs, rhs, targets.definitions, targets.typeMap);
|
|
2789
|
+
if (targets.funcPropDefs)
|
|
2790
|
+
handleFuncPropAssignment(lhs, rhs, targets.funcPropDefs);
|
|
2791
|
+
}
|
|
2792
|
+
}
|
|
2793
|
+
break;
|
|
2794
|
+
}
|
|
2795
|
+
case 'new_expression': {
|
|
2796
|
+
const name = extractNewExprTypeName(node);
|
|
2797
|
+
if (name)
|
|
2798
|
+
targets.newExpressions.push(name);
|
|
2799
|
+
break;
|
|
2800
|
+
}
|
|
2801
|
+
case 'field_definition':
|
|
2802
|
+
case 'public_field_definition':
|
|
2803
|
+
if (targets.classMemberDefs)
|
|
2804
|
+
handleFieldDef(node, targets.classMemberDefs);
|
|
2805
|
+
break;
|
|
2806
|
+
case 'class_static_block':
|
|
2807
|
+
if (targets.classMemberDefs)
|
|
2808
|
+
handleStaticBlock(node, targets.classMemberDefs);
|
|
2809
|
+
break;
|
|
2810
|
+
}
|
|
2811
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
2812
|
+
walk(node.child(i), depth + 1, childInDynamicImport);
|
|
2813
|
+
}
|
|
2814
|
+
};
|
|
2815
|
+
walk(rootNode, 0, false);
|
|
2816
|
+
}
|
|
1356
2817
|
function findAnonymousCallback(argsNode) {
|
|
1357
2818
|
for (let i = 0; i < argsNode.childCount; i++) {
|
|
1358
2819
|
const child = argsNode.child(i);
|
|
@@ -1409,7 +2870,7 @@ const EVENT_METHODS = new Set(['on', 'once', 'addEventListener', 'addListener'])
|
|
|
1409
2870
|
function extractCallbackDefinition(callNode, fn) {
|
|
1410
2871
|
if (!fn)
|
|
1411
2872
|
fn = callNode.childForFieldName('function');
|
|
1412
|
-
if (
|
|
2873
|
+
if (fn?.type !== 'member_expression')
|
|
1413
2874
|
return null;
|
|
1414
2875
|
const prop = fn.childForFieldName('property');
|
|
1415
2876
|
if (!prop)
|
|
@@ -1443,7 +2904,7 @@ function extractCallbackDefinition(callNode, fn) {
|
|
|
1443
2904
|
// Express: app.get('/path', callback)
|
|
1444
2905
|
if (EXPRESS_METHODS.has(method)) {
|
|
1445
2906
|
const strArg = findFirstStringArg(args);
|
|
1446
|
-
if (!strArg
|
|
2907
|
+
if (!strArg?.startsWith('/'))
|
|
1447
2908
|
return null;
|
|
1448
2909
|
const cb = findAnonymousCallback(args);
|
|
1449
2910
|
if (!cb)
|
|
@@ -1485,7 +2946,7 @@ function extractSuperclass(heritage) {
|
|
|
1485
2946
|
}
|
|
1486
2947
|
return null;
|
|
1487
2948
|
}
|
|
1488
|
-
const JS_CLASS_TYPES = ['class_declaration', 'class'];
|
|
2949
|
+
const JS_CLASS_TYPES = ['class_declaration', 'abstract_class_declaration', 'class'];
|
|
1489
2950
|
function findParentClass(node) {
|
|
1490
2951
|
return findParentNode(node, JS_CLASS_TYPES);
|
|
1491
2952
|
}
|
|
@@ -1529,7 +2990,7 @@ function extractDynamicImportNames(callNode) {
|
|
|
1529
2990
|
if (current && current.type === 'await_expression')
|
|
1530
2991
|
current = current.parent;
|
|
1531
2992
|
// We should now be at a variable_declarator (or not, if standalone import())
|
|
1532
|
-
if (
|
|
2993
|
+
if (current?.type !== 'variable_declarator')
|
|
1533
2994
|
return [];
|
|
1534
2995
|
const nameNode = current.childForFieldName('name');
|
|
1535
2996
|
if (!nameNode)
|
|
@@ -1573,4 +3034,153 @@ function extractDynamicImportNames(callNode) {
|
|
|
1573
3034
|
}
|
|
1574
3035
|
return [];
|
|
1575
3036
|
}
|
|
3037
|
+
// ── Phase 8.X: Prototype-based method extraction ────────────────────────────
|
|
3038
|
+
/**
|
|
3039
|
+
* Walk the AST and extract prototype-based method definitions and aliases.
|
|
3040
|
+
*
|
|
3041
|
+
* Handles three patterns:
|
|
3042
|
+
* 1. `Foo.prototype.bar = function(){...}` — emits Foo.bar as method definition
|
|
3043
|
+
* 2. `Foo.prototype.bar = identifier` — sets typeMap['Foo.bar'] = { type: identifier }
|
|
3044
|
+
* 3. `Foo.prototype = { bar: fn, ... }` — emits defs and typeMap entries per property
|
|
3045
|
+
*
|
|
3046
|
+
* Emitting definitions under the canonical `ClassName.methodName` name lets the
|
|
3047
|
+
* existing typeMap-based call resolver find them when a typed receiver dispatches
|
|
3048
|
+
* `instance.method()` (lookup.byName('C.foo') in resolveByMethodOrGlobal).
|
|
3049
|
+
*
|
|
3050
|
+
* typeMap entries for identifier aliases (`Foo.bar → { type: 'someId' }`) are
|
|
3051
|
+
* consumed by the prototype-alias fallback added to resolveByMethodOrGlobal.
|
|
3052
|
+
*/
|
|
3053
|
+
// Prototype-method assignments (`Foo.prototype.bar = fn`) are collected inline
|
|
3054
|
+
// in runCollectorWalk's expression_statement case via handlePrototypeAssignment.
|
|
3055
|
+
/**
|
|
3056
|
+
* Handle an assignment_expression that may be a prototype assignment.
|
|
3057
|
+
*
|
|
3058
|
+
* Matches:
|
|
3059
|
+
* - `Foo.prototype.bar = rhs` (lhs ends in .prototype.bar)
|
|
3060
|
+
* - `Foo.prototype = { ... }` (lhs ends in .prototype, rhs is object literal)
|
|
3061
|
+
*/
|
|
3062
|
+
function handlePrototypeAssignment(lhs, rhs, definitions, typeMap) {
|
|
3063
|
+
if (lhs.type !== 'member_expression')
|
|
3064
|
+
return;
|
|
3065
|
+
const lhsObj = lhs.childForFieldName('object');
|
|
3066
|
+
const lhsProp = lhs.childForFieldName('property');
|
|
3067
|
+
if (!lhsObj || !lhsProp)
|
|
3068
|
+
return;
|
|
3069
|
+
// Pattern 1: `Foo.prototype.bar = rhs`
|
|
3070
|
+
// lhs.object is `Foo.prototype` (member_expression), lhs.property is `bar`
|
|
3071
|
+
if (lhsObj.type === 'member_expression' &&
|
|
3072
|
+
(lhsProp.type === 'property_identifier' || lhsProp.type === 'identifier')) {
|
|
3073
|
+
const protoObj = lhsObj.childForFieldName('object');
|
|
3074
|
+
const protoProp = lhsObj.childForFieldName('property');
|
|
3075
|
+
if (protoObj?.type === 'identifier' &&
|
|
3076
|
+
protoProp?.text === 'prototype' &&
|
|
3077
|
+
!BUILTIN_GLOBALS.has(protoObj.text)) {
|
|
3078
|
+
emitPrototypeMethod(protoObj.text, lhsProp.text, rhs, definitions, typeMap);
|
|
3079
|
+
}
|
|
3080
|
+
return;
|
|
3081
|
+
}
|
|
3082
|
+
// Pattern 2: `Foo.prototype = { bar: fn, ... }`
|
|
3083
|
+
// lhs.object is `Foo` (identifier), lhs.property is `prototype`
|
|
3084
|
+
if (lhsObj.type === 'identifier' &&
|
|
3085
|
+
lhsProp.text === 'prototype' &&
|
|
3086
|
+
!BUILTIN_GLOBALS.has(lhsObj.text) &&
|
|
3087
|
+
rhs.type === 'object') {
|
|
3088
|
+
extractPrototypeObjectLiteral(lhsObj.text, rhs, definitions, typeMap);
|
|
3089
|
+
}
|
|
3090
|
+
}
|
|
3091
|
+
/** Emit one prototype method definition or typeMap alias for `ClassName.methodName = rhs`. */
|
|
3092
|
+
function emitPrototypeMethod(className, methodName, rhs, definitions, typeMap) {
|
|
3093
|
+
const fullName = `${className}.${methodName}`;
|
|
3094
|
+
if (rhs.type === 'function_expression' || rhs.type === 'arrow_function') {
|
|
3095
|
+
definitions.push({
|
|
3096
|
+
name: fullName,
|
|
3097
|
+
kind: 'method',
|
|
3098
|
+
line: nodeStartLine(rhs),
|
|
3099
|
+
endLine: nodeEndLine(rhs),
|
|
3100
|
+
});
|
|
3101
|
+
}
|
|
3102
|
+
else if (rhs.type === 'identifier' && !BUILTIN_GLOBALS.has(rhs.text)) {
|
|
3103
|
+
// Prototype alias: `A.prototype.t = f` → typeMap['A.t'] = { type: 'f' }
|
|
3104
|
+
// Consumed by the prototype-alias fallback in resolveByMethodOrGlobal.
|
|
3105
|
+
setTypeMapEntry(typeMap, fullName, rhs.text, 0.9);
|
|
3106
|
+
}
|
|
3107
|
+
}
|
|
3108
|
+
/**
|
|
3109
|
+
* Extract function-as-object property method definitions.
|
|
3110
|
+
*
|
|
3111
|
+
* Handles `fn.method = function() {}` and `fn.method = () => {}` patterns.
|
|
3112
|
+
* Emits a `method` definition named `fn.method` so that:
|
|
3113
|
+
* 1. `findCaller` attributes calls inside the body to `fn.method`
|
|
3114
|
+
* 2. `resolveByMethodOrGlobal` resolves `this.other()` inside `fn.method` to `fn.other`
|
|
3115
|
+
*
|
|
3116
|
+
* Excludes BUILTIN_GLOBALS objects and `.prototype` (handled by extractPrototypeMethodsWalk).
|
|
3117
|
+
*/
|
|
3118
|
+
// Function-as-object-property assignments (`fn.method = function(){}`) are
|
|
3119
|
+
// collected inline in runCollectorWalk's expression_statement case (walk path
|
|
3120
|
+
// only — the query path captures them via the `assign_left`/`assign_right`
|
|
3121
|
+
// query pattern in dispatchQueryMatch).
|
|
3122
|
+
function handleFuncPropAssignment(lhs, rhs, definitions) {
|
|
3123
|
+
if (lhs.type !== 'member_expression')
|
|
3124
|
+
return;
|
|
3125
|
+
if (rhs.type !== 'function_expression' && rhs.type !== 'arrow_function')
|
|
3126
|
+
return;
|
|
3127
|
+
const obj = lhs.childForFieldName('object');
|
|
3128
|
+
const prop = lhs.childForFieldName('property');
|
|
3129
|
+
if (!obj || !prop)
|
|
3130
|
+
return;
|
|
3131
|
+
if (obj.type !== 'identifier')
|
|
3132
|
+
return;
|
|
3133
|
+
if (prop.type !== 'property_identifier' && prop.type !== 'identifier')
|
|
3134
|
+
return;
|
|
3135
|
+
if (BUILTIN_GLOBALS.has(obj.text))
|
|
3136
|
+
return;
|
|
3137
|
+
if (prop.text === 'prototype')
|
|
3138
|
+
return;
|
|
3139
|
+
const params = extractParameters(rhs);
|
|
3140
|
+
definitions.push({
|
|
3141
|
+
name: `${obj.text}.${prop.text}`,
|
|
3142
|
+
kind: 'method',
|
|
3143
|
+
line: nodeStartLine(rhs),
|
|
3144
|
+
endLine: nodeEndLine(rhs),
|
|
3145
|
+
children: params.length > 0 ? params : undefined,
|
|
3146
|
+
});
|
|
3147
|
+
}
|
|
3148
|
+
/** Iterate over an object literal assigned to `Foo.prototype` and emit defs/aliases. */
|
|
3149
|
+
function extractPrototypeObjectLiteral(className, objNode, definitions, typeMap) {
|
|
3150
|
+
for (let i = 0; i < objNode.childCount; i++) {
|
|
3151
|
+
const child = objNode.child(i);
|
|
3152
|
+
if (!child)
|
|
3153
|
+
continue;
|
|
3154
|
+
if (child.type === 'method_definition') {
|
|
3155
|
+
// Shorthand method: `Foo.prototype = { bar() {} }`
|
|
3156
|
+
const nameNode = child.childForFieldName('name');
|
|
3157
|
+
if (nameNode) {
|
|
3158
|
+
definitions.push({
|
|
3159
|
+
name: `${className}.${nameNode.text}`,
|
|
3160
|
+
kind: 'method',
|
|
3161
|
+
line: nodeStartLine(child),
|
|
3162
|
+
endLine: nodeEndLine(child),
|
|
3163
|
+
});
|
|
3164
|
+
}
|
|
3165
|
+
continue;
|
|
3166
|
+
}
|
|
3167
|
+
if (child.type === 'shorthand_property_identifier') {
|
|
3168
|
+
// ES6 shorthand: `Foo.prototype = { bar }` → alias typeMap['Foo.bar'] = { type: 'bar' }
|
|
3169
|
+
if (!BUILTIN_GLOBALS.has(child.text)) {
|
|
3170
|
+
setTypeMapEntry(typeMap, `${className}.${child.text}`, child.text, 0.9);
|
|
3171
|
+
}
|
|
3172
|
+
continue;
|
|
3173
|
+
}
|
|
3174
|
+
if (child.type !== 'pair')
|
|
3175
|
+
continue;
|
|
3176
|
+
const keyNode = child.childForFieldName('key');
|
|
3177
|
+
const valueNode = child.childForFieldName('value');
|
|
3178
|
+
if (!keyNode || !valueNode)
|
|
3179
|
+
continue;
|
|
3180
|
+
const methodName = keyNode.type === 'string' ? keyNode.text.replace(/['"]/g, '') : keyNode.text;
|
|
3181
|
+
if (!methodName)
|
|
3182
|
+
continue;
|
|
3183
|
+
emitPrototypeMethod(className, methodName, valueNode, definitions, typeMap);
|
|
3184
|
+
}
|
|
3185
|
+
}
|
|
1576
3186
|
//# sourceMappingURL=javascript.js.map
|