fallow-api 3.1.0

Programmatic API contract types for fallow
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
//! Shared health CodeClimate issue construction.

use std::path::Path;

use fallow_output::{
    CodeClimateIssue, CodeClimateIssueInput, CodeClimateSeverity, ComplexityViolation,
    CoverageIntelligenceFinding, CoverageIntelligenceRecommendation, CoverageIntelligenceVerdict,
    ExceededThreshold, FindingSeverity, HealthReport, RuntimeCoverageFinding,
    RuntimeCoverageVerdict, StylingFinding, StylingFindingSeverity, UntestedExportFinding,
    UntestedFileFinding, build_codeclimate_issue, codeclimate_fingerprint_hash, normalize_uri,
};

struct HealthCodeClimateContext<'a> {
    root: &'a Path,
    cyc_t: u16,
    cog_t: u16,
    crap_t: f64,
}

impl HealthCodeClimateContext<'_> {
    fn complexity_issue(&self, finding: &ComplexityViolation) -> CodeClimateIssue {
        let path = codeclimate_path(&finding.path, self.root);
        let check_name = complexity_check_name(finding);
        let line_str = finding.line.to_string();
        let fp = codeclimate_fingerprint_hash(&[check_name, &path, &line_str, &finding.name]);
        build_codeclimate_issue(CodeClimateIssueInput {
            check_name,
            description: &self.complexity_description(finding),
            severity: health_finding_severity(finding.severity),
            category: "Complexity",
            path: &path,
            begin_line: Some(finding.line),
            fingerprint: &fp,
        })
    }

    fn styling_issue(&self, finding: &StylingFinding) -> CodeClimateIssue {
        let path = codeclimate_path(Path::new(&finding.path), self.root);
        let check_name = format!("fallow/{}", finding.code);
        let description = format!("[{}] {}: {}", finding.code, finding.sub_kind, finding.value);
        let line_str = finding.line.to_string();
        let fp = codeclimate_fingerprint_hash(&[
            &check_name,
            &path,
            &line_str,
            &finding.sub_kind,
            &finding.value,
        ]);
        build_codeclimate_issue(CodeClimateIssueInput {
            check_name: &check_name,
            description: &description,
            severity: styling_finding_severity(finding.effective_severity),
            category: "Style",
            path: &path,
            begin_line: Some(finding.line),
            fingerprint: &fp,
        })
    }

    fn complexity_description(&self, finding: &ComplexityViolation) -> String {
        match finding.exceeded {
            ExceededThreshold::Both => format!(
                "'{}' has cyclomatic complexity {} (threshold: {}) and cognitive complexity {} (threshold: {})",
                finding.name, finding.cyclomatic, self.cyc_t, finding.cognitive, self.cog_t
            ),
            ExceededThreshold::Cyclomatic => format!(
                "'{}' has cyclomatic complexity {} (threshold: {})",
                finding.name, finding.cyclomatic, self.cyc_t
            ),
            ExceededThreshold::Cognitive => format!(
                "'{}' has cognitive complexity {} (threshold: {})",
                finding.name, finding.cognitive, self.cog_t
            ),
            ExceededThreshold::Crap
            | ExceededThreshold::CyclomaticCrap
            | ExceededThreshold::CognitiveCrap
            | ExceededThreshold::All => {
                let crap = finding.crap.unwrap_or(0.0);
                let coverage = finding
                    .coverage_pct
                    .map(|pct| format!(", coverage {pct:.0}%"))
                    .unwrap_or_default();
                format!(
                    "'{}' has CRAP score {crap:.1} (threshold: {:.1}, cyclomatic {}{coverage})",
                    finding.name, self.crap_t, finding.cyclomatic,
                )
            }
        }
    }

    fn runtime_coverage_issue(&self, finding: &RuntimeCoverageFinding) -> CodeClimateIssue {
        let path = codeclimate_path(&finding.path, self.root);
        let check_name = runtime_coverage_check_name(finding.verdict);
        let invocations_hint = finding.invocations.map_or_else(
            || "untracked".to_owned(),
            |hits| format!("{hits} invocations"),
        );
        let description = format!(
            "'{}' runtime coverage verdict: {} ({})",
            finding.function,
            finding.verdict.human_label(),
            invocations_hint,
        );
        let fp = codeclimate_fingerprint_hash(&[
            check_name,
            &path,
            &finding.line.to_string(),
            &finding.function,
        ]);
        build_codeclimate_issue(CodeClimateIssueInput {
            check_name,
            description: &description,
            severity: runtime_coverage_severity(finding.verdict),
            category: "Bug Risk",
            path: &path,
            begin_line: Some(finding.line),
            fingerprint: &fp,
        })
    }

    fn coverage_intelligence_issue(
        &self,
        finding: &CoverageIntelligenceFinding,
    ) -> Option<CodeClimateIssue> {
        let severity = coverage_intelligence_severity(finding.verdict)?;
        let path = codeclimate_path(&finding.path, self.root);
        let check_name = coverage_intelligence_check_name(finding.recommendation);
        let identity = finding.identity.as_deref().unwrap_or("code");
        let description = format!(
            "'{}' coverage intelligence verdict: {} ({})",
            identity, finding.verdict, finding.recommendation,
        );
        let fp = codeclimate_fingerprint_hash(&[
            check_name,
            &path,
            &finding.line.to_string(),
            identity,
            &finding.id,
        ]);
        Some(build_codeclimate_issue(CodeClimateIssueInput {
            check_name,
            description: &description,
            severity,
            category: "Bug Risk",
            path: &path,
            begin_line: Some(finding.line),
            fingerprint: &fp,
        }))
    }

    fn untested_file_issue(&self, item: &UntestedFileFinding) -> CodeClimateIssue {
        let path = codeclimate_path(&item.file.path, self.root);
        let description = format!(
            "File is runtime-reachable but has no test dependency path ({} value export{})",
            item.file.value_export_count,
            if item.file.value_export_count == 1 {
                ""
            } else {
                "s"
            },
        );
        let fp = codeclimate_fingerprint_hash(&["fallow/untested-file", &path]);
        build_codeclimate_issue(CodeClimateIssueInput {
            check_name: "fallow/untested-file",
            description: &description,
            severity: CodeClimateSeverity::Minor,
            category: "Coverage",
            path: &path,
            begin_line: None,
            fingerprint: &fp,
        })
    }

    fn untested_export_issue(&self, item: &UntestedExportFinding) -> CodeClimateIssue {
        let path = codeclimate_path(&item.export.path, self.root);
        let description = format!(
            "Export '{}' is runtime-reachable but never referenced by test-reachable modules",
            item.export.export_name
        );
        let line_str = item.export.line.to_string();
        let fp = codeclimate_fingerprint_hash(&[
            "fallow/untested-export",
            &path,
            &line_str,
            &item.export.export_name,
        ]);
        build_codeclimate_issue(CodeClimateIssueInput {
            check_name: "fallow/untested-export",
            description: &description,
            severity: CodeClimateSeverity::Minor,
            category: "Coverage",
            path: &path,
            begin_line: Some(item.export.line),
            fingerprint: &fp,
        })
    }
}

/// Build CodeClimate issues from health / complexity analysis results.
#[must_use]
pub fn build_health_codeclimate(report: &HealthReport, root: &Path) -> Vec<CodeClimateIssue> {
    let mut issues = Vec::new();
    let ctx = HealthCodeClimateContext {
        root,
        cyc_t: report.summary.max_cyclomatic_threshold,
        cog_t: report.summary.max_cognitive_threshold,
        crap_t: report.summary.max_crap_threshold,
    };

    for finding in &report.findings {
        issues.push(ctx.complexity_issue(finding));
    }
    for finding in &report.styling_findings {
        issues.push(ctx.styling_issue(finding));
    }

    if let Some(ref production) = report.runtime_coverage {
        for finding in &production.findings {
            issues.push(ctx.runtime_coverage_issue(finding));
        }
    }

    if let Some(ref intelligence) = report.coverage_intelligence {
        for finding in &intelligence.findings {
            if let Some(issue) = ctx.coverage_intelligence_issue(finding) {
                issues.push(issue);
            }
        }
    }

    if let Some(ref gaps) = report.coverage_gaps {
        for item in &gaps.files {
            issues.push(ctx.untested_file_issue(item));
        }

        for item in &gaps.exports {
            issues.push(ctx.untested_export_issue(item));
        }
    }

    issues
}

fn codeclimate_path(path: &Path, root: &Path) -> String {
    normalize_uri(
        &path
            .strip_prefix(root)
            .unwrap_or(path)
            .display()
            .to_string(),
    )
}

const fn coverage_intelligence_check_name(
    recommendation: CoverageIntelligenceRecommendation,
) -> &'static str {
    match recommendation {
        CoverageIntelligenceRecommendation::AddTestOrSplitBeforeMerge => {
            "fallow/coverage-intelligence-risky-change"
        }
        CoverageIntelligenceRecommendation::DeleteAfterConfirmingOwner => {
            "fallow/coverage-intelligence-delete"
        }
        CoverageIntelligenceRecommendation::ReviewBeforeChanging => {
            "fallow/coverage-intelligence-review"
        }
        CoverageIntelligenceRecommendation::RefactorCarefullyKeepBehavior => {
            "fallow/coverage-intelligence-refactor"
        }
    }
}

const fn complexity_check_name(finding: &ComplexityViolation) -> &'static str {
    match finding.exceeded {
        ExceededThreshold::Both => "fallow/high-complexity",
        ExceededThreshold::Cyclomatic => "fallow/high-cyclomatic-complexity",
        ExceededThreshold::Cognitive => "fallow/high-cognitive-complexity",
        ExceededThreshold::Crap
        | ExceededThreshold::CyclomaticCrap
        | ExceededThreshold::CognitiveCrap
        | ExceededThreshold::All => "fallow/high-crap-score",
    }
}

const fn health_finding_severity(severity: FindingSeverity) -> CodeClimateSeverity {
    match severity {
        FindingSeverity::Critical => CodeClimateSeverity::Critical,
        FindingSeverity::High => CodeClimateSeverity::Major,
        FindingSeverity::Moderate => CodeClimateSeverity::Minor,
    }
}

const fn styling_finding_severity(severity: StylingFindingSeverity) -> CodeClimateSeverity {
    match severity {
        StylingFindingSeverity::Error => CodeClimateSeverity::Major,
        StylingFindingSeverity::Warn => CodeClimateSeverity::Minor,
    }
}

const fn runtime_coverage_check_name(verdict: RuntimeCoverageVerdict) -> &'static str {
    match verdict {
        RuntimeCoverageVerdict::SafeToDelete => "fallow/runtime-safe-to-delete",
        RuntimeCoverageVerdict::ReviewRequired => "fallow/runtime-review-required",
        RuntimeCoverageVerdict::LowTraffic => "fallow/runtime-low-traffic",
        RuntimeCoverageVerdict::CoverageUnavailable => "fallow/runtime-coverage-unavailable",
        RuntimeCoverageVerdict::Active | RuntimeCoverageVerdict::Unknown => {
            "fallow/runtime-coverage"
        }
    }
}

const fn runtime_coverage_severity(verdict: RuntimeCoverageVerdict) -> CodeClimateSeverity {
    match verdict {
        RuntimeCoverageVerdict::SafeToDelete => CodeClimateSeverity::Critical,
        RuntimeCoverageVerdict::ReviewRequired => CodeClimateSeverity::Major,
        _ => CodeClimateSeverity::Minor,
    }
}

const fn coverage_intelligence_severity(
    verdict: CoverageIntelligenceVerdict,
) -> Option<CodeClimateSeverity> {
    match verdict {
        CoverageIntelligenceVerdict::RiskyChangeDetected
        | CoverageIntelligenceVerdict::HighConfidenceDelete => Some(CodeClimateSeverity::Major),
        CoverageIntelligenceVerdict::ReviewRequired
        | CoverageIntelligenceVerdict::RefactorCarefully => Some(CodeClimateSeverity::Minor),
        CoverageIntelligenceVerdict::Clean | CoverageIntelligenceVerdict::Unknown => None,
    }
}

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

    use fallow_output::{
        ComplexityViolation, ExceededThreshold, FindingSeverity, HealthReport, HealthSummary,
        StylingFinding, StylingFindingSeverity,
    };

    use super::*;

    #[test]
    fn health_codeclimate_uses_relative_normalized_paths() {
        let report = HealthReport {
            summary: HealthSummary {
                max_cyclomatic_threshold: 10,
                max_cognitive_threshold: 8,
                max_crap_threshold: 30.0,
                ..HealthSummary::default()
            },
            findings: vec![
                ComplexityViolation {
                    path: PathBuf::from("/root/app/[id]/page.tsx"),
                    name: "render".to_string(),
                    line: 7,
                    col: 0,
                    cyclomatic: 12,
                    cognitive: 9,
                    line_count: 20,
                    param_count: 1,
                    react_hook_count: 0,
                    react_jsx_max_depth: 0,
                    react_prop_count: 0,
                    react_hook_profile: None,
                    exceeded: ExceededThreshold::Both,
                    severity: FindingSeverity::High,
                    coverage_pct: None,
                    crap: None,
                    coverage_tier: None,
                    coverage_source: None,
                    inherited_from: None,
                    component_rollup: None,
                    contributions: Vec::new(),
                    effective_thresholds: None,
                    threshold_source: None,
                }
                .into(),
            ],
            ..HealthReport::default()
        };

        let issues = build_health_codeclimate(&report, Path::new("/root"));

        assert_eq!(issues.len(), 1);
        let issue = &issues[0];
        assert_eq!(issue.check_name, "fallow/high-complexity");
        assert_eq!(issue.location.path, "app/%5Bid%5D/page.tsx");
        assert_eq!(issue.location.lines.begin, 7);
        assert_eq!(issue.severity, CodeClimateSeverity::Major);
    }

    #[test]
    fn health_codeclimate_includes_styling_findings() {
        let report = HealthReport {
            styling_findings: vec![StylingFinding {
                code: "css-selector-complexity".to_string(),
                sub_kind: "high-specificity".to_string(),
                path: "src/styles.css".to_string(),
                line: 4,
                value: "#app .card .title".to_string(),
                effective_severity: StylingFindingSeverity::Error,
                blast_radius: None,
                confidence: None,
                agent_disposition: None,
                nearest_token: None,
                fix_hint: None,
                actions: Vec::new(),
            }],
            ..HealthReport::default()
        };

        let issues = build_health_codeclimate(&report, Path::new("/root"));

        assert_eq!(issues.len(), 1);
        let issue = &issues[0];
        assert_eq!(issue.check_name, "fallow/css-selector-complexity");
        assert_eq!(issue.location.path, "src/styles.css");
        assert_eq!(issue.location.lines.begin, 4);
        assert_eq!(issue.severity, CodeClimateSeverity::Major);
    }
}