repotoire 0.8.0

Graph-powered code analysis CLI. 110 detectors for security, architecture, bus factor, and code quality.
Documentation
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
use super::*;
use std::path::PathBuf;

#[test]
fn test_parse_simple_function() {
    let source = r#"
function hello(name: string): string {
    return `Hello, ${name}!`;
}
"#;
    let path = PathBuf::from("test.ts");
    let result = parse_source(source, &path, "ts").expect("should parse simple function");

    assert_eq!(result.functions.len(), 1);
    let func = &result.functions[0];
    assert_eq!(func.name, "hello");
}

#[test]
fn test_parse_async_function() {
    let source = r#"
async function fetchData(url: string): Promise<string> {
    return await fetch(url);
}
"#;
    let path = PathBuf::from("test.ts");
    let result = parse_source(source, &path, "ts").expect("should parse async function");

    assert_eq!(result.functions.len(), 1);
    let func = &result.functions[0];
    assert!(func.is_async);
}

#[test]
fn test_parse_arrow_function() {
    let source = r#"
const add = (a: number, b: number): number => a + b;
"#;
    let path = PathBuf::from("test.ts");
    let result = parse_source(source, &path, "ts").expect("should parse arrow function");

    assert!(result.functions.iter().any(|f| f.name == "add"));
}

#[test]
fn test_parse_class() {
    let source = r#"
class MyClass extends BaseClass implements Interface {
    constructor() {
        super();
    }

    method(): void {
        console.log("hello");
    }
}
"#;
    let path = PathBuf::from("test.ts");
    let result = parse_source(source, &path, "ts").expect("should parse class");

    assert_eq!(result.classes.len(), 1);
    let class = &result.classes[0];
    assert_eq!(class.name, "MyClass");
}

#[test]
fn test_parse_interface() {
    let source = r#"
interface MyInterface {
    name: string;
    doSomething(): void;
}
"#;
    let path = PathBuf::from("test.ts");
    let result = parse_source(source, &path, "ts").expect("should parse interface");

    assert_eq!(result.classes.len(), 1);
    let iface = &result.classes[0];
    assert_eq!(iface.name, "MyInterface");
}

#[test]
fn test_parse_imports() {
    let source = r#"
import { Component } from 'react';
import axios from 'axios';
import * as fs from 'fs';

export function main() {}
"#;
    let path = PathBuf::from("test.ts");
    let result = parse_source(source, &path, "ts").expect("should parse imports");

    assert!(result.imports.iter().any(|i| i.path == "react"));
    assert!(result.imports.iter().any(|i| i.path == "axios"));
}

#[test]
fn test_parse_javascript() {
    let source = r#"
function greet(name) {
    return "Hello, " + name;
}
"#;
    let path = PathBuf::from("test.js");
    let result = parse_source(source, &path, "js").expect("should parse JavaScript");

    assert_eq!(result.functions.len(), 1);
    let func = &result.functions[0];
    assert_eq!(func.name, "greet");
}

#[test]
fn test_complexity_simple() {
    let source = r#"
function simple(): number {
    return 42;
}
"#;
    let path = PathBuf::from("test.ts");
    let result =
        parse_source(source, &path, "ts").expect("should parse simple function for complexity");

    let func = &result.functions[0];
    assert_eq!(func.name, "simple");
    // Simple function should have complexity 1
    assert_eq!(func.complexity, Some(1));
}

#[test]
fn test_complexity_with_branches() {
    let source = r#"
function complex(x: number): string {
    if (x > 10) {
        return "big";
    } else if (x > 5) {
        return "medium";
    } else if (x > 0) {
        return "small";
    }
    return "zero";
}
"#;
    let path = PathBuf::from("test.ts");
    let result = parse_source(source, &path, "ts").expect("should parse complex function");

    let func = &result.functions[0];
    assert_eq!(func.name, "complex");
    // if + else if + else if = 3 branches, base 1 = 4 total
    assert!(
        func.complexity.unwrap_or(0) >= 4,
        "Expected complexity >= 4, got {:?}",
        func.complexity
    );
}

#[test]
fn test_complexity_with_loops_and_ternary() {
    let source = r#"
function loopy(items: string[]): number {
    let count = 0;
    for (const item of items) {
        if (item.length > 5) {
            count++;
        }
    }
    return count > 0 ? count : -1;
}
"#;
    let path = PathBuf::from("test.ts");
    let result = parse_source(source, &path, "ts").expect("should parse loopy function");

    let func = &result.functions[0];
    assert_eq!(func.name, "loopy");
    // for + if + ternary = 3 branches + base 1 = 4
    assert!(
        func.complexity.unwrap_or(0) >= 4,
        "Expected complexity >= 4, got {:?}",
        func.complexity
    );
}

#[test]
fn test_parse_calls() {
    let source = r#"
function helperA() {
    console.log("hello");
}

function helperB() {
    helperA();
}

async function main() {
    helperB();
}
"#;
    let path = PathBuf::from("test.ts");
    let result = parse_source(source, &path, "ts").expect("should parse calls");

    assert_eq!(result.functions.len(), 3, "Expected 3 functions");

    // Debug: print what we got
    eprintln!(
        "Functions: {:?}",
        result
            .functions
            .iter()
            .map(|f| (&f.name, f.line_start, f.line_end))
            .collect::<Vec<_>>()
    );
    eprintln!("Calls: {:?}", result.calls);

    // helperB calls helperA
    assert!(
        result
            .calls
            .iter()
            .any(|(caller, callee)| caller.contains("helperB") && callee == "helperA"),
        "Expected helperB -> helperA call, got {:?}",
        result.calls
    );

    // main calls helperB
    assert!(
        result
            .calls
            .iter()
            .any(|(caller, callee)| caller.contains("main") && callee == "helperB"),
        "Expected main -> helperB call, got {:?}",
        result.calls
    );
}

#[test]
fn test_method_count_excludes_nested() {
    // Issue #18: Parser should not count closures/callbacks as class methods
    let source = r#"
class Foo {
    bar() {
        const inner = () => {};  // NOT a method - nested arrow function
        items.map(x => x);       // NOT a method - callback
        function localHelper() {} // NOT a method - nested function
    }
    baz() {}  // IS a method
    qux = () => {};  // IS a method - arrow function class field
}
"#;
    let path = PathBuf::from("test.ts");
    let result = parse_source(source, &path, "ts").expect("should parse class methods");

    assert_eq!(result.classes.len(), 1, "Expected 1 class");
    let class = &result.classes[0];
    assert_eq!(class.name, "Foo");

    // Should have exactly 3 methods: bar, baz, qux
    // NOT: inner, map callback, localHelper (these are nested)
    assert_eq!(
        class.methods.len(),
        3,
        "Expected 3 methods (bar, baz, qux), got {:?}",
        class.methods
    );
    assert!(
        class.methods.contains(&"bar".to_string()),
        "Missing 'bar' method"
    );
    assert!(
        class.methods.contains(&"baz".to_string()),
        "Missing 'baz' method"
    );
    assert!(
        class.methods.contains(&"qux".to_string()),
        "Missing 'qux' arrow field"
    );
}

#[test]
fn test_method_count_excludes_property_values() {
    // Ensure non-function class fields are not counted as methods
    let source = r#"
class Config {
    name = "test";      // NOT a method - string property
    count = 42;         // NOT a method - number property
    items = [1, 2, 3];  // NOT a method - array property
    handler = () => {}; // IS a method - arrow function
    process() {}        // IS a method
}
"#;
    let path = PathBuf::from("test.ts");
    let result = parse_source(source, &path, "ts").expect("should parse property values");

    let class = &result.classes[0];
    assert_eq!(
        class.methods.len(),
        2,
        "Expected 2 methods (handler, process), got {:?}",
        class.methods
    );
    assert!(class.methods.contains(&"handler".to_string()));
    assert!(class.methods.contains(&"process".to_string()));
}

#[test]
fn test_js_method_count_excludes_nested() {
    // Same test for JavaScript
    let source = r#"
class Service {
    constructor() {
        this.callbacks = [];
    }

    register(callback) {
        const wrapper = () => callback();  // nested, not a method
        this.callbacks.push(wrapper);
    }

    execute() {
        this.callbacks.forEach(cb => cb());  // callback, not a method
    }
}
"#;
    let path = PathBuf::from("test.js");
    let result = parse_source(source, &path, "js").expect("should parse JS class methods");

    let class = &result.classes[0];
    assert_eq!(
        class.methods.len(),
        3,
        "Expected 3 methods (constructor, register, execute), got {:?}",
        class.methods
    );
}

#[test]
fn test_jsdoc_extracted() {
    let source = r#"
/**
 * Adds two numbers together.
 * @param a - first number
 * @param b - second number
 * @returns the sum
 */
function add(a: number, b: number): number {
    return a + b;
}
"#;
    let path = PathBuf::from("test.ts");
    let result = parse_source(source, &path, "ts").expect("should parse JSDoc");

    let func = &result.functions[0];
    assert_eq!(func.name, "add");
    assert!(func.doc_comment.is_some(), "Should have JSDoc");
    let doc = func
        .doc_comment
        .as_ref()
        .expect("doc_comment should be Some");
    assert!(doc.contains("Adds two numbers"), "Got: {}", doc);
}

#[test]
fn test_jsdoc_on_arrow_function() {
    let source = r#"
/** Multiplies two values */
const multiply = (a: number, b: number): number => a * b;
"#;
    let path = PathBuf::from("test.ts");
    let result = parse_source(source, &path, "ts").expect("should parse arrow function JSDoc");

    let func = result
        .functions
        .iter()
        .find(|f| f.name == "multiply")
        .expect("should find multiply function");
    assert!(
        func.doc_comment.is_some(),
        "Arrow function should have JSDoc"
    );
    assert!(func
        .doc_comment
        .as_ref()
        .expect("doc_comment should be Some")
        .contains("Multiplies"));
}

#[test]
fn test_react_component_detected() {
    let source = r#"
function MyComponent({ name }: { name: string }) {
    return <div>Hello {name}</div>;
}

function helperFunction() {
    return 42;
}
"#;
    let path = PathBuf::from("test.tsx");
    let result = parse_source(source, &path, "tsx").expect("should parse TSX components");

    let component = result
        .functions
        .iter()
        .find(|f| f.name == "MyComponent")
        .expect("should find MyComponent");
    assert!(
        component
            .annotations
            .contains(&"react:component".to_string()),
        "Should detect React component, got: {:?}",
        component.annotations
    );

    let helper = result
        .functions
        .iter()
        .find(|f| f.name == "helperFunction")
        .expect("should find helperFunction");
    assert!(
        !helper.annotations.contains(&"react:component".to_string()),
        "helperFunction should not be a React component"
    );
}

#[test]
fn test_react_hooks_detected() {
    let source = r#"
function Counter() {
    const [count, setCount] = useState(0);
    useEffect(() => {
        document.title = `Count: ${count}`;
    }, [count]);
    const ref = useRef(null);
    return <div>{count}</div>;
}
"#;
    let path = PathBuf::from("test.tsx");
    let result = parse_source(source, &path, "tsx").expect("should parse React hooks");

    let counter = result
        .functions
        .iter()
        .find(|f| f.name == "Counter")
        .expect("should find Counter");
    assert!(
        counter
            .annotations
            .iter()
            .any(|a| a == "react:hook:useState"),
        "Should detect useState hook, got: {:?}",
        counter.annotations
    );
    assert!(
        counter
            .annotations
            .iter()
            .any(|a| a == "react:hook:useEffect"),
        "Should detect useEffect hook, got: {:?}",
        counter.annotations
    );
    assert!(
        counter.annotations.iter().any(|a| a == "react:hook:useRef"),
        "Should detect useRef hook, got: {:?}",
        counter.annotations
    );
}

#[test]
fn test_decorator_extraction_ts() {
    let code = r#"
@Controller('/users')
class UserController {
    @Get('/')
    getUsers() {
        return [];
    }

    @Post('/create')
    createUser() {
        return {};
    }
}

class NoDecorators {
    hello() {}
}
"#;
    let path = PathBuf::from("test.ts");
    let result = parse_source(code, &path, "ts").expect("should parse decorated TypeScript");

    let controller = result
        .classes
        .iter()
        .find(|c| c.name == "UserController")
        .expect("should find UserController class");
    assert!(
        controller
            .annotations
            .iter()
            .any(|a| a.contains("Controller")),
        "UserController should have @Controller annotation, got: {:?}",
        controller.annotations
    );

    let no_dec = result
        .classes
        .iter()
        .find(|c| c.name == "NoDecorators")
        .expect("should find NoDecorators class");
    assert!(
        no_dec.annotations.is_empty(),
        "NoDecorators should have no annotations, got: {:?}",
        no_dec.annotations
    );

    // Check method-level decorators
    let get_users = result
        .functions
        .iter()
        .find(|f| f.name == "getUsers")
        .expect("should find getUsers method");
    assert!(
        get_users.annotations.iter().any(|a| a.contains("Get")),
        "getUsers should have @Get annotation, got: {:?}",
        get_users.annotations
    );

    let create_user = result
        .functions
        .iter()
        .find(|f| f.name == "createUser")
        .expect("should find createUser method");
    assert!(
        create_user.annotations.iter().any(|a| a.contains("Post")),
        "createUser should have @Post annotation, got: {:?}",
        create_user.annotations
    );
}

#[test]
fn test_export_detection_ts() {
    let code = r#"
export function publicFunc() {}

function privateFunc() {}

export class PublicClass {}

class PrivateClass {}
"#;
    let path = PathBuf::from("test.ts");
    let result = parse_source(code, &path, "ts").expect("should parse TS exports");

    let public = result
        .functions
        .iter()
        .find(|f| f.name == "publicFunc")
        .unwrap();
    assert!(
        public.annotations.iter().any(|a| a == "exported"),
        "export function should be exported, annotations: {:?}",
        public.annotations
    );

    let private = result
        .functions
        .iter()
        .find(|f| f.name == "privateFunc")
        .unwrap();
    assert!(
        !private.annotations.iter().any(|a| a == "exported"),
        "non-export function should NOT be exported"
    );

    let public_class = result
        .classes
        .iter()
        .find(|c| c.name == "PublicClass")
        .unwrap();
    assert!(
        public_class.annotations.iter().any(|a| a == "exported"),
        "export class should be exported, annotations: {:?}",
        public_class.annotations
    );

    let private_class = result
        .classes
        .iter()
        .find(|c| c.name == "PrivateClass")
        .unwrap();
    assert!(
        !private_class.annotations.iter().any(|a| a == "exported"),
        "non-export class should NOT be exported"
    );
}

#[test]
fn test_export_detection_js_default() {
    let code = r#"
export default function mainHandler() {}

function helperFunc() {}
"#;
    let path = PathBuf::from("test.js");
    let result = parse_source(code, &path, "js").expect("should parse JS default exports");

    let main = result
        .functions
        .iter()
        .find(|f| f.name == "mainHandler")
        .unwrap();
    assert!(
        main.annotations.iter().any(|a| a == "exported"),
        "export default function should be exported, annotations: {:?}",
        main.annotations
    );

    let helper = result
        .functions
        .iter()
        .find(|f| f.name == "helperFunc")
        .unwrap();
    assert!(
        !helper.annotations.iter().any(|a| a == "exported"),
        "non-export function should NOT be exported"
    );
}

#[test]
fn test_callback_argument_detection() {
    let code = r#"
function handler(req, res) {
    res.send('ok');
}

function transform(item) {
    return item.name;
}

app.get('/path', handler);
items.map(transform);
"#;
    let path = PathBuf::from("test.js");
    let result = parse_source(code, &path, "js").expect("should parse callback arguments");

    // "handler" should appear as a callee (called by module scope via app.get)
    let handler_called = result.calls.iter().any(|(_, callee)| callee == "handler");
    assert!(
        handler_called,
        "handler should be in calls list as callback arg. Calls: {:?}",
        result.calls
    );

    // "transform" should appear as a callee (called by module scope via items.map)
    let transform_called = result.calls.iter().any(|(_, callee)| callee == "transform");
    assert!(
        transform_called,
        "transform should be in calls list as callback arg. Calls: {:?}",
        result.calls
    );
}

#[test]
fn test_exported_class_methods_get_exported_annotation() {
    let source = r#"
export class UserService {
    public getUser(id: string) { return id; }
    private secretMethod() {}
    protected helperMethod() {}
    defaultMethod() {}
}

class InternalService {
    public doStuff() {}
}
"#;
    let path = PathBuf::from("service.ts");
    let result = parse_source(source, &path, "ts").expect("should parse TS");

    // Methods in exported class
    let get_user = result
        .functions
        .iter()
        .find(|f| f.name == "getUser")
        .expect("should find getUser");
    assert!(
        get_user.annotations.contains(&"exported".to_string()),
        "public method in exported class should be exported, got: {:?}",
        get_user.annotations
    );

    let secret = result
        .functions
        .iter()
        .find(|f| f.name == "secretMethod")
        .expect("should find secretMethod");
    assert!(
        !secret.annotations.contains(&"exported".to_string()),
        "private method should not be exported"
    );

    let helper = result
        .functions
        .iter()
        .find(|f| f.name == "helperMethod")
        .expect("should find helperMethod");
    assert!(
        !helper.annotations.contains(&"exported".to_string()),
        "protected method should not be exported"
    );

    let default_m = result
        .functions
        .iter()
        .find(|f| f.name == "defaultMethod")
        .expect("should find defaultMethod");
    assert!(
        default_m.annotations.contains(&"exported".to_string()),
        "default (public) method in exported class should be exported, got: {:?}",
        default_m.annotations
    );

    // Methods in non-exported class
    let do_stuff = result
        .functions
        .iter()
        .find(|f| f.name == "doStuff")
        .expect("should find doStuff");
    assert!(
        !do_stuff.annotations.contains(&"exported".to_string()),
        "method in non-exported class should not be exported"
    );
}