Skip to main content

fallow_api/diff_risk/
mod.rs

1//! Diff-risk rules of `fallow audit`.
2//!
3//! The rules compare the base and head text of each changed file. They report
4//! a fact about the change, so every finding is introduced. The pass reads the
5//! base text through one `git cat-file --batch` process at the merge base of
6//! the base ref and `HEAD`.
7//!
8//! - `weakened-tests`: a new skip or focus marker, fewer test cases across the
9//!   changed test files, fewer assertions in a test file, or a less strict
10//!   coverage threshold.
11//! - `added-suppression`: a new lint, type-check, coverage or Fallow
12//!   suppression comment.
13//! - `test-only-branch`: a new test-environment branch in a non-test source
14//!   file.
15//! - `diverged-clone-sibling`: one copy of a base clone group was edited out
16//!   of the group, and no sibling changed. Needs the base snapshot.
17//! - `added-dependency`: a new `package.json` dependency. Off by default.
18
19pub mod clones;
20pub mod text;
21
22use std::path::{Path, PathBuf};
23
24use fallow_config::{ResolvedConfig, RulesConfig, Severity};
25use fallow_engine::changed_files::RenamedFile;
26use fallow_engine::module_graph::relative_key_path;
27use fallow_output::{
28    DiffRiskAction, DiffRiskActionType, DiffRiskFinding, DiffRiskLocation, DiffRiskReport,
29    DiffRiskRule, DiffRiskSeverity, DiffRiskSuppression, WeakenedTestsKind,
30};
31use fallow_types::duplicates::CloneGroup;
32use fallow_types::suppress::{IssueKind, Suppression, SuppressionTarget};
33use rustc_hash::{FxHashMap, FxHashSet};
34
35use crate::audit_run::{BaseFileReader, BaseRead};
36use clones::{BaseCloneGroup, CloneSpan, CloneTexts};
37
38/// Extensions of files that hold JavaScript-style comments, where an added
39/// suppression or a test-only branch is meaningful.
40const SOURCE_EXTENSIONS: &[&str] = &[
41    "js", "jsx", "ts", "tsx", "mjs", "cjs", "mts", "cts", "vue", "svelte", "astro",
42];
43/// Extensions where a suppression comment is meaningful. Adds styles and
44/// markup to [`SOURCE_EXTENSIONS`].
45const STYLE_EXTENSIONS: &[&str] = &["css", "scss", "sass", "less", "html"];
46
47/// Where the rule severities come from.
48#[derive(Clone, Copy)]
49pub enum DiffRiskRules<'a> {
50    /// The resolved config, with per-path `overrides`.
51    Config(&'a ResolvedConfig),
52    /// One rules table for every path.
53    Rules(&'a RulesConfig),
54}
55
56impl DiffRiskRules<'_> {
57    fn for_path(self, abs: &Path) -> RulesConfig {
58        match self {
59            Self::Config(config) => config.resolve_rules_for_path(abs),
60            Self::Rules(rules) => rules.clone(),
61        }
62    }
63}
64
65/// Inputs of one diff-risk pass.
66pub struct DiffRiskInput<'a> {
67    /// Head analysis root. Finding paths are relative to it.
68    pub root: &'a Path,
69    /// Resolved base ref of the audit.
70    pub base_ref: &'a str,
71    /// Absolute paths of the changed files.
72    pub changed_files: &'a FxHashSet<PathBuf>,
73    /// Renames between base and head.
74    pub renames: &'a [RenamedFile],
75    /// Rule severities.
76    pub rules: DiffRiskRules<'a>,
77    /// Clone groups of the base snapshot. `None` turns off
78    /// `diverged-clone-sibling`: without a base pass no copy can be compared.
79    pub base_clone_groups: Option<&'a [BaseCloneGroup]>,
80    /// Instance locations of every head clone group.
81    pub head_clone_groups: Vec<Vec<CloneSpan>>,
82}
83
84/// The instance locations of a clone group, relative to `root`.
85#[must_use]
86pub fn clone_spans(group: &CloneGroup, root: &Path) -> Vec<CloneSpan> {
87    group
88        .instances
89        .iter()
90        .map(|instance| CloneSpan {
91            path: relative_key_path(&instance.file, root),
92            start_line: u32::try_from(instance.start_line).unwrap_or(u32::MAX),
93            end_line: u32::try_from(instance.end_line).unwrap_or(u32::MAX),
94        })
95        .collect()
96}
97
98/// The base and head text of one changed file.
99struct FileTexts {
100    /// Head path relative to the analysis root.
101    rel: String,
102    /// Absolute head path.
103    abs: PathBuf,
104    /// Base text; `None` when the file is new.
105    base: Option<String>,
106    /// Head text; `None` when the change deleted the file.
107    head: Option<String>,
108}
109
110/// A finding before the severity and suppression pass.
111struct RawFinding {
112    rule: DiffRiskRule,
113    kind: Option<WeakenedTestsKind>,
114    path: String,
115    line: Option<u32>,
116    message: String,
117    evidence: String,
118    related: Vec<DiffRiskLocation>,
119}
120
121/// Run every diff-risk rule over the changed files.
122///
123/// Best-effort: a file that cannot be read is skipped, and a base-reader
124/// error stops the scan of the remaining files, so a broken pipe never reads
125/// as an empty base file.
126#[must_use]
127pub fn detect(input: &DiffRiskInput<'_>) -> DiffRiskReport {
128    let Ok(git_root) = fallow_engine::changed_files::resolve_git_toplevel(input.root) else {
129        return DiffRiskReport::default();
130    };
131    let Some(reader) = BaseFileReader::spawn(input.root) else {
132        return DiffRiskReport::default();
133    };
134    let mut base = BaseSource {
135        git_root: dunce::canonicalize(&git_root).unwrap_or(git_root),
136        reader,
137        commit: merge_base(input.root, input.base_ref),
138    };
139    let files = read_files(input, &mut base);
140
141    let mut raw = Vec::new();
142    for file in &files {
143        let rules = input.rules.for_path(&file.abs);
144        detect_file(input.root, file, &rules, &mut raw);
145    }
146    detect_removed_tests(input.root, &files, &mut raw);
147    if let Some(base_groups) = input.base_clone_groups {
148        detect_diverged_clones(input, &mut base, base_groups, &files, &mut raw);
149    }
150
151    let findings = finalize(input, &files, raw);
152    DiffRiskReport { findings }
153}
154
155/// Base-side text access: one `git cat-file --batch` reader at the merge
156/// base.
157struct BaseSource {
158    git_root: PathBuf,
159    reader: BaseFileReader,
160    commit: String,
161}
162
163impl BaseSource {
164    /// Read the base text of an absolute path. A path outside the repository
165    /// reads as an error.
166    fn read(&mut self, abs: &Path) -> BaseRead {
167        match abs.strip_prefix(&self.git_root) {
168            Ok(relative) => self.reader.read(&self.commit, relative),
169            Err(_) => BaseRead::Error,
170        }
171    }
172}
173
174/// The merge base of `base_ref` and `HEAD`, or `base_ref` when git cannot
175/// resolve one. The changed-file list uses the merge base too, so a base
176/// branch that moved on does not read as a change of this branch.
177pub(crate) fn merge_base(root: &Path, base_ref: &str) -> String {
178    let mut command = fallow_engine::changed_files::git_command_in(root);
179    command
180        .args(["merge-base", "--end-of-options", base_ref, "HEAD"])
181        .stderr(std::process::Stdio::null());
182    command
183        .output()
184        .ok()
185        .filter(|output| output.status.success())
186        .map(|output| String::from_utf8_lossy(&output.stdout).trim().to_owned())
187        .filter(|sha| !sha.is_empty())
188        .unwrap_or_else(|| base_ref.to_owned())
189}
190
191/// Whether a diff-risk rule can read this file. The rules read discovered
192/// source files (every clone group member is one), test files, test-runner
193/// and coverage configs, and package manifests. Other changed files, such as
194/// a lockfile or an image, are not read.
195fn is_rule_relevant(root: &Path, rel: &str) -> bool {
196    fallow_engine::discover::SOURCE_EXTENSIONS.contains(&extension(rel))
197        || takes_suppression_comments(rel)
198        || is_js_test_code(root, rel)
199        || text::is_coverage_config_path(rel)
200        || crate::dependency_deltas::is_manifest_path(rel)
201}
202
203fn read_files(input: &DiffRiskInput<'_>, source: &mut BaseSource) -> Vec<FileTexts> {
204    let rename_sources: FxHashSet<&Path> = input
205        .renames
206        .iter()
207        .map(|rename| rename.from.as_path())
208        .collect();
209    let rename_targets: FxHashMap<&Path, &Path> = input
210        .renames
211        .iter()
212        .map(|rename| (rename.to.as_path(), rename.from.as_path()))
213        .collect();
214    let mut paths: Vec<&PathBuf> = input.changed_files.iter().collect();
215    paths.sort();
216
217    let mut files = Vec::new();
218    for abs in paths {
219        // The old path of a rename is not a deletion: its text lives on at
220        // the new path, which reads its base text from the old path.
221        if rename_sources.contains(abs.as_path()) {
222            continue;
223        }
224        if !abs.starts_with(input.root) {
225            continue;
226        }
227        let base_abs = rename_targets
228            .get(abs.as_path())
229            .copied()
230            .unwrap_or(abs.as_path());
231        if !base_abs.starts_with(&source.git_root) {
232            continue;
233        }
234        let rel = relative_key_path(abs, input.root);
235        if !is_rule_relevant(input.root, &rel) {
236            continue;
237        }
238        let head = match std::fs::read(abs) {
239            Ok(bytes) => Some(String::from_utf8_lossy(&bytes).into_owned()),
240            Err(error) if error.kind() == std::io::ErrorKind::NotFound => None,
241            Err(_) => continue,
242        };
243        let base = match source.read(base_abs) {
244            BaseRead::Content(base) => Some(base),
245            BaseRead::Missing => None,
246            BaseRead::Error => break,
247        };
248        if base.is_none() && head.is_none() {
249            continue;
250        }
251        files.push(FileTexts {
252            rel,
253            abs: abs.clone(),
254            base,
255            head,
256        });
257    }
258    files
259}
260
261fn extension(rel: &str) -> &str {
262    let name = rel.rsplit('/').next().unwrap_or(rel);
263    name.rsplit_once('.').map_or("", |(_, ext)| ext)
264}
265
266fn is_source_file(rel: &str) -> bool {
267    SOURCE_EXTENSIONS.contains(&extension(rel))
268}
269
270fn takes_suppression_comments(rel: &str) -> bool {
271    is_source_file(rel) || STYLE_EXTENSIONS.contains(&extension(rel))
272}
273
274/// A JavaScript-family test file, where the test rules apply. A test file in
275/// another language, such as a Rust integration test, can hold JavaScript
276/// test text in a string, so it is not read.
277fn is_js_test_code(root: &Path, rel: &str) -> bool {
278    is_source_file(rel) && fallow_engine::test_paths::is_test_code_path_str(root, rel)
279}
280
281/// A test file, or a type test of the `tsd` and Vitest conventions
282/// (`test-d/`, `*.test-d.ts`).
283fn is_type_or_test_file(root: &Path, rel: &str) -> bool {
284    fallow_engine::test_paths::is_test_path_str(root, rel)
285        || rel.split('/').any(|segment| segment == "test-d")
286        || rel.contains(".test-d.")
287}
288
289/// Directory names of test fixtures and generated code. A tool writes these
290/// files, or a test reads them as data, so their suppression comments are
291/// not a choice of the change author.
292const FIXTURE_OR_GENERATED_DIRS: &[&str] = &[
293    "fixture",
294    "fixtures",
295    "__fixtures__",
296    "__snapshots__",
297    "generated",
298    "__generated__",
299];
300
301fn is_fixture_or_generated(rel: &str) -> bool {
302    let mut dirs = rel.split('/').rev().skip(1);
303    dirs.any(|dir| FIXTURE_OR_GENERATED_DIRS.contains(&dir))
304}
305
306/// File-name markers of story files (`button.stories.tsx`).
307const STORY_FILE_MARKERS: &[&str] = &[".stories.", ".story."];
308
309/// Test or story code. Each copy there shows one case or one state, so the
310/// copies differ on purpose.
311fn is_example_copy(root: &Path, rel: &str) -> bool {
312    if fallow_engine::test_paths::is_test_path_str(root, rel) {
313        return true;
314    }
315    let name = rel.rsplit('/').next().unwrap_or(rel).to_ascii_lowercase();
316    STORY_FILE_MARKERS
317        .iter()
318        .any(|marker| name.contains(marker))
319}
320
321/// A tool config such as `vite.config.ts`. A test-environment check is
322/// normal there.
323fn is_tool_config(rel: &str) -> bool {
324    let name = rel.rsplit('/').next().unwrap_or(rel);
325    // An rc file is a dot file whose first name part ends in `rc`, such as
326    // `.eslintrc.js`. `src.ts` and `arc.ts` are not rc files.
327    let dot_rc = name.strip_prefix('.').is_some_and(|rest| {
328        rest.split_once('.')
329            .is_some_and(|(stem, _ext)| stem.ends_with("rc"))
330    });
331    name.contains(".config.") || name.contains(".conf.") || dot_rc
332}
333
334fn detect_file(root: &Path, file: &FileTexts, rules: &RulesConfig, raw: &mut Vec<RawFinding>) {
335    let base = file.base.as_deref().unwrap_or("");
336    let Some(head) = file.head.as_deref() else {
337        return;
338    };
339    let rel = file.rel.as_str();
340    let is_test = is_js_test_code(root, rel);
341
342    if rules.weakened_tests != Severity::Off {
343        if is_test {
344            for hit in text::skip_only_hits(base, head) {
345                raw.push(RawFinding {
346                    rule: DiffRiskRule::WeakenedTests,
347                    kind: Some(WeakenedTestsKind::SkipOnly),
348                    path: rel.to_owned(),
349                    line: Some(hit.line),
350                    message: format!(
351                        "The change adds `{}`, so other tests or this test do not run",
352                        hit.evidence
353                    ),
354                    evidence: hit.evidence,
355                    related: Vec::new(),
356                });
357            }
358        }
359        if text::is_coverage_config_path(rel) {
360            for hit in text::weakened_threshold_hits(base, head) {
361                raw.push(RawFinding {
362                    rule: DiffRiskRule::WeakenedTests,
363                    kind: Some(WeakenedTestsKind::CoverageThreshold),
364                    path: rel.to_owned(),
365                    line: Some(hit.line),
366                    message: format!(
367                        "The change makes a threshold less strict ({})",
368                        hit.evidence
369                    ),
370                    evidence: hit.evidence,
371                    related: Vec::new(),
372                });
373            }
374        }
375    }
376
377    if rules.added_suppression != Severity::Off
378        && takes_suppression_comments(rel)
379        && !is_fixture_or_generated(rel)
380    {
381        let type_test = is_type_or_test_file(root, rel);
382        for hit in text::added_suppression_hits(base, head) {
383            // In a test, `@ts-expect-error` asserts that a type error exists.
384            if type_test && hit.evidence == "@ts-expect-error" {
385                continue;
386            }
387            raw.push(RawFinding {
388                rule: DiffRiskRule::AddedSuppression,
389                kind: None,
390                path: rel.to_owned(),
391                line: Some(hit.line),
392                message: format!("The change adds a `{}` suppression comment", hit.evidence),
393                evidence: hit.evidence,
394                related: Vec::new(),
395            });
396        }
397    }
398
399    if rules.test_only_branch != Severity::Off
400        && is_source_file(rel)
401        && !fallow_engine::test_paths::is_test_path_str(root, rel)
402        && !is_tool_config(rel)
403    {
404        for hit in text::test_only_branch_hits(base, head) {
405            raw.push(RawFinding {
406                rule: DiffRiskRule::TestOnlyBranch,
407                kind: None,
408                path: rel.to_owned(),
409                line: Some(hit.line),
410                message: format!(
411                    "The change adds a `{}` check to source code, so tests and production can take different paths",
412                    hit.evidence
413                ),
414                evidence: hit.evidence,
415                related: Vec::new(),
416            });
417        }
418    }
419
420    if rules.added_dependency != Severity::Off && crate::dependency_deltas::is_manifest_path(rel) {
421        let deltas = crate::dependency_deltas::manifest_dependency_deltas(base, head);
422        for added in deltas.added {
423            let evidence = format!("{}@{} ({})", added.name, added.to, added.section);
424            raw.push(RawFinding {
425                rule: DiffRiskRule::AddedDependency,
426                kind: None,
427                path: rel.to_owned(),
428                line: (added.line > 0).then_some(added.line),
429                message: format!("The change adds the dependency `{}`", added.name),
430                evidence,
431                related: Vec::new(),
432            });
433        }
434    }
435}
436
437/// The names that the change removed from the project.
438///
439/// - Identifiers that a changed non-test source file held in base and no
440///   longer holds in head, such as a deleted function or a dropped call.
441///   Identifiers of a deleted file count only when no changed non-test source
442///   file still holds them in head, and names that the deleted file imports
443///   never count.
444/// - The file stem of each deleted file without test cases, and each of its
445///   directories that is gone at head, such as a deleted fixture directory.
446///
447/// Reserved words and common globals never count.
448struct RemovedNames {
449    identifiers: FxHashSet<String>,
450    segments: FxHashSet<String>,
451}
452
453/// The shortest name that can explain a test removal. Shorter names, such as
454/// `id` or `get`, are too common to tie a test to the removed code.
455const MIN_REMOVED_NAME_LEN: usize = 4;
456
457/// JS and TS reserved words and common globals. Each source file holds them,
458/// so they do not tie a test to the removed code.
459const NOT_REMOVED_NAMES: &[&str] = &[
460    "async",
461    "await",
462    "break",
463    "case",
464    "catch",
465    "class",
466    "console",
467    "const",
468    "continue",
469    "default",
470    "delete",
471    "else",
472    "enum",
473    "export",
474    "exports",
475    "extends",
476    "false",
477    "finally",
478    "from",
479    "function",
480    "import",
481    "instanceof",
482    "interface",
483    "module",
484    "null",
485    "require",
486    "return",
487    "static",
488    "super",
489    "switch",
490    "this",
491    "throw",
492    "true",
493    "type",
494    "typeof",
495    "undefined",
496    "void",
497    "while",
498    "with",
499    "yield",
500];
501
502/// Whether a token can name removed code.
503fn is_removed_name_candidate(name: &str) -> bool {
504    name.len() >= MIN_REMOVED_NAME_LEN && !NOT_REMOVED_NAMES.contains(&name)
505}
506
507/// The names that the import statements of a file bind. An imported name is
508/// declared in another file, so its use in a deleted file does not show that
509/// the name left the project.
510fn imported_names(src: &str) -> FxHashSet<String> {
511    let mut statements = String::new();
512    let mut in_import = false;
513    for line in src.lines() {
514        let trimmed = line.trim_start();
515        if !in_import && trimmed.starts_with("import ") {
516            in_import = true;
517        }
518        if !in_import {
519            continue;
520        }
521        statements.push_str(line);
522        statements.push('\n');
523        if trimmed.ends_with(';') || trimmed.contains("from ") || trimmed.ends_with("from") {
524            in_import = false;
525        }
526    }
527    text::code_identifiers(&statements)
528}
529
530impl RemovedNames {
531    fn collect(root: &Path, files: &[FileTexts]) -> Self {
532        let mut identifiers = FxHashSet::default();
533        let mut deleted_identifiers = FxHashSet::default();
534        let mut segments = FxHashSet::default();
535        let mut head_union = FxHashSet::default();
536        for file in files {
537            // A deleted file with test cases does not explain its own removal.
538            if file.head.is_none()
539                && file
540                    .base
541                    .as_deref()
542                    .is_some_and(|base| text::test_case_count(base) == 0)
543            {
544                segments.extend(deleted_path_segments(root, &file.rel));
545            }
546            if !is_source_file(&file.rel)
547                || fallow_engine::test_paths::is_test_path_str(root, &file.rel)
548            {
549                continue;
550            }
551            if let Some(head) = file.head.as_deref() {
552                head_union.extend(text::code_identifiers(head));
553            }
554            let Some(base) = file.base.as_deref() else {
555                continue;
556            };
557            match file.head.as_deref() {
558                Some(head) => {
559                    let head = text::code_identifiers(head);
560                    identifiers.extend(
561                        text::code_identifiers(base)
562                            .into_iter()
563                            .filter(|name| is_removed_name_candidate(name) && !head.contains(name)),
564                    );
565                }
566                None => {
567                    let imported = imported_names(base);
568                    deleted_identifiers.extend(text::code_identifiers(base).into_iter().filter(
569                        |name| is_removed_name_candidate(name) && !imported.contains(name),
570                    ));
571                }
572            }
573        }
574        // A name of a deleted file that a changed source file still holds did
575        // not leave the project, for example a function that moved from the
576        // deleted file.
577        identifiers.extend(
578            deleted_identifiers
579                .into_iter()
580                .filter(|name| !head_union.contains(name)),
581        );
582        Self {
583            identifiers,
584            segments,
585        }
586    }
587
588    /// Whether the lines that a test file lost name removed code that the
589    /// head test no longer names. Then the tests left together with the code
590    /// they covered. A path segment matches only a whole token of a line, so
591    /// a deleted `core` directory does not match `score`.
592    fn explain(&self, base: &str, head: &str) -> bool {
593        let removed: Vec<&str> = text::added_lines(head, base)
594            .into_iter()
595            .map(|(_, line)| line)
596            .collect();
597        if removed.is_empty() {
598            return false;
599        }
600        let head_identifiers = text::code_identifiers(head);
601        let names_removed_identifier = removed.iter().any(|line| {
602            text::code_identifiers(line)
603                .iter()
604                .any(|name| self.identifiers.contains(name) && !head_identifiers.contains(name))
605        });
606        names_removed_identifier
607            || self.segments.iter().any(|segment| {
608                !head.contains(segment.as_str())
609                    && removed
610                        .iter()
611                        .any(|line| path_tokens(line).any(|token| token == segment))
612            })
613    }
614}
615
616/// The path segments that a deleted file takes out of the project: its file
617/// stem, and each directory that is gone at head. A directory that still
618/// exists, such as `src/components`, did not leave the project, so a test that
619/// imports another file from it is not explained.
620fn deleted_path_segments(root: &Path, rel: &str) -> Vec<String> {
621    let stem_path = rel.rsplit_once('.').map_or(rel, |(stem, _)| stem);
622    let mut found = Vec::new();
623    let mut prefix_len = 0;
624    let parts: Vec<&str> = stem_path.split('/').collect();
625    let last = parts.len().saturating_sub(1);
626    for (index, part) in parts.iter().enumerate() {
627        prefix_len += part.len();
628        let is_stem = index == last;
629        let gone = is_stem || !root.join(&rel[..prefix_len]).exists();
630        if gone && part.len() >= MIN_REMOVED_NAME_LEN {
631            found.push((*part).to_owned());
632        }
633        prefix_len += 1;
634    }
635    found
636}
637
638/// The tokens of a line split on path and string delimiters.
639fn path_tokens(line: &str) -> impl Iterator<Item = &str> {
640    line.split(['/', '.', '\'', '"', '`'])
641}
642
643/// `removed-tests` and `assertion-drop`. Both compare per file, and both
644/// report only when the total over every changed test file also dropped, so
645/// tests or assertions that moved to another changed file do not report. A
646/// drop does not report when the lost test lines name code that the change
647/// removed: the tests went together with that code. An assertion drop reports
648/// only when assertions are at least half of the code lines that the file
649/// lost. Otherwise the change rewrote the test, for example it moved setup
650/// checks into a shared helper.
651fn detect_removed_tests(root: &Path, files: &[FileTexts], raw: &mut Vec<RawFinding>) {
652    struct Counts<'a> {
653        rel: &'a str,
654        base_cases: usize,
655        head_cases: usize,
656        base_asserts: usize,
657        head_asserts: usize,
658        explained: bool,
659        assertions_led: bool,
660    }
661    let removed_names = RemovedNames::collect(root, files);
662    let counts: Vec<Counts<'_>> = files
663        .iter()
664        .filter(|file| is_js_test_code(root, &file.rel))
665        .map(|file| {
666            let base = file.base.as_deref().unwrap_or("");
667            let head = file.head.as_deref().unwrap_or("");
668            Counts {
669                rel: &file.rel,
670                base_cases: text::test_case_count(base),
671                head_cases: text::test_case_count(head),
672                base_asserts: text::assertion_count(base),
673                head_asserts: text::assertion_count(head),
674                explained: removed_names.explain(base, head),
675                assertions_led: {
676                    let (asserts, code) = text::removed_assertion_lines(base, head);
677                    asserts * 2 >= code
678                },
679            }
680        })
681        .collect();
682    let total = |pick: fn(&Counts<'_>) -> usize| counts.iter().map(pick).sum::<usize>();
683    let cases_dropped = total(|c| c.head_cases) < total(|c| c.base_cases);
684    let asserts_dropped = total(|c| c.head_asserts) < total(|c| c.base_asserts);
685    for count in counts.iter().filter(|count| !count.explained) {
686        if cases_dropped && count.head_cases < count.base_cases {
687            let evidence = format!("test cases: {} -> {}", count.base_cases, count.head_cases);
688            raw.push(RawFinding {
689                rule: DiffRiskRule::WeakenedTests,
690                kind: Some(WeakenedTestsKind::RemovedTests),
691                path: count.rel.to_owned(),
692                line: None,
693                message: format!("The change removes test cases ({evidence})"),
694                evidence,
695                related: Vec::new(),
696            });
697        } else if asserts_dropped
698            && count.assertions_led
699            && count.head_cases >= count.base_cases
700            && count.head_asserts < count.base_asserts
701        {
702            let evidence = format!(
703                "assertions: {} -> {}",
704                count.base_asserts, count.head_asserts
705            );
706            raw.push(RawFinding {
707                rule: DiffRiskRule::WeakenedTests,
708                kind: Some(WeakenedTestsKind::AssertionDrop),
709                path: count.rel.to_owned(),
710                line: None,
711                message: format!("The change removes assertions ({evidence})"),
712                evidence,
713                related: Vec::new(),
714            });
715        }
716    }
717}
718
719fn detect_diverged_clones(
720    input: &DiffRiskInput<'_>,
721    source: &mut BaseSource,
722    base_groups: &[BaseCloneGroup],
723    files: &[FileTexts],
724    raw: &mut Vec<RawFinding>,
725) {
726    let changed: FxHashSet<String> = files.iter().map(|file| file.rel.clone()).collect();
727    let renames: FxHashMap<String, String> = input
728        .renames
729        .iter()
730        .map(|rename| {
731            (
732                relative_key_path(&rename.from, input.root),
733                relative_key_path(&rename.to, input.root),
734            )
735        })
736        .collect();
737    let head_by_rel: FxHashMap<&str, Option<&str>> = files
738        .iter()
739        .map(|file| (file.rel.as_str(), file.head.as_deref()))
740        .collect();
741    let root = input.root;
742    let mut base_text = |rel: &str| match source.read(&root.join(rel)) {
743        BaseRead::Content(text) => Some(text),
744        BaseRead::Missing | BaseRead::Error => None,
745    };
746    let mut head_text = |rel: &str| match head_by_rel.get(rel) {
747        Some(head) => head.map(str::to_owned),
748        None => std::fs::read_to_string(root.join(rel)).ok(),
749    };
750    let found = clones::diverged_clone_siblings(
751        base_groups,
752        &input.head_clone_groups,
753        &changed,
754        &renames,
755        &mut CloneTexts {
756            base: &mut base_text,
757            head: &mut head_text,
758        },
759    );
760    for diverged in found {
761        // Test and story copies differ case by case on purpose, so an edit to
762        // one of them is not a missed sibling.
763        if is_example_copy(root, &diverged.path) {
764            continue;
765        }
766        let rules = input.rules.for_path(&root.join(&diverged.path));
767        if rules.diverged_clone_sibling == Severity::Off {
768            continue;
769        }
770        let related: Vec<DiffRiskLocation> = diverged
771            .siblings
772            .iter()
773            .map(|sibling| DiffRiskLocation {
774                path: sibling.path.clone(),
775                line: sibling.start_line,
776            })
777            .collect();
778        let evidence = related
779            .iter()
780            .map(|location| format!("{}:{}", location.path, location.line))
781            .collect::<Vec<_>>()
782            .join(", ");
783        raw.push(RawFinding {
784            rule: DiffRiskRule::DivergedCloneSibling,
785            kind: None,
786            path: diverged.path,
787            line: diverged.line,
788            message: format!(
789                "The change edits one copy of duplicated code, and its {} unchanged copies no longer match",
790                related.len()
791            ),
792            evidence: format!("unchanged copies: {evidence}"),
793            related,
794        });
795    }
796}
797
798const fn issue_kind(rule: DiffRiskRule) -> IssueKind {
799    match rule {
800        DiffRiskRule::WeakenedTests => IssueKind::WeakenedTests,
801        DiffRiskRule::AddedSuppression => IssueKind::AddedSuppression,
802        DiffRiskRule::TestOnlyBranch => IssueKind::TestOnlyBranch,
803        DiffRiskRule::DivergedCloneSibling => IssueKind::DivergedCloneSibling,
804        DiffRiskRule::AddedDependency => IssueKind::AddedDependency,
805    }
806}
807
808const fn rule_severity(rules: &RulesConfig, rule: DiffRiskRule) -> Severity {
809    rules.severity_for_kind(issue_kind(rule))
810}
811
812/// Whether a head suppression covers the finding. An `added-suppression`
813/// finding honors only a suppression that names its rule: a new blanket
814/// `fallow-ignore` comment must not hide itself.
815fn is_suppressed(suppressions: &[Suppression], rule: DiffRiskRule, line: Option<u32>) -> bool {
816    let kind = issue_kind(rule);
817    suppressions.iter().any(|suppression| {
818        let names_rule = matches!(
819            &suppression.target,
820            Some(SuppressionTarget::Issue(target)) if *target == kind
821        );
822        if rule == DiffRiskRule::AddedSuppression && !names_rule {
823            return false;
824        }
825        let line = line.unwrap_or(0);
826        if line == 0 {
827            suppression.line == 0 && suppression.matches_issue_kind(0, kind)
828        } else {
829            suppression.matches_issue_kind(line, kind)
830        }
831    })
832}
833
834const SECURITY_WITHOUT_REASON_MESSAGE: &str = "The change adds a `fallow-ignore` comment that hides a security finding and gives no `-- <reason>`";
835
836/// The parsed `fallow-ignore` comment of an `added-suppression` finding, or
837/// `None` for another rule, another suppression token, or a comment that the
838/// suppression parser does not read (for example a trailing comment).
839///
840/// Only a comment that names a security kind sets `security_without_reason`.
841/// A blanket comment also hides security findings, but it does not set the
842/// flag, so projects without security analysis get no extra signal.
843fn added_fallow_ignore(list: &[Suppression], finding: &RawFinding) -> Option<DiffRiskSuppression> {
844    if finding.rule != DiffRiskRule::AddedSuppression
845        || finding.evidence != text::FALLOW_IGNORE_TOKEN
846    {
847        return None;
848    }
849    let line = finding.line?;
850    let mut on_line = list
851        .iter()
852        .filter(|suppression| suppression.comment_line == line)
853        .peekable();
854    on_line.peek()?;
855    let mut kinds = Vec::new();
856    let mut reason = None;
857    let mut names_security = false;
858    for suppression in on_line {
859        reason = reason.or_else(|| suppression.reason.clone());
860        names_security |= matches!(
861            suppression.issue_kind_target(),
862            Some(IssueKind::SecuritySink | IssueKind::SecurityClientServerLeak)
863        );
864        if let Some(token) = suppression.target_token() {
865            kinds.push(token);
866        }
867    }
868    let security_without_reason = names_security && reason.is_none();
869    Some(DiffRiskSuppression {
870        kinds,
871        reason,
872        security_without_reason,
873    })
874}
875
876fn finalize(
877    input: &DiffRiskInput<'_>,
878    files: &[FileTexts],
879    raw: Vec<RawFinding>,
880) -> Vec<DiffRiskFinding> {
881    let mut suppressions: FxHashMap<&str, Vec<Suppression>> = FxHashMap::default();
882    let head_exists: FxHashMap<&str, bool> = files
883        .iter()
884        .map(|file| (file.rel.as_str(), file.head.is_some()))
885        .collect();
886    for file in files {
887        if let Some(head) = file.head.as_deref() {
888            suppressions.insert(
889                file.rel.as_str(),
890                fallow_engine::source::suppressions_from_text(head),
891            );
892        }
893    }
894    let mut findings: Vec<DiffRiskFinding> = raw
895        .into_iter()
896        .filter_map(|finding| {
897            let rules = input.rules.for_path(&input.root.join(&finding.path));
898            let severity = match rule_severity(&rules, finding.rule) {
899                Severity::Off => return None,
900                Severity::Warn => DiffRiskSeverity::Warn,
901                Severity::Error => DiffRiskSeverity::Error,
902            };
903            let head_list = suppressions.get(finding.path.as_str());
904            if head_list.is_some_and(|list| is_suppressed(list, finding.rule, finding.line)) {
905                return None;
906            }
907            let exists = head_exists
908                .get(finding.path.as_str())
909                .copied()
910                .unwrap_or(true);
911            let actions = suppression_actions(finding.rule, &finding.path, finding.line, exists);
912            let suppression = head_list.and_then(|list| added_fallow_ignore(list, &finding));
913            let message = if suppression
914                .as_ref()
915                .is_some_and(|detail| detail.security_without_reason)
916            {
917                SECURITY_WITHOUT_REASON_MESSAGE.to_owned()
918            } else {
919                finding.message
920            };
921            Some(DiffRiskFinding {
922                rule: finding.rule,
923                kind: finding.kind,
924                path: finding.path,
925                line: finding.line,
926                message,
927                evidence: finding.evidence,
928                severity,
929                introduced: true,
930                related: finding.related,
931                actions,
932                suppression,
933            })
934        })
935        .collect();
936    findings.sort_by(|a, b| {
937        a.path
938            .cmp(&b.path)
939            .then(a.line.cmp(&b.line))
940            .then(a.rule.cmp(&b.rule))
941            .then(a.kind.cmp(&b.kind))
942            .then(a.evidence.cmp(&b.evidence))
943    });
944    findings
945}
946
947/// The comment markers of a file type, `None` when the file cannot hold a
948/// suppression comment.
949fn comment_markers(rel: &str) -> Option<(&'static str, &'static str)> {
950    match extension(rel) {
951        "css" | "scss" | "sass" | "less" => Some(("/* ", " */")),
952        "html" => Some(("<!-- ", " -->")),
953        _ if is_source_file(rel) => Some(("// ", "")),
954        _ => None,
955    }
956}
957
958fn suppression_actions(
959    rule: DiffRiskRule,
960    path: &str,
961    line: Option<u32>,
962    head_exists: bool,
963) -> Vec<DiffRiskAction> {
964    let code = rule.code();
965    let markers = comment_markers(path).filter(|_| head_exists);
966    let Some((open, close)) = markers else {
967        return vec![DiffRiskAction {
968            kind: DiffRiskActionType::SuppressInConfig,
969            auto_fixable: false,
970            description: format!(
971                "Set `{code}` to `off` for `{path}` in an `overrides` entry of the Fallow config"
972            ),
973            comment: None,
974        }];
975    };
976    let mut actions = Vec::new();
977    if line.is_some() {
978        actions.push(DiffRiskAction {
979            kind: DiffRiskActionType::SuppressLine,
980            auto_fixable: false,
981            description: format!("Suppress this `{code}` finding with a comment above the line"),
982            comment: Some(format!("{open}fallow-ignore-next-line {code}{close}")),
983        });
984    }
985    actions.push(DiffRiskAction {
986        kind: DiffRiskActionType::SuppressFile,
987        auto_fixable: false,
988        description: format!("Suppress `{code}` for the whole file"),
989        comment: Some(format!("{open}fallow-ignore-file {code}{close}")),
990    });
991    actions
992}
993
994#[cfg(test)]
995mod tests {
996    use super::*;
997
998    fn suppressions(src: &str) -> Vec<Suppression> {
999        fallow_engine::source::suppressions_from_text(src)
1000    }
1001
1002    fn file(rel: &str, base: &str, head: &str) -> FileTexts {
1003        FileTexts {
1004            rel: rel.to_owned(),
1005            abs: PathBuf::from("/project").join(rel),
1006            base: Some(base.to_owned()),
1007            head: Some(head.to_owned()),
1008        }
1009    }
1010
1011    fn rules_of(raw: &[RawFinding]) -> Vec<(DiffRiskRule, &str)> {
1012        raw.iter()
1013            .map(|finding| (finding.rule, finding.evidence.as_str()))
1014            .collect()
1015    }
1016
1017    #[test]
1018    fn only_files_a_rule_reads_are_relevant() {
1019        let root = Path::new("/project");
1020        for rel in [
1021            "src/a.ts",
1022            "src/a.test.ts",
1023            "src/page.mdx",
1024            "src/query.graphql",
1025            "styles/app.css",
1026            "package.json",
1027            "packages/ui/package.json",
1028            "vitest.config.ts",
1029            ".nycrc",
1030            ".fallowrc.json",
1031        ] {
1032            assert!(is_rule_relevant(root, rel), "{rel} must be read");
1033        }
1034        for rel in [
1035            "pnpm-lock.yaml",
1036            "package-lock.json",
1037            "yarn.lock",
1038            "assets/logo.png",
1039            "fonts/inter.woff2",
1040            "README.md",
1041        ] {
1042            assert!(!is_rule_relevant(root, rel), "{rel} must not be read");
1043        }
1044    }
1045
1046    fn removed_test_findings(files: &[FileTexts]) -> Vec<(String, String)> {
1047        removed_test_findings_in(Path::new("/project"), files)
1048    }
1049
1050    fn removed_test_findings_in(root: &Path, files: &[FileTexts]) -> Vec<(String, String)> {
1051        let mut raw = Vec::new();
1052        detect_removed_tests(root, files, &mut raw);
1053        let mut found: Vec<(String, String)> = raw
1054            .into_iter()
1055            .map(|finding| (finding.path, finding.evidence))
1056            .collect();
1057        found.sort_unstable();
1058        found
1059    }
1060
1061    fn pairs(list: &[(&str, &str)]) -> Vec<(String, String)> {
1062        list.iter()
1063            .map(|(a, b)| ((*a).to_owned(), (*b).to_owned()))
1064            .collect()
1065    }
1066
1067    const ERRORS_BASE: &str = "export const ERR_A = 'a';\nexport const ERR_GONE = 'gone';\n";
1068    const ERRORS_HEAD: &str = "export const ERR_A = 'a';\n";
1069    const ERRORS_TEST_BASE: &str = "test('ERR_A', () => {\n  expect(ERR_A).toBe('a');\n});\ntest('ERR_GONE', () => {\n  expect(ERR_GONE).toBe('gone');\n});\n";
1070    const ERRORS_TEST_HEAD: &str = "test('ERR_A', () => {\n  expect(ERR_A).toBe('a');\n});\n";
1071
1072    #[test]
1073    fn tests_removed_with_the_source_name_they_cover_are_not_reported() {
1074        let files = [
1075            file("src/errors.ts", ERRORS_BASE, ERRORS_HEAD),
1076            file("src/errors.test.ts", ERRORS_TEST_BASE, ERRORS_TEST_HEAD),
1077        ];
1078        assert!(removed_test_findings(&files).is_empty());
1079    }
1080
1081    #[test]
1082    fn tests_removed_while_their_source_name_stays_are_reported() {
1083        let files = [
1084            file("src/errors.ts", ERRORS_BASE, ERRORS_BASE),
1085            file("src/errors.test.ts", ERRORS_TEST_BASE, ERRORS_TEST_HEAD),
1086        ];
1087        assert_eq!(
1088            removed_test_findings(&files),
1089            pairs(&[("src/errors.test.ts", "test cases: 2 -> 1")])
1090        );
1091    }
1092
1093    #[test]
1094    fn assertions_removed_with_the_source_name_they_cover_are_not_reported() {
1095        let base = "test('errors', () => {\n  expect(ERR_A).toBe('a');\n  expect(ERR_GONE).toBe('gone');\n});\n";
1096        let head = "test('errors', () => {\n  expect(ERR_A).toBe('a');\n});\n";
1097        let files = [
1098            file("src/errors.ts", ERRORS_BASE, ERRORS_HEAD),
1099            file("src/errors.test.ts", base, head),
1100        ];
1101        assert!(removed_test_findings(&files).is_empty());
1102        let files = [
1103            file("src/errors.ts", ERRORS_BASE, ERRORS_BASE),
1104            file("src/errors.test.ts", base, head),
1105        ];
1106        assert_eq!(
1107            removed_test_findings(&files),
1108            pairs(&[("src/errors.test.ts", "assertions: 2 -> 1")])
1109        );
1110    }
1111
1112    #[test]
1113    fn tests_removed_with_a_deleted_fixture_directory_are_not_reported() {
1114        let base = "test('a', () => run('basic'));\ntest('legacy', () => run('legacy-types'));\n";
1115        let head = "test('a', () => run('basic'));\n";
1116        let files = [
1117            file("test/package.spec.js", base, head),
1118            FileTexts {
1119                rel: "test/fixtures/legacy-types/src/index.ts".to_owned(),
1120                abs: PathBuf::from("/project/test/fixtures/legacy-types/src/index.ts"),
1121                base: Some("export const a = 1;\n".to_owned()),
1122                head: None,
1123            },
1124        ];
1125        assert!(removed_test_findings(&files).is_empty());
1126    }
1127
1128    #[test]
1129    fn assertions_that_leave_with_rewritten_setup_code_are_not_reported() {
1130        let base = "test('creates', async () => {\n  const response = await client.post('/api').send({ query: CREATE });\n  expect(response.body.errors).toBeUndefined();\n  const id = response.body.data.create.id;\n  expect(await read(id)).toEqual({ id });\n});\n";
1131        let head = "test('creates', async () => {\n  const { id } = await createRecord();\n  expect(await read(id)).toEqual({ id });\n});\n";
1132        let files = [file("test/create.spec.ts", base, head)];
1133        assert!(removed_test_findings(&files).is_empty());
1134        let head = "test('creates', async () => {\n  const response = await client.post('/api').send({ query: CREATE });\n  const id = response.body.data.create.id;\n});\n";
1135        let files = [file("test/create.spec.ts", base, head)];
1136        assert_eq!(
1137            removed_test_findings(&files),
1138            pairs(&[("test/create.spec.ts", "assertions: 2 -> 0")])
1139        );
1140    }
1141
1142    #[test]
1143    fn tests_of_a_call_that_one_source_file_dropped_are_not_reported() {
1144        let hook_base = "export const useCommands = () => {\n  const enabled = useIsFeatureEnabled('CORE');\n  return enabled ? commands : [];\n};\n";
1145        let hook_head = "export const useCommands = () => commands;\n";
1146        let other = "export const usePage = () => useIsFeatureEnabled('PAGE');\n";
1147        let test_base = "jest.mock('./flags', () => ({ useIsFeatureEnabled: () => flag() }));\nit('shows commands', () => {\n  expect(useCommands()).toEqual(commands);\n});\nit('hides commands with the flag off', () => {\n  flag.mockReturnValue(false);\n  expect(useCommands()).toEqual([]);\n});\n";
1148        let test_head =
1149            "it('shows commands', () => {\n  expect(useCommands()).toEqual(commands);\n});\n";
1150        let files = [
1151            file("src/useCommands.ts", hook_base, hook_head),
1152            file("src/usePage.ts", other, other),
1153            file("src/useCommands.test.ts", test_base, test_head),
1154        ];
1155        assert!(removed_test_findings(&files).is_empty());
1156    }
1157
1158    #[test]
1159    fn words_in_comments_and_test_titles_do_not_explain_a_removal() {
1160        let source_base =
1161            "// Hide the items when the page is disabled.\nexport const items = (all) => all;\n";
1162        let source_head = "export const items = (all) => all;\n";
1163        let test_base = "it('keeps items', () => {\n  expect(items([1])).toEqual([1]);\n});\nit('keeps items when the page is disabled', () => {\n  expect(items([])).toEqual([]);\n});\n";
1164        let test_head = "it('keeps items', () => {\n  expect(items([1])).toEqual([1]);\n});\n";
1165        let files = [
1166            file("src/items.ts", source_base, source_head),
1167            file("src/items.test.ts", test_base, test_head),
1168        ];
1169        assert_eq!(
1170            removed_test_findings(&files),
1171            pairs(&[("src/items.test.ts", "test cases: 2 -> 1")])
1172        );
1173    }
1174
1175    #[test]
1176    fn a_deleted_test_file_does_not_explain_its_own_removal() {
1177        let files = [FileTexts {
1178            rel: "src/zero.test.ts".to_owned(),
1179            abs: PathBuf::from("/project/src/zero.test.ts"),
1180            base: Some("import { price } from './zero';\nit('keeps zero', () => {\n  expect(price(0)).toBe(0);\n});\n".to_owned()),
1181            head: None,
1182        }];
1183        assert_eq!(
1184            removed_test_findings(&files),
1185            pairs(&[("src/zero.test.ts", "test cases: 1 -> 0")])
1186        );
1187    }
1188
1189    fn deleted(rel: &str, base: &str) -> FileTexts {
1190        FileTexts {
1191            rel: rel.to_owned(),
1192            abs: PathBuf::from("/project").join(rel),
1193            base: Some(base.to_owned()),
1194            head: None,
1195        }
1196    }
1197
1198    #[test]
1199    fn an_unrelated_deleted_source_file_does_not_explain_a_deleted_test_file() {
1200        let files = [
1201            deleted(
1202                "src/legacy.ts",
1203                "import { a } from './a';\nimport { price } from './price';\nexport const legacy = price(a);\n",
1204            ),
1205            deleted(
1206                "src/price.test.ts",
1207                "import { price } from './price';\nit('keeps zero', () => {\n  expect(price(0)).toBe(0);\n});\n",
1208            ),
1209        ];
1210        assert_eq!(
1211            removed_test_findings(&files),
1212            pairs(&[("src/price.test.ts", "test cases: 1 -> 0")])
1213        );
1214    }
1215
1216    #[test]
1217    fn a_name_that_a_changed_file_still_holds_does_not_explain_a_removal() {
1218        let files = [
1219            deleted("src/legacy.ts", "export const formatPrice = (n) => n;\n"),
1220            file(
1221                "src/format.ts",
1222                "export const format = (n) => n;\n",
1223                "export const formatPrice = (n) => n;\n",
1224            ),
1225            file(
1226                "src/format.test.ts",
1227                "it('formats', () => {\n  expect(formatPrice(1)).toBe(1);\n});\n",
1228                "",
1229            ),
1230        ];
1231        assert_eq!(
1232            removed_test_findings(&files),
1233            pairs(&[("src/format.test.ts", "test cases: 1 -> 0")])
1234        );
1235    }
1236
1237    #[test]
1238    fn a_deleted_directory_name_matches_whole_path_segments_only() {
1239        let root = tempfile::tempdir().expect("tempdir");
1240        let base =
1241            "it('a', () => run());\nit('scores', () => {\n  expect(score(1)).toBe(1);\n});\n";
1242        let head = "it('a', () => run());\n";
1243        let files = [
1244            file("src/score.test.ts", base, head),
1245            deleted("src/core/x.ts", "export const x = 1;\n"),
1246        ];
1247        assert_eq!(
1248            removed_test_findings_in(root.path(), &files),
1249            pairs(&[("src/score.test.ts", "test cases: 2 -> 1")])
1250        );
1251        let base =
1252            "import { a } from '../core/a';\nit('a', () => run(a));\nit('b', () => run(b));\n";
1253        let head = "it('a', () => run(a));\n";
1254        let files = [
1255            file("src/run.test.ts", base, head),
1256            deleted("src/core/x.ts", "export const x = 1;\n"),
1257        ];
1258        assert!(removed_test_findings_in(root.path(), &files).is_empty());
1259    }
1260
1261    #[test]
1262    fn a_directory_that_still_exists_does_not_explain_a_removal() {
1263        let root = tempfile::tempdir().expect("tempdir");
1264        std::fs::create_dir_all(root.path().join("src/components")).expect("components dir");
1265        let files = [
1266            deleted(
1267                "src/components/Legacy.tsx",
1268                "export const Legacy = () => null;\n",
1269            ),
1270            deleted(
1271                "src/__tests__/button.test.tsx",
1272                "import { Button } from '../components/Button';\nit('renders', () => {\n  expect(Button()).toBeTruthy();\n});\n",
1273            ),
1274        ];
1275        assert_eq!(
1276            removed_test_findings_in(root.path(), &files),
1277            pairs(&[("src/__tests__/button.test.tsx", "test cases: 1 -> 0")])
1278        );
1279    }
1280
1281    #[test]
1282    fn edited_story_copy_is_not_a_diverged_clone() {
1283        for rel in [
1284            "apps/storybook/stories/select.stories.tsx",
1285            "src/button.story.tsx",
1286        ] {
1287            assert!(is_example_copy(Path::new("/project"), rel), "{rel}");
1288        }
1289        assert!(!is_example_copy(Path::new("/project"), "src/store.ts"));
1290    }
1291
1292    #[test]
1293    fn tool_config_names_match_config_and_dot_rc_files_only() {
1294        for name in [
1295            "vite.config.ts",
1296            "pkg/karma.conf.js",
1297            ".eslintrc.js",
1298            "pkg/.mocharc.cjs",
1299        ] {
1300            assert!(is_tool_config(name), "{name}");
1301        }
1302        for name in ["src.ts", "src/arc.ts", "lib/orc.js", "src/index.ts"] {
1303            assert!(!is_tool_config(name), "{name}");
1304        }
1305    }
1306
1307    #[test]
1308    fn suppressions_in_fixture_and_generated_files_are_not_reported() {
1309        let head = "// @ts-nocheck\nexport const a = 1;\n";
1310        let mut raw = Vec::new();
1311        let root = Path::new("/project");
1312        let rules = RulesConfig::default();
1313        for rel in [
1314            "src/__tests__/upstream/fixture/generated/schema.ts",
1315            "test/fixtures/app/src/index.ts",
1316            "src/__generated__/graphql.ts",
1317            "src/generated/client.ts",
1318        ] {
1319            detect_file(root, &file(rel, "", head), &rules, &mut raw);
1320        }
1321        assert!(raw.is_empty(), "{:?}", rules_of(&raw));
1322        detect_file(root, &file("src/schema.ts", "", head), &rules, &mut raw);
1323        assert_eq!(
1324            rules_of(&raw),
1325            vec![(DiffRiskRule::AddedSuppression, "@ts-nocheck")]
1326        );
1327    }
1328
1329    #[test]
1330    fn ts_expect_error_in_type_tests_is_an_assertion_not_a_suppression() {
1331        let head = "// @ts-expect-error\nconst x: string = 1;\n";
1332        let mut raw = Vec::new();
1333        let root = Path::new("/project");
1334        let rules = RulesConfig::default();
1335        detect_file(root, &file("test-d/hooks.ts", "", head), &rules, &mut raw);
1336        detect_file(root, &file("src/a.test-d.ts", "", head), &rules, &mut raw);
1337        detect_file(root, &file("src/a.test.ts", "", head), &rules, &mut raw);
1338        assert!(raw.is_empty(), "{:?}", rules_of(&raw));
1339        detect_file(root, &file("src/a.ts", "", head), &rules, &mut raw);
1340        assert_eq!(
1341            rules_of(&raw),
1342            vec![(DiffRiskRule::AddedSuppression, "@ts-expect-error")]
1343        );
1344    }
1345
1346    /// A test file in another language (a Rust integration test under
1347    /// `tests/`) can hold JavaScript test text in a string. The test rules
1348    /// read JavaScript-family test files only.
1349    #[test]
1350    fn test_rules_read_javascript_test_files_only() {
1351        let root = Path::new("/project");
1352        let rules = RulesConfig::default();
1353        let base = "it('a', f);\nit('b', f);\n";
1354        let head = "it.skip('a', f);\n";
1355        let mut raw = Vec::new();
1356        let rust = file("crates/cli/tests/integration/a_tests.rs", base, head);
1357        detect_file(root, &rust, &rules, &mut raw);
1358        detect_removed_tests(root, std::slice::from_ref(&rust), &mut raw);
1359        assert!(raw.is_empty(), "{:?}", rules_of(&raw));
1360
1361        let js = file("src/a.test.ts", base, head);
1362        detect_file(root, &js, &rules, &mut raw);
1363        detect_removed_tests(root, std::slice::from_ref(&js), &mut raw);
1364        assert_eq!(
1365            rules_of(&raw),
1366            vec![
1367                (DiffRiskRule::WeakenedTests, "it.skip"),
1368                (DiffRiskRule::WeakenedTests, "test cases: 2 -> 1"),
1369            ]
1370        );
1371    }
1372
1373    #[test]
1374    fn named_line_suppression_hides_the_finding() {
1375        let list = suppressions("// fallow-ignore-next-line weakened-tests\nit.skip('a', f);\n");
1376        assert!(is_suppressed(&list, DiffRiskRule::WeakenedTests, Some(2)));
1377        assert!(!is_suppressed(&list, DiffRiskRule::TestOnlyBranch, Some(2)));
1378    }
1379
1380    #[test]
1381    fn file_suppression_hides_a_file_level_finding() {
1382        let list = suppressions("// fallow-ignore-file weakened-tests\n");
1383        assert!(is_suppressed(&list, DiffRiskRule::WeakenedTests, None));
1384    }
1385
1386    fn added(evidence: &str, line: u32) -> RawFinding {
1387        RawFinding {
1388            rule: DiffRiskRule::AddedSuppression,
1389            kind: None,
1390            path: "src/a.ts".to_owned(),
1391            line: Some(line),
1392            message: String::new(),
1393            evidence: evidence.to_owned(),
1394            related: Vec::new(),
1395        }
1396    }
1397
1398    #[test]
1399    fn added_fallow_ignore_reads_kinds_and_reason() {
1400        let list = suppressions(
1401            "// fallow-ignore-next-line unused-export, security-sink\nexport const a = 1;\n// fallow-ignore-file\n// fallow-ignore-next-line security-client-server-leak -- reviewed\nconst b = 1;\n",
1402        );
1403        let mixed = added_fallow_ignore(&list, &added("fallow-ignore", 1)).expect("parsed");
1404        assert_eq!(mixed.kinds, vec!["unused-export", "security-sink"]);
1405        assert!(mixed.security_without_reason);
1406
1407        let blanket = added_fallow_ignore(&list, &added("fallow-ignore", 3)).expect("parsed");
1408        assert!(blanket.kinds.is_empty());
1409        assert!(!blanket.security_without_reason);
1410
1411        let reasoned = added_fallow_ignore(&list, &added("fallow-ignore", 4)).expect("parsed");
1412        assert_eq!(reasoned.reason.as_deref(), Some("reviewed"));
1413        assert!(!reasoned.security_without_reason);
1414
1415        assert!(added_fallow_ignore(&list, &added("eslint-disable", 1)).is_none());
1416        assert!(added_fallow_ignore(&list, &added("fallow-ignore", 2)).is_none());
1417    }
1418
1419    #[test]
1420    fn blanket_suppression_does_not_hide_an_added_suppression() {
1421        let list = suppressions("// fallow-ignore-file\nconst a = 1;\n");
1422        assert!(!is_suppressed(
1423            &list,
1424            DiffRiskRule::AddedSuppression,
1425            Some(1)
1426        ));
1427        assert!(is_suppressed(&list, DiffRiskRule::TestOnlyBranch, Some(2)));
1428    }
1429
1430    #[test]
1431    fn json_and_deleted_files_get_a_config_action() {
1432        let actions =
1433            suppression_actions(DiffRiskRule::AddedDependency, "package.json", Some(3), true);
1434        assert_eq!(actions.len(), 1);
1435        assert_eq!(actions[0].kind, DiffRiskActionType::SuppressInConfig);
1436        let actions = suppression_actions(DiffRiskRule::WeakenedTests, "a.test.ts", None, false);
1437        assert_eq!(actions[0].kind, DiffRiskActionType::SuppressInConfig);
1438    }
1439
1440    #[test]
1441    fn source_files_get_line_and_file_comments() {
1442        let actions = suppression_actions(DiffRiskRule::TestOnlyBranch, "src/a.ts", Some(3), true);
1443        let comments: Vec<_> = actions
1444            .iter()
1445            .filter_map(|a| a.comment.as_deref())
1446            .collect();
1447        assert_eq!(
1448            comments,
1449            vec![
1450                "// fallow-ignore-next-line test-only-branch",
1451                "// fallow-ignore-file test-only-branch"
1452            ]
1453        );
1454        let actions =
1455            suppression_actions(DiffRiskRule::AddedSuppression, "src/a.css", Some(3), true);
1456        assert_eq!(
1457            actions[0].comment.as_deref(),
1458            Some("/* fallow-ignore-next-line added-suppression */")
1459        );
1460    }
1461}