|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use crate::core; |
| 4 | + |
| 5 | +const MIN_BLAST_RADIUS_FILES: usize = 3; |
| 6 | +const MAX_LISTED_DEPENDENTS: usize = 3; |
| 7 | +const BLAST_RADIUS_TAG: &str = "blast-radius"; |
| 8 | +const BLAST_RADIUS_PREFIX: &str = "Blast radius:"; |
| 9 | + |
| 10 | +pub(super) fn apply_blast_radius_summaries( |
| 11 | + mut comments: Vec<core::Comment>, |
| 12 | + symbol_index: Option<&core::SymbolIndex>, |
| 13 | +) -> Vec<core::Comment> { |
| 14 | + let Some(symbol_index) = symbol_index else { |
| 15 | + return comments; |
| 16 | + }; |
| 17 | + |
| 18 | + for comment in &mut comments { |
| 19 | + let mut dependents = symbol_index.reverse_deps(&comment.file_path); |
| 20 | + dependents.sort(); |
| 21 | + dependents.dedup(); |
| 22 | + |
| 23 | + if dependents.len() < MIN_BLAST_RADIUS_FILES { |
| 24 | + continue; |
| 25 | + } |
| 26 | + |
| 27 | + if !comment.content.contains(BLAST_RADIUS_PREFIX) { |
| 28 | + comment.content.push_str("\n\n"); |
| 29 | + comment |
| 30 | + .content |
| 31 | + .push_str(&format_blast_radius_summary(&dependents)); |
| 32 | + } |
| 33 | + |
| 34 | + push_unique_tag(&mut comment.tags, BLAST_RADIUS_TAG); |
| 35 | + push_unique_tag( |
| 36 | + &mut comment.tags, |
| 37 | + &format!("blast-radius:{}", dependents.len()), |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + comments |
| 42 | +} |
| 43 | + |
| 44 | +fn format_blast_radius_summary(dependents: &[PathBuf]) -> String { |
| 45 | + let listed = dependents |
| 46 | + .iter() |
| 47 | + .take(MAX_LISTED_DEPENDENTS) |
| 48 | + .map(|path| path.display().to_string()) |
| 49 | + .collect::<Vec<_>>(); |
| 50 | + let remaining = dependents.len().saturating_sub(listed.len()); |
| 51 | + |
| 52 | + let mut summary = format!( |
| 53 | + "{BLAST_RADIUS_PREFIX} {} dependent files reference this file ({})", |
| 54 | + dependents.len(), |
| 55 | + listed.join(", ") |
| 56 | + ); |
| 57 | + if remaining > 0 { |
| 58 | + summary.push_str(&format!(", +{remaining} more")); |
| 59 | + } |
| 60 | + summary.push('.'); |
| 61 | + summary |
| 62 | +} |
| 63 | + |
| 64 | +fn push_unique_tag(tags: &mut Vec<String>, tag: &str) { |
| 65 | + if !tags.iter().any(|existing| existing == tag) { |
| 66 | + tags.push(tag.to_string()); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#[cfg(test)] |
| 71 | +mod tests { |
| 72 | + use super::apply_blast_radius_summaries; |
| 73 | + use crate::core; |
| 74 | + use crate::core::comment::{Category, CommentStatus, FixEffort, Severity}; |
| 75 | + use std::fs; |
| 76 | + use std::path::{Path, PathBuf}; |
| 77 | + |
| 78 | + fn build_index(repo_root: &Path) -> core::SymbolIndex { |
| 79 | + core::SymbolIndex::build(repo_root, 32, 128 * 1024, 8, |_path| false).unwrap() |
| 80 | + } |
| 81 | + |
| 82 | + fn write_repo_file(repo_root: &Path, relative: &str, content: &str) { |
| 83 | + let path = repo_root.join(relative); |
| 84 | + if let Some(parent) = path.parent() { |
| 85 | + fs::create_dir_all(parent).unwrap(); |
| 86 | + } |
| 87 | + fs::write(path, content).unwrap(); |
| 88 | + } |
| 89 | + |
| 90 | + fn make_comment(file_path: &str) -> core::Comment { |
| 91 | + core::Comment { |
| 92 | + id: "comment-1".to_string(), |
| 93 | + file_path: PathBuf::from(file_path), |
| 94 | + line_number: 1, |
| 95 | + content: "Shared helper has a correctness bug.".to_string(), |
| 96 | + rule_id: None, |
| 97 | + severity: Severity::Warning, |
| 98 | + category: Category::Bug, |
| 99 | + suggestion: None, |
| 100 | + confidence: 0.9, |
| 101 | + code_suggestion: None, |
| 102 | + tags: Vec::new(), |
| 103 | + fix_effort: FixEffort::Medium, |
| 104 | + feedback: None, |
| 105 | + status: CommentStatus::Open, |
| 106 | + resolved_at: None, |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn applies_blast_radius_summary_for_many_dependents() { |
| 112 | + let dir = tempfile::tempdir().unwrap(); |
| 113 | + write_repo_file(dir.path(), "src/shared.rs", "pub fn helper() {}\n"); |
| 114 | + write_repo_file( |
| 115 | + dir.path(), |
| 116 | + "src/a.rs", |
| 117 | + "const SHARED: &str = include_str!(\"./shared.rs\");\n", |
| 118 | + ); |
| 119 | + write_repo_file( |
| 120 | + dir.path(), |
| 121 | + "src/b.rs", |
| 122 | + "const SHARED: &str = include_str!(\"./shared.rs\");\n", |
| 123 | + ); |
| 124 | + write_repo_file( |
| 125 | + dir.path(), |
| 126 | + "src/c.rs", |
| 127 | + "const SHARED: &str = include_str!(\"./shared.rs\");\n", |
| 128 | + ); |
| 129 | + |
| 130 | + let index = build_index(dir.path()); |
| 131 | + let comments = |
| 132 | + apply_blast_radius_summaries(vec![make_comment("src/shared.rs")], Some(&index)); |
| 133 | + |
| 134 | + assert_eq!(comments.len(), 1); |
| 135 | + assert!(comments[0] |
| 136 | + .content |
| 137 | + .contains("Blast radius: 3 dependent files reference this file")); |
| 138 | + assert!(comments[0].content.contains("src/a.rs")); |
| 139 | + assert!(comments[0].tags.iter().any(|tag| tag == "blast-radius")); |
| 140 | + assert!(comments[0].tags.iter().any(|tag| tag == "blast-radius:3")); |
| 141 | + } |
| 142 | + |
| 143 | + #[test] |
| 144 | + fn skips_blast_radius_summary_below_threshold() { |
| 145 | + let dir = tempfile::tempdir().unwrap(); |
| 146 | + write_repo_file(dir.path(), "src/shared.rs", "pub fn helper() {}\n"); |
| 147 | + write_repo_file( |
| 148 | + dir.path(), |
| 149 | + "src/a.rs", |
| 150 | + "const SHARED: &str = include_str!(\"./shared.rs\");\n", |
| 151 | + ); |
| 152 | + write_repo_file( |
| 153 | + dir.path(), |
| 154 | + "src/b.rs", |
| 155 | + "const SHARED: &str = include_str!(\"./shared.rs\");\n", |
| 156 | + ); |
| 157 | + |
| 158 | + let index = build_index(dir.path()); |
| 159 | + let comments = |
| 160 | + apply_blast_radius_summaries(vec![make_comment("src/shared.rs")], Some(&index)); |
| 161 | + |
| 162 | + assert_eq!(comments[0].content, "Shared helper has a correctness bug."); |
| 163 | + assert!(comments[0].tags.is_empty()); |
| 164 | + } |
| 165 | +} |
0 commit comments