fallow-engine 3.29.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
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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
//! Per-finding rule severity for dead-code results.
//!
//! One table in this module maps each dead-code finding to the rule that
//! decides its severity. Three consumers read it:
//!
//! - [`apply_effective_severities`] writes the severity onto each finding for
//!   the CI formats (SARIF, CodeClimate, GitHub annotations);
//! - `has_error_severity_issues` in `crates/engine/src/error_severity.rs`
//!   decides the exit code, the combined verdict and the audit `all` gate;
//! - the audit ledger in `crates/api/src/audit_keys.rs` decides the audit
//!   `new-only` gate.
//!
//! The rules of the table:
//!
//! - a file-scoped finding resolves `overrides[].rules` for its own path;
//! - a circular dependency takes the highest severity of the files in the
//!   cycle;
//! - a project-level finding (dependencies, catalog entries, duplicate
//!   exports, re-export cycles) uses the base rules.
//!
//! Empty catalog groups and dependency overrides are file-scoped: they sit on
//! the file that declares them (`pnpm-workspace.yaml` or a `package.json`), so
//! an override for that file decides.
//!
//! Policy violations carry their own `severity`. Prop-drilling, thin-wrapper
//! and duplicate-prop-shape records are health signals that never gate the
//! run. They carry their base rule severity, so `fallow report --from` can
//! render their level without the config, but the exit-code check skips them.

use std::path::Path;

use fallow_config::{ResolvedConfig, RulesConfig, Severity};
use fallow_types::output_dead_code::{
    BoundaryCallViolationFinding, BoundaryCoverageViolationFinding, BoundaryViolationFinding,
    CircularDependencyFinding, DeprecatedExportInUseFinding, DevDependencyInProductionFinding,
    DuplicateExportFinding, DynamicSegmentNameConflictFinding, EffectiveSeverity,
    EmptyCatalogGroupFinding, GatedFinding, InvalidClientExportFinding,
    MisconfiguredDependencyOverrideFinding, MisplacedDirectiveFinding,
    MixedClientServerBarrelFinding, PolicyViolationFinding, PrivateTypeLeakFinding,
    ReExportCycleFinding, RouteCollisionFinding, TestOnlyDependencyFinding,
    TypeOnlyDependencyFinding, UnlistedDependencyFinding, UnprovidedInjectFinding,
    UnrenderedComponentFinding, UnresolvedCatalogReferenceFinding, UnresolvedImportFinding,
    UnusedCatalogEntryFinding, UnusedClassMemberFinding, UnusedComponentEmitFinding,
    UnusedComponentInputFinding, UnusedComponentOutputFinding, UnusedComponentPropFinding,
    UnusedDependencyFinding, UnusedDependencyOverrideFinding, UnusedDevDependencyFinding,
    UnusedEnumMemberFinding, UnusedExportFinding, UnusedFileFinding, UnusedLoadDataKeyFinding,
    UnusedOptionalDependencyFinding, UnusedServerActionFinding, UnusedStoreMemberFinding,
    UnusedSvelteEventFinding, UnusedTypeFinding,
};
use fallow_types::results::{AnalysisResults, PolicyViolationSeverity, StaleSuppression};

use crate::error_severity::promote_warns_to_errors;

fn gate(severity: Severity) -> Option<EffectiveSeverity> {
    match severity {
        Severity::Error => Some(EffectiveSeverity::Error),
        Severity::Warn => Some(EffectiveSeverity::Warn),
        Severity::Off => None,
    }
}

/// The rules that give a finding its severity.
#[derive(Clone, Copy)]
pub struct SeveritySource<'a> {
    base: &'a RulesConfig,
    overrides: Option<&'a ResolvedConfig>,
    promote_warns: bool,
}

impl<'a> SeveritySource<'a> {
    /// The rules of `config`, with its `overrides` for file-scoped findings.
    #[must_use]
    pub fn from_config(config: &'a ResolvedConfig) -> Self {
        Self::new(&config.rules, Some(config), false)
    }

    /// Explicit base rules, with the `overrides` of `config` when it has any.
    ///
    /// `promote_warns` raises a `warn` that an override resolves to `error`.
    /// The caller promotes `base` itself.
    #[must_use]
    pub fn new(
        base: &'a RulesConfig,
        config: Option<&'a ResolvedConfig>,
        promote_warns: bool,
    ) -> Self {
        Self {
            base,
            overrides: config.filter(|config| !config.overrides.is_empty()),
            promote_warns,
        }
    }

    fn for_path(&self, path: &Path, rule: fn(&RulesConfig) -> Severity) -> Severity {
        let Some(config) = self.overrides else {
            return rule(self.base);
        };
        let mut rules = config.resolve_rules_for_path(path);
        if self.promote_warns {
            promote_warns_to_errors(&mut rules);
        }
        rule(&rules)
    }

    fn project(&self, rule: fn(&RulesConfig) -> Severity) -> Severity {
        rule(self.base)
    }

    /// The base rule when no `overrides` apply, so every finding of a
    /// file-scoped kind has the same severity.
    fn uniform(&self, rule: fn(&RulesConfig) -> Severity) -> Option<Severity> {
        self.overrides.is_none().then(|| rule(self.base))
    }
}

/// A dead-code finding whose severity comes from the configured rules.
pub trait RuleSeverity {
    /// The severity of this finding under `source`.
    fn rule_severity(&self, source: &SeveritySource<'_>) -> Severity;

    /// The severity that every finding of this kind has under `source`, or
    /// `None` when the severity can differ from finding to finding.
    ///
    /// The exit-code check reads this once per collection instead of once
    /// per finding.
    fn uniform_severity(_source: &SeveritySource<'_>) -> Option<Severity>
    where
        Self: Sized,
    {
        None
    }
}

macro_rules! file_scoped {
    ($($finding:ty => $path:ident . $field:ident, $rule:ident;)+) => {
        $(
            impl RuleSeverity for $finding {
                fn rule_severity(&self, source: &SeveritySource<'_>) -> Severity {
                    source.for_path(&self.$path.$field, |rules| rules.$rule)
                }

                fn uniform_severity(source: &SeveritySource<'_>) -> Option<Severity> {
                    source.uniform(|rules| rules.$rule)
                }
            }
        )+
    };
}

macro_rules! project_level {
    ($($finding:ty => $rule:ident;)+) => {
        $(
            impl RuleSeverity for $finding {
                fn rule_severity(&self, source: &SeveritySource<'_>) -> Severity {
                    source.project(|rules| rules.$rule)
                }

                fn uniform_severity(source: &SeveritySource<'_>) -> Option<Severity> {
                    Some(source.project(|rules| rules.$rule))
                }
            }
        )+
    };
}

file_scoped! {
    UnusedFileFinding => file.path, unused_files;
    UnusedExportFinding => export.path, unused_exports;
    UnusedTypeFinding => export.path, unused_types;
    PrivateTypeLeakFinding => leak.path, private_type_leaks;
    DeprecatedExportInUseFinding => export.path, deprecated_exports_in_use;
    UnusedEnumMemberFinding => member.path, unused_enum_members;
    UnusedClassMemberFinding => member.path, unused_class_members;
    UnusedStoreMemberFinding => member.path, unused_store_members;
    UnprovidedInjectFinding => inject.path, unprovided_injects;
    UnresolvedImportFinding => import.path, unresolved_imports;
    UnrenderedComponentFinding => component.path, unrendered_components;
    UnusedComponentPropFinding => prop.path, unused_component_props;
    UnusedComponentEmitFinding => emit.path, unused_component_emits;
    UnusedComponentInputFinding => input.path, unused_component_inputs;
    UnusedComponentOutputFinding => output.path, unused_component_outputs;
    UnusedSvelteEventFinding => event.path, unused_svelte_events;
    UnusedServerActionFinding => action.path, unused_server_actions;
    UnusedLoadDataKeyFinding => key.path, unused_load_data_keys;
    InvalidClientExportFinding => export.path, invalid_client_export;
    MixedClientServerBarrelFinding => barrel.path, mixed_client_server_barrel;
    MisplacedDirectiveFinding => directive_site.path, misplaced_directive;
    RouteCollisionFinding => collision.path, route_collision;
    DynamicSegmentNameConflictFinding => conflict.path, dynamic_segment_name_conflict;
    BoundaryViolationFinding => violation.from_path, boundary_violation;
    BoundaryCoverageViolationFinding => violation.path, boundary_violation;
    BoundaryCallViolationFinding => violation.path, boundary_violation;
    UnresolvedCatalogReferenceFinding => reference.path, unresolved_catalog_references;
    EmptyCatalogGroupFinding => group.path, empty_catalog_groups;
    UnusedDependencyOverrideFinding => entry.path, unused_dependency_overrides;
    MisconfiguredDependencyOverrideFinding => entry.path, misconfigured_dependency_overrides;
}

project_level! {
    UnusedDependencyFinding => unused_dependencies;
    UnusedDevDependencyFinding => unused_dev_dependencies;
    UnusedOptionalDependencyFinding => unused_optional_dependencies;
    UnlistedDependencyFinding => unlisted_dependencies;
    DuplicateExportFinding => duplicate_exports;
    TypeOnlyDependencyFinding => type_only_dependencies;
    TestOnlyDependencyFinding => test_only_dependencies;
    DevDependencyInProductionFinding => dev_dependencies_in_production;
    ReExportCycleFinding => re_export_cycle;
    UnusedCatalogEntryFinding => unused_catalog_entries;
}

impl RuleSeverity for CircularDependencyFinding {
    fn rule_severity(&self, source: &SeveritySource<'_>) -> Severity {
        self.cycle
            .files
            .iter()
            .map(|path| source.for_path(path, |rules| rules.circular_dependencies))
            .max_by_key(|severity| severity_rank(*severity))
            .unwrap_or_else(|| source.project(|rules| rules.circular_dependencies))
    }

    fn uniform_severity(source: &SeveritySource<'_>) -> Option<Severity> {
        source.uniform(|rules| rules.circular_dependencies)
    }
}

impl RuleSeverity for StaleSuppression {
    fn rule_severity(&self, source: &SeveritySource<'_>) -> Severity {
        if self.missing_reason {
            source.for_path(&self.path, |rules| rules.require_suppression_reason)
        } else {
            source.for_path(&self.path, |rules| rules.stale_suppressions)
        }
    }

    fn uniform_severity(source: &SeveritySource<'_>) -> Option<Severity> {
        let stale = source.uniform(|rules| rules.stale_suppressions)?;
        let missing_reason = source.uniform(|rules| rules.require_suppression_reason)?;
        (stale == missing_reason).then_some(stale)
    }
}

impl RuleSeverity for PolicyViolationFinding {
    fn rule_severity(&self, _source: &SeveritySource<'_>) -> Severity {
        match self.violation.severity {
            PolicyViolationSeverity::Error => Severity::Error,
            PolicyViolationSeverity::Warn => Severity::Warn,
        }
    }
}

const fn severity_rank(severity: Severity) -> u8 {
    match severity {
        Severity::Off => 0,
        Severity::Warn => 1,
        Severity::Error => 2,
    }
}

/// A finding that has a rule severity and carries a gate severity.
trait GatedRuleFinding: GatedFinding + RuleSeverity {}

impl<T: GatedFinding + RuleSeverity> GatedRuleFinding for T {}

/// Write the gate severity onto each dead-code finding in `results`.
///
/// Call this after the findings whose rule is `off` are removed. The function
/// overwrites any earlier value, so a second call with the same config gives
/// the same result.
pub fn apply_effective_severities(results: &mut AnalysisResults, config: &ResolvedConfig) {
    let source = SeveritySource::from_config(config);
    for_each_gated_finding(results, &mut |finding| {
        let severity = finding.rule_severity(&source);
        finding.set_effective_severity(gate(severity));
    });
    apply_non_gating_severities(results, &config.rules);
}

/// Write the rule severity onto each prop-drilling, thin-wrapper and
/// duplicate-prop-shape finding.
///
/// The analysis reads only the base rules for these types, so the value is
/// the base rule. These findings never gate the run: the exit-code check and
/// `--fail-on-issues` skip them.
fn apply_non_gating_severities(results: &mut AnalysisResults, rules: &RulesConfig) {
    set_all(&mut results.prop_drilling_chains, rules.prop_drilling);
    set_all(&mut results.thin_wrappers, rules.thin_wrapper);
    set_all(
        &mut results.duplicate_prop_shapes,
        rules.duplicate_prop_shape,
    );
}

fn set_all<T: GatedFinding>(findings: &mut [T], rule: Severity) {
    for finding in findings {
        finding.set_effective_severity(gate(rule));
    }
}

/// The number of gated dead-code findings in `results` that carry no saved
/// severity, for example in a report from an older version. A renderer then
/// takes their level from the configured rules.
///
/// Policy violations carry their own severity and do not count. Neither do
/// prop-drilling, thin-wrapper and duplicate-prop-shape findings: only SARIF
/// renders them, always at level `warning`, so the rules never change their
/// level.
#[must_use]
pub fn findings_without_severity(mut results: AnalysisResults) -> usize {
    let mut missing = 0;
    for_each_gated_finding(&mut results, &mut |finding| {
        if finding.effective_severity().is_none() {
            missing += 1;
        }
    });
    missing
}

/// Raise every `warn` gate severity to `error`, for `--fail-on-issues`.
///
/// Under that flag every reported finding fails the run, so every CI format
/// must state `error` too.
pub fn promote_effective_warns(results: &mut AnalysisResults) {
    for_each_gated_finding(results, &mut |finding| {
        if finding.effective_severity() == Some(EffectiveSeverity::Warn) {
            finding.set_effective_severity(Some(EffectiveSeverity::Error));
        }
    });
}

/// Whether any dead-code finding in `results` has `severity` under `source`.
///
/// Policy violations count with their own severity.
#[must_use]
pub fn any_finding_with_severity(
    results: &AnalysisResults,
    source: &SeveritySource<'_>,
    severity: Severity,
) -> bool {
    results
        .policy_violations
        .iter()
        .any(|finding| finding.rule_severity(source) == severity)
        || any_gated_finding(results, source, severity)
}

fn visit<T: GatedRuleFinding>(findings: &mut [T], f: &mut dyn FnMut(&mut dyn GatedRuleFinding)) {
    for finding in findings {
        f(finding);
    }
}

/// Whether any finding in `findings` has `severity` under `source`.
///
/// When every finding of the kind has the same severity, one table lookup
/// answers for the whole collection.
fn any<T: RuleSeverity>(findings: &[T], source: &SeveritySource<'_>, severity: Severity) -> bool {
    if findings.is_empty() {
        return false;
    }
    match T::uniform_severity(source) {
        Some(uniform) => uniform == severity,
        None => findings
            .iter()
            .any(|finding| finding.rule_severity(source) == severity),
    }
}

/// Visit every finding that carries a gate severity.
///
/// The destructure has no `..`, so a new field on [`AnalysisResults`] fails to
/// compile here until it is listed.
#[expect(
    clippy::too_many_lines,
    reason = "one exhaustive list of finding collections; splitting it would lose the compile-time guard"
)]
fn for_each_gated_finding(
    results: &mut AnalysisResults,
    f: &mut dyn FnMut(&mut dyn GatedRuleFinding),
) {
    let AnalysisResults {
        unused_files,
        unused_exports,
        unused_types,
        private_type_leaks,
        deprecated_exports_in_use,
        unused_dependencies,
        unused_dev_dependencies,
        unused_optional_dependencies,
        unused_enum_members,
        unused_class_members,
        unused_store_members,
        unresolved_imports,
        unlisted_dependencies,
        duplicate_exports,
        type_only_dependencies,
        test_only_dependencies,
        dev_dependencies_in_production,
        circular_dependencies,
        re_export_cycles,
        boundary_violations,
        boundary_coverage_violations,
        boundary_call_violations,
        stale_suppressions,
        unused_catalog_entries,
        empty_catalog_groups,
        unresolved_catalog_references,
        unused_dependency_overrides,
        misconfigured_dependency_overrides,
        invalid_client_exports,
        mixed_client_server_barrels,
        misplaced_directives,
        unprovided_injects,
        unrendered_components,
        route_collisions,
        dynamic_segment_name_conflicts,
        unused_component_props,
        unused_component_emits,
        unused_component_inputs,
        unused_component_outputs,
        unused_svelte_events,
        unused_server_actions,
        unused_load_data_keys,
        // Policy violations carry their own evaluated `severity`.
        policy_violations: _,
        // Health signals that never gate the run.
        prop_drilling_chains: _,
        thin_wrappers: _,
        duplicate_prop_shapes: _,
        // Not findings: counts, flags and metadata.
        unused_load_data_keys_global_abstain: _,
        suppression_count: _,
        unused_component_props_exempted: _,
        active_suppressions: _,
        feature_flags: _,
        export_usages: _,
        entry_point_summary: _,
        render_fan_in: _,
        react_component_intel: _,
        semantic_framework_contracts: _,
        // Security findings belong to `fallow security` and its own gate.
        security_findings: _,
        security_unresolved_edge_files: _,
        security_unresolved_callee_sites: _,
        security_unresolved_callee_diagnostics: _,
    } = results;
    visit(unused_files, f);
    visit(unused_exports, f);
    visit(unused_types, f);
    visit(private_type_leaks, f);
    visit(deprecated_exports_in_use, f);
    visit(unused_dependencies, f);
    visit(unused_dev_dependencies, f);
    visit(unused_optional_dependencies, f);
    visit(unused_enum_members, f);
    visit(unused_class_members, f);
    visit(unused_store_members, f);
    visit(unresolved_imports, f);
    visit(unlisted_dependencies, f);
    visit(duplicate_exports, f);
    visit(type_only_dependencies, f);
    visit(test_only_dependencies, f);
    visit(dev_dependencies_in_production, f);
    visit(circular_dependencies, f);
    visit(re_export_cycles, f);
    visit(boundary_violations, f);
    visit(boundary_coverage_violations, f);
    visit(boundary_call_violations, f);
    visit(stale_suppressions, f);
    visit(unused_catalog_entries, f);
    visit(empty_catalog_groups, f);
    visit(unresolved_catalog_references, f);
    visit(unused_dependency_overrides, f);
    visit(misconfigured_dependency_overrides, f);
    visit(invalid_client_exports, f);
    visit(mixed_client_server_barrels, f);
    visit(misplaced_directives, f);
    visit(unprovided_injects, f);
    visit(unrendered_components, f);
    visit(route_collisions, f);
    visit(dynamic_segment_name_conflicts, f);
    visit(unused_component_props, f);
    visit(unused_component_emits, f);
    visit(unused_component_inputs, f);
    visit(unused_component_outputs, f);
    visit(unused_svelte_events, f);
    visit(unused_server_actions, f);
    visit(unused_load_data_keys, f);
}

/// Whether any finding that carries a gate severity has `severity` under
/// `source`.
///
/// Exhaustive like [`for_each_gated_finding`]: a new field on
/// [`AnalysisResults`] fails to compile here until it is listed.
#[expect(
    clippy::too_many_lines,
    reason = "one exhaustive list of finding collections; splitting it would lose the compile-time guard"
)]
fn any_gated_finding(
    results: &AnalysisResults,
    source: &SeveritySource<'_>,
    severity: Severity,
) -> bool {
    let AnalysisResults {
        unused_files,
        unused_exports,
        unused_types,
        private_type_leaks,
        deprecated_exports_in_use,
        unused_dependencies,
        unused_dev_dependencies,
        unused_optional_dependencies,
        unused_enum_members,
        unused_class_members,
        unused_store_members,
        unresolved_imports,
        unlisted_dependencies,
        duplicate_exports,
        type_only_dependencies,
        test_only_dependencies,
        dev_dependencies_in_production,
        circular_dependencies,
        re_export_cycles,
        boundary_violations,
        boundary_coverage_violations,
        boundary_call_violations,
        stale_suppressions,
        unused_catalog_entries,
        empty_catalog_groups,
        unresolved_catalog_references,
        unused_dependency_overrides,
        misconfigured_dependency_overrides,
        invalid_client_exports,
        mixed_client_server_barrels,
        misplaced_directives,
        unprovided_injects,
        unrendered_components,
        route_collisions,
        dynamic_segment_name_conflicts,
        unused_component_props,
        unused_component_emits,
        unused_component_inputs,
        unused_component_outputs,
        unused_svelte_events,
        unused_server_actions,
        unused_load_data_keys,
        policy_violations: _,
        prop_drilling_chains: _,
        thin_wrappers: _,
        duplicate_prop_shapes: _,
        unused_load_data_keys_global_abstain: _,
        suppression_count: _,
        unused_component_props_exempted: _,
        active_suppressions: _,
        feature_flags: _,
        export_usages: _,
        entry_point_summary: _,
        render_fan_in: _,
        react_component_intel: _,
        semantic_framework_contracts: _,
        security_findings: _,
        security_unresolved_edge_files: _,
        security_unresolved_callee_sites: _,
        security_unresolved_callee_diagnostics: _,
    } = results;
    any(unused_files, source, severity)
        || any(unused_exports, source, severity)
        || any(unused_types, source, severity)
        || any(private_type_leaks, source, severity)
        || any(deprecated_exports_in_use, source, severity)
        || any(unused_dependencies, source, severity)
        || any(unused_dev_dependencies, source, severity)
        || any(unused_optional_dependencies, source, severity)
        || any(unused_enum_members, source, severity)
        || any(unused_class_members, source, severity)
        || any(unused_store_members, source, severity)
        || any(unresolved_imports, source, severity)
        || any(unlisted_dependencies, source, severity)
        || any(duplicate_exports, source, severity)
        || any(type_only_dependencies, source, severity)
        || any(test_only_dependencies, source, severity)
        || any(dev_dependencies_in_production, source, severity)
        || any(circular_dependencies, source, severity)
        || any(re_export_cycles, source, severity)
        || any(boundary_violations, source, severity)
        || any(boundary_coverage_violations, source, severity)
        || any(boundary_call_violations, source, severity)
        || any(stale_suppressions, source, severity)
        || any(unused_catalog_entries, source, severity)
        || any(empty_catalog_groups, source, severity)
        || any(unresolved_catalog_references, source, severity)
        || any(unused_dependency_overrides, source, severity)
        || any(misconfigured_dependency_overrides, source, severity)
        || any(invalid_client_exports, source, severity)
        || any(mixed_client_server_barrels, source, severity)
        || any(misplaced_directives, source, severity)
        || any(unprovided_injects, source, severity)
        || any(unrendered_components, source, severity)
        || any(route_collisions, source, severity)
        || any(dynamic_segment_name_conflicts, source, severity)
        || any(unused_component_props, source, severity)
        || any(unused_component_emits, source, severity)
        || any(unused_component_inputs, source, severity)
        || any(unused_component_outputs, source, severity)
        || any(unused_svelte_events, source, severity)
        || any(unused_server_actions, source, severity)
        || any(unused_load_data_keys, source, severity)
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use fallow_types::output_dead_code::{
        CircularDependencyFinding, MisconfiguredDependencyOverrideFinding, UnusedDependencyFinding,
        UnusedExportFinding,
    };
    use fallow_types::results::StaleSuppression;
    use serde_json::json;

    use super::*;

    const ROOT: &str = "/project";

    fn config(json: &str) -> ResolvedConfig {
        serde_json::from_str::<fallow_config::FallowConfig>(json)
            .expect("config parses")
            .resolve(
                PathBuf::from(ROOT),
                fallow_config::OutputFormat::Human,
                1,
                true,
                true,
                None,
            )
    }

    fn legacy_warn_config() -> ResolvedConfig {
        config(
            r#"{
                "rules": {
                    "unused-exports": "error",
                    "circular-dependencies": "error",
                    "unused-dependencies": "error",
                    "stale-suppressions": "warn",
                    "require-suppression-reason": "error"
                },
                "overrides": [{
                    "files": ["src/legacy/**", "package.json"],
                    "rules": {
                        "unused-exports": "warn",
                        "circular-dependencies": "warn",
                        "unused-dependencies": "warn",
                        "require-suppression-reason": "warn"
                    }
                }]
            }"#,
        )
    }

    fn export(path: &str) -> UnusedExportFinding {
        serde_json::from_value(json!({
            "path": format!("{ROOT}/{path}"),
            "export_name": "unused",
            "is_type_only": false,
            "line": 1,
            "col": 0,
            "span_start": 0,
            "is_re_export": false,
            "actions": [],
        }))
        .expect("export finding")
    }

    fn cycle(files: &[&str]) -> CircularDependencyFinding {
        serde_json::from_value(json!({
            "files": files.iter().map(|file| format!("{ROOT}/{file}")).collect::<Vec<_>>(),
            "length": files.len(),
            "line": 1,
            "col": 0,
            "actions": [],
        }))
        .expect("cycle finding")
    }

    fn stale(path: &str, missing_reason: bool) -> StaleSuppression {
        serde_json::from_value(json!({
            "path": format!("{ROOT}/{path}"),
            "line": 1,
            "col": 0,
            "origin": { "type": "comment", "is_file_level": false },
            "missing_reason": missing_reason,
            "actions": [],
        }))
        .expect("stale suppression")
    }

    #[test]
    fn file_scoped_findings_follow_the_override_for_their_path() {
        let mut results = AnalysisResults::default();
        results.unused_exports.push(export("src/app.ts"));
        results.unused_exports.push(export("src/legacy/old.ts"));

        apply_effective_severities(&mut results, &legacy_warn_config());

        let severities: Vec<_> = results
            .unused_exports
            .iter()
            .map(|finding| finding.effective_severity)
            .collect();
        assert_eq!(
            severities,
            vec![
                Some(EffectiveSeverity::Error),
                Some(EffectiveSeverity::Warn)
            ]
        );
    }

    #[test]
    fn a_cycle_is_error_when_any_file_in_it_resolves_to_error() {
        let mut results = AnalysisResults::default();
        results
            .circular_dependencies
            .push(cycle(&["src/legacy/a.ts", "src/b.ts"]));
        results
            .circular_dependencies
            .push(cycle(&["src/legacy/a.ts", "src/legacy/b.ts"]));

        apply_effective_severities(&mut results, &legacy_warn_config());

        assert_eq!(
            results.circular_dependencies[0].effective_severity,
            Some(EffectiveSeverity::Error)
        );
        assert_eq!(
            results.circular_dependencies[1].effective_severity,
            Some(EffectiveSeverity::Warn)
        );
    }

    #[test]
    fn project_level_findings_use_the_base_rules() {
        let mut results = AnalysisResults::default();
        results.unused_dependencies.push(
            serde_json::from_value::<UnusedDependencyFinding>(json!({
                "package_name": "left-pad",
                "location": "dependencies",
                "path": format!("{ROOT}/package.json"),
                "line": 3,
                "actions": [],
            }))
            .expect("dependency finding"),
        );

        apply_effective_severities(&mut results, &legacy_warn_config());

        assert_eq!(
            results.unused_dependencies[0].effective_severity,
            Some(EffectiveSeverity::Error)
        );
    }

    #[test]
    fn a_dependency_override_follows_the_override_for_its_file() {
        let config = config(
            r#"{
                "rules": { "misconfigured-dependency-overrides": "error" },
                "overrides": [{
                    "files": ["pnpm-workspace.yaml"],
                    "rules": { "misconfigured-dependency-overrides": "warn" }
                }]
            }"#,
        );
        let mut results = AnalysisResults::default();
        for file in ["pnpm-workspace.yaml", "package.json"] {
            results.misconfigured_dependency_overrides.push(
                serde_json::from_value::<MisconfiguredDependencyOverrideFinding>(json!({
                    "raw_key": "",
                    "raw_value": "^1.0.0",
                    "reason": "empty-value",
                    "source": file,
                    "path": format!("{ROOT}/{file}"),
                    "line": 2,
                    "actions": [],
                }))
                .expect("override finding"),
            );
        }

        apply_effective_severities(&mut results, &config);

        let severities: Vec<_> = results
            .misconfigured_dependency_overrides
            .iter()
            .map(|finding| finding.effective_severity)
            .collect();
        assert_eq!(
            severities,
            vec![
                Some(EffectiveSeverity::Warn),
                Some(EffectiveSeverity::Error)
            ]
        );
    }

    #[test]
    fn a_stale_suppression_reads_the_rule_for_its_kind() {
        let mut results = AnalysisResults::default();
        results.stale_suppressions.push(stale("src/app.ts", false));
        results.stale_suppressions.push(stale("src/app.ts", true));
        results
            .stale_suppressions
            .push(stale("src/legacy/old.ts", true));

        apply_effective_severities(&mut results, &legacy_warn_config());

        let severities: Vec<_> = results
            .stale_suppressions
            .iter()
            .map(|finding| finding.effective_severity)
            .collect();
        assert_eq!(
            severities,
            vec![
                Some(EffectiveSeverity::Warn),
                Some(EffectiveSeverity::Error),
                Some(EffectiveSeverity::Warn),
            ]
        );
    }

    #[test]
    fn fail_on_issues_promotion_raises_warn_and_keeps_error() {
        let mut results = AnalysisResults::default();
        results.unused_exports.push(export("src/app.ts"));
        results.unused_exports.push(export("src/legacy/old.ts"));
        apply_effective_severities(&mut results, &legacy_warn_config());

        promote_effective_warns(&mut results);

        assert!(
            results
                .unused_exports
                .iter()
                .all(|finding| finding.effective_severity == Some(EffectiveSeverity::Error))
        );
    }

    /// `apply_rule_severities` removes such a cycle before it writes the
    /// severities. The table must still agree with itself when a caller
    /// skips that filter.
    #[test]
    fn a_cycle_whose_files_all_resolve_to_off_gets_no_severity() {
        let config = config(
            r#"{
                "rules": { "circular-dependencies": "error" },
                "overrides": [{
                    "files": ["src/legacy/**"],
                    "rules": { "circular-dependencies": "off" }
                }]
            }"#,
        );
        let mut results = AnalysisResults::default();
        results
            .circular_dependencies
            .push(cycle(&["src/legacy/a.ts", "src/legacy/b.ts"]));

        apply_effective_severities(&mut results, &config);

        assert_eq!(results.circular_dependencies[0].effective_severity, None);
        assert!(!crate::error_severity::has_error_severity_issues(
            &results,
            &config.rules,
            Some(&config),
            false
        ));
        // The audit ledger reads this value for each finding.
        assert_eq!(
            results.circular_dependencies[0].rule_severity(&SeveritySource::from_config(&config)),
            Severity::Off
        );
    }

    #[test]
    fn the_collection_check_agrees_with_the_per_finding_check_without_overrides() {
        let configs = [
            r#"{ "rules": { "unused-exports": "error", "circular-dependencies": "warn",
                 "unused-dependencies": "off", "stale-suppressions": "warn",
                 "require-suppression-reason": "warn" } }"#,
            r#"{ "rules": { "unused-exports": "warn", "circular-dependencies": "error",
                 "unused-dependencies": "error", "stale-suppressions": "warn",
                 "require-suppression-reason": "error" } }"#,
            r#"{ "rules": { "unused-exports": "off", "circular-dependencies": "off",
                 "unused-dependencies": "warn", "stale-suppressions": "error",
                 "require-suppression-reason": "off" } }"#,
        ];
        let mut results = AnalysisResults::default();
        results.unused_exports.push(export("src/app.ts"));
        results
            .circular_dependencies
            .push(cycle(&["src/a.ts", "src/b.ts"]));
        results.circular_dependencies.push(cycle(&[]));
        results.unused_dependencies.push(
            serde_json::from_value::<UnusedDependencyFinding>(json!({
                "package_name": "left-pad",
                "location": "dependencies",
                "path": format!("{ROOT}/package.json"),
                "line": 3,
                "actions": [],
            }))
            .expect("dependency finding"),
        );
        results.stale_suppressions.push(stale("src/app.ts", false));
        results.stale_suppressions.push(stale("src/app.ts", true));

        for json in configs {
            let config = config(json);
            for promote in [false, true] {
                let mut rules = config.rules.clone();
                if promote {
                    promote_warns_to_errors(&mut rules);
                }
                let source = SeveritySource::new(&rules, Some(&config), promote);
                assert!(source.overrides.is_none());
                for severity in [Severity::Error, Severity::Warn, Severity::Off] {
                    let mut per_finding = false;
                    for_each_gated_finding(&mut results, &mut |finding| {
                        per_finding |= finding.rule_severity(&source) == severity;
                    });
                    assert_eq!(
                        any_gated_finding(&results, &source, severity),
                        per_finding,
                        "{json} promote={promote} {severity:?}"
                    );
                }
            }
        }
    }

    type AddFinding = fn(&mut AnalysisResults);

    #[test]
    fn the_exit_code_rule_fails_exactly_when_a_finding_is_stamped_error() {
        let config = legacy_warn_config();
        let cases: [(&str, AddFinding); 4] = [
            ("legacy export", |r| {
                r.unused_exports.push(export("src/legacy/old.ts"));
            }),
            ("app export", |r| {
                r.unused_exports.push(export("src/app.ts"));
            }),
            ("legacy cycle", |r| {
                r.circular_dependencies
                    .push(cycle(&["src/legacy/a.ts", "src/legacy/b.ts"]));
            }),
            ("stale suppression", |r| {
                r.stale_suppressions.push(stale("src/app.ts", false));
            }),
        ];
        for (name, add) in cases {
            let mut results = AnalysisResults::default();
            add(&mut results);
            apply_effective_severities(&mut results, &config);
            let mut stamped_error = false;
            for_each_gated_finding(&mut results, &mut |finding| {
                stamped_error |= finding.effective_severity() == Some(EffectiveSeverity::Error);
            });
            assert_eq!(
                crate::error_severity::has_error_severity_issues(
                    &results,
                    &config.rules,
                    Some(&config),
                    false
                ),
                stamped_error,
                "{name}"
            );
        }
    }
}