fallow-engine 3.49.0

Typed analysis engine facade for fallow consumers
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
//! Shallow directories: directories whose interface is as large as what they
//! hide.
//!
//! An interface item is a module-scope function or an exported class. A
//! directory shows an item when a file outside the directory uses it, and it
//! hides an item that no outside file uses. The unit is recursive: a
//! directory holds the items of its subdirectories too.
//!
//! The directories are checked from the deepest up. A reported directory
//! claims its items, so a parent directory counts only the items that no
//! reported child claimed. Without this rule one wide child makes each of
//! its parents shallow too.

use std::path::{Path, PathBuf};

use fallow_config::ShallowDirectoriesConfig;
use fallow_graph::graph::{ExportNamespace, ModuleGraph, ModuleNode, ReExportEdge, ReferenceKind};
use fallow_output::{
    ShallowDirectoriesReport, ShallowDirectoryFinding, ShallowDirectoryItem,
    ShallowDirectoryItemKind,
};
use globset::{GlobSet, GlobSetBuilder};
use rustc_hash::{FxHashMap, FxHashSet};

use crate::discover::FileId;
use crate::source::ModuleInfo;
use fallow_types::extract::{ExportName, MemberKind};

/// Shown items listed per directory. The rest is a count.
const MAX_LISTED_ITEMS: usize = 7;

/// Below this mean of shown items per file, a directory is a catalog.
const MIN_ITEMS_PER_FILE: f64 = 1.5;

/// A directory whose shown items come from fewer files is a file finding.
const MIN_SHOWN_FILES: usize = 2;

/// From this share of shown items with one parameter list, the directory is
/// a family of functions with one role.
const MAX_FAMILY_SHARE: f64 = 0.3;

/// A family needs at least this many functions with one parameter list.
const MIN_FAMILY_SIZE: usize = 3;

/// Below this mean of outside files per shown item, the interface has few
/// readers.
const MIN_MEAN_OUTSIDE_FILES: f64 = 2.0;

/// The parameter name that [`signature_of`] writes for a destructured
/// parameter.
const DESTRUCTURED_PARAM: &str = "{}";

/// File extensions that can be an anchor file.
const SCRIPT_EXTENSIONS: &[&str] = &["ts", "tsx", "mts", "cts", "js", "jsx", "mjs", "cjs"];

/// File extensions whose PascalCase functions and classes are UI components.
const COMPONENT_FILE_EXTENSIONS: &[&str] = &["tsx", "jsx"];

/// File-name markers of story files. A story renders a component the way a
/// test does, so it is not an outside user.
const STORY_FILE_MARKERS: &[&str] = &[".stories.", ".story."];

/// Inputs of [`find_shallow_directories`].
pub(super) struct ShallowDirectoriesInput<'a> {
    pub(super) graph: &'a ModuleGraph,
    pub(super) modules: &'a [ModuleInfo],
    pub(super) root: &'a Path,
    pub(super) config: &'a ShallowDirectoriesConfig,
}

/// One interface item before the per-directory counts.
struct Item {
    /// The exported name, or the local binding (else the file stem) of a
    /// default export.
    name: String,
    is_default: bool,
    file: FileId,
    /// Directory of the declaring file, relative to the root.
    dir: PathBuf,
    line: u32,
    kind: ShallowDirectoryItemKind,
    members: Option<u32>,
    /// Directories (relative to the root) of the files that use the item and
    /// count as outside users of some directory: no test, story or the
    /// declaring file itself.
    users: Vec<(FileId, PathBuf)>,
    /// Parameter names of a function item, for the shape rule.
    signature: Option<String>,
}

impl Item {
    /// Whether a file outside `dir` uses the item. `dir` holds the item.
    fn shown_in(&self, dir: &Path) -> bool {
        self.users.iter().any(|(_, user)| !user.starts_with(dir))
    }

    fn outside_files(&self, dir: &Path) -> u32 {
        let count = self
            .users
            .iter()
            .filter(|(_, user)| !user.starts_with(dir))
            .count();
        u32::try_from(count).unwrap_or(u32::MAX)
    }
}

/// Find the shallow directories of the project.
pub(super) fn find_shallow_directories(
    input: &ShallowDirectoriesInput<'_>,
) -> ShallowDirectoriesReport {
    let ignore = build_ignore_set(&input.config.ignore);
    let modules_by_id: FxHashMap<FileId, &ModuleInfo> =
        input.modules.iter().map(|m| (m.file_id, m)).collect();
    let relative_paths: FxHashMap<FileId, PathBuf> = input
        .graph
        .modules()
        .iter()
        .map(|node| (node.file_id, relative_to(input.root, &node.path)))
        .collect();

    let published = published_exports(input.graph);
    let items: Vec<Item> = input
        .graph
        .modules()
        .iter()
        .filter(|node| {
            relative_paths
                .get(&node.file_id)
                .is_some_and(|path| !is_excluded_file(input.root, path))
        })
        .filter_map(|node| {
            let module = modules_by_id.get(&node.file_id)?;
            Some(collect_file_items(
                node,
                module,
                input.root,
                &relative_paths,
                &published,
            ))
        })
        .flatten()
        .collect();

    let directories =
        evaluate_directories(input.root, &items, &relative_paths, &ignore, input.config);
    ShallowDirectoriesReport {
        min_interface: input.config.min_interface,
        total: directories.len(),
        directories,
    }
}

/// The exports that an entry point re-exports, directly or through a chain of
/// re-exports, as `(file, export name)` pairs. Such an export is public API:
/// the package shows it on purpose, so it is no item.
fn published_exports(graph: &ModuleGraph) -> FxHashSet<(FileId, String)> {
    let nodes: FxHashMap<FileId, &ModuleNode> = graph
        .modules()
        .iter()
        .map(|node| (node.file_id, node))
        .collect();
    let mut walk = PublishWalk {
        nodes: &nodes,
        published: FxHashSet::default(),
        star_seen: FxHashSet::default(),
        stack: graph
            .modules()
            .iter()
            .filter(|node| node.is_entry_point())
            .map(|node| (node.file_id, None))
            .collect(),
    };
    while let Some((file, name)) = walk.stack.pop() {
        let Some(node) = nodes.get(&file) else {
            continue;
        };
        for edge in &node.re_exports {
            if !edge.is_type_only && forwards(edge, name.as_deref()) {
                walk.follow(edge, name.as_deref());
            }
        }
    }
    walk.published
}

/// Whether a re-export edge forwards `name`, or any export when `name` is
/// `None`.
fn forwards(edge: &ReExportEdge, name: Option<&str>) -> bool {
    name.is_none_or(|name| edge.exported_name == name || edge.exported_name == STAR)
}

/// The state of the walk from the entry points along re-export edges.
struct PublishWalk<'a> {
    nodes: &'a FxHashMap<FileId, &'a ModuleNode>,
    published: FxHashSet<(FileId, String)>,
    /// Modules whose exports are all published already.
    star_seen: FxHashSet<FileId>,
    /// Modules to visit, with the one export name to follow, or `None` for
    /// all exports. A module and name go on it only once, so a cycle of
    /// re-exports ends.
    stack: Vec<(FileId, Option<String>)>,
}

impl PublishWalk<'_> {
    fn follow(&mut self, edge: &ReExportEdge, name: Option<&str>) {
        if edge.imported_name != STAR {
            if self
                .published
                .insert((edge.source_file, edge.imported_name.clone()))
            {
                self.stack
                    .push((edge.source_file, Some(edge.imported_name.clone())));
            }
            return;
        }
        // `export * from` forwards the one name that the walk follows.
        // `export * as ns from` and a walk of all exports forward every export
        // of the source module.
        match name.filter(|_| edge.exported_name == STAR) {
            Some(name) => {
                if self.published.insert((edge.source_file, name.to_string())) {
                    self.stack.push((edge.source_file, Some(name.to_string())));
                }
            }
            None => self.publish_all(edge.source_file),
        }
    }

    fn publish_all(&mut self, file: FileId) {
        if !self.star_seen.insert(file) {
            return;
        }
        if let Some(source) = self.nodes.get(&file) {
            for export in &source.exports {
                self.published.insert((file, export.name.to_string()));
            }
        }
        self.stack.push((file, None));
    }
}

/// The name that a star re-export edge carries.
const STAR: &str = "*";

/// Whether a glob set matches a directory. A pattern such as `src/gen/**`
/// matches the files below `src/gen` but not the path `src/gen` itself, so the
/// check also tries a path one level below the directory.
pub(super) fn matches_directory(set: &GlobSet, dir: &Path) -> bool {
    set.is_match(dir) || set.is_match(dir.join(DIRECTORY_PROBE))
}

/// The file name that [`matches_directory`] puts below a directory.
const DIRECTORY_PROBE: &str = "_";

/// Config validation rejects an invalid pattern before the run, so a pattern
/// that does not compile here cannot occur, and skipping it is safe.
fn build_ignore_set(patterns: &[String]) -> GlobSet {
    let mut builder = GlobSetBuilder::new();
    for pattern in patterns {
        if let Ok(glob) = fallow_config::compile_path_glob(pattern) {
            builder.add(glob);
        }
    }
    builder.build().unwrap_or_else(|_| GlobSet::empty())
}

fn relative_to(root: &Path, path: &Path) -> PathBuf {
    path.strip_prefix(root).unwrap_or(path).to_path_buf()
}

/// Test, test support and story files neither hold items nor count as
/// outside users.
fn is_excluded_file(root: &Path, relative: &Path) -> bool {
    if crate::test_paths::is_test_path(root, relative) {
        return true;
    }
    relative
        .file_name()
        .and_then(|name| name.to_str())
        .is_some_and(|name| {
            STORY_FILE_MARKERS
                .iter()
                .any(|marker| name.contains(marker))
        })
}

/// The items that one file declares: its module-scope functions and its
/// exported classes.
fn collect_file_items(
    node: &ModuleNode,
    module: &ModuleInfo,
    root: &Path,
    relative_paths: &FxHashMap<FileId, PathBuf>,
    published: &FxHashSet<(FileId, String)>,
) -> Vec<Item> {
    let relative = &relative_paths[&node.file_id];
    let dir = relative.parent().map(Path::to_path_buf).unwrap_or_default();
    let line_of = |offset: u32| {
        fallow_types::extract::byte_offset_to_line_col(&module.line_offsets, offset).0
    };

    // Local binding name of each value export, by export name.
    let export_bindings: FxHashMap<&ExportName, &str> = module
        .exports
        .iter()
        .filter(|export| !export.is_type_only)
        .filter_map(|export| {
            let binding = export.local_name.as_deref().or(match &export.name {
                ExportName::Named(name) => Some(name.as_str()),
                ExportName::Default => None,
            })?;
            Some((&export.name, binding))
        })
        .collect();

    let mut items = Vec::new();
    let mut exported_bindings: FxHashSet<&str> = FxHashSet::default();
    for symbol in node.exports.iter().filter(|symbol| !symbol.is_type_only) {
        // An exported binding is never a hidden module-scope function, also
        // when the export is no item (published, a component, a constant).
        if let Some(binding) = export_bindings.get(&symbol.name) {
            exported_bindings.insert(binding);
        }
        if published.contains(&(node.file_id, symbol.name.to_string())) {
            continue;
        }
        let users = outside_users(symbol, node.file_id, root, relative_paths);
        let kind = item_kind(symbol, module, export_bindings.get(&symbol.name).copied());
        let Some(kind) = kind else {
            continue;
        };
        if is_component(
            relative,
            &symbol.name,
            export_bindings.get(&symbol.name).copied(),
        ) {
            continue;
        }
        let members = (kind == ShallowDirectoryItemKind::Class).then(|| {
            let count = symbol
                .members
                .iter()
                .filter(|member| is_public_class_member(member.kind, member.is_protected))
                .count();
            u32::try_from(count).unwrap_or(u32::MAX)
        });
        let name = match &symbol.name {
            ExportName::Named(name) => name.clone(),
            ExportName::Default => export_bindings
                .get(&symbol.name)
                .map(|binding| (*binding).to_string())
                .or_else(|| file_stem(relative))
                .unwrap_or_else(|| "default".to_string()),
        };
        items.push(Item {
            name,
            is_default: matches!(symbol.name, ExportName::Default),
            file: node.file_id,
            dir: dir.clone(),
            line: line_of(symbol.span.start),
            kind,
            members,
            users,
            signature: export_bindings
                .get(&symbol.name)
                .and_then(|binding| signature_of(module, binding)),
        });
    }

    // Module-scope functions that no export shows are hidden items.
    if let Some(contracts) = module.function_contracts.as_deref() {
        for contract in &contracts.declarations {
            if exported_bindings.contains(&*contract.binding_name)
                || is_component_name(relative, &contract.binding_name)
            {
                continue;
            }
            items.push(Item {
                name: contract.binding_name.to_string(),
                is_default: false,
                file: node.file_id,
                dir: dir.clone(),
                line: line_of(contract.span_start),
                kind: ShallowDirectoryItemKind::Function,
                members: None,
                users: Vec::new(),
                signature: None,
            });
        }
    }
    items
}

/// A UI component is a unit of its own with props as its interface, so it is
/// no item. A component kit directory then reports nothing.
fn is_component(relative: &Path, name: &ExportName, binding: Option<&str>) -> bool {
    let name = match name {
        ExportName::Named(name) => name.as_str(),
        ExportName::Default => match binding {
            Some(binding) => binding,
            None => return is_component_file(relative),
        },
    };
    is_component_name(relative, name)
}

fn is_component_name(relative: &Path, name: &str) -> bool {
    is_component_file(relative) && name.starts_with(|c: char| c.is_ascii_uppercase())
}

fn is_component_file(relative: &Path) -> bool {
    relative
        .extension()
        .and_then(|ext| ext.to_str())
        .is_some_and(|ext| COMPONENT_FILE_EXTENSIONS.contains(&ext))
}

fn file_stem(relative: &Path) -> Option<String> {
    relative
        .file_stem()
        .and_then(|stem| stem.to_str())
        .map(|stem| stem.split('.').next().unwrap_or(stem).to_string())
}

fn is_public_class_member(kind: MemberKind, is_protected: bool) -> bool {
    matches!(kind, MemberKind::ClassMethod | MemberKind::ClassProperty) && !is_protected
}

/// Whether an export is a function or a class item. A function export binds a
/// module-scope function (call facts). A class export has class members.
fn item_kind(
    symbol: &fallow_graph::graph::ExportSymbol,
    module: &ModuleInfo,
    binding: Option<&str>,
) -> Option<ShallowDirectoryItemKind> {
    // A class without a method holds data only, like a constant.
    if symbol
        .members
        .iter()
        .any(|member| member.kind == MemberKind::ClassMethod)
    {
        return Some(ShallowDirectoryItemKind::Class);
    }
    if symbol
        .members
        .iter()
        .any(|member| member.kind == MemberKind::ClassProperty)
    {
        return None;
    }
    let binding = binding?;
    let contracts = module.function_contracts.as_deref()?;
    contracts
        .declarations
        .iter()
        .any(|contract| &*contract.binding_name == binding)
        .then_some(ShallowDirectoryItemKind::Function)
}

/// Directories of the files that use an export. A re-export is not a use:
/// the graph credits the origin export with each file that imports it through
/// the barrel.
fn outside_users(
    symbol: &fallow_graph::graph::ExportSymbol,
    own_file: FileId,
    root: &Path,
    relative_paths: &FxHashMap<FileId, PathBuf>,
) -> Vec<(FileId, PathBuf)> {
    let mut seen: FxHashSet<FileId> = FxHashSet::default();
    symbol
        .references
        .iter()
        .filter(|reference| {
            reference.namespace == ExportNamespace::Value
                && reference.from_file != own_file
                && !matches!(
                    reference.kind,
                    ReferenceKind::ReExport | ReferenceKind::SideEffectImport
                )
        })
        .filter(|reference| seen.insert(reference.from_file))
        .filter_map(|reference| {
            relative_paths
                .get(&reference.from_file)
                .map(|path| (reference.from_file, path))
        })
        .filter(|(_, path)| !is_excluded_file(root, path))
        .map(|(file, path)| {
            (
                file,
                path.parent().map(Path::to_path_buf).unwrap_or_default(),
            )
        })
        .collect()
}

/// Whether a directory that passes the counts acts as one module. Three
/// shapes do not, and a directory of such a shape claims no items, so its
/// parent directory counts them:
///
/// - A catalog: the shown items come about one per file (one hook, model or
///   task per file), or all from one file. Each file is then the unit that
///   hides its code.
/// - A family: many shown functions share one parameter list, for example a
///   set of editor commands `(state, dispatch)`. The width is the product.
/// - Low reuse: each shown item has about one outside user. Such an item is a
///   helper in the wrong place, and the wide interface costs few readers.
fn is_module(dir: &Path, shown: &[usize], items: &[Item]) -> bool {
    let files: FxHashSet<FileId> = shown.iter().map(|&index| items[index].file).collect();
    if files.len() < MIN_SHOWN_FILES || ratio(shown.len(), files.len()) < MIN_ITEMS_PER_FILE {
        return false;
    }
    if family_share(shown, items) >= MAX_FAMILY_SHARE {
        return false;
    }
    let outside_uses: usize = shown
        .iter()
        .map(|&index| items[index].outside_files(dir) as usize)
        .sum();
    ratio(outside_uses, shown.len()) >= MIN_MEAN_OUTSIDE_FILES
}

/// The share of shown items that have the most common parameter list. Only
/// lists with a named parameter count: an empty list or one destructured
/// object says little about the role of a function.
fn family_share(shown: &[usize], items: &[Item]) -> f64 {
    let mut counts: FxHashMap<&str, usize> = FxHashMap::default();
    for &index in shown {
        if let Some(signature) = items[index].signature.as_deref()
            && signature
                .split(',')
                .any(|param| param != DESTRUCTURED_PARAM && !param.is_empty())
        {
            *counts.entry(signature).or_default() += 1;
        }
    }
    let largest = counts.values().copied().max().unwrap_or(0);
    if largest < MIN_FAMILY_SIZE {
        return 0.0;
    }
    ratio(largest, shown.len())
}

#[expect(
    clippy::cast_precision_loss,
    reason = "item and file counts stay far below 2^52"
)]
fn ratio(part: usize, whole: usize) -> f64 {
    part as f64 / whole.max(1) as f64
}

/// Per-directory counts, deepest directory first, with the claim rule.
fn evaluate_directories(
    root: &Path,
    items: &[Item],
    relative_paths: &FxHashMap<FileId, PathBuf>,
    ignore: &GlobSet,
    config: &ShallowDirectoriesConfig,
) -> Vec<ShallowDirectoryFinding> {
    let anchors = anchor_candidates(root, relative_paths);
    let mut items_by_dir: FxHashMap<PathBuf, Vec<usize>> = FxHashMap::default();
    for (index, item) in items.iter().enumerate() {
        for dir in item
            .dir
            .ancestors()
            .filter(|dir| !dir.as_os_str().is_empty())
        {
            items_by_dir
                .entry(dir.to_path_buf())
                .or_default()
                .push(index);
        }
    }
    let mut dirs: Vec<&PathBuf> = items_by_dir.keys().collect();
    dirs.sort_by(|a, b| {
        b.components()
            .count()
            .cmp(&a.components().count())
            .then_with(|| a.cmp(b))
    });

    let mut claimed = vec![false; items.len()];
    let mut findings = Vec::new();
    for dir in dirs {
        let members: Vec<usize> = items_by_dir[dir]
            .iter()
            .copied()
            .filter(|&index| !claimed[index])
            .collect();
        let (shown, hidden): (Vec<usize>, Vec<usize>) = members
            .iter()
            .partition(|&&index| items[index].shown_in(dir));
        let shown_count = u32::try_from(shown.len()).unwrap_or(u32::MAX);
        let hidden_count = u32::try_from(hidden.len()).unwrap_or(u32::MAX);
        if shown_count < config.min_interface || shown_count < hidden_count {
            continue;
        }
        if !is_module(dir, &shown, items) {
            continue;
        }
        for &index in &members {
            claimed[index] = true;
        }
        if matches_directory(ignore, dir) {
            continue;
        }
        findings.push(build_finding(
            &anchors,
            dir,
            &shown,
            hidden_count,
            items,
            relative_paths,
        ));
    }
    findings.sort_by(|a, b| {
        b.shown_count
            .cmp(&a.shown_count)
            .then_with(|| a.path.cmp(&b.path))
    });
    findings
}

fn build_finding(
    anchors: &[&PathBuf],
    dir: &Path,
    shown: &[usize],
    hidden_count: u32,
    items: &[Item],
    relative_paths: &FxHashMap<FileId, PathBuf>,
) -> ShallowDirectoryFinding {
    let mut listed: Vec<ShallowDirectoryItem> = shown
        .iter()
        .map(|&index| {
            let item = &items[index];
            ShallowDirectoryItem {
                name: item.name.clone(),
                is_default: item.is_default,
                path: relative_paths[&item.file].clone(),
                line: item.line,
                kind: item.kind,
                outside_files: item.outside_files(dir),
                members: item.members,
            }
        })
        .collect();
    listed.sort_by(|a, b| {
        b.outside_files
            .cmp(&a.outside_files)
            .then_with(|| a.name.cmp(&b.name))
            .then_with(|| a.path.cmp(&b.path))
    });
    let more_items =
        u32::try_from(listed.len().saturating_sub(MAX_LISTED_ITEMS)).unwrap_or(u32::MAX);
    listed.truncate(MAX_LISTED_ITEMS);
    let relative_dir = dir.to_string_lossy().replace('\\', "/");
    let shown_files: FxHashSet<FileId> = shown.iter().map(|&index| items[index].file).collect();
    ShallowDirectoryFinding {
        path: dir.to_path_buf(),
        anchor_file: anchor_file(dir, anchors),
        shown_count: u32::try_from(shown.len()).unwrap_or(u32::MAX),
        shown_files: u32::try_from(shown_files.len()).unwrap_or(u32::MAX),
        hidden_count,
        items: listed,
        more_items,
        actions: ShallowDirectoryFinding::actions_for(&globset::escape(&relative_dir)),
    }
}

/// The `index` file directly in the directory, else the first file below it.
/// The script files that can be an anchor file, sorted by path.
fn anchor_candidates<'a>(
    root: &Path,
    relative_paths: &'a FxHashMap<FileId, PathBuf>,
) -> Vec<&'a PathBuf> {
    let mut candidates: Vec<&PathBuf> = relative_paths
        .values()
        .filter(|path| is_script_file(path) && !is_excluded_file(root, path))
        .collect();
    candidates.sort();
    candidates
}

/// A path sorts after its directory and before the next sibling directory, so
/// the files below `dir` are one range of the sorted candidates.
fn anchor_file(dir: &Path, anchors: &[&PathBuf]) -> PathBuf {
    let start = anchors.partition_point(|path| path.as_path() < dir);
    let below: Vec<&PathBuf> = anchors[start..]
        .iter()
        .take_while(|path| path.starts_with(dir))
        .copied()
        .collect();
    below
        .iter()
        .find(|path| {
            path.parent() == Some(dir)
                && path
                    .file_stem()
                    .is_some_and(|stem| stem.eq_ignore_ascii_case("index"))
        })
        .or_else(|| below.first())
        .map_or_else(|| dir.to_path_buf(), |path| (*path).clone())
}

fn is_script_file(path: &Path) -> bool {
    path.extension()
        .and_then(|ext| ext.to_str())
        .is_some_and(|ext| SCRIPT_EXTENSIONS.contains(&ext))
}

fn signature_of(module: &ModuleInfo, binding: &str) -> Option<String> {
    let contracts = module.function_contracts.as_deref()?;
    let contract = contracts
        .declarations
        .iter()
        .find(|contract| &*contract.binding_name == binding)?;
    Some(
        contract
            .params
            .iter()
            .map(|param| param.name.as_deref().unwrap_or(DESTRUCTURED_PARAM))
            .collect::<Vec<_>>()
            .join(","),
    )
}