-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathjavascript.test.ts
More file actions
782 lines (692 loc) · 31.7 KB
/
Copy pathjavascript.test.ts
File metadata and controls
782 lines (692 loc) · 31.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
/**
* JavaScript/TypeScript parser tests.
*
* NOTE: These tests require vitest and web-tree-sitter to be installed.
* Run: npm install
* Then: npm test
*/
import { beforeAll, describe, expect, it } from 'vitest';
import { createParsers, extractSymbols } from '../../src/domain/parser.js';
import { setTypeMapEntry } from '../../src/extractors/helpers.js';
describe('JavaScript parser', () => {
let parsers: any;
beforeAll(async () => {
parsers = await createParsers();
});
function parseJS(code) {
const parser = parsers.get('javascript');
const tree = parser.parse(code);
return extractSymbols(tree, 'test.js');
}
it('extracts named function declarations', () => {
const symbols = parseJS(`function greet(name) { return "hello " + name; }`);
expect(symbols.definitions).toContainEqual(
expect.objectContaining({ name: 'greet', kind: 'function', line: 1 }),
);
});
it('extracts arrow function assignments', () => {
const symbols = parseJS(`const add = (a, b) => a + b;`);
expect(symbols.definitions).toContainEqual(
expect.objectContaining({ name: 'add', kind: 'function' }),
);
});
it('extracts generator function declarations', () => {
const symbols = parseJS(`function* gen() { yield 1; }`);
expect(symbols.definitions).toContainEqual(
expect.objectContaining({ name: 'gen', kind: 'function' }),
);
});
it('extracts variable-declared generator functions', () => {
const symbols = parseJS(`const gen = function*() { yield 1; };`);
expect(symbols.definitions).toContainEqual(
expect.objectContaining({ name: 'gen', kind: 'function' }),
);
});
it('attributes calls inside generator body to the generator', () => {
// Use multi-line generators so line ranges are non-overlapping and the
// attribution can be verified by line number containment.
const symbols = parseJS(
'function* gen9() {\n yield* gen8();\n}\nfunction* gen8() { yield 1; }',
);
const gen9Def = symbols.definitions.find((d) => d.name === 'gen9');
const gen8Def = symbols.definitions.find((d) => d.name === 'gen8');
expect(gen9Def).toBeDefined();
expect(gen8Def).toBeDefined();
// The call to gen8 must exist.
const gen8Call = symbols.calls.find((c) => c.name === 'gen8');
expect(gen8Call).toBeDefined();
// The call's line must fall within gen9's range — proving it is attributed
// to gen9's body, not to file level or to gen8 itself.
expect(gen8Call!.line).toBeGreaterThanOrEqual(gen9Def!.line);
expect(gen8Call!.line).toBeLessThanOrEqual(gen9Def!.endLine!);
// Negative: the call must NOT fall within gen8's own range (not self-attributed).
const callIsInsideGen8 =
gen8Call!.line >= gen8Def!.line && gen8Call!.line <= (gen8Def!.endLine ?? gen8Def!.line);
expect(callIsInsideGen8).toBe(false);
});
it('captures calls inside yield* expressions', () => {
const symbols = parseJS(`function* delegator() { yield* inner(); }`);
expect(symbols.calls).toContainEqual(expect.objectContaining({ name: 'inner' }));
});
it('extracts class declarations', () => {
const symbols = parseJS(`class Foo { bar() {} }`);
expect(symbols.definitions).toContainEqual(
expect.objectContaining({ name: 'Foo', kind: 'class' }),
);
expect(symbols.definitions).toContainEqual(
expect.objectContaining({ name: 'Foo.bar', kind: 'method' }),
);
});
it('extracts import statements', () => {
const symbols = parseJS(`import { foo, bar } from './baz';`);
expect(symbols.imports).toHaveLength(1);
expect(symbols.imports[0].source).toBe('./baz');
expect(symbols.imports[0].names).toContain('foo');
expect(symbols.imports[0].names).toContain('bar');
});
it('extracts call expressions', () => {
const symbols = parseJS(`import { foo } from './bar'; foo(); baz();`);
expect(symbols.calls).toContainEqual(expect.objectContaining({ name: 'foo' }));
expect(symbols.calls).toContainEqual(expect.objectContaining({ name: 'baz' }));
});
it('extracts class instantiation as calls', () => {
const symbols = parseJS(`
const e = new CodegraphError("msg");
new Foo();
throw new ParseError("x");
const bar = new ns.Bar();
`);
expect(symbols.calls).toContainEqual(expect.objectContaining({ name: 'CodegraphError' }));
expect(symbols.calls).toContainEqual(expect.objectContaining({ name: 'Foo' }));
expect(symbols.calls).toContainEqual(expect.objectContaining({ name: 'ParseError' }));
expect(symbols.calls).toContainEqual(expect.objectContaining({ name: 'Bar', receiver: 'ns' }));
});
it('handles re-exports from barrel files', () => {
const symbols = parseJS(`export { default as Widget } from './Widget';`);
expect(symbols.imports).toHaveLength(1);
expect(symbols.imports[0].reexport).toBe(true);
});
it('detects dynamic call patterns', () => {
const symbols = parseJS(`fn.call(null, arg); obj.apply(undefined, args);`);
const dynamicCalls = symbols.calls.filter((c) => c.dynamic);
expect(dynamicCalls.length).toBeGreaterThanOrEqual(1);
});
it('captures receiver for method calls', () => {
const symbols = parseJS(`
obj.method();
standalone();
this.foo();
arr[0].bar();
a.b.c();
`);
const method = symbols.calls.find((c) => c.name === 'method');
expect(method).toBeDefined();
expect(method.receiver).toBe('obj');
const standalone = symbols.calls.find((c) => c.name === 'standalone');
expect(standalone).toBeDefined();
expect(standalone.receiver).toBeUndefined();
const foo = symbols.calls.find((c) => c.name === 'foo');
expect(foo).toBeDefined();
expect(foo.receiver).toBe('this');
const c = symbols.calls.find((c) => c.name === 'c');
expect(c).toBeDefined();
expect(c.receiver).toBe('a.b');
});
describe('typeMap extraction', () => {
function parseTS(code) {
const parser = parsers.get('typescript');
const tree = parser.parse(code);
return extractSymbols(tree, 'test.ts');
}
it('extracts typeMap from type annotations with confidence 0.9', () => {
const symbols = parseTS(`const x: Router = express.Router();`);
expect(symbols.typeMap).toBeInstanceOf(Map);
expect(symbols.typeMap.get('x')).toEqual({ type: 'Router', confidence: 0.9 });
});
it('extracts typeMap from generic types', () => {
const symbols = parseTS(`const m: Map<string, number> = new Map();`);
expect(symbols.typeMap.get('m')).toEqual(
expect.objectContaining({ type: 'Map', confidence: 1.0 }),
);
});
it('infers type from new expressions with confidence 1.0', () => {
const symbols = parseTS(`const r = new Router();`);
expect(symbols.typeMap.get('r')).toEqual({ type: 'Router', confidence: 1.0 });
});
it('extracts parameter types into typeMap with confidence 0.9', () => {
const symbols = parseTS(`function process(req: Request, res: Response) {}`);
expect(symbols.typeMap.get('req')).toEqual({ type: 'Request', confidence: 0.9 });
expect(symbols.typeMap.get('res')).toEqual({ type: 'Response', confidence: 0.9 });
});
it('returns empty typeMap when no annotations', () => {
const symbols = parseJS(`const x = 42; function foo(a, b) {}`);
expect(symbols.typeMap).toBeInstanceOf(Map);
expect(symbols.typeMap.size).toBe(0);
});
it('skips union and intersection types', () => {
const symbols = parseTS(`const x: string | number = 42;`);
expect(symbols.typeMap.has('x')).toBe(false);
});
it('handles let/var declarations with type annotations', () => {
const symbols = parseTS(`let app: Express = createApp();`);
expect(symbols.typeMap.get('app')).toEqual({ type: 'Express', confidence: 0.9 });
});
it('prefers constructor over annotation on the same declaration', () => {
const symbols = parseTS(`const x: Base = new Derived();`);
// Constructor on same declaration wins (confidence 1.0) because the runtime type
// is what matters for call resolution: x.render() → Derived.render, not Base.render.
// Cross-scope pollution is prevented by setTypeMapEntry's higher-confidence gate.
expect(symbols.typeMap.get('x')).toEqual({ type: 'Derived', confidence: 1.0 });
});
it('extracts factory method patterns with confidence 0.7', () => {
const symbols = parseJS(`const client = HttpClient.create();`);
expect(symbols.typeMap.get('client')).toEqual({ type: 'HttpClient', confidence: 0.7 });
});
it('ignores lowercase factory calls', () => {
const symbols = parseJS(`const result = utils.create();`);
expect(symbols.typeMap.has('result')).toBe(false);
});
it('ignores built-in globals like Math, JSON, Promise', () => {
const symbols = parseJS(`
const r = Math.random();
const d = JSON.parse('{}');
const p = Promise.resolve(42);
`);
expect(symbols.typeMap.has('r')).toBe(false);
expect(symbols.typeMap.has('d')).toBe(false);
expect(symbols.typeMap.has('p')).toBe(false);
});
// Regression: GH #964 — tree-sitter can produce partial/corrupted trees in
// which an identifier node has empty `text`. Previously the factory path
// crashed with "Cannot read properties of undefined (reading 'toLowerCase')"
// because `objName[0]` is undefined for an empty string. The guard now
// mirrors the Python extractor's short-circuit check.
it('does not crash when factory call has an empty-text identifier', () => {
// Build a mock tree that mimics `const x = <empty-identifier>.create()`.
// The walk path calls handleVarDeclaratorTypeMap → factory branch, which
// reads `obj.text` ("") and would previously call "".toLowerCase() via
// `objName[0]!.toLowerCase()`. The fix's `objName[0] &&` guard short-circuits.
const pos = { row: 0, column: 0 };
const makeNode = (
type: string,
text = '',
fields: Record<string, any> = {},
children: any[] = [],
) => {
const node: any = {
type,
text,
startPosition: pos,
endPosition: pos,
childCount: children.length,
child: (i: number) => children[i] ?? null,
childForFieldName: (name: string) => fields[name] ?? null,
parent: null,
};
for (const c of children) {
c.parent = node;
}
return node;
};
const emptyIdentifier = makeNode('identifier', '');
const createName = makeNode('property_identifier', 'create');
const memberExpr = makeNode(
'member_expression',
'.create',
{
object: emptyIdentifier,
property: createName,
},
[emptyIdentifier, createName],
);
const callExpr = makeNode(
'call_expression',
'.create()',
{
function: memberExpr,
},
[memberExpr],
);
const nameIdent = makeNode('identifier', 'x');
const declarator = makeNode(
'variable_declarator',
'x = .create()',
{
name: nameIdent,
value: callExpr,
},
[nameIdent, callExpr],
);
const lexDecl = makeNode('lexical_declaration', 'const x = .create();', {}, [declarator]);
const root = makeNode('program', '', {}, [lexDecl]);
const fakeTree: any = { rootNode: root };
// Before the fix this would throw TypeError. Now it should complete and
// simply leave `x` out of the typeMap (empty identifier is ignored).
expect(() => extractSymbols(fakeTree, 'test.js')).not.toThrow();
const symbols = extractSymbols(fakeTree, 'test.js');
expect(symbols.typeMap.has('x')).toBe(false);
});
});
describe('Phase 8.3d: property write pts tracking', () => {
function parseJS(code) {
const parser = parsers.get('javascript');
const tree = parser.parse(code);
return extractSymbols(tree, 'test.js');
}
it('seeds typeMap with composite key for obj.prop = identifier', () => {
const symbols = parseJS(`
const handlers = {};
handlers.auth = authMiddleware;
`);
expect(symbols.typeMap.get('handlers.auth')).toEqual({
type: 'authMiddleware',
confidence: 0.85,
});
});
it('ignores chained writes (a.b.c = x)', () => {
const symbols = parseJS(`a.b.c = handler;`);
expect(symbols.typeMap.has('a.b.c')).toBe(false);
expect(symbols.typeMap.has('b.c')).toBe(false);
});
it('seeds typeMap for this.prop = new ClassName() with confidence 1.0', () => {
const symbols = parseJS(`
class UserService {
constructor() {
this.logger = new Logger('UserService');
}
}
`);
expect(symbols.typeMap.get('this.logger')).toEqual({ type: 'Logger', confidence: 1.0 });
});
it('does not seed typeMap for this.prop = identifier (only new expressions)', () => {
const symbols = parseJS(`
class Foo {
init(logger) { this.logger = logger; }
}
`);
expect(symbols.typeMap.has('this.logger')).toBe(false);
});
it('ignores non-identifier RHS (a.prop = obj.method)', () => {
const symbols = parseJS(`router.use = obj.method;`);
expect(symbols.typeMap.has('router.use')).toBe(false);
});
it('ignores BUILTIN_GLOBALS as object names', () => {
const symbols = parseJS(`
console.warn = customWarn;
Object.assign = myAssign;
process.on = myHandler;
window.onload = myHandler;
document.ready = myHandler;
globalThis.fetch = myFetch;
`);
expect(symbols.typeMap.has('console.warn')).toBe(false);
expect(symbols.typeMap.has('Object.assign')).toBe(false);
expect(symbols.typeMap.has('process.on')).toBe(false);
expect(symbols.typeMap.has('window.onload')).toBe(false);
expect(symbols.typeMap.has('document.ready')).toBe(false);
expect(symbols.typeMap.has('globalThis.fetch')).toBe(false);
});
it('first-write wins when same key appears twice at equal confidence', () => {
const parser = parsers.get('typescript');
const tree = parser.parse(`
handlers.auth = firstMiddleware;
handlers.auth = secondMiddleware;
`);
const symbols = extractSymbols(tree, 'test.ts');
// Both writes are at 0.85; first-write wins (equal confidence does not promote)
expect(symbols.typeMap.get('handlers.auth')?.type).toBe('firstMiddleware');
});
it('higher-confidence entry promotes over lower-confidence entry (setTypeMapEntry)', () => {
const typeMap = new Map<string, { type: string; confidence: number }>();
// Seed with a low-confidence write (property-write confidence: 0.85)
setTypeMapEntry(typeMap, 'handlers.auth', 'firstMiddleware', 0.85);
// A higher-confidence annotation (0.9) should overwrite
setTypeMapEntry(typeMap, 'handlers.auth', 'AnnotatedHandler', 0.9);
expect(typeMap.get('handlers.auth')).toEqual({ type: 'AnnotatedHandler', confidence: 0.9 });
});
});
describe('Phase 8.2: inter-procedural return-type propagation', () => {
function parseTS(code) {
const parser = parsers.get('typescript');
const tree = parser.parse(code);
return extractSymbols(tree, 'test.ts');
}
describe('returnTypeMap extraction', () => {
it('records explicit TS return type annotation with confidence 1.0', () => {
const symbols = parseTS(`function createUser(): User { return new User(); }`);
expect(symbols.returnTypeMap).toBeInstanceOf(Map);
expect(symbols.returnTypeMap.get('createUser')).toEqual({ type: 'User', confidence: 1.0 });
});
it('infers return type from return new Constructor() with confidence 0.85', () => {
const symbols = parseTS(`function buildRouter() { return new Router(); }`);
expect(symbols.returnTypeMap.get('buildRouter')).toEqual({
type: 'Router',
confidence: 0.85,
});
});
it('prefers annotation over inferred return type', () => {
const symbols = parseTS(`function create(): Service { return new OtherService(); }`);
expect(symbols.returnTypeMap.get('create')).toEqual({ type: 'Service', confidence: 1.0 });
});
it('qualifies method return types with class name', () => {
const symbols = parseTS(`
class UserService {
getUser(): User { return new User(); }
}
`);
expect(symbols.returnTypeMap.get('UserService.getUser')).toEqual({
type: 'User',
confidence: 1.0,
});
});
it('records arrow function return type from variable declarator', () => {
const symbols = parseTS(`const createRepo = (): Repo => new Repo();`);
expect(symbols.returnTypeMap.get('createRepo')).toEqual({ type: 'Repo', confidence: 1.0 });
});
it('does not record constructor methods', () => {
const symbols = parseTS(`class Foo { constructor() {} }`);
expect(symbols.returnTypeMap.has('Foo.constructor')).toBe(false);
});
});
describe('intra-file propagation via returnTypeMap', () => {
it('propagates return type of annotated function — confidence 0.9 (1.0 - 0.1 × hop 1)', () => {
const symbols = parseTS(`
function createUser(): User { return new User(); }
const u = createUser();
`);
expect(symbols.typeMap.get('u')).toEqual({ type: 'User', confidence: 0.9 });
});
it('propagates return type inferred from return new — confidence 0.75 (0.85 - 0.1)', () => {
const symbols = parseTS(`
function buildRouter() { return new Router(); }
const r = buildRouter();
`);
expect(symbols.typeMap.get('r')).toEqual({ type: 'Router', confidence: 0.75 });
});
it('propagates return type via method call on typed receiver', () => {
const symbols = parseTS(`
class UserService {
getUser(): User { return new User(); }
}
const svc: UserService = new UserService();
const u = svc.getUser();
`);
expect(symbols.typeMap.get('u')).toEqual({ type: 'User', confidence: 0.9 });
});
it('resolves one-hop method chain — getService().getRepo()', () => {
const symbols = parseTS(`
function getService(): UserService { return new UserService(); }
class UserService {
getRepo(): Repo { return new Repo(); }
}
const repo = getService().getRepo();
`);
expect(symbols.typeMap.get('repo')).toEqual({ type: 'Repo', confidence: 0.8 });
});
it('does not override higher-confidence annotation with propagated type', () => {
const symbols = parseTS(`
function createUser(): User { return new User(); }
const u: Admin = createUser();
`);
// Annotation (0.9) wins over propagated (0.9) — setTypeMapEntry keeps first seen
expect(symbols.typeMap.get('u')?.type).toBe('Admin');
});
it('does not propagate for plain function calls with no return type info', () => {
const symbols = parseTS(`
function doSomething() { return 42; }
const x = doSomething();
`);
expect(symbols.typeMap.has('x')).toBe(false);
});
});
});
it('does not set receiver for .call()/.apply()/.bind() unwrapped calls', () => {
const symbols = parseJS(`fn.call(null, arg);`);
const fnCall = symbols.calls.find((c) => c.name === 'fn');
expect(fnCall).toBeDefined();
expect(fnCall.receiver).toBeUndefined();
});
describe('callback pattern extraction', () => {
// Commander patterns
it('extracts Commander .command().action() with arrow function', () => {
const symbols = parseJS(
`program.command('build [dir]').action(async (dir, opts) => { run(); });`,
);
const def = symbols.definitions.find((d) => d.name === 'command:build');
expect(def).toBeDefined();
expect(def.kind).toBe('function');
});
it('extracts Commander command with angle-bracket arg', () => {
const symbols = parseJS(`program.command('query <name>').action(() => { search(); });`);
const def = symbols.definitions.find((d) => d.name === 'command:query');
expect(def).toBeDefined();
expect(def.kind).toBe('function');
});
it('does not extract Commander action with named handler', () => {
const symbols = parseJS(`program.command('test').action(handleTest);`);
const defs = symbols.definitions.filter((d) => d.name.startsWith('command:'));
expect(defs).toHaveLength(0);
});
it('still extracts calls inside Commander callback body', () => {
const symbols = parseJS(
`program.command('build [dir]').action(async (dir) => { buildGraph(dir); });`,
);
expect(symbols.calls).toContainEqual(expect.objectContaining({ name: 'buildGraph' }));
});
// Express patterns
it('extracts Express app.get route', () => {
const symbols = parseJS(`app.get('/api/users', (req, res) => { res.json([]); });`);
const def = symbols.definitions.find((d) => d.name === 'route:GET /api/users');
expect(def).toBeDefined();
expect(def.kind).toBe('function');
});
it('extracts Express router.post route', () => {
const symbols = parseJS(`router.post('/api/items', async (req, res) => { save(); });`);
const def = symbols.definitions.find((d) => d.name === 'route:POST /api/items');
expect(def).toBeDefined();
expect(def.kind).toBe('function');
});
it('does not extract Map.get as Express route', () => {
const symbols = parseJS(`myMap.get('someKey');`);
const defs = symbols.definitions.filter((d) => d.name.startsWith('route:'));
expect(defs).toHaveLength(0);
});
// Event patterns
it('extracts emitter.on event callback', () => {
const symbols = parseJS(`emitter.on('data', (chunk) => { process(chunk); });`);
const def = symbols.definitions.find((d) => d.name === 'event:data');
expect(def).toBeDefined();
expect(def.kind).toBe('function');
});
it('extracts server.once event callback', () => {
const symbols = parseJS(`server.once('listening', () => { log(); });`);
const def = symbols.definitions.find((d) => d.name === 'event:listening');
expect(def).toBeDefined();
expect(def.kind).toBe('function');
});
it('does not extract event with named handler as definition', () => {
const symbols = parseJS(`emitter.on('data', handleData);`);
const defs = symbols.definitions.filter((d) => d.name.startsWith('event:'));
expect(defs).toHaveLength(0);
// But we DO get a call edge to the named handler
expect(symbols.calls).toContainEqual(
expect.objectContaining({ name: 'handleData', dynamic: true }),
);
});
// Callback reference calls (named functions passed as arguments)
it('extracts named middleware in router.use()', () => {
const symbols = parseJS(`router.use(handleToken);`);
expect(symbols.calls).toContainEqual(
expect.objectContaining({ name: 'handleToken', dynamic: true }),
);
});
it('extracts multiple named middleware arguments', () => {
const symbols = parseJS(`app.get('/api', authenticate, validate, handler);`);
expect(symbols.calls).toContainEqual(
expect.objectContaining({ name: 'authenticate', dynamic: true }),
);
expect(symbols.calls).toContainEqual(
expect.objectContaining({ name: 'validate', dynamic: true }),
);
expect(symbols.calls).toContainEqual(
expect.objectContaining({ name: 'handler', dynamic: true }),
);
});
it('extracts member expression callbacks (auth.validate)', () => {
const symbols = parseJS(`app.use(auth.validate);`);
expect(symbols.calls).toContainEqual(
expect.objectContaining({ name: 'validate', receiver: 'auth', dynamic: true }),
);
});
it('extracts callback in array methods (.map, .filter)', () => {
const symbols = parseJS(`items.map(transform);`);
expect(symbols.calls).toContainEqual(
expect.objectContaining({ name: 'transform', dynamic: true }),
);
});
it('extracts callback in Promise .then/.catch', () => {
const symbols = parseJS(`promise.then(onSuccess).catch(onError);`);
expect(symbols.calls).toContainEqual(
expect.objectContaining({ name: 'onSuccess', dynamic: true }),
);
expect(symbols.calls).toContainEqual(
expect.objectContaining({ name: 'onError', dynamic: true }),
);
});
it('does not create dynamic calls for string/number/object arguments', () => {
const symbols = parseJS(`app.get('/path', {key: 1}, [], 42);`);
const dynamicCalls = symbols.calls.filter((c) => c.dynamic);
expect(dynamicCalls).toHaveLength(0);
});
it('does not treat member_expression args as callbacks for non-allowlisted callees', () => {
// `store.set(user.id, user)` — `user.id` is a property read passed as a
// value (map key), NOT a callback. Only allowlisted callees (use, then,
// map, addEventListener, etc.) get member_expression args emitted as
// dynamic calls. See issue #971.
const symbols = parseJS(`store.set(user.id, user);`);
const dynamicMemberCalls = symbols.calls.filter((c) => c.dynamic && c.name === 'id');
expect(dynamicMemberCalls).toHaveLength(0);
});
it('still emits member_expression args for allowlisted callees (regression guard)', () => {
// Positive companion to the test above: `app.use(auth.validate)` and
// `promise.then(handlers.onSuccess)` must still produce dynamic calls,
// because `use` and `then` are callback-accepting APIs.
const useSymbols = parseJS(`app.use(auth.validate);`);
expect(useSymbols.calls).toContainEqual(
expect.objectContaining({ name: 'validate', receiver: 'auth', dynamic: true }),
);
const thenSymbols = parseJS(`promise.then(handlers.onSuccess);`);
expect(thenSymbols.calls).toContainEqual(
expect.objectContaining({ name: 'onSuccess', receiver: 'handlers', dynamic: true }),
);
});
it('does not treat cache/Map .get/.put as callback-accepting (HTTP-verb guard)', () => {
// `cache.get(user.id)` shares the verb name `get` with Express routes,
// but has no string-literal route path first arg — so member-expr args
// must not be emitted as dynamic calls. Same for `repo.put`, `map.delete`.
const cacheSymbols = parseJS(`cache.get(user.id);`);
expect(cacheSymbols.calls.filter((c) => c.dynamic && c.name === 'id')).toHaveLength(0);
const repoSymbols = parseJS(`repo.put(record.key, value);`);
expect(repoSymbols.calls.filter((c) => c.dynamic && c.name === 'key')).toHaveLength(0);
const mapSymbols = parseJS(`map.delete(entry.id);`);
expect(mapSymbols.calls.filter((c) => c.dynamic && c.name === 'id')).toHaveLength(0);
});
it('still emits member-expr args for Express HTTP routes with string path', () => {
// Positive regression guard: HTTP-verb calls with a string-literal
// first arg (Express route signature) must still emit member-expr args.
const routerSymbols = parseJS(`router.get('/users/:id', auth.check);`);
expect(routerSymbols.calls).toContainEqual(
expect.objectContaining({ name: 'check', receiver: 'auth', dynamic: true }),
);
const templateSymbols = parseJS('app.post(`/api`, handlers.create);');
expect(templateSymbols.calls).toContainEqual(
expect.objectContaining({ name: 'create', receiver: 'handlers', dynamic: true }),
);
});
it('handles optional-chaining callees in allowlist (obj?.on)', () => {
// `obj?.on(event, handler.fn)` — tree-sitter-javascript/typescript
// represent `obj?.on` as a `member_expression` with an `optional_chain`
// child, so `extractCalleeName` still returns `on` and the allowlist
// gate works. Guards against a previously-flagged false-negative class.
const symbols = parseJS(`emitter?.on('tick', handlers.fn);`);
expect(symbols.calls).toContainEqual(
expect.objectContaining({ name: 'fn', receiver: 'handlers', dynamic: true }),
);
});
it('extracts callback in plain function calls like setTimeout', () => {
const symbols = parseJS(`setTimeout(tick, 1000);`);
expect(symbols.calls).toContainEqual(
expect.objectContaining({ name: 'tick', dynamic: true }),
);
});
it('does not duplicate call for call-expression arguments', () => {
const symbols = parseJS(`router.use(checkPermissions(['admin']));`);
const cpCalls = symbols.calls.filter((c) => c.name === 'checkPermissions');
expect(cpCalls).toHaveLength(1);
});
// Destructured bindings
it('extracts definitions from destructured const bindings', () => {
const symbols = parseJS(`const { handleToken, checkPermissions } = initAuth(config);`);
expect(symbols.definitions).toContainEqual(
expect.objectContaining({ name: 'handleToken', kind: 'function' }),
);
expect(symbols.definitions).toContainEqual(
expect.objectContaining({ name: 'checkPermissions', kind: 'function' }),
);
});
it('extracts definitions from exported destructured const bindings', () => {
const symbols = parseJS(`export const { handleToken, checkPermissions } = initAuth(config);`);
expect(symbols.definitions).toContainEqual(
expect.objectContaining({ name: 'handleToken', kind: 'function' }),
);
expect(symbols.definitions).toContainEqual(
expect.objectContaining({ name: 'checkPermissions', kind: 'function' }),
);
});
it('does not extract definitions from let/var destructured bindings', () => {
const letSymbols = parseJS(`let { userId, email } = parseRequest(req);`);
expect(letSymbols.definitions).not.toContainEqual(
expect.objectContaining({ name: 'userId' }),
);
expect(letSymbols.definitions).not.toContainEqual(expect.objectContaining({ name: 'email' }));
const varSymbols = parseJS(`var { foo, bar } = getConfig();`);
expect(varSymbols.definitions).not.toContainEqual(expect.objectContaining({ name: 'foo' }));
expect(varSymbols.definitions).not.toContainEqual(expect.objectContaining({ name: 'bar' }));
});
it('extracts renamed destructured const binding under its local alias', () => {
const symbols = parseJS(`const { original: renamed } = initAuth();`);
expect(symbols.definitions).toContainEqual(
expect.objectContaining({ name: 'renamed', kind: 'function' }),
);
expect(symbols.definitions).not.toContainEqual(expect.objectContaining({ name: 'original' }));
});
it('does not extract destructured bindings declared inside function scope', () => {
// Parity with the query path (extractDestructuredBindingsWalk) and the
// Rust walk path (handle_var_decl) — both skip FUNCTION_SCOPE_TYPES.
const symbols = parseJS(
`function setup() { const { handleToken, checkPermissions } = initAuth(config); }`,
);
expect(symbols.definitions).not.toContainEqual(
expect.objectContaining({ name: 'handleToken' }),
);
expect(symbols.definitions).not.toContainEqual(
expect.objectContaining({ name: 'checkPermissions' }),
);
});
// Line range verification
it('sets correct line and endLine on callback definition', () => {
const code = [
'app.get("/users",', // line 1
' (req, res) => {', // line 2 — callback starts
' res.json([]);', // line 3
' }', // line 4 — callback ends
');', // line 5
].join('\n');
const symbols = parseJS(code);
const def = symbols.definitions.find((d) => d.name === 'route:GET /users');
expect(def).toBeDefined();
expect(def.line).toBe(2);
expect(def.endLine).toBe(4);
});
});
});