devscout-rs 0.9.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
use super::*;

// C# cannot name a type declared in an assembly the site's project does not
// reference, so with a project model the namespace-level walk passes over a
// level's own type, or a using-imported candidate, declared only in a project
// outside the site project's reference closure. Every fixture except the
// graph-wide-unique one declares two same-named types, so the unique-name
// fallback can never be the step that answers.

fn member_targets<'a>(g: &'a Graph, from: &str, member: &str) -> Vec<&'a str> {
    member_edges_named(g, from, member)
        .into_iter()
        .map(|(to, _)| to)
        .collect()
}

fn with_model(files: &[(String, Fragment)], units: Vec<crate::project::Unit>) -> Graph {
    let model = model_of(units);
    resolve_graph_with_model(&no_git_root(), files, &[], Some(&model), None)
}

const COMPOSITE_FILTER_FIXTURE: &[(&str, &str)] = &[
    (
        "src/Abs/Filters/CompositeFilter.cs",
        "namespace App.Filters { public class CompositeFilter<T> { public void Includes(T x) { } } }",
    ),
    (
        "src/Core/Configuration/CompositeFilter.cs",
        "namespace App.Configuration { public class CompositeFilter<T> { public void Includes(T x) { } } }",
    ),
    (
        "src/Abs/Configuration/ExceptionSpecification.cs",
        "namespace App.Configuration\n{\n    using Filters;\n\n    public class ExceptionSpecification\n    {\n        readonly CompositeFilter<int> _f;\n\n        public void Include(int x)\n        {\n            _f.Includes(x);\n        }\n    }\n}\n",
    ),
];

fn abs_and_core() -> Vec<crate::project::Unit> {
    vec![
        unit("src/Abs/Abs.csproj", &[], false),
        unit("src/Core/Core.csproj", &["src/Abs/Abs.csproj"], false),
    ]
}

#[test]
fn a_namespace_level_type_in_an_unreferenced_project_yields_to_the_same_level_using() {
    let files = fragments_for(COMPOSITE_FILTER_FIXTURE);
    let site = "src/Abs/Configuration/ExceptionSpecification.cs";

    let g = with_model(&files, abs_and_core());
    assert_eq!(
        type_edge_targets_from(&g, site),
        vec!["App.Filters.CompositeFilter"]
    );
    assert_eq!(
        member_targets(&g, site, "Includes"),
        vec!["App.Filters.CompositeFilter"]
    );

    let g = resolve_graph(&no_git_root(), &files);
    assert_eq!(
        type_edge_targets_from(&g, site),
        vec!["App.Configuration.CompositeFilter"]
    );
    assert_eq!(
        member_targets(&g, site, "Includes"),
        vec!["App.Configuration.CompositeFilter"]
    );
}

#[test]
fn a_namespace_level_enum_in_an_unreferenced_project_yields_to_a_using_read_from_that_level() {
    let files = fragments_for(&[
        (
            "src/Rmq/Topology/Options.cs",
            "namespace App { public enum Options { None, MaintainHierarchy } }",
        ),
        (
            "src/Amq/Topology/Options.cs",
            "namespace App.Amq.Topology { public enum Options { None, MaintainHierarchy } }",
        ),
        (
            "src/Amq/Topology/IPublishTopology.cs",
            "namespace App\n{\n    using Amq.Topology;\n\n    public class PublishTopology\n    {\n        Options _options;\n\n        public void Set()\n        {\n            var o = Options.MaintainHierarchy;\n        }\n    }\n}\n",
        ),
    ]);
    let site = "src/Amq/Topology/IPublishTopology.cs";
    let g = with_model(
        &files,
        vec![
            unit("src/Amq/Amq.csproj", &[], false),
            unit("src/Rmq/Rmq.csproj", &[], false),
        ],
    );
    assert_eq!(
        type_edge_targets_from(&g, site),
        vec!["App.Amq.Topology.Options"]
    );
    assert_eq!(
        member_targets(&g, site, "MaintainHierarchy"),
        vec!["App.Amq.Topology.Options.MaintainHierarchy"]
    );
}

const FEATURE_WIDGET_SITE: (&str, &str) = (
    "src/Site/Feature/Panel.cs",
    "namespace App.Feature\n{\n    public class Panel\n    {\n        Widget _widget;\n    }\n}\n",
);

#[test]
fn an_unreachable_enclosing_namespace_type_yields_to_an_outer_level_type() {
    let files = fragments_for(&[
        (
            "src/Other/Feature/Widget.cs",
            "namespace App.Feature { public class Widget { } }",
        ),
        (
            "src/Base/Widget.cs",
            "namespace App { public class Widget { } }",
        ),
        FEATURE_WIDGET_SITE,
    ]);
    let site = FEATURE_WIDGET_SITE.0;
    let g = with_model(
        &files,
        vec![
            unit("src/Site/Site.csproj", &["src/Base/Base.csproj"], false),
            unit("src/Base/Base.csproj", &[], false),
            unit("src/Other/Other.csproj", &[], false),
        ],
    );
    assert_eq!(type_edge_targets_from(&g, site), vec!["App.Widget"]);

    let g = resolve_graph(&no_git_root(), &files);
    assert_eq!(type_edge_targets_from(&g, site), vec!["App.Feature.Widget"]);
}

#[test]
fn an_unreachable_candidate_leaves_one_using_at_the_level_and_no_ambiguity() {
    let files = fragments_for(&[
        ("src/Near/T.cs", "namespace X { public class T { } }"),
        ("src/Far/T.cs", "namespace Y { public class T { } }"),
        (
            "src/Site/Holder.cs",
            "namespace A\n{\n    using X;\n    using Y;\n\n    public class Holder\n    {\n        T _t;\n    }\n}\n",
        ),
    ]);
    let site = "src/Site/Holder.cs";
    let g = with_model(
        &files,
        vec![
            unit("src/Site/Site.csproj", &["src/Near/Near.csproj"], false),
            unit("src/Near/Near.csproj", &[], false),
            unit("src/Far/Far.csproj", &[], false),
        ],
    );
    assert_eq!(type_edge_targets_from(&g, site), vec!["X.T"]);
    assert!(ambiguous_edges_from(&g, site).is_empty());
    assert_eq!(g.stats.ambiguous_count, 0);
}

#[test]
fn an_enclosing_namespace_type_still_answers_first_when_both_candidates_are_reachable() {
    let files = fragments_for(&[
        (
            "src/Framework/TestInstance.cs",
            "namespace App.Framework.Sagas { public class TestInstance { public int Key { get; set; } } }",
        ),
        (
            "tests/Tests/Container_Specs.cs",
            "namespace App.Tests\n{\n    namespace ContainerTests\n    {\n        using Framework.Sagas;\n\n        public class TestInstance { public int Key { get; set; } }\n\n        public class Spec\n        {\n            TestInstance _instance;\n\n            public int Read(TestInstance x)\n            {\n                return x.Key;\n            }\n        }\n    }\n}\n",
        ),
    ]);
    let site = "tests/Tests/Container_Specs.cs";
    let g = with_model(
        &files,
        vec![
            unit("src/Framework/Framework.csproj", &[], false),
            unit(
                "tests/Tests/Tests.csproj",
                &["src/Framework/Framework.csproj"],
                true,
            ),
        ],
    );
    let types = type_edge_targets_from(&g, site);
    assert!(
        types.contains(&"App.Tests.ContainerTests.TestInstance"),
        "{types:?}"
    );
    assert!(
        !types.contains(&"App.Framework.Sagas.TestInstance"),
        "{types:?}"
    );
    assert_eq!(
        member_targets(&g, site, "Key"),
        vec!["App.Tests.ContainerTests.TestInstance"]
    );
}

#[test]
fn an_unowned_site_file_keeps_the_unfiltered_answer() {
    let site = ("loose/Panel.cs", FEATURE_WIDGET_SITE.1);
    let files = fragments_for(&[
        (
            "src/Other/Feature/Widget.cs",
            "namespace App.Feature { public class Widget { } }",
        ),
        (
            "src/Base/Widget.cs",
            "namespace App { public class Widget { } }",
        ),
        site,
    ]);
    let g = with_model(
        &files,
        vec![
            unit("src/Base/Base.csproj", &[], false),
            unit("src/Other/Other.csproj", &[], false),
        ],
    );
    assert_eq!(
        type_edge_targets_from(&g, site.0),
        vec!["App.Feature.Widget"]
    );
}

#[test]
fn an_unowned_declaring_file_keeps_the_unfiltered_answer() {
    let files = fragments_for(&[
        (
            "loose/Feature/Widget.cs",
            "namespace App.Feature { public class Widget { } }",
        ),
        (
            "src/Base/Widget.cs",
            "namespace App { public class Widget { } }",
        ),
        FEATURE_WIDGET_SITE,
    ]);
    let g = with_model(
        &files,
        vec![
            unit("src/Site/Site.csproj", &["src/Base/Base.csproj"], false),
            unit("src/Base/Base.csproj", &[], false),
        ],
    );
    assert_eq!(
        type_edge_targets_from(&g, FEATURE_WIDGET_SITE.0),
        vec!["App.Feature.Widget"]
    );
}

#[test]
fn a_partial_type_with_one_part_in_a_reachable_project_is_not_passed_over() {
    let files = fragments_for(&[
        (
            "src/Other/Feature/Widget.cs",
            "namespace App.Feature { public partial class Widget { } }",
        ),
        (
            "src/Near/Feature/Widget.cs",
            "namespace App.Feature { public partial class Widget { } }",
        ),
        (
            "src/Base/Widget.cs",
            "namespace App { public class Widget { } }",
        ),
        FEATURE_WIDGET_SITE,
    ]);
    let g = with_model(
        &files,
        vec![
            unit(
                "src/Site/Site.csproj",
                &["src/Base/Base.csproj", "src/Near/Near.csproj"],
                false,
            ),
            unit("src/Base/Base.csproj", &[], false),
            unit("src/Near/Near.csproj", &[], false),
            unit("src/Other/Other.csproj", &[], false),
        ],
    );
    assert_eq!(
        type_edge_targets_from(&g, FEATURE_WIDGET_SITE.0),
        vec!["App.Feature.Widget"]
    );
}

#[test]
fn a_referenced_test_project_is_inside_the_closure_of_a_non_test_site() {
    let files = fragments_for(&[
        (
            "tests/Helpers/Feature/Widget.cs",
            "namespace App.Feature { public class Widget { } }",
        ),
        (
            "src/Base/Widget.cs",
            "namespace App { public class Widget { } }",
        ),
        FEATURE_WIDGET_SITE,
    ]);
    let g = with_model(
        &files,
        vec![
            unit(
                "src/Site/Site.csproj",
                &["src/Base/Base.csproj", "tests/Helpers/Helpers.csproj"],
                false,
            ),
            unit("src/Base/Base.csproj", &[], false),
            unit("tests/Helpers/Helpers.csproj", &[], true),
        ],
    );
    assert_eq!(
        type_edge_targets_from(&g, FEATURE_WIDGET_SITE.0),
        vec!["App.Feature.Widget"]
    );
}

#[test]
fn a_base_named_at_its_declaration_resolves_within_the_declaring_project_closure() {
    let files = fragments_for(&[
        (
            "src/Other/Lib/Base.cs",
            "namespace App.Lib { public class Base { public void Run() { } } }",
        ),
        (
            "src/Lib/Bases/Base.cs",
            "namespace App.Lib.Bases { public class Base { public void Run() { } } }",
        ),
        (
            "src/Lib/Derived.cs",
            "namespace App.Lib\n{\n    using Bases;\n\n    public class Derived : Base { }\n}\n",
        ),
        (
            "src/Site/Runner.cs",
            "namespace App.Site\n{\n    using App.Lib;\n\n    public class Runner\n    {\n        public void Go(Derived d)\n        {\n            d.Run();\n        }\n    }\n}\n",
        ),
    ]);
    let site = "src/Site/Runner.cs";
    let g = with_model(
        &files,
        vec![
            unit("src/Site/Site.csproj", &["src/Lib/Lib.csproj"], false),
            unit("src/Lib/Lib.csproj", &[], false),
            unit("src/Other/Other.csproj", &[], false),
        ],
    );
    assert_eq!(member_targets(&g, site, "Run"), vec!["App.Lib.Bases.Base"]);

    let g = resolve_graph(&no_git_root(), &files);
    assert_eq!(member_targets(&g, site, "Run"), vec!["App.Lib.Base"]);
}

#[test]
fn a_passed_over_type_with_a_graph_unique_name_still_binds_at_the_unique_name_step() {
    let files = fragments_for(&[
        (
            "src/Other/Feature/Gadget.cs",
            "namespace App.Feature { public class Gadget { } }",
        ),
        (
            "src/Site/Feature/Panel.cs",
            "namespace App.Feature\n{\n    public class Panel\n    {\n        Gadget _gadget;\n    }\n}\n",
        ),
    ]);
    let g = with_model(
        &files,
        vec![
            unit("src/Site/Site.csproj", &[], false),
            unit("src/Other/Other.csproj", &[], false),
        ],
    );
    assert_eq!(
        type_edge_targets_from(&g, "src/Site/Feature/Panel.cs"),
        vec!["App.Feature.Gadget"]
    );
}

#[test]
fn a_directive_whose_own_reading_is_passed_over_is_not_read_further_out() {
    let files = fragments_for(&[
        ("src/Far/T.cs", "namespace A.B.Sub { public class T { } }"),
        ("src/Near/T.cs", "namespace Sub { public class T { } }"),
        ("src/Near/Other/T.cs", "namespace Other { public class T { } }"),
        (
            "src/Site/Holder.cs",
            "namespace A.B\n{\n    using Sub;\n\n    public class Holder\n    {\n        T _t;\n    }\n}\n",
        ),
    ]);
    let site = "src/Site/Holder.cs";
    let g = with_model(
        &files,
        vec![
            unit("src/Site/Site.csproj", &["src/Near/Near.csproj"], false),
            unit("src/Near/Near.csproj", &[], false),
            unit("src/Far/Far.csproj", &[], false),
        ],
    );
    assert!(
        type_edge_targets_from(&g, site).is_empty(),
        "{:?}",
        type_edge_targets_from(&g, site)
    );
    let ambiguous = ambiguous_edges_from(&g, site);
    assert_eq!(ambiguous.len(), 1, "{ambiguous:?}");
    assert_eq!(ambiguous[0].2, vec!["Other.T", "Sub.T"]);
}