fallow-output 3.48.0

Output contract types for fallow reports
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
use std::collections::BTreeMap;

use fallow_types::envelope::{Meta, MetaMetric, MetaRule};
pub use fallow_types::issue_meta::{CODECLIMATE_RESULT_CODES, TsAliasMeta};
use fallow_types::issue_meta::{
    IssueResultMeta, issue_meta_by_code, issue_result_meta_by_code, result_issue_metas,
};
use fallow_types::results::AnalysisResults;

const DOCS_BASE: &str = "https://fallow.tools/docs";

/// Docs URL for the dead-code/check command.
pub const CHECK_DOCS: &str = "https://fallow.tools/docs/cli/dead-code/";

/// Docs URL for the architecture command.
pub const ARCHITECTURE_DOCS: &str = "https://fallow.tools/docs/cli/architecture/";

/// `_meta` description for the per-finding `actions[]` array shared across
/// JSON output.
pub const ACTIONS_FIELD_DEFINITION: &str = "Per-finding fix and suppression suggestions. Each entry carries a `type` discriminant (kebab-case) plus a per-action `auto_fixable` bool. Consumers dispatch on `type` to choose the remediation and filter on `auto_fixable` of each individual entry.";

/// `_meta` description for the per-action `auto_fixable` bool.
pub const ACTIONS_AUTO_FIXABLE_FIELD_DEFINITION: &str = "Evaluated PER FINDING, not per action type. The same `type` may carry `auto_fixable: true` on one finding and `auto_fixable: false` on another when per-instance guards in the `fallow fix` applier discriminate. Filter on this bool of each individual action, not on `type` alone. Current per-instance flips: (1) `remove-catalog-entry` is `true` only when the finding's `hardcoded_consumers` array is empty (else fallow fix skips the entry to avoid breaking `pnpm install`); (2) the primary dependency action flips between `remove-dependency` (`auto_fixable: true`) and `move-dependency` (`auto_fixable: false`) based on `used_in_workspaces`; (3) `add-to-config` for `ignoreExports` is `true` when fallow fix can safely apply the action, which means EITHER a fallow config file already exists OR no config exists and the working directory is NOT inside a monorepo subpackage (the applier then creates `.fallowrc.json` using `fallow init`'s framework-aware scaffolding and layers the new rules on top); `false` inside a monorepo subpackage with no workspace-root config because the applier refuses to fragment per-package configs; (4) `update-catalog-reference` is always `false`, because `fallow fix` cannot switch a catalog reference. All `suppress-line` and `suppress-file` actions are uniformly `false`.";

/// Output-facing contract metadata for a serialized dead-code result row.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IssueOutputContract {
    /// Canonical issue code that owns this result array.
    pub code: &'static str,
    /// Serialized `AnalysisResults` array key that carries this issue row.
    pub result_key: &'static str,
    /// Whether `result_key` contributes to `AnalysisResults::total_issues()`.
    pub counts_in_total: bool,
    /// Label used by CI summary tables.
    pub summary_label: &'static str,
    /// Documentation anchor used by CI summary tables.
    pub summary_docs_anchor: &'static str,
    /// Human-readable name emitted in dead-code `_meta.rules`.
    pub meta_name: &'static str,
    /// Explanation emitted in dead-code `_meta.rules`.
    pub meta_description: &'static str,
    /// Documentation path emitted in dead-code `_meta.rules`.
    pub meta_docs_path: &'static str,
    /// SARIF rule ids used by the CLI SARIF formatter for this result row.
    pub sarif_rule_ids: Vec<String>,
    /// CodeClimate check names used by the CodeClimate formatter.
    pub codeclimate_check_names: Vec<String>,
    /// Published TypeScript alias policy for backwards-compatible bare names.
    pub ts_alias: Option<TsAliasMeta>,
}

impl IssueOutputContract {
    #[must_use]
    fn from_result_meta(meta: &IssueResultMeta) -> Self {
        let issue = issue_meta_by_code(meta.code).unwrap_or_else(|| {
            panic!(
                "output contract must reference IssueKindMeta row: {}",
                meta.code
            )
        });
        Self {
            code: meta.code,
            result_key: meta.result_key,
            counts_in_total: meta.counts_in_total,
            summary_label: meta.summary_label,
            summary_docs_anchor: meta.docs_anchor,
            meta_name: meta.meta_name,
            meta_description: meta.meta_description,
            meta_docs_path: meta.meta_docs_path,
            sarif_rule_ids: issue.sarif_rule_ids(),
            codeclimate_check_names: issue.codeclimate_check_names(),
            ts_alias: issue.ts_alias(),
        }
    }
}

/// Build the `_meta` object for `fallow dead-code --format json --explain`.
#[must_use]
pub fn check_meta() -> Meta {
    let mut rules = BTreeMap::new();
    for contract in issue_output_contracts() {
        rules.insert(
            contract.code.to_string(),
            MetaRule {
                name: Some(contract.meta_name.to_string()),
                description: Some(contract.meta_description.to_string()),
                docs: Some(rule_docs_url(contract.meta_docs_path)),
            },
        );
    }
    rules.insert(
        MISSING_SUPPRESSION_REASON_CODE.to_string(),
        MetaRule {
            name: Some("Missing Suppression Reason".to_string()),
            description: Some("A fallow-ignore-next-line or fallow-ignore-file suppression omits the explanatory reason required by the requireSuppressionReason rule. Add a short reason after the suppression token, or remove the suppression if the issue is no longer intentional.".to_string()),
            docs: Some(rule_docs_url("rules/missing-suppression-reason")),
        },
    );

    Meta {
        docs: Some(CHECK_DOCS.to_string()),
        field_definitions: BTreeMap::from([
            (
                "actions[]".to_string(),
                ACTIONS_FIELD_DEFINITION.to_string(),
            ),
            (
                "actions[].auto_fixable".to_string(),
                ACTIONS_AUTO_FIXABLE_FIELD_DEFINITION.to_string(),
            ),
        ]),
        rules,
        metrics: BTreeMap::from([(
            "summary.prop_drilling_chains".to_string(),
            MetaMetric {
                name: Some("Enforced prop-drilling chains".to_string()),
                description: Some("Number of prop-drilling chains with saved effective_severity set to error. Warning chains are excluded; the counter is omitted when zero.".to_string()),
                range: Some("Non-negative integer".to_string()),
                interpretation: Some("Each counted chain contributes to total_issues and CI error gates. The finding array can also contain advisory chains excluded from this counter.".to_string()),
            },
        ),
        ("summary.thin_wrappers".to_owned(), component_error_metric("Thin wrappers")),
        ("summary.duplicate_prop_shapes".to_owned(), component_error_metric("Duplicate prop shapes")),
        ]),
        ..Meta::default()
    }
}

/// Rule code of the stale-suppression findings that report a missing reason.
const MISSING_SUPPRESSION_REASON_CODE: &str = "missing-suppression-reason";
/// Rule code of the other stale-suppression findings.
const STALE_SUPPRESSION_CODE: &str = "stale-suppression";

/// Keep only the `_meta.rules` entries of rules that have a finding in one
/// of `results`. A grouped report passes the results of each group it shows.
///
/// A rule without a finding in the run tells the reader nothing about the
/// report, so `--explain` lists only the rules the report uses. The CLI, the
/// MCP server and the programmatic API call this on the same results, so the
/// three list the same rules.
pub fn retain_reported_rules<'a>(
    meta: &mut Meta,
    results: impl IntoIterator<Item = &'a AnalysisResults>,
) {
    let results: Vec<&AnalysisResults> = results.into_iter().collect();
    let reported: Vec<&'static str> = issue_output_contracts()
        .filter(|contract| {
            results.iter().any(|results| {
                // A result key without a mapping keeps its rule, so a new
                // result family never loses its explanation.
                results
                    .result_key_has_findings(contract.result_key)
                    .unwrap_or(true)
            })
        })
        .map(|contract| contract.code)
        .collect();
    let stale = || {
        results
            .iter()
            .flat_map(|results| results.stale_suppressions.iter())
    };
    let missing_reason = stale().any(|finding| finding.missing_reason);
    let stale_without_missing_reason = stale().any(|finding| !finding.missing_reason);
    meta.rules.retain(|code, _| match code.as_str() {
        MISSING_SUPPRESSION_REASON_CODE => missing_reason,
        STALE_SUPPRESSION_CODE => stale_without_missing_reason,
        code => reported.contains(&code),
    });
}

fn component_error_metric(name: &str) -> MetaMetric {
    MetaMetric {
        name: Some(format!("Enforced {name}")),
        description: Some("Number of findings with saved effective_severity set to error. Warnings are excluded; the counter is omitted when zero.".to_owned()),
        range: Some("Non-negative integer".to_owned()),
        interpretation: Some("Each counted finding contributes to total_issues and CI error gates. The finding array can also contain advisory warnings.".to_owned()),
    }
}

/// The `AnalysisResults` arrays that `fallow architecture` reports: import
/// cycles, boundary violations and rule-pack policy violations.
///
/// One list for the `_meta.rules` filter, the human report and the
/// programmatic selection, so the three cannot disagree.
pub const ARCHITECTURE_RESULT_KEYS: &[&str] = &[
    "circular_dependencies",
    "re_export_cycles",
    "package_cycles",
    "boundary_violations",
    "boundary_coverage_violations",
    "boundary_call_violations",
    "policy_violations",
];

/// Build the `_meta` object for `fallow architecture --format json --explain`.
///
/// It has the dead-code field definitions, the architecture docs URL, and
/// only the rules of the [`ARCHITECTURE_RESULT_KEYS`] arrays.
#[must_use]
pub fn architecture_meta() -> Meta {
    let mut meta = check_meta();
    let architecture_codes: Vec<&'static str> = issue_output_contracts()
        .filter(|contract| ARCHITECTURE_RESULT_KEYS.contains(&contract.result_key))
        .map(|contract| contract.code)
        .collect();
    meta.rules
        .retain(|code, _| architecture_codes.contains(&code.as_str()));
    for key in fallow_types::results::ERROR_ONLY_RESULT_KEYS {
        meta.metrics.remove(&format!("summary.{key}"));
    }
    meta.docs = Some(ARCHITECTURE_DOCS.to_string());
    meta
}

/// Public docs URL for a rule's `docs_path` relative to the docs site root.
///
/// A page URL ends with a slash, before any `#anchor`, because that is the
/// final address of the page: `cli/flags#x` becomes `.../cli/flags/#x`.
#[must_use]
pub fn rule_docs_url(docs_path: &str) -> String {
    match docs_path.split_once('#') {
        Some((page, anchor)) => format!("{DOCS_BASE}/{page}/#{anchor}"),
        None => format!("{DOCS_BASE}/{docs_path}/"),
    }
}

/// Output-facing dead-code result contracts in stable registry order.
pub fn issue_output_contracts() -> impl Iterator<Item = IssueOutputContract> {
    result_issue_metas().map(IssueOutputContract::from_result_meta)
}

/// Output-facing dead-code result contract by issue code.
#[must_use]
pub fn issue_output_contract_by_code(code: &str) -> Option<IssueOutputContract> {
    issue_result_meta_by_code(code).map(IssueOutputContract::from_result_meta)
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeSet;

    use super::*;

    fn stale_suppression(missing_reason: bool) -> fallow_types::results::StaleSuppression {
        fallow_types::results::StaleSuppression {
            path: std::path::PathBuf::from("src/a.ts"),
            line: 1,
            col: 0,
            origin: fallow_types::results::SuppressionOrigin::JsdocTag {
                export_name: "a".to_string(),
                reason: None,
            },
            missing_reason,
            finding_id: None,
            actions: Vec::new(),
            effective_severity: None,
        }
    }

    #[test]
    fn every_contract_result_key_maps_to_a_result_array() {
        let results = AnalysisResults::default();
        for contract in issue_output_contracts() {
            assert_eq!(
                results.result_key_has_findings(contract.result_key),
                Some(false),
                "{} has no result array mapping",
                contract.result_key
            );
        }
    }

    #[test]
    fn retain_reported_rules_keeps_only_rules_with_findings() {
        let mut meta = check_meta();
        retain_reported_rules(&mut meta, [&AnalysisResults::default()]);
        assert!(
            meta.rules.is_empty(),
            "no finding, no rule: {:?}",
            meta.rules
        );

        let mut meta = check_meta();
        let mut results = AnalysisResults::default();
        results.stale_suppressions.push(stale_suppression(true));
        retain_reported_rules(&mut meta, [&results]);
        assert_eq!(
            meta.rules.keys().map(String::as_str).collect::<Vec<_>>(),
            vec!["missing-suppression-reason"],
            "a missing reason does not list the stale-suppression rule"
        );

        let mut meta = check_meta();
        let mut other_group = AnalysisResults::default();
        other_group
            .stale_suppressions
            .push(stale_suppression(false));
        retain_reported_rules(&mut meta, [&results, &other_group]);
        assert_eq!(
            meta.rules.keys().map(String::as_str).collect::<Vec<_>>(),
            vec!["missing-suppression-reason", "stale-suppression"],
            "a grouped report lists the rules of every group"
        );
    }

    #[test]
    fn architecture_meta_lists_only_architecture_rules() {
        let meta = architecture_meta();
        let expected: BTreeSet<&str> = issue_output_contracts()
            .filter(|contract| ARCHITECTURE_RESULT_KEYS.contains(&contract.result_key))
            .map(|contract| contract.code)
            .collect();
        let actual: BTreeSet<&str> = meta.rules.keys().map(String::as_str).collect();

        assert_eq!(actual, expected);
        assert_eq!(meta.docs.as_deref(), Some(ARCHITECTURE_DOCS));
        assert_eq!(meta.field_definitions, check_meta().field_definitions);
        assert!(!meta.metrics.contains_key("summary.prop_drilling_chains"));
    }

    #[test]
    fn every_architecture_result_key_names_a_result_row() {
        let keys: BTreeSet<&str> = issue_output_contracts()
            .map(|contract| contract.result_key)
            .collect();
        for key in ARCHITECTURE_RESULT_KEYS {
            assert!(keys.contains(key), "`{key}` is not a result array key");
        }
        let architecture_rows = issue_output_contracts()
            .filter(|contract| ARCHITECTURE_RESULT_KEYS.contains(&contract.result_key))
            .count();
        assert_eq!(architecture_rows, ARCHITECTURE_RESULT_KEYS.len());
    }

    #[test]
    fn every_result_row_has_output_contract() {
        let result_codes: BTreeSet<&str> = result_issue_metas().map(|meta| meta.code).collect();
        let output_codes: BTreeSet<&str> = issue_output_contracts()
            .map(|contract| contract.code)
            .collect();
        assert_eq!(result_codes, output_codes);
    }

    #[test]
    fn summary_contracts_are_present() {
        for contract in issue_output_contracts() {
            assert!(!contract.summary_label.is_empty());
            assert!(!contract.summary_docs_anchor.is_empty());
            assert!(!contract.meta_name.is_empty());
            assert!(!contract.meta_description.is_empty());
            assert!(!contract.meta_docs_path.is_empty());
        }
    }

    #[test]
    fn check_meta_uses_output_contracts() {
        let meta = check_meta();
        assert_eq!(meta.docs.as_deref(), Some(CHECK_DOCS));
        assert!(
            meta.field_definitions["actions[].auto_fixable"].contains("PER FINDING"),
            "auto_fixable definition should preserve per-finding guidance"
        );
        assert!(meta.rules.contains_key("unused-export"));
        assert!(meta.rules.contains_key("missing-suppression-reason"));
        assert_eq!(
            meta.rules["unused-dev-dependency"].docs.as_deref(),
            Some("https://fallow.tools/docs/rules/unused-dev-dependency/")
        );
    }

    /// Each `_meta.rules` key is an issue type, and the docs site has one
    /// rule page for each issue type at `rules/<id>/`.
    #[test]
    fn check_meta_links_each_rule_to_its_rule_page() {
        for (code, rule) in check_meta().rules {
            assert!(
                code == MISSING_SUPPRESSION_REASON_CODE
                    || issue_output_contract_by_code(&code).is_some(),
                "_meta.rules key {code} is not an issue type"
            );
            assert_eq!(
                rule.docs,
                Some(format!("https://fallow.tools/docs/rules/{code}/")),
                "_meta.rules.{code} must link to its rule page"
            );
        }
    }

    #[test]
    fn ci_format_contracts_are_present() {
        for contract in issue_output_contracts() {
            assert!(
                contract
                    .sarif_rule_ids
                    .contains(&format!("fallow/{}", contract.code)),
                "result metadata code {} has wrong SARIF rule id",
                contract.code
            );
            for rule_id in contract.sarif_rule_ids {
                assert!(
                    rule_id.starts_with("fallow/"),
                    "result metadata code {} has unprefixed SARIF rule id {rule_id}",
                    contract.code
                );
            }
            for check_name in &contract.codeclimate_check_names {
                assert!(
                    check_name.starts_with("fallow/"),
                    "result metadata code {} has unprefixed CodeClimate check name {check_name}",
                    contract.code
                );
            }
            assert!(
                !contract.codeclimate_check_names.is_empty(),
                "result metadata code {} has no CodeClimate check names",
                contract.code
            );
        }
    }

    #[test]
    fn component_health_codeclimate_contracts_are_present() {
        for code in ["duplicate-prop-shape", "thin-wrapper"] {
            let contract = issue_output_contracts()
                .find(|contract| contract.code == code)
                .expect("component-health contract must be present");
            let expected = format!("fallow/{code}");
            assert_eq!(contract.codeclimate_check_names, vec![expected]);
        }
    }

    #[test]
    fn codeclimate_result_codes_match_result_metadata() {
        let result_codes: BTreeSet<&str> = result_issue_metas().map(|meta| meta.code).collect();
        let codeclimate_codes: BTreeSet<&str> = CODECLIMATE_RESULT_CODES.iter().copied().collect();
        assert!(codeclimate_codes.is_subset(&result_codes));
    }

    #[test]
    fn ts_alias_policy_is_explicit() {
        let aliases: BTreeSet<(&str, &str)> = issue_output_contracts()
            .filter_map(|contract| contract.ts_alias.map(|alias| (alias.name, alias.parent)))
            .collect();

        assert_eq!(
            BTreeSet::from([
                ("BoundaryViolation", "BoundaryViolationFinding"),
                ("CircularDependency", "CircularDependencyFinding"),
                ("DeprecatedExportInUse", "DeprecatedExportInUseFinding"),
                (
                    "DevDependencyInProduction",
                    "DevDependencyInProductionFinding",
                ),
                ("DuplicateExport", "DuplicateExportFinding"),
                ("DuplicateExportAlias", "DuplicateExportAliasFinding"),
                ("EmptyCatalogGroup", "EmptyCatalogGroupFinding"),
                (
                    "MisconfiguredDependencyOverride",
                    "MisconfiguredDependencyOverrideFinding",
                ),
                ("PackageCycle", "PackageCycleFinding"),
                ("PrivateTypeLeak", "PrivateTypeLeakFinding"),
                ("ReExportCycle", "ReExportCycleFinding"),
                ("TestOnlyDependency", "TestOnlyDependencyFinding"),
                ("TypeOnlyDependency", "TypeOnlyDependencyFinding"),
                ("UnlistedDependency", "UnlistedDependencyFinding"),
                ("UnlistedBinary", "UnlistedBinaryFinding"),
                (
                    "UnresolvedCatalogReference",
                    "UnresolvedCatalogReferenceFinding",
                ),
                ("UnresolvedImport", "UnresolvedImportFinding"),
                ("UnusedCatalogEntry", "UnusedCatalogEntryFinding"),
                ("UnusedDependency", "UnusedDependencyFinding"),
                ("UnusedDependency", "UnusedDevDependencyFinding"),
                ("UnusedDependency", "UnusedOptionalDependencyFinding"),
                (
                    "UnusedDependencyOverride",
                    "UnusedDependencyOverrideFinding",
                ),
                ("UnusedExport", "UnusedExportFinding"),
                ("UnusedFile", "UnusedFileFinding"),
                ("NamespaceEscape", "NamespaceEscapeFinding"),
                ("UnusedMember", "UnusedClassMemberFinding"),
                ("UnusedMember", "UnusedEnumMemberFinding"),
                ("UnusedMember", "UnusedNamespaceMemberFinding"),
                ("UnusedMember", "UnusedProtectedClassMemberFinding"),
                ("UnusedMember", "UnusedStoreMemberFinding"),
            ]),
            aliases
        );
    }
}