devscout-rs 0.10.0

Fast code index for C# and TypeScript codebases -- map, find, refs, impact from the CLI.
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
use super::*;

// Real C# through this crate's own extractor, so a fact that never gets
// recorded fails here rather than passing vacuously.

// A receiver DECLARED with a generic-final qualified type reaches the ladder's
// exact-qualified step with its type-argument list still attached, and no def
// id carries a list. Every fixture below therefore puts an `App`-rooted and a
// `Lib`-rooted twin at the SAME written tail, so the enclosing-namespace walk
// is the only step that can answer and the dotted-suffix step can only answer
// ambiguous -- which is what makes a lost enclosing-namespace decision
// observable at all.

fn line_of(source: &str, marker: &str) -> usize {
    source
        .lines()
        .position(|line| line.contains(marker))
        .expect("fixture marker is present")
        + 1
}

/// One file's precise uses-member edges as (target id, line), sorted.
fn site_edges<'a>(g: &'a Graph, file: &str) -> Vec<(&'a str, usize)> {
    let mut edges = member_edges_from(g, file);
    edges.sort_unstable();
    edges
}

const BOX_TWINS: &[(&str, &str)] = &[
    (
        "App/Widgets/Box.cs",
        "namespace App.Widgets { public class Box<T> { public void Render() { } } }",
    ),
    (
        "App/Widgets/Pair.cs",
        "namespace App.Widgets { public class Pair<T1, T2> { public void Render() { } } }",
    ),
    (
        "App/Widgets/Gadget.cs",
        "namespace App.Widgets { public class Gadget { public void Render() { } } }",
    ),
    (
        "Lib/Widgets/Box.cs",
        "namespace Lib.Widgets { public class Box<T> { public void Render() { } } }",
    ),
    (
        "Lib/Widgets/Pair.cs",
        "namespace Lib.Widgets { public class Pair<T1, T2> { public void Render() { } } }",
    ),
    (
        "Lib/Widgets/Gadget.cs",
        "namespace Lib.Widgets { public class Gadget { public void Render() { } } }",
    ),
];

const SITE: &str = r#"
namespace App
{
    public class Site
    {
        public void Run(Widgets.Box<int> parameter)
        {
            Widgets.Box<int> local = null;
            Widgets.Box<int>? nullable = null;
            Widgets.Pair<int, string> pair = null;
            Widgets.Gadget gadget = null;
            local.Render();
            parameter.Render();
            nullable.Render();
            pair.Render();
            gadget.Render();
            if (parameter is Widgets.Box<int> pattern)
            {
                pattern.Render();
            }
        }
    }
}
"#;

fn site_graph() -> Graph {
    let mut files = fragments_for(BOX_TWINS);
    files.push((
        "App/Site.cs".to_string(),
        crate::graph::fragment_from_extraction(&crate::extract::extract(SITE)),
    ));
    resolve_graph(&no_git_root(), &files)
}

#[test]
fn generic_receiver_path_ac_1_site_declared_receivers_take_the_namespace_walk() {
    let g = site_graph();
    assert_eq!(
        site_edges(&g, "App/Site.cs"),
        vec![
            ("App.Widgets.Box", line_of(SITE, "local.Render()")),
            ("App.Widgets.Box", line_of(SITE, "parameter.Render()")),
            ("App.Widgets.Box", line_of(SITE, "nullable.Render()")),
            ("App.Widgets.Box", line_of(SITE, "pattern.Render()")),
            ("App.Widgets.Gadget", line_of(SITE, "gadget.Render()")),
            ("App.Widgets.Pair", line_of(SITE, "pair.Render()")),
        ],
        "every declared receiver binds the App-rooted twin, as the non-generic local does"
    );
}

#[test]
fn generic_receiver_path_ac_1_binds_precisely_and_never_the_lib_twin() {
    let g = site_graph();
    let tiers: Vec<Option<HeuristicTier>> = member_edges_with_file(&g, "App/Site.cs")
        .into_iter()
        .map(|(_, _, _, tier)| tier)
        .collect();
    assert!(
        tiers.iter().all(Option::is_none),
        "each read is precise -- the tier the non-generic local gets in the same fixture"
    );
    assert!(
        heuristic_member_edges_from(&g, "App/Site.cs").is_empty(),
        "no receiver here reaches a guess tier"
    );
    for twin in ["Lib.Widgets.Box", "Lib.Widgets.Pair", "Lib.Widgets.Gadget"] {
        assert!(
            !any_member_edge_targets(&g, twin),
            "the sibling root earns no edge at all, precise or guessed"
        );
    }
}

const OTHER_DECLARATION_SHAPES: &str = r#"
namespace App
{
    public class Site
    {
        public void Run(System.Collections.Generic.IEnumerable<Widgets.Box<int>> boxes)
        {
            foreach (Widgets.Box<int> item in boxes)
            {
                item.Render();
            }
            try
            {
                Widgets.Box<int> ignored = null;
            }
            catch (Widgets.Box<int> fault)
            {
                fault.Render();
            }
            using (Widgets.Box<int> scoped = null)
            {
                scoped.Render();
            }
            TryFill(out Widgets.Box<int> produced);
            produced.Render();
        }

        private static void TryFill(out Widgets.Box<int> value)
        {
            value = null;
        }
    }
}
"#;

#[test]
fn generic_receiver_path_ac_1_foreach_catch_using_and_out_receivers_take_the_walk_too() {
    let mut files = fragments_for(BOX_TWINS);
    files.push((
        "App/Site.cs".to_string(),
        crate::graph::fragment_from_extraction(&crate::extract::extract(OTHER_DECLARATION_SHAPES)),
    ));
    let g = resolve_graph(&no_git_root(), &files);
    let mut expected: Vec<(&str, usize)> = [
        "item.Render()",
        "fault.Render()",
        "scoped.Render()",
        "produced.Render()",
    ]
    .iter()
    .map(|marker| ("App.Widgets.Box", line_of(OTHER_DECLARATION_SHAPES, marker)))
    .collect();
    expected.sort_unstable();
    assert_eq!(
        site_edges(&g, "App/Site.cs"),
        expected,
        "every declaration a receiver may be written on reaches the same walk"
    );
}

const SIBLING_DECLARING: &str = r#"
namespace App
{
    public partial class Host
    {
        private Widgets.Box<int> _box;
    }
}
"#;

const SIBLING_READING: &str = r#"
namespace App
{
    public partial class Host
    {
        public void Run()
        {
            _box.Render();
        }
    }
}
"#;

const BASE_DECLARING: &str = r#"
namespace App
{
    public class Base
    {
        public Widgets.Box<int> Current { get; }
    }
}
"#;

const DERIVED_READING: &str = r#"
namespace App
{
    public class Derived : Base
    {
        public void Run()
        {
            Current.Render();
        }
    }
}
"#;

// The two fallbacks that reach the ladder with no recorded arity: a field
// merged in from a partial sibling file and a property read off a base class.
// Both carry the written path and nothing else, so the written list's own
// argument count is the only gate available to them.
#[test]
fn generic_receiver_path_ac_2_field_and_property_fallbacks_bind_the_namespace_walk() {
    let source =
        |text: &str| crate::graph::fragment_from_extraction(&crate::extract::extract(text));
    let mut files = fragments_for(BOX_TWINS);
    files.push((
        "App/Host.Declaring.cs".to_string(),
        source(SIBLING_DECLARING),
    ));
    files.push(("App/Host.Sibling.cs".to_string(), source(SIBLING_READING)));
    files.push(("App/Base.cs".to_string(), source(BASE_DECLARING)));
    files.push(("App/Derived.cs".to_string(), source(DERIVED_READING)));
    let g = resolve_graph(&no_git_root(), &files);
    assert_eq!(
        site_edges(&g, "App/Host.Sibling.cs"),
        vec![("App.Widgets.Box", line_of(SIBLING_READING, "_box.Render()"))],
        "a field read from a partial sibling file binds the namespace walk"
    );
    assert_eq!(
        site_edges(&g, "App/Derived.cs"),
        vec![(
            "App.Widgets.Box",
            line_of(DERIVED_READING, "Current.Render()")
        )],
        "a property declared on a base class and read from a derived one binds it too"
    );
}

const BOX_PLAIN: (&str, &str) = (
    "App/Widgets/BoxPlain.cs",
    "namespace App.Widgets { public class Box { public void Render() { } } }",
);

const BOX_GENERIC: (&str, &str) = (
    "App/Widgets/BoxGeneric.cs",
    "namespace App.Widgets { public class Box<T> { public void Render() { } } }",
);

const ARITY_SITE: &str = r#"
namespace App
{
    public class Site
    {
        public void Run()
        {
            Widgets.Box<int> generic = null;
            Widgets.Box plain = null;
            Widgets.Box<int, string> unmatched = null;
            generic.Render();
            plain.Render();
            unmatched.Render();
        }
    }
}
"#;

const SHAPES_DECLARING: &str = r#"
namespace App
{
    public partial class Shapes
    {
        private Widgets.Box<int> _one;
        private Widgets.Box<List<int>> _nested;
        private Widgets.Box<(int, string)> _tuple;
        private Widgets.Box<int[,]> _array;
    }
}
"#;

const SHAPES_READING: &str = r#"
namespace App
{
    public partial class Shapes
    {
        public void Run()
        {
            _one.Render();
            _nested.Render();
            _tuple.Render();
            _array.Render();
        }
    }
}
"#;

/// The same fixture with the non-generic and the generic `Box` swapped in the
/// file order, so neither declaration order can decide which def an arity-blind
/// lookup would find.
fn arity_graph(plain_first: bool) -> Graph {
    let source =
        |text: &str| crate::graph::fragment_from_extraction(&crate::extract::extract(text));
    let order = if plain_first {
        [BOX_PLAIN, BOX_GENERIC]
    } else {
        [BOX_GENERIC, BOX_PLAIN]
    };
    let mut files: Vec<(String, Fragment)> = order
        .iter()
        .map(|(path, text)| ((*path).to_string(), source(text)))
        .collect();
    files.push(("Lib/Widgets/Box.cs".to_string(), source(BOX_TWINS[3].1)));
    files.push(("App/Site.cs".to_string(), source(ARITY_SITE)));
    files.push((
        "App/Shapes.Declaring.cs".to_string(),
        source(SHAPES_DECLARING),
    ));
    files.push(("App/Shapes.Reading.cs".to_string(), source(SHAPES_READING)));
    resolve_graph(&no_git_root(), &files)
}

/// One file's PRECISE member edges as (target id, target file, line) -- the
/// target file is what tells two defs sharing one id apart, and the tier filter
/// is what keeps a guessed edge out of a claim about binding.
fn edges_with_file<'a>(g: &'a Graph, file: &str) -> Vec<(&'a str, &'a str, usize)> {
    let mut edges: Vec<(&str, &str, usize)> = member_edges_with_file(g, file)
        .into_iter()
        .filter(|(_, _, _, tier)| tier.is_none())
        .map(|(to, line, to_file, _)| (to, to_file, line))
        .collect();
    edges.sort_unstable();
    edges
}

// `Box` and `Box<T>` share one id and one arity-blind lookup slot, so the
// count a lookup is keyed by is the whole difference between binding the def
// the declaration names and binding whichever sibling the index met first.
#[test]
fn generic_receiver_path_ac_3_the_declared_arity_selects_the_sibling() {
    for plain_first in [true, false] {
        let g = arity_graph(plain_first);
        assert_eq!(
            edges_with_file(&g, "App/Site.cs"),
            vec![
                (
                    "App.Widgets.Box",
                    "App/Widgets/BoxGeneric.cs",
                    line_of(ARITY_SITE, "generic.Render()")
                ),
                (
                    "App.Widgets.Box",
                    "App/Widgets/BoxPlain.cs",
                    line_of(ARITY_SITE, "plain.Render()")
                ),
            ],
            "`Box<int>` takes the one-parameter def and bare `Box` the arity-zero one, \
             whichever order the two were declared in"
        );
        assert!(
            !edges_with_file(&g, "App/Site.cs")
                .iter()
                .any(|(_, _, line)| *line == line_of(ARITY_SITE, "unmatched.Render()")),
            "no two-parameter `Box` is declared, so `Box<int, string>` earns no precise edge"
        );
    }
}

#[test]
fn generic_receiver_path_ac_3_the_written_count_gates_the_arity_less_fallback() {
    for plain_first in [true, false] {
        let g = arity_graph(plain_first);
        assert_eq!(
            edges_with_file(&g, "App/Shapes.Reading.cs")
                .into_iter()
                .map(|(to, to_file, line)| (to, to_file, line))
                .collect::<Vec<_>>(),
            [
                "_one.Render()",
                "_nested.Render()",
                "_tuple.Render()",
                "_array.Render()"
            ]
            .iter()
            .map(|marker| (
                "App.Widgets.Box",
                "App/Widgets/BoxGeneric.cs",
                line_of(SHAPES_READING, marker)
            ))
            .collect::<Vec<_>>(),
            "with no recorded arity the written count decides, and a nested list, a tuple \
             and an array each count one argument"
        );
    }
}

const QUALIFIED_TWINS: &[(&str, &str)] = &[BOX_TWINS[0], BOX_TWINS[3]];

fn qualified_receiver_ref(text: &str, args: Option<&[&str]>) -> FragRef {
    FragRef {
        receiver_type: Some("Box".into()),
        receiver_type_qualified: Some(text.into()),
        receiver_args: args.map(|list| list.iter().map(|a| (*a).to_string()).collect()),
        ..member_ref("box", None, "Render", "App")
    }
}

fn probe_graph(text: &str, args: Option<&[&str]>) -> Graph {
    let mut files = fragments_for(QUALIFIED_TWINS);
    files.push((
        "App/Site.cs".to_string(),
        frag(
            vec![def("App.Site", "Site", "App", "class")],
            vec![],
            vec![qualified_receiver_ref(text, args)],
        ),
    ));
    resolve_graph(&no_git_root(), &files)
}

// One catch-all refusal: a written path that is not exactly one well-formed
// list closing its final segment keeps step 1's verbatim lookup, so a shape
// the rule cannot read answers the way the base answers it. Every refused
// spelling below would reach `App.Widgets.Box` through the namespace walk if
// it were accepted, which is what makes "the base's answer" falsifiable.
#[test]
fn generic_receiver_path_ac_4_every_refused_shape_keeps_the_base_answer() {
    for (text, args) in [
        ("Widgets.Box<>", Some(&[][..])),
        ("Widgets.Box<int,>", Some(&["int"][..])),
        ("Widgets.Box<(int, string>", Some(&["*"][..])),
        ("Widgets.Box<int>>", Some(&["int"][..])),
        ("Widgets.Box<int]>", Some(&["int"][..])),
        ("Widgets.Box<[int", Some(&["int"][..])),
        ("global::App.Widgets.Box<int>", None),
    ] {
        assert_eq!(
            member_edge_targets(&probe_graph(text, args)),
            Vec::<&str>::new(),
            "`{text}` is not one well-formed list closing the final segment, so step 1 \
             keeps the verbatim lookup the base answers with"
        );
    }
}

// Every index the list is read at is an ASCII delimiter byte, so no slice can
// land inside a multi-byte character, and a well-formed list longer than any
// decl it could name still only fails to match.
#[test]
fn generic_receiver_path_ac_4_a_non_ascii_or_over_long_path_answers_empty() {
    for (text, args) in [
        ("Wîdgets.Box<int>", Some(&["int"][..])),
        ("Widgets.Box<int, string, bool>", Some(&["*", "*", "*"][..])),
    ] {
        assert_eq!(
            member_edge_targets(&probe_graph(text, args)),
            Vec::<&str>::new(),
            "`{text}` names nothing this graph declares"
        );
    }
}

#[test]
fn generic_receiver_path_ac_3_the_recorded_arity_outranks_the_written_count() {
    assert_eq!(
        member_edge_targets(&probe_graph("Widgets.Box<int>", Some(&["int"]))),
        vec!["App.Widgets.Box"],
        "the control the three cases below are read against: one well-formed list \
         closing the final segment takes the namespace walk"
    );
    assert_eq!(
        member_edge_targets(&probe_graph("Widgets.Box<int>", Some(&[]))),
        vec!["App.Widgets.Box"],
        "a recorded zero is the extractor's sentinel for an argument shape it could not \
         read, so the written list's own count decides"
    );
    assert_eq!(
        member_edge_targets(&probe_graph("Widgets.Box<int>", Some(&["*", "*"]))),
        Vec::<&str>::new(),
        "a probe recorded with two arguments is never answered by a one-parameter def"
    );
    assert_eq!(
        member_edge_targets(&probe_graph("Widgets.Box<int, string>", Some(&["*", "*"]))),
        Vec::<&str>::new(),
        "and no two-parameter `Box` is declared for it to reach"
    );
}

const NESTED_DECLARED: &[(&str, &str)] = &[
    (
        "App/Widgets/Outer.cs",
        "namespace App.Widgets { public class Outer<T> { public class Inner { public void Render() { } } } }",
    ),
    (
        "App/Widgets/Outer/Inner.cs",
        "namespace App.Widgets.Outer { public class Inner { public void Render() { } } }",
    ),
];

const NESTED_SITE: &str = r#"
namespace App
{
    public class Site
    {
        public void Run(Widgets.Outer<int>.Inner inner)
        {
            inner.Render();
        }
    }
}
"#;

// A list on an earlier segment belongs to a nested type's CONTAINER, and def
// ids join nesting with `+`, so a `.`-joined candidate can never name one.
// Stripping that list instead would let the NAMESPACE-level
// `App.Widgets.Outer.Inner` answer for the nested `App.Widgets.Outer+Inner`;
// both are declared here so the wrong lookup would find exactly the wrong one.
#[test]
fn generic_receiver_path_ac_4_a_list_on_an_earlier_segment_keeps_the_verbatim_lookup() {
    let mut files = fragments_for(NESTED_DECLARED);
    files.push((
        "App/Site.cs".to_string(),
        crate::graph::fragment_from_extraction(&crate::extract::extract(NESTED_SITE)),
    ));
    let g = resolve_graph(&no_git_root(), &files);
    assert_eq!(
        site_edges(&g, "App/Site.cs"),
        Vec::<(&str, usize)>::new(),
        "the non-final-segment list is not stripped into a lookup that finds the \
         namespace-level `Inner`"
    );
}

const ALIAS_SITE: &str = r#"
namespace App
{
    public class Site
    {
        public void Run(global::App.Widgets.Box<int> box)
        {
            box.Render();
        }
    }
}
"#;

// `global::` is a qualifier rather than a segment, so a path carrying one is
// never a final-segment list however its tail reads. The alias-aware steps
// answer it exactly as before.
#[test]
fn generic_receiver_path_ac_4_an_alias_qualified_receiver_keeps_its_answer() {
    let mut files = fragments_for(QUALIFIED_TWINS);
    files.push((
        "App/Site.cs".to_string(),
        crate::graph::fragment_from_extraction(&crate::extract::extract(ALIAS_SITE)),
    ));
    let g = resolve_graph(&no_git_root(), &files);
    assert_eq!(
        site_edges(&g, "App/Site.cs"),
        vec![("App.Widgets.Box", line_of(ALIAS_SITE, "box.Render()"))],
        "the alias qualifier is dropped by the alias-aware step, not by this rule"
    );
}