From 3245aa6994c12bf058bfcc6de3d7ffce6c03d1e0 Mon Sep 17 00:00:00 2001 From: Dmytro Gajewski <142790186+dmytrogajewski@users.noreply.github.com> Date: Thu, 23 Jul 2026 18:16:47 +0300 Subject: [PATCH 1/2] fix(static): honor --per-file in imports, halstead, and cohesion JSON reports (#21) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Only static/complexity threaded the --per-file flag into its JSON builder; the imports/halstead/cohesion handlers dropped it, so their sections never gained the per-file `files` array the reference implementation emits (StaticService.enrichWithPerFileData over the PerFileRetainer snapshots). The three walks now retain per-file snapshots (imports: the file's deduplicated import list; halstead: file-level scalars + the function range; cohesion: the visitor report's scalars, message, and functions) and new *_flags JSON entry points build one JSONFileEntry per analyzed file — function-free/import-free files included — between `issues` and `score`, in renderer.JSONFileEntry field order. handlers.rs passes the flag on the three json dispatch arms. Verified against the live Go reference binary on a mixed fixture (multi-function, function-free, import-free, markdown files): the `files` arrays are identical after the harness's set-order canonicalization. Adds red-first unit tests per handler (tempfile dev dependency) and fixes two trivial pre-existing clippy warnings in the crate (nonminimal_bool in shotness_run.rs, useless_vec in plot_sections/imports.rs). --- Cargo.lock | 1 + crates/cf-commands/Cargo.toml | 3 + crates/cf-commands/src/handlers.rs | 10 +- .../src/handlers/plot_sections/imports.rs | 2 +- .../cf-commands/src/handlers/shotness_run.rs | 2 +- .../src/handlers/static_cohesion.rs | 249 +++++++++++++++- .../src/handlers/static_halstead.rs | 267 +++++++++++++++++- .../src/handlers/static_imports.rs | 187 +++++++++++- 8 files changed, 699 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 01e28237..a9c923eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -406,6 +406,7 @@ dependencies = [ "cf-version", "clap", "clap_complete", + "tempfile", "thiserror", ] diff --git a/crates/cf-commands/Cargo.toml b/crates/cf-commands/Cargo.toml index 10f8dbdb..bc409ddb 100644 --- a/crates/cf-commands/Cargo.toml +++ b/crates/cf-commands/Cargo.toml @@ -104,3 +104,6 @@ cf-budget = { path = "../cf-budget", optional = true } cf-observability = { path = "../cf-observability", optional = true } cf-storage = { path = "../cf-storage", optional = true } cf-mcp = { path = "../cf-mcp", optional = true } + +[dev-dependencies] +tempfile = "3" diff --git a/crates/cf-commands/src/handlers.rs b/crates/cf-commands/src/handlers.rs index 79b359cc..9c34b33a 100644 --- a/crates/cf-commands/src/handlers.rs +++ b/crates/cf-commands/src/handlers.rs @@ -995,7 +995,9 @@ fn h_static_cohesion(ctx: &RunContext, format: &str) -> Option> { // `bin` CLI alias has already been normalized to `binary` (formats.rs // `normalize_format`). Match the normalized name (accept the raw alias too). match format { - "json" => static_cohesion::cohesion_report_json(path), + "json" => { + static_cohesion::cohesion_report_json_flags(path, ctx.matches.get_flag("per-file")) + } "yaml" => static_cohesion::cohesion_report_yaml(path), "binary" | "bin" => static_cohesion::cohesion_report_bin(path), "compact" => Some(section_render::render_compact_report( @@ -1034,7 +1036,9 @@ fn h_static_halstead(ctx: &RunContext, format: &str) -> Option> { // `normalize_format`). Match the normalized name so `--format bin` dispatches // to the CFB1 envelope builder rather than falling through to `None`. match format { - "json" => static_halstead::halstead_json_report(path), + "json" => { + static_halstead::halstead_json_report_flags(path, ctx.matches.get_flag("per-file")) + } "yaml" => static_halstead::halstead_yaml_report(path), "binary" => static_halstead::halstead_bin_report(path), "compact" => Some(section_render::render_compact_report( @@ -1054,7 +1058,7 @@ fn h_static_imports(ctx: &RunContext, format: &str) -> Option> { // `normalize_format`). Match the normalized name so `--format bin` dispatches // to the CFB1 envelope builder rather than falling through to `None`. match format { - "json" => static_imports::imports_report_json(path), + "json" => static_imports::imports_report_json_flags(path, ctx.matches.get_flag("per-file")), "yaml" => static_imports::imports_report_yaml(path), "binary" => static_imports::imports_report_bin(path), "compact" => Some(section_render::render_compact_report( diff --git a/crates/cf-commands/src/handlers/plot_sections/imports.rs b/crates/cf-commands/src/handlers/plot_sections/imports.rs index 8b16574b..56fd4f28 100644 --- a/crates/cf-commands/src/handlers/plot_sections/imports.rs +++ b/crates/cf-commands/src/handlers/plot_sections/imports.rs @@ -315,7 +315,7 @@ mod tests { use cf_gojson::{GoMap, MapOrigin}; fn raw_report() -> GoValue { - let imports = vec!["fmt", "github.com/x/y", "./../../../deep"]; + let imports = ["fmt", "github.com/x/y", "./../../../deep"]; let mut counts = GoMap::new(MapOrigin::Map); counts.push("./../../../deep", GoValue::Int(1)); counts.push("fmt", GoValue::Int(3)); diff --git a/crates/cf-commands/src/handlers/shotness_run.rs b/crates/cf-commands/src/handlers/shotness_run.rs index 2423aa5c..444634bb 100644 --- a/crates/cf-commands/src/handlers/shotness_run.rs +++ b/crates/cf-commands/src/handlers/shotness_run.rs @@ -326,7 +326,7 @@ pub(crate) struct ShotnessReducer { impl ShotnessReducer { /// shouldConsumeCommit: a merge commit is processed only the first time. pub(crate) fn should_consume(&mut self, hash: cf_gitlib::Hash, num_parents: usize) -> bool { - !(num_parents > 1 && !self.seen_merges.insert(hash)) + num_parents <= 1 || self.seen_merges.insert(hash) } /// Applies one commit's products to the cumulative state and returns the diff --git a/crates/cf-commands/src/handlers/static_cohesion.rs b/crates/cf-commands/src/handlers/static_cohesion.rs index c996e64b..c409ce97 100644 --- a/crates/cf-commands/src/handlers/static_cohesion.rs +++ b/crates/cf-commands/src/handlers/static_cohesion.rs @@ -107,6 +107,23 @@ struct Aggregated { /// Functions keyed by the composite `(_source_file, name)` dedup key; last /// write wins (reference: buffer overwrite). functions: BTreeMap, + /// Per-file snapshots (the reference `PerFileRetainer` clones of each + /// visitor report) consumed by the `--per-file` section enrichment; walk + /// order. + per_file: Vec, +} + +/// One retained per-file cohesion report: the visitor result's scalars, +/// per-file `message`, and this file's `(name, cohesion)` function list. +struct PerFileCohesion { + /// Path relative to the analyzed root (the `StampSourceFile` stamp). + rel: String, + total_functions: i64, + lcom: f64, + cohesion_score: f64, + function_cohesion: f64, + message: String, + functions: Vec<(String, f64)>, } impl Aggregated { @@ -203,6 +220,7 @@ fn walk( let stamped = make_relative_path(&path_str, root_path); accumulate(agg, &report, &stamped); + retain_per_file(agg, &report, &stamped); } } @@ -261,6 +279,55 @@ fn accumulate(agg: &mut Aggregated, report: &Report, source_file: &str) { } } +/// Retains the per-file visitor report for `--per-file` (the reference +/// `PerFileRetainer.Retain` shallow clone, keyed by the stamped path): the +/// scalar metrics, the ANALYZER's per-file `message` (`No functions found` +/// for function-free files), and this file's `(name, cohesion)` functions. +fn retain_per_file(agg: &mut Aggregated, report: &Report, source_file: &str) { + let get_f = |key: &str| { + report + .get(key) + .and_then(ReportValue::as_float) + .unwrap_or(0.0) + }; + let functions: Vec<(String, f64)> = report + .get("functions") + .and_then(ReportValue::as_functions) + .map(|fns| { + fns.iter() + .map(|f| { + ( + f.get("name") + .and_then(ReportValue::as_str) + .unwrap_or_default() + .to_string(), + f.get("cohesion") + .and_then(ReportValue::as_float) + .unwrap_or(0.0), + ) + }) + .collect() + }) + .unwrap_or_default(); + + agg.per_file.push(PerFileCohesion { + rel: source_file.to_string(), + total_functions: report + .get("total_functions") + .and_then(ReportValue::as_int) + .unwrap_or(0), + lcom: get_f("lcom"), + cohesion_score: get_f("cohesion_score"), + function_cohesion: get_f("function_cohesion"), + message: report + .get("message") + .and_then(ReportValue::as_str) + .unwrap_or_default() + .to_string(), + functions, + }); +} + /// The collected functions sorted by `name` (the reference `GetSortedData`, last identifier /// key). Equal-name order is nondeterministic in the reference implementation and canonicalized by the /// harness; a stable secondary order keeps our output deterministic. @@ -291,7 +358,17 @@ fn make_relative_path(file_path: &str, root_path: &str) -> String { /// path cannot be read. #[must_use] pub fn cohesion_report_json(root_path: &str) -> Option> { - let report = cohesion_report_value(root_path)?; + cohesion_report_json_flags(root_path, false) +} + +/// [`cohesion_report_json`] with the `--per-file` flag applied: `per_file` +/// enables the reference implementation's section enrichment +/// (`StaticService.enrichWithPerFileData` → `JSONReport.EnrichWithPerFileData` +/// over the `PerFileRetainer` snapshots — one `JSONFileEntry` per ANALYZED +/// file, function-free files included, keyed into the section's `files` array). +#[must_use] +pub fn cohesion_report_json_flags(root_path: &str, per_file: bool) -> Option> { + let report = cohesion_report_value_flags(root_path, per_file)?; let bytes = Encoder::indented(" ") .with_trailing_newline(true) .encode_to_vec(&report); @@ -303,8 +380,22 @@ pub fn cohesion_report_json(root_path: &str) -> Option> { /// static-JSON merge. `None` when the path cannot be walked. #[must_use] pub fn cohesion_report_value(root_path: &str) -> Option { + cohesion_report_value_flags(root_path, false) +} + +/// [`cohesion_report_value`] with the `--per-file` section enrichment. +#[must_use] +pub fn cohesion_report_value_flags(root_path: &str, per_file: bool) -> Option { let agg = aggregate(root_path)?; - Some(build_json_report(&agg)) + // --per-file: one JSONFileEntry per ANALYZED file, in walk order (the + // reference implementation ranges the retainer map here — run-to-run + // random; the oracle's measured-variance canonicalization compares the set). + let file_entries: Option> = if per_file { + Some(agg.per_file.iter().map(build_file_entry).collect()) + } else { + None + }; + Some(build_json_report(&agg, file_entries)) } /// Builds the `static/cohesion` section tree in the reference implementation's `AggregationModeSummaryOnly` @@ -315,7 +406,7 @@ pub fn cohesion_report_value(root_path: &str) -> Option { pub fn cohesion_report_value_summary(root_path: &str) -> Option { let mut agg = aggregate(root_path)?; agg.functions.clear(); - Some(build_json_report(&agg)) + Some(build_json_report(&agg, None)) } /// Builds the AGGREGATED RAW `analyze.Report` GoValue for `static/cohesion` — @@ -396,7 +487,97 @@ fn cohesion_message(score: f64) -> &'static str { } } -fn build_json_report(agg: &Aggregated) -> GoValue { +/// Builds one `renderer.JSONFileEntry` for `--per-file` (the reference +/// `SectionToJSONFileEntry` over `cohesion.NewReportSection(perFileReport)`): +/// status/score come from the PER-FILE report (`message` / `cohesion_score`), +/// the metrics are the per-file scalars, and the distribution and +/// value-string-ascending issues cover this file's functions only. +fn build_file_entry(pf: &PerFileCohesion) -> GoValue { + let score = pf.cohesion_score; + let status = if pf.message.is_empty() { + DEFAULT_STATUS_MESSAGE.to_string() + } else { + pf.message.clone() + }; + + let metrics = GoValue::Array(vec![ + metric(METRIC_TOTAL_FUNCTIONS, &pf.total_functions.to_string()), + metric(METRIC_LCOM, &format_float(pf.lcom)), + metric(METRIC_COHESION_SCORE, &format_float(pf.cohesion_score)), + metric( + METRIC_FUNCTION_COHESION, + &format_float(pf.function_cohesion), + ), + ]); + + // Distribution over THIS file's functions (absent for function-free files + // — Distribution() returns nil ⇒ omitempty). + let total = pf.functions.len() as i64; + let mut dist_items = Vec::new(); + if total != 0 { + let mut excellent = 0i64; + let mut good = 0i64; + let mut fair = 0i64; + let mut poor = 0i64; + for (_, coh) in &pf.functions { + if *coh >= DIST_EXCELLENT_MIN { + excellent += 1; + } else if *coh >= DIST_GOOD_MIN { + good += 1; + } else if *coh >= DIST_FAIR_MIN { + fair += 1; + } else { + poor += 1; + } + } + dist_items.push(dist_item( + DIST_LABEL_EXCELLENT, + pct(excellent, total), + excellent, + )); + dist_items.push(dist_item(DIST_LABEL_GOOD, pct(good, total), good)); + dist_items.push(dist_item(DIST_LABEL_FAIR, pct(fair, total), fair)); + dist_items.push(dist_item(DIST_LABEL_POOR, pct(poor, total), poor)); + } + + // Issues: this file's functions sorted by the FormatFloat string ascending + // (the section comparator; name tie-break keeps our output deterministic + // where the reference unstable sort varies). + let mut issues: Vec<&(String, f64)> = pf.functions.iter().collect(); + issues.sort_by(|a, b| { + format_float(a.1) + .cmp(&format_float(b.1)) + .then_with(|| a.0.cmp(&b.0)) + }); + let issue_items: Vec = issues + .iter() + .map(|(name, coh)| { + let mut iss = GoMap::new(MapOrigin::Struct); + iss.push("name", GoValue::Str(name.clone())); + iss.push("location", GoValue::Str(pf.rel.clone())); + iss.push("value", GoValue::Str(format_float(*coh))); + iss.push( + "severity", + GoValue::Str(severity_for_cohesion(*coh).to_string()), + ); + GoValue::Map(iss) + }) + .collect(); + + let mut entry = GoMap::new(MapOrigin::Struct); + entry.push("file_path", GoValue::Str(pf.rel.clone())); + entry.push("score_label", GoValue::Str(score_label(score))); + entry.push("status", GoValue::Str(status)); + entry.push("metrics", metrics); + if !dist_items.is_empty() { + entry.push("distribution", GoValue::Array(dist_items)); + } + entry.push("issues", GoValue::Array(issue_items)); + entry.push("score", GoValue::Float(score)); + GoValue::Map(entry) +} + +fn build_json_report(agg: &Aggregated, file_entries: Option>) -> GoValue { let cohesion_score = agg.cohesion_score_avg(); let functions = sorted_functions(agg); @@ -484,6 +665,11 @@ fn build_json_report(agg: &Aggregated) -> GoValue { section.push("distribution", GoValue::Array(dist_items)); } section.push("issues", GoValue::Array(issue_items)); + // --per-file enrichment: `files` sits between `issues` and `score` + // (renderer.JSONSection field order; omitempty without the flag). + if let Some(entries) = file_entries { + section.push("files", GoValue::Array(entries)); + } section.push("score", GoValue::Float(cohesion_score)); // ---- report ---- @@ -687,3 +873,58 @@ fn score_label(score: f64) -> String { let n = (score * 10.0).round() as i64; format!("{n}/10") } + +#[cfg(test)] +mod per_file_tests { + use super::*; + + /// Fixture: one file with a function, one function-free file. + fn fixture() -> tempfile::TempDir { + let dir = tempfile::tempdir().expect("tempdir"); + std::fs::write( + dir.path().join("a.go"), + "package main\n\nfunc add(a, b int) int {\n\tc := a + b\n\treturn c\n}\n", + ) + .unwrap(); + std::fs::write( + dir.path().join("types.go"), + "package main\n\ntype Config struct {\n\tName string\n}\n", + ) + .unwrap(); + dir + } + + #[test] + fn per_file_flag_emits_files_entries() { + let dir = fixture(); + let bytes = cohesion_report_json_flags(dir.path().to_str().unwrap(), true).unwrap(); + let json = String::from_utf8(bytes).unwrap(); + assert!(json.contains("\"files\""), "files key missing:\n{json}"); + assert!( + json.contains("\"file_path\": \"a.go\""), + "a.go entry missing:\n{json}" + ); + assert!( + json.contains("\"file_path\": \"types.go\""), + "types.go entry missing:\n{json}" + ); + // Per-file status is the ANALYZER's per-file message; a function-free + // file reports the empty-result message. + assert!( + json.contains("\"status\": \"No functions found\""), + "per-file empty-result status missing:\n{json}" + ); + } + + #[test] + fn no_per_file_flag_omits_files() { + let dir = fixture(); + let bytes = cohesion_report_json_flags(dir.path().to_str().unwrap(), false).unwrap(); + let json = String::from_utf8(bytes).unwrap(); + assert!( + !json.contains("\"files\""), + "files key must be omitted:\n{json}" + ); + assert!(!json.contains("\"file_path\"")); + } +} diff --git a/crates/cf-commands/src/handlers/static_halstead.rs b/crates/cf-commands/src/handlers/static_halstead.rs index b58e2f68..e4579ba4 100644 --- a/crates/cf-commands/src/handlers/static_halstead.rs +++ b/crates/cf-commands/src/handlers/static_halstead.rs @@ -607,6 +607,22 @@ struct Aggregate { averages: HashMap<&'static str, f64>, total_functions: i64, functions: Vec, + /// Per-file snapshots (the reference `PerFileRetainer` clones) consumed by + /// the `--per-file` section enrichment; walk order. + file_reports: Vec, +} + +/// One retained per-file report: the FILE-level scalars plus the range of this +/// file's functions inside the concatenated [`Aggregate::functions`] list +/// (empty range for function-free files). +struct PerFileHalstead { + /// Path relative to the analyzed root (the `StampSourceFile` stamp). + rel: String, + /// FILE-level scalar metrics (exact per-file values, not averages). + scalars: HashMap<&'static str, f64>, + total_functions: i64, + start: usize, + end: usize, } /// Walks `root` (lexical order, `.git` skipped, vendor/generated excluded), @@ -634,6 +650,10 @@ fn aggregate_opts(root_path: &str, opts: &Options) -> Option { // per-file function WITHOUT deduplication (so two same-named functions in one // file are both kept), in file-processing (lexical walk) order. let mut functions: Vec = Vec::new(); + // Per-file snapshots for --per-file (rel path + file-level scalars + the + // function range just appended); retained unconditionally — one small map + // and two indices per file. + let mut file_reports: Vec = Vec::new(); let mut files: Vec = Vec::new(); collect_files(root, &parser, opts, &mut files); @@ -672,7 +692,15 @@ fn aggregate_opts(root_path: &str, opts: &Options) -> Option { } total_functions += report.total_functions; + let start = functions.len(); functions.extend(report.functions); + file_reports.push(PerFileHalstead { + rel, + scalars: report.scalars, + total_functions: report.total_functions, + start, + end: functions.len(), + }); } if report_count == 0 { @@ -688,6 +716,7 @@ fn aggregate_opts(root_path: &str, opts: &Options) -> Option { averages, total_functions, functions, + file_reports, }) } @@ -1528,11 +1557,154 @@ fn format_issue_value(effort: f64, volume: f64, bugs: f64) -> String { ) } +/// The reference `ReportFormatter.GetHalsteadMessage` — the PER-FILE report +/// `message` the analyzer builds from the file-level volume/difficulty/effort +/// (an all-zero function-free file lands in the "Excellent" bucket, matching +/// the visitor-path reports the reference retainer snapshots). +fn per_file_message(volume: f64, difficulty: f64, effort: f64) -> &'static str { + if volume <= 100.0 && difficulty <= 5.0 && effort <= 1000.0 { + "Excellent complexity - code is simple and maintainable" + } else if volume <= 1000.0 && difficulty <= 15.0 && effort <= 10000.0 { + "Good complexity - code is reasonably complex" + } else if volume <= 5000.0 && difficulty <= 30.0 && effort <= 50000.0 { + "Fair complexity - consider simplifying some functions" + } else { + "High complexity - code should be refactored for better maintainability" + } +} + +/// Builds one `renderer.JSONFileEntry` for `--per-file` (the reference +/// `SectionToJSONFileEntry` over `halstead.NewReportSection(perFileReport)`): +/// the per-file report is the analyzer's single-file result, so the ten key +/// metrics are the FILE-level scalars (exact ints, not cross-file averages), +/// the status is the per-file `GetHalsteadMessage` label, the distribution and +/// effort-descending issues cover this file's functions only, and the score +/// derives from the FILE difficulty. +fn build_file_entry(f: &PerFileHalstead, functions: &[FunctionMetrics]) -> GoValue { + let s = |k: &str| f.scalars.get(k).copied().unwrap_or(0.0); + let difficulty = s("difficulty"); + let score = section_score(difficulty); + + let fmt_f = cf_reportutil::format_float; + let metric = |label: &str, value: String| { + let mut m = GoMap::new(MapOrigin::Struct); + m.push("label", GoValue::Str(label.to_string())); + m.push("value", GoValue::Str(value)); + GoValue::Map(m) + }; + let metrics = vec![ + metric("Total Functions", f.total_functions.to_string()), + metric( + "Distinct Operators (n1)", + get_int(s("distinct_operators")).to_string(), + ), + metric( + "Distinct Operands (n2)", + get_int(s("distinct_operands")).to_string(), + ), + metric( + "Total Operators (N1)", + get_int(s("total_operators")).to_string(), + ), + metric( + "Total Operands (N2)", + get_int(s("total_operands")).to_string(), + ), + metric("Vocabulary", get_int(s("vocabulary")).to_string()), + metric("Volume", fmt_f(s("volume"))), + metric("Difficulty", fmt_f(difficulty)), + metric("Effort", fmt_f(s("effort"))), + metric("Est. Bugs", fmt_f(s("delivered_bugs"))), + ]; + + // Distribution over THIS file's functions (4 volume buckets; absent for + // function-free files — Distribution() returns nil ⇒ omitempty). + let frecs = &functions[f.start..f.end]; + let total = frecs.len(); + let (mut low, mut medium, mut high, mut very_high) = (0i64, 0i64, 0i64, 0i64); + for r in frecs { + if r.volume <= DIST_LOW_MAX { + low += 1; + } else if r.volume <= DIST_MED_MAX { + medium += 1; + } else if r.volume <= DIST_HIGH_MAX { + high += 1; + } else { + very_high += 1; + } + } + let dist_item = |label: &str, count: i64| { + let mut m = GoMap::new(MapOrigin::Struct); + m.push("label", GoValue::Str(label.to_string())); + m.push("percent", GoValue::Float(count as f64 / total as f64)); + m.push("count", GoValue::Int(count)); + GoValue::Map(m) + }; + let distribution: Vec = if total == 0 { + Vec::new() + } else { + vec![ + dist_item("Low (<=100)", low), + dist_item("Medium (101-1000)", medium), + dist_item("High (1001-5000)", high), + dist_item("Very High (>5000)", very_high), + ] + }; + + // Issues: this file's functions sorted by effort descending (the same + // unstable-sort reproduction the aggregate section uses). + let mut issue_funcs: Vec<&FunctionMetrics> = frecs.iter().collect(); + let mut efforts: Vec = issue_funcs.iter().map(|f| f.effort).collect(); + gosort::slice_by_volume_desc(&mut efforts, &mut issue_funcs); + let issues: Vec = issue_funcs + .iter() + .map(|r| { + let mut m = GoMap::new(MapOrigin::Struct); + m.push("name", GoValue::Str(r.name.clone())); + m.push("location", GoValue::Str(r.source_file.clone())); + m.push( + "value", + GoValue::Str(format_issue_value(r.effort, r.volume, r.delivered_bugs)), + ); + m.push( + "severity", + GoValue::Str(severity_for_function(r.effort, r.delivered_bugs).to_string()), + ); + GoValue::Map(m) + }) + .collect(); + + let mut entry = GoMap::new(MapOrigin::Struct); + entry.push("file_path", GoValue::Str(f.rel.clone())); + entry.push("score_label", GoValue::Str(format_score(score))); + entry.push( + "status", + GoValue::Str(per_file_message(s("volume"), difficulty, s("effort")).to_string()), + ); + entry.push("metrics", GoValue::Array(metrics)); + if !distribution.is_empty() { + entry.push("distribution", GoValue::Array(distribution)); + } + entry.push("issues", GoValue::Array(issues)); + entry.push("score", GoValue::Float(score)); + GoValue::Map(entry) +} + /// Builds the `static/halstead --format json` structured report bytes for /// `root_path`, or `None` when the folder cannot be walked. #[must_use] pub fn halstead_json_report(root_path: &str) -> Option> { - let root = halstead_report_value(root_path)?; + halstead_json_report_flags(root_path, false) +} + +/// [`halstead_json_report`] with the `--per-file` flag applied: `per_file` +/// enables the reference implementation's section enrichment +/// (`StaticService.enrichWithPerFileData` → `JSONReport.EnrichWithPerFileData` +/// over the `PerFileRetainer` snapshots — one `JSONFileEntry` per ANALYZED +/// file, function-free files included, keyed into the section's `files` array). +#[must_use] +pub fn halstead_json_report_flags(root_path: &str, per_file: bool) -> Option> { + let root = halstead_report_value_flags(root_path, per_file)?; Some( cf_gojson::Encoder::indented(" ") .with_trailing_newline(true) @@ -1548,6 +1720,12 @@ pub fn halstead_report_value(root_path: &str) -> Option { halstead_report_value_mode(root_path, false) } +/// [`halstead_report_value`] with the `--per-file` section enrichment. +#[must_use] +pub fn halstead_report_value_flags(root_path: &str, per_file: bool) -> Option { + halstead_report_value_mode_flags(root_path, false, per_file) +} + /// Builds the `static/halstead` section tree in the reference implementation's `AggregationModeSummaryOnly` /// shape (`text` / `compact`): the detailed `functions` collection is a no-op, so /// the per-function volume distribution and the top-issues list are absent while @@ -1558,7 +1736,32 @@ pub fn halstead_report_value_summary(root_path: &str) -> Option { } fn halstead_report_value_mode(root_path: &str, summary_only: bool) -> Option { + halstead_report_value_mode_flags(root_path, summary_only, false) +} + +fn halstead_report_value_mode_flags( + root_path: &str, + summary_only: bool, + per_file: bool, +) -> Option { let mut agg = aggregate(root_path)?; + + // --per-file: one JSONFileEntry per ANALYZED file, in walk order (the + // reference implementation ranges the retainer map here — run-to-run + // random; the oracle's measured-variance canonicalization compares the + // set). Built BEFORE the summary-only clear (the modes never combine, but + // the ranges index into `functions`). + let file_entries: Option> = if per_file { + Some( + agg.file_reports + .iter() + .map(|f| build_file_entry(f, &agg.functions)) + .collect(), + ) + } else { + None + }; + if summary_only { agg.functions.clear(); } @@ -1680,7 +1883,12 @@ fn halstead_report_value_mode(root_path: &str, summary_only: bool) -> Option Option tempfile::TempDir { + let dir = tempfile::tempdir().expect("tempdir"); + std::fs::write( + dir.path().join("a.go"), + "package main\n\nfunc add(a, b int) int {\n\treturn a + b\n}\n", + ) + .unwrap(); + std::fs::write( + dir.path().join("types.go"), + "package main\n\ntype Config struct {\n\tName string\n}\n", + ) + .unwrap(); + dir + } + + #[test] + fn per_file_flag_emits_files_entries() { + let dir = fixture(); + let bytes = halstead_json_report_flags(dir.path().to_str().unwrap(), true).unwrap(); + let json = String::from_utf8(bytes).unwrap(); + assert!(json.contains("\"files\""), "files key missing:\n{json}"); + assert!( + json.contains("\"file_path\": \"a.go\""), + "a.go entry missing:\n{json}" + ); + assert!( + json.contains("\"file_path\": \"types.go\""), + "types.go entry missing:\n{json}" + ); + // Per-file status is the reference GetHalsteadMessage over the + // FILE-level metrics (all-zero for the function-free file). + assert!( + json.contains("\"status\": \"Excellent complexity - code is simple and maintainable\""), + "per-file halstead message missing:\n{json}" + ); + } + + #[test] + fn no_per_file_flag_omits_files() { + let dir = fixture(); + let bytes = halstead_json_report_flags(dir.path().to_str().unwrap(), false).unwrap(); + let json = String::from_utf8(bytes).unwrap(); + assert!( + !json.contains("\"files\""), + "files key must be omitted:\n{json}" + ); + assert!(!json.contains("\"file_path\"")); + } +} diff --git a/crates/cf-commands/src/handlers/static_imports.rs b/crates/cf-commands/src/handlers/static_imports.rs index 2a0aae08..316cff37 100644 --- a/crates/cf-commands/src/handlers/static_imports.rs +++ b/crates/cf-commands/src/handlers/static_imports.rs @@ -101,7 +101,17 @@ fn aggregate_report_value(root_path: &str) -> Option { /// ordering: count descending, ties broken by the aggregator's sorted import /// keys (the same set the reference implementation emits, just stably ordered). pub fn imports_report_json(root_path: &str) -> Option> { - let root = imports_report_value(root_path)?; + imports_report_json_flags(root_path, false) +} + +/// [`imports_report_json`] with the `--per-file` flag applied: `per_file` +/// enables the reference implementation's section enrichment +/// (`StaticService.enrichWithPerFileData` → `JSONReport.EnrichWithPerFileData` +/// over the `PerFileRetainer` snapshots — one `JSONFileEntry` per ANALYZED +/// file, import-free files included, keyed into the section's `files` array). +#[must_use] +pub fn imports_report_json_flags(root_path: &str, per_file: bool) -> Option> { + let root = imports_report_value_flags(root_path, per_file)?; Some( cf_gojson::Encoder::indented(" ") .with_trailing_newline(true) @@ -114,7 +124,18 @@ pub fn imports_report_json(root_path: &str) -> Option> { /// static-JSON merge. `None` when the path cannot be walked. #[must_use] pub fn imports_report_value(root_path: &str) -> Option { - let (all_imports, total_files) = walk_and_count(root_path)?; + imports_report_value_flags(root_path, false) +} + +/// [`imports_report_value`] with the `--per-file` section enrichment. +#[must_use] +pub fn imports_report_value_flags(root_path: &str, per_file: bool) -> Option { + let mut per_file_data: Vec<(String, Vec)> = Vec::new(); + let (all_imports, total_files) = walk_and_count_opts( + root_path, + &Options::default(), + per_file.then_some(&mut per_file_data), + )?; let count = all_imports.len() as i64; // --- status / score (info-only) --- @@ -154,6 +175,20 @@ pub fn imports_report_value(root_path: &str) -> Option { }) .collect(); + // --- --per-file: one JSONFileEntry per ANALYZED file, in walk order (the + // reference implementation ranges the retainer map here — run-to-run + // random; the oracle's measured-variance canonicalization compares the set) --- + let file_entries: Option> = if per_file { + Some( + per_file_data + .iter() + .map(|(rel, imports)| build_file_entry(rel, imports)) + .collect(), + ) + } else { + None + }; + // --- section (JSONSection field order) --- let mut section = GoMap::new(MapOrigin::Struct); section.push("title", GoValue::Str("IMPORTS".to_string())); @@ -162,6 +197,11 @@ pub fn imports_report_value(root_path: &str) -> Option { section.push("metrics", GoValue::Array(metrics)); // Distribution() returns nil ⇒ omitempty omits it. section.push("issues", GoValue::Array(issues)); + // --per-file enrichment: `files` sits between `issues` and `score` + // (renderer.JSONSection field order; omitempty without the flag). + if let Some(entries) = file_entries { + section.push("files", GoValue::Array(entries)); + } section.push("score", GoValue::Float(-1.0)); // --- top-level JSONReport (overall info-only) --- @@ -173,6 +213,51 @@ pub fn imports_report_value(root_path: &str) -> Option { Some(GoValue::Map(root)) } +/// Builds one `renderer.JSONFileEntry` for `--per-file` (the reference +/// `SectionToJSONFileEntry` over `imports.CreateReportSection(perFileReport)`): +/// the per-file report is the analyzer's `{imports, count}` map — no +/// `import_counts` — so the issues take the alphabetical-list fallback, each +/// valued `"1"` and located at the stamped relative path, and `Total Files` +/// reads the absent key as 0. Imports stays info-only per file (`score = -1`). +fn build_file_entry(rel_path: &str, imports: &[String]) -> GoValue { + let count = imports.len() as i64; + + let metric = |label: &str, value: String| { + let mut m = GoMap::new(MapOrigin::Struct); + m.push("label", GoValue::Str(label.to_string())); + m.push("value", GoValue::Str(value)); + GoValue::Map(m) + }; + let metrics = vec![ + metric("Unique Imports", cf_reportutil::format_int(count)), + metric("Total Files", cf_reportutil::format_int(0)), + ]; + + let mut sorted: Vec<&String> = imports.iter().collect(); + sorted.sort(); + let issues: Vec = sorted + .iter() + .map(|name| { + let mut m = GoMap::new(MapOrigin::Struct); + m.push("name", GoValue::Str((*name).clone())); + m.push("location", GoValue::Str(rel_path.to_string())); + m.push("value", GoValue::Str("1".to_string())); + m.push("severity", GoValue::Str("info".to_string())); + GoValue::Map(m) + }) + .collect(); + + let mut entry = GoMap::new(MapOrigin::Struct); + entry.push("file_path", GoValue::Str(rel_path.to_string())); + entry.push("score_label", GoValue::Str("Info".to_string())); + entry.push("status", GoValue::Str(build_status_message(count))); + entry.push("metrics", GoValue::Array(metrics)); + // Distribution() returns nil for imports ⇒ omitempty omits it. + entry.push("issues", GoValue::Array(issues)); + entry.push("score", GoValue::Float(-1.0)); + GoValue::Map(entry) +} + /// Builds the AGGREGATED RAW `analyze.Report` GoValue for `static/imports` — /// the value the reference implementation's `imports.Aggregator.GetResult()` returns, /// which is what `--format plot` consumes and what `writeReportJSON` @@ -185,7 +270,7 @@ pub fn imports_report_value(root_path: &str) -> Option { /// * `count`: unique import count; `total_files`: every analyzed file. #[must_use] pub fn imports_raw_report_value(root_path: &str, opts: &Options) -> Option { - let (all_imports, total_files) = walk_and_count_opts(root_path, opts)?; + let (all_imports, total_files) = walk_and_count_opts(root_path, opts, None)?; let imports: Vec = all_imports .keys() @@ -218,14 +303,18 @@ fn build_status_message(count: i64) -> String { /// alongside the total analyzed-file count. Returns `None` when the path is /// missing. Mirrors the per-file analyze + `imports.Aggregator` accumulation. fn walk_and_count(root_path: &str) -> Option<(std::collections::BTreeMap, i64)> { - walk_and_count_opts(root_path, &Options::default()) + walk_and_count_opts(root_path, &Options::default(), None) } /// [`walk_and_count`] with explicit path-policy options (the plot path passes -/// the run flags; the stdout formats keep the defaults). +/// the run flags; the stdout formats keep the defaults). When `per_file_out` +/// is set, each analyzed file's deduplicated import list is also pushed as +/// `(relative path, imports)` in walk order — the reference `PerFileRetainer` +/// snapshot the `--per-file` section enrichment consumes. fn walk_and_count_opts( root_path: &str, opts: &Options, + mut per_file_out: Option<&mut Vec<(String, Vec)>>, ) -> Option<(std::collections::BTreeMap, i64)> { let root = Path::new(root_path); if !root.exists() { @@ -251,14 +340,17 @@ fn walk_and_count_opts( total_files += 1; // Parse failures on supported files (markdown without a wired grammar) // fold as an empty report: +1 file, no imports (see module docs). - let Ok(node) = parser.parse(path, &content) else { - continue; - }; - // extract_imports_from_uast deduplicates within the file in first-seen // order; the cross-file aggregator then counts file occurrences. - for imp in extract_imports_from_uast(&node) { - *all_imports.entry(imp).or_insert(0) += 1; + let file_imports: Vec = match parser.parse(path, &content) { + Ok(node) => extract_imports_from_uast(&node), + Err(_) => Vec::new(), + }; + for imp in &file_imports { + *all_imports.entry(imp.clone()).or_insert(0) += 1; + } + if let Some(out) = per_file_out.as_deref_mut() { + out.push((make_relative_path(path, root_path), file_imports)); } } @@ -268,6 +360,18 @@ fn walk_and_count_opts( Some((all_imports, total_files)) } +/// The reference `filepath.Rel(rootPath, filePath)` (flat repos → path under +/// the root) — the `StampSourceFile` path stamped onto per-file reports. +fn make_relative_path(file_path: &str, root_path: &str) -> String { + if root_path.is_empty() { + return file_path.to_string(); + } + match Path::new(file_path).strip_prefix(Path::new(root_path)) { + Ok(rel) => rel.to_string_lossy().into_owned(), + Err(_) => file_path.to_string(), + } +} + /// Recursively collects UAST-supported, non-excluded regular files under `dir` /// (lexical order; `.git` skipped). Mirrors `streamFiles` / /// `ShouldSkipFolderNode`. @@ -411,3 +515,64 @@ fn parse_import_format(path: &str) -> String { } String::new() } + +#[cfg(test)] +mod per_file_tests { + use super::*; + + /// Fixture: one file with two imports, one import-free file. + fn fixture() -> tempfile::TempDir { + let dir = tempfile::tempdir().expect("tempdir"); + std::fs::write( + dir.path().join("a.go"), + "package main\n\nimport (\n\t\"fmt\"\n\t\"os\"\n)\n\nfunc main() {\n\tfmt.Println(os.Args)\n}\n", + ) + .unwrap(); + std::fs::write( + dir.path().join("types.go"), + "package main\n\ntype Config struct {\n\tName string\n}\n", + ) + .unwrap(); + dir + } + + #[test] + fn per_file_flag_emits_files_entries() { + let dir = fixture(); + let bytes = imports_report_json_flags(dir.path().to_str().unwrap(), true).unwrap(); + let json = String::from_utf8(bytes).unwrap(); + // The section gains a `files` array with one JSONFileEntry per + // ANALYZED file (import-free files included). + assert!(json.contains("\"files\""), "files key missing:\n{json}"); + assert!( + json.contains("\"file_path\": \"a.go\""), + "a.go entry missing:\n{json}" + ); + assert!( + json.contains("\"file_path\": \"types.go\""), + "types.go entry missing:\n{json}" + ); + // Per-file status uses the per-file unique-import count. + assert!( + json.contains("\"status\": \"No import data available\""), + "import-free per-file status missing:\n{json}" + ); + // Per-file issues carry the stamped relative path as location. + assert!( + json.contains("\"location\": \"a.go\""), + "per-file issue location missing:\n{json}" + ); + } + + #[test] + fn no_per_file_flag_omits_files() { + let dir = fixture(); + let bytes = imports_report_json_flags(dir.path().to_str().unwrap(), false).unwrap(); + let json = String::from_utf8(bytes).unwrap(); + assert!( + !json.contains("\"files\""), + "files key must be omitted:\n{json}" + ); + assert!(!json.contains("\"file_path\"")); + } +} From 1076f517ba37fc9a5c683b97cb6d2b1df83298eb Mon Sep 17 00:00:00 2001 From: Dmytro Gajewski <142790186+dmytrogajewski@users.noreply.github.com> Date: Thu, 23 Jul 2026 18:49:54 +0300 Subject: [PATCH 2/2] =?UTF-8?q?fix(static):=20complete=20--per-file=20cove?= =?UTF-8?q?rage=20=E2=80=94=20comments,=20composition,=20clones,=20and=20t?= =?UTF-8?q?he=20multi-analyzer=20JSON=20merge=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes the gaps left by the previous commit so --per-file now matches the reference implementation across the whole static JSON surface: - static/comments: the walk retains per-file snapshots (scalars, the per-file message, undocumented-function names); entries carry per-file score/status, the six key metrics, the Documented/Undocumented distribution, and name-sorted undocumented issues. Parse-failure files fold as the reference buildEmptyResult snapshot ("No comments found"). - static/composition: one entry per WALKED file (raw-file analyzer — non-UAST files included). The reference retains the raw classify report, which lacks every aggregate key the section reads, so each entry is the degenerate empty-report section ("No files analyzed", zero metrics, info score) with only file_path varying — reproduced byte-for-byte. - static/clones: not PerFileModeEnabled in the reference, but EnrichWithPerFileData still initializes every section with an empty files array — the flag now adds `files: []` via a shared ensure_sections_files_key helper (the enrichment-initialization step). - static_multi_json: the STATIC_JSON_VALUE_BUILDERS table is now flag-aware and the run's --per-file flag is threaded from lib.rs, so every merged section carries files (real entries for retaining analyzers, the initialized empty array for clones). Verified against the live Go reference binary on a fixture with Go, markdown, plain-text, and binary files: the files arrays are identical (after set-order canonicalization) for all three single-analyzer runs AND the full `-a 'static/*' --per-file` merged run. Red-first unit tests added for each path plus the merge. Co-authored-by: Dmitriy Co-authored-by: Claude Fable 5 --- crates/cf-commands/src/handlers.rs | 136 ++++++++- .../cf-commands/src/handlers/static_clones.rs | 65 ++++- .../src/handlers/static_comments.rs | 267 +++++++++++++++++- .../src/handlers/static_complexity.rs | 7 + .../cf-commands/src/handlers/static_json.rs | 151 +++++++++- crates/cf-commands/src/lib.rs | 4 +- 6 files changed, 598 insertions(+), 32 deletions(-) diff --git a/crates/cf-commands/src/handlers.rs b/crates/cf-commands/src/handlers.rs index 9c34b33a..b8260477 100644 --- a/crates/cf-commands/src/handlers.rs +++ b/crates/cf-commands/src/handlers.rs @@ -470,22 +470,37 @@ pub fn static_multi_bin(patterns: &[&str], path: &str) -> Option> { /// static analyzer id → the crate-owned builder of that analyzer's single-section /// `renderer.JSONReport` GoValue. Used by [`static_multi_json`] to merge several /// analyzers' sections; the merge never branches per format — the same GoValue -/// feeds the serializer. -type ReportValueFn = fn(&str) -> Option; +/// feeds the serializer. The `bool` is the run's `--per-file` flag: every +/// builder owns its section's `files` enrichment (the reference +/// `EnrichWithPerFileData` gives EVERY section at least an empty `files` array +/// under the flag, so no post-processing happens in the merge). +type ReportValueFn = fn(&str, bool) -> Option; use cf_gojson::{GoMap, GoValue, MapOrigin}; const STATIC_JSON_VALUE_BUILDERS: &[(&str, ReportValueFn)] = &[ - ("static/clones", static_clones::clones_report_value), + ("static/clones", static_clones::clones_report_value_flags), ( "static/complexity", - static_complexity::complexity_report_value, + static_complexity::complexity_report_value_flags, + ), + ( + "static/comments", + static_comments::comments_report_value_flags, + ), + ( + "static/halstead", + static_halstead::halstead_report_value_flags, + ), + ( + "static/cohesion", + static_cohesion::cohesion_report_value_flags, + ), + ("static/imports", static_imports::imports_report_value_flags), + ( + "static/composition", + static_json::composition_report_value_flags, ), - ("static/comments", static_comments::comments_report_value), - ("static/halstead", static_halstead::halstead_report_value), - ("static/cohesion", static_cohesion::cohesion_report_value), - ("static/imports", static_imports::imports_report_value), - ("static/composition", static_json::composition_report_value), ]; /// Pulls the `sections` array and `overall_score` out of a single-analyzer @@ -533,7 +548,7 @@ fn overall_score_label(score: f64) -> String { /// selected or any selected analyzer cannot produce a report (the caller then /// falls through to the same error path the reference implementation takes). #[must_use] -pub fn static_multi_json(patterns: &[&str], path: &str) -> Option> { +pub fn static_multi_json(patterns: &[&str], path: &str, per_file: bool) -> Option> { let mut sections: Vec = Vec::new(); let mut score_total = 0.0_f64; let mut score_count = 0_usize; @@ -549,7 +564,7 @@ pub fn static_multi_json(patterns: &[&str], path: &str) -> Option> { if !matched { continue; } - let report = build(path)?; + let report = build(path, per_file)?; for section in extract_sections(&report) { let s = section_score(§ion); if s >= 0.0 { @@ -584,6 +599,49 @@ pub fn static_multi_json(patterns: &[&str], path: &str) -> Option> { Some(bytes) } +/// The reference `EnrichWithPerFileData` INITIALIZATION step: under +/// `--per-file` every section gets at least an EMPTY `files` array (inserted +/// between `issues` and `score`, the renderer.JSONSection field order), even +/// when its analyzer retains no per-file snapshots (clones is not +/// `PerFileModeEnabled`). Sections that already carry `files` are unchanged. +pub(crate) fn ensure_sections_files_key(report: GoValue) -> GoValue { + let GoValue::Map(root) = report else { + return report; + }; + let mut new_root = GoMap::new(MapOrigin::Struct); + for (key, value) in root.iter() { + if key != "sections" { + new_root.push(key, value.clone()); + continue; + } + let GoValue::Array(sections) = value else { + new_root.push(key, value.clone()); + continue; + }; + let enriched: Vec = sections + .iter() + .map(|section| { + let GoValue::Map(m) = section else { + return section.clone(); + }; + if m.get("files").is_some() { + return section.clone(); + } + let mut out = GoMap::new(MapOrigin::Struct); + for (k, v) in m.iter() { + if k == "score" { + out.push("files", GoValue::Array(Vec::new())); + } + out.push(k, v.clone()); + } + GoValue::Map(out) + }) + .collect(); + new_root.push(key, GoValue::Array(enriched)); + } + GoValue::Map(new_root) +} + /// True when `patterns` select MORE THAN ONE static analyzer (a literal multi-id /// list or a glob matching several), so the JSON path must merge sections rather /// than emit a single-analyzer document. @@ -943,7 +1001,7 @@ fn h_static_clones(ctx: &RunContext, format: &str) -> Option> { // `"binary"` (the reference `ValidateFormat` maps the `bin` alias to `binary`); accept // both spellings for robustness. match format { - "json" => static_clones::clones_report_json(path), + "json" => static_clones::clones_report_json_flags(path, ctx.matches.get_flag("per-file")), "yaml" => static_clones::clones_report_yaml(path), "binary" | "bin" => static_clones::clones_report_bin(path), "compact" => static_clones::clones_report_compact(path), @@ -1013,7 +1071,7 @@ fn h_static_cohesion(ctx: &RunContext, format: &str) -> Option> { fn h_static_composition(ctx: &RunContext, format: &str) -> Option> { let path = &ctx.path; match format { - "json" => static_json::composition_report(path), + "json" => static_json::composition_report_flags(path, ctx.matches.get_flag("per-file")), "yaml" => static_json::composition_yaml(path), // The pipeline resolves the `bin` alias to the canonical `binary` // (formats::normalize_format) before dispatch, so match that; accept the @@ -1074,7 +1132,9 @@ fn h_static_imports(ctx: &RunContext, format: &str) -> Option> { fn h_static_comments(ctx: &RunContext, format: &str) -> Option> { let path = &ctx.path; match format { - "json" => static_comments::comments_report_json(path), + "json" => { + static_comments::comments_report_json_flags(path, ctx.matches.get_flag("per-file")) + } "yaml" => static_comments::comments_report_yaml(path), // The pipeline resolves the `bin` alias to the canonical `binary` // (formats::normalize_format) before dispatch, so match that; accept the @@ -1293,3 +1353,51 @@ pub fn default_registry() -> Registry { r } + +#[cfg(test)] +mod per_file_multi_tests { + use super::*; + + /// Fixture: one Go file with functions/comments/imports plus a plain-text + /// file, exercising every static analyzer through the merged JSON path. + fn fixture() -> tempfile::TempDir { + let dir = tempfile::tempdir().expect("tempdir"); + std::fs::write( + dir.path().join("a.go"), + "package main\n\nimport \"fmt\"\n\n// main prints.\nfunc main() {\n\tfmt.Println(\"hi\")\n}\n", + ) + .unwrap(); + std::fs::write(dir.path().join("notes.txt"), "just text\n").unwrap(); + dir + } + + #[test] + fn per_file_flag_enriches_every_merged_section() { + let dir = fixture(); + let bytes = static_multi_json(&["static/*"], dir.path().to_str().unwrap(), true).unwrap(); + let json = String::from_utf8(bytes).unwrap(); + // Every merged section gets a files key — the retaining analyzers with + // real entries, clones with the initialized empty array. + let sections = json.matches("\"title\"").count(); + let files_keys = json.matches("\"files\"").count(); + assert_eq!( + sections, files_keys, + "every section must carry a files key (sections={sections} files={files_keys}):\n{json}" + ); + assert!( + json.contains("\"file_path\": \"a.go\""), + "per-file entries missing:\n{json}" + ); + } + + #[test] + fn no_per_file_flag_omits_files_in_merge() { + let dir = fixture(); + let bytes = static_multi_json(&["static/*"], dir.path().to_str().unwrap(), false).unwrap(); + let json = String::from_utf8(bytes).unwrap(); + assert!( + !json.contains("\"files\""), + "files key must be omitted:\n{json}" + ); + } +} diff --git a/crates/cf-commands/src/handlers/static_clones.rs b/crates/cf-commands/src/handlers/static_clones.rs index cdbda32f..0fb83d43 100644 --- a/crates/cf-commands/src/handlers/static_clones.rs +++ b/crates/cf-commands/src/handlers/static_clones.rs @@ -54,7 +54,17 @@ use cf_uast_node::Node as UastNode; /// `None` when the path cannot be read. #[must_use] pub fn clones_report_json(root_path: &str) -> Option> { - let value = clones_report_value(root_path)?; + clones_report_json_flags(root_path, false) +} + +/// [`clones_report_json`] with the `--per-file` flag applied. The clones +/// aggregator retains NO per-file snapshots (it is not `PerFileModeEnabled` in +/// the reference implementation), but `EnrichWithPerFileData` still initializes +/// EVERY section with an EMPTY `files` array under `--per-file` — so the flag +/// adds `files: []` to the clones section, exactly as the reference emits. +#[must_use] +pub fn clones_report_json_flags(root_path: &str, per_file: bool) -> Option> { + let value = clones_report_value_flags(root_path, per_file)?; // Reference: json.NewEncoder(w).SetIndent("", " ").Encode(report) -> two-space // indent + one trailing newline. let bytes = Encoder::indented(" ") @@ -68,8 +78,20 @@ pub fn clones_report_json(root_path: &str) -> Option> { /// static-JSON merge. `None` when the path cannot be walked. #[must_use] pub fn clones_report_value(root_path: &str) -> Option { + clones_report_value_flags(root_path, false) +} + +/// [`clones_report_value`] with the `--per-file` section enrichment (an empty +/// `files` array — see [`clones_report_json_flags`]). +#[must_use] +pub fn clones_report_value_flags(root_path: &str, per_file: bool) -> Option { let report = aggregate_report(root_path)?; - Some(report_section_json_value(&report)) + let value = report_section_json_value(&report); + if per_file { + Some(super::ensure_sections_files_key(value)) + } else { + Some(value) + } } /// Builds the `static/clones --format yaml` report bytes for `root_path` @@ -208,3 +230,42 @@ fn make_relative_path(file_path: &str, root_path: &str) -> String { Err(_) => file_path.to_string(), } } + +#[cfg(test)] +mod per_file_tests { + use super::*; + + fn fixture() -> tempfile::TempDir { + let dir = tempfile::tempdir().expect("tempdir"); + std::fs::write( + dir.path().join("a.go"), + "package main\n\nfunc add(a, b int) int {\n\treturn a + b\n}\n", + ) + .unwrap(); + dir + } + + #[test] + fn per_file_flag_emits_empty_files_array() { + let dir = fixture(); + let bytes = clones_report_json_flags(dir.path().to_str().unwrap(), true).unwrap(); + let json = String::from_utf8(bytes).unwrap(); + // Clones retains no per-file data, but the enrichment still initializes + // the section with an EMPTY files array (present, not omitted). + assert!( + json.contains("\"files\": []"), + "empty files array missing:\n{json}" + ); + } + + #[test] + fn no_per_file_flag_omits_files() { + let dir = fixture(); + let bytes = clones_report_json_flags(dir.path().to_str().unwrap(), false).unwrap(); + let json = String::from_utf8(bytes).unwrap(); + assert!( + !json.contains("\"files\""), + "files key must be omitted:\n{json}" + ); + } +} diff --git a/crates/cf-commands/src/handlers/static_comments.rs b/crates/cf-commands/src/handlers/static_comments.rs index c60c9b28..cafd1c7f 100644 --- a/crates/cf-commands/src/handlers/static_comments.rs +++ b/crates/cf-commands/src/handlers/static_comments.rs @@ -94,6 +94,48 @@ struct Aggregated { sum_good_comments_ratio: f64, sum_documentation_coverage: f64, report_count: i64, + /// Per-file snapshots (the reference `PerFileRetainer` clones of each + /// analyzer report) consumed by the `--per-file` section enrichment; walk + /// order. + per_file: Vec, +} + +/// One retained per-file comments report: the analyzer result's scalars, +/// per-file `message`, and this file's undocumented function names. +struct PerFileComments { + /// Path relative to the analyzed root (the `StampSourceFile` stamp). + rel: String, + message: String, + overall_score: f64, + total_comments: i64, + good_comments: i64, + bad_comments: i64, + doc_coverage: f64, + good_ratio: f64, + total_functions: i64, + documented_functions: i64, + /// Function names with the `"❌ No Comment"` assessment. + undocumented: Vec, +} + +impl PerFileComments { + /// The reference `buildEmptyResult` snapshot: what the analyzer reports + /// for a file with no comments (all zeros, the empty-result message). + fn empty(rel: String) -> Self { + PerFileComments { + rel, + message: "No comments found".to_string(), + overall_score: 0.0, + total_comments: 0, + good_comments: 0, + bad_comments: 0, + doc_coverage: 0.0, + good_ratio: 0.0, + total_functions: 0, + documented_functions: 0, + undocumented: Vec::new(), + } + } } /// Builds the `static/comments --format yaml` report bytes for `root_path`, or @@ -139,7 +181,17 @@ pub fn comments_report_bin(root_path: &str) -> Option> { /// issue list is sorted by name (file-walk order within equal names). #[must_use] pub fn comments_report_json(root_path: &str) -> Option> { - let root = comments_report_value(root_path)?; + comments_report_json_flags(root_path, false) +} + +/// [`comments_report_json`] with the `--per-file` flag applied: `per_file` +/// enables the reference implementation's section enrichment +/// (`StaticService.enrichWithPerFileData` → `JSONReport.EnrichWithPerFileData` +/// over the `PerFileRetainer` snapshots — one `JSONFileEntry` per ANALYZED +/// file, comment-free files included, keyed into the section's `files` array). +#[must_use] +pub fn comments_report_json_flags(root_path: &str, per_file: bool) -> Option> { + let root = comments_report_value_flags(root_path, per_file)?; Some( cf_gojson::Encoder::indented(" ") .with_trailing_newline(true) @@ -152,7 +204,13 @@ pub fn comments_report_json(root_path: &str) -> Option> { /// static-JSON merge. `None` when the path cannot be walked. #[must_use] pub fn comments_report_value(root_path: &str) -> Option { - comments_report_value_mode(root_path, false) + comments_report_value_flags(root_path, false) +} + +/// [`comments_report_value`] with the `--per-file` section enrichment. +#[must_use] +pub fn comments_report_value_flags(root_path: &str, per_file: bool) -> Option { + comments_report_value_mode_flags(root_path, false, per_file) } /// Builds the `static/comments` section tree in the reference implementation's `AggregationModeSummaryOnly` @@ -167,7 +225,25 @@ pub fn comments_report_value_summary(root_path: &str) -> Option { } fn comments_report_value_mode(root_path: &str, summary_only: bool) -> Option { + comments_report_value_mode_flags(root_path, summary_only, false) +} + +fn comments_report_value_mode_flags( + root_path: &str, + summary_only: bool, + per_file: bool, +) -> Option { let mut agg = comments_aggregate(root_path)?; + + // --per-file: one JSONFileEntry per ANALYZED file, in walk order (the + // reference implementation ranges the retainer map here — run-to-run + // random; the oracle's measured-variance canonicalization compares the set). + let file_entries: Option> = if per_file { + Some(agg.per_file.iter().map(build_file_entry).collect()) + } else { + None + }; + if summary_only { agg.functions.clear(); } @@ -274,6 +350,11 @@ fn comments_report_value_mode(root_path: &str, summary_only: bool) -> Option Option GoValue { + let score = pf.overall_score; + let status = if pf.message.is_empty() { + "No comment data available".to_string() + } else { + pf.message.clone() + }; + + let metric = |label: &str, value: String| { + let mut m = GoMap::new(MapOrigin::Struct); + m.push("label", GoValue::Str(label.to_string())); + m.push("value", GoValue::Str(value)); + GoValue::Map(m) + }; + let metrics = vec![ + metric( + "Total Comments", + cf_reportutil::format_int(pf.total_comments), + ), + metric("Good Comments", cf_reportutil::format_int(pf.good_comments)), + metric("Bad Comments", cf_reportutil::format_int(pf.bad_comments)), + metric("Doc Coverage", format_percent(pf.doc_coverage)), + metric("Good Ratio", format_percent(pf.good_ratio)), + metric( + "Total Functions", + cf_reportutil::format_int(pf.total_functions), + ), + ]; + + // Distribution over THIS file's functions (absent for function-free files + // — Distribution() returns nil ⇒ omitempty). + let total_fns = pf.total_functions; + let distribution: Vec = if total_fns == 0 { + Vec::new() + } else { + let documented = pf.documented_functions; + let undocumented = total_fns - documented; + let dist = |label: &str, count: i64| { + let mut m = GoMap::new(MapOrigin::Struct); + m.push("label", GoValue::Str(label.to_string())); + m.push("percent", GoValue::Float(count as f64 / total_fns as f64)); + m.push("count", GoValue::Int(count)); + GoValue::Map(m) + }; + vec![ + dist("Documented", documented), + dist("Undocumented", undocumented), + ] + }; + + // Issues: this file's undocumented functions, name ascending. + let mut names: Vec<&String> = pf.undocumented.iter().collect(); + names.sort(); + let issues: Vec = names + .iter() + .map(|name| { + let mut m = GoMap::new(MapOrigin::Struct); + m.push("name", GoValue::Str((*name).clone())); + m.push("location", GoValue::Str(pf.rel.clone())); + m.push("value", GoValue::Str("undocumented".to_string())); + m.push("severity", GoValue::Str("poor".to_string())); + GoValue::Map(m) + }) + .collect(); + + let mut entry = GoMap::new(MapOrigin::Struct); + entry.push("file_path", GoValue::Str(pf.rel.clone())); + entry.push("score_label", GoValue::Str(format_score(score))); + entry.push("status", GoValue::Str(status)); + entry.push("metrics", GoValue::Array(metrics)); + if !distribution.is_empty() { + entry.push("distribution", GoValue::Array(distribution)); + } + entry.push("issues", GoValue::Array(issues)); + entry.push("score", GoValue::Float(score)); + GoValue::Map(entry) +} + /// Builds the AGGREGATED RAW `analyze.Report` GoValue for `static/comments` — /// the value the reference implementation's `comments.Aggregator.GetResult()` returns (the base /// `BuildCollectionResult` + the `DetailedDataCollector.AddToResult` @@ -460,17 +625,20 @@ fn comments_aggregate_opts(root_path: &str, opts: &PathPolicyOptions) -> Option< // for some supported extensions (markdown), so a parse failure on a // supported file is folded as that same empty report — keeping the mean // denominator (and thus the averaged scalar metrics) byte-identical. + // Stamp metadata exactly as StampSourceFile / StampLanguage. + let stamped = make_relative_path(&path_str, root_path); + let Ok(node) = parser.parse(&path_str, &content) else { agg.report_count += 1; + agg.per_file.push(PerFileComments::empty(stamped)); continue; }; let Ok(report) = analyzer.analyze(Some(&node)) else { agg.report_count += 1; + agg.per_file.push(PerFileComments::empty(stamped)); continue; }; - // Stamp metadata exactly as StampSourceFile / StampLanguage. - let stamped = make_relative_path(&path_str, root_path); let directory = parent_dir(&stamped); let language = parser.get_language(&path_str); @@ -574,6 +742,36 @@ fn aggregate_report( agg.sum_overall_score += scalar_float(report, "overall_score"); agg.sum_good_comments_ratio += scalar_float(report, "good_comments_ratio"); agg.sum_documentation_coverage += scalar_float(report, "documentation_coverage"); + + // PerFileRetainer.Retain: snapshot the per-file report for --per-file. + let undocumented: Vec = map_get(report, "functions") + .and_then(as_array) + .map(|items| { + items + .iter() + .filter(|item| { + map_get(item, "assessment").and_then(as_str) == Some("❌ No Comment") + }) + .map(|item| item_str(item, "function")) + .collect() + }) + .unwrap_or_default(); + agg.per_file.push(PerFileComments { + rel: source_file.to_string(), + message: map_get(report, "message") + .and_then(as_str) + .unwrap_or_default() + .to_string(), + overall_score: scalar_float(report, "overall_score"), + total_comments: scalar_int(report, "total_comments"), + good_comments: scalar_int(report, "good_comments"), + bad_comments: scalar_int(report, "bad_comments"), + doc_coverage: scalar_float(report, "documentation_coverage"), + good_ratio: scalar_float(report, "good_comments_ratio"), + total_functions: scalar_int(report, "total_functions"), + documented_functions: scalar_int(report, "documented_functions"), + undocumented, + }); } /// Builds the `comments.ComputedMetrics` GoValue (struct-origin: declaration @@ -777,3 +975,64 @@ fn parent_dir(stamped: &str) -> String { None => ".".to_string(), } } + +#[cfg(test)] +mod per_file_tests { + use super::*; + + /// Fixture: one file with a documented + an undocumented function, one + /// function-free file. + fn fixture() -> tempfile::TempDir { + let dir = tempfile::tempdir().expect("tempdir"); + std::fs::write( + dir.path().join("a.go"), + "package main\n\n// add sums two ints.\nfunc add(a, b int) int {\n\treturn a + b\n}\n\nfunc sub(a, b int) int {\n\treturn a - b\n}\n", + ) + .unwrap(); + std::fs::write( + dir.path().join("types.go"), + "package main\n\ntype Config struct {\n\tName string\n}\n", + ) + .unwrap(); + dir + } + + #[test] + fn per_file_flag_emits_files_entries() { + let dir = fixture(); + let bytes = comments_report_json_flags(dir.path().to_str().unwrap(), true).unwrap(); + let json = String::from_utf8(bytes).unwrap(); + assert!(json.contains("\"files\""), "files key missing:\n{json}"); + assert!( + json.contains("\"file_path\": \"a.go\""), + "a.go entry missing:\n{json}" + ); + assert!( + json.contains("\"file_path\": \"types.go\""), + "types.go entry missing:\n{json}" + ); + // Per-file status is the ANALYZER's per-file message; a comment-free + // file reports the empty-result message. + assert!( + json.contains("\"status\": \"No comments found\""), + "per-file empty-result status missing:\n{json}" + ); + // The undocumented function surfaces as a located per-file issue. + assert!( + json.contains("\"location\": \"a.go\""), + "per-file issue location missing:\n{json}" + ); + } + + #[test] + fn no_per_file_flag_omits_files() { + let dir = fixture(); + let bytes = comments_report_json_flags(dir.path().to_str().unwrap(), false).unwrap(); + let json = String::from_utf8(bytes).unwrap(); + assert!( + !json.contains("\"files\""), + "files key must be omitted:\n{json}" + ); + assert!(!json.contains("\"file_path\"")); + } +} diff --git a/crates/cf-commands/src/handlers/static_complexity.rs b/crates/cf-commands/src/handlers/static_complexity.rs index 174d35e1..9bcb1a3b 100644 --- a/crates/cf-commands/src/handlers/static_complexity.rs +++ b/crates/cf-commands/src/handlers/static_complexity.rs @@ -136,6 +136,13 @@ pub fn complexity_report_value(root_path: &str) -> Option { complexity_report_value_opts(root_path, false, &Options::default(), false) } +/// [`complexity_report_value`] with the `--per-file` section enrichment (the +/// multi-analyzer static-JSON merge threads the run flag through this). +#[must_use] +pub fn complexity_report_value_flags(root_path: &str, per_file: bool) -> Option { + complexity_report_value_opts(root_path, false, &Options::default(), per_file) +} + /// Builds the `static/complexity` section tree in the reference implementation's `AggregationModeSummaryOnly` /// shape used for the `text` / `compact` formats: the per-item `functions` /// detailed collection is a no-op, so the distribution and top-issues sections diff --git a/crates/cf-commands/src/handlers/static_json.rs b/crates/cf-commands/src/handlers/static_json.rs index 64ba3ffc..2b8a9d4c 100644 --- a/crates/cf-commands/src/handlers/static_json.rs +++ b/crates/cf-commands/src/handlers/static_json.rs @@ -57,6 +57,11 @@ struct Counts { total_files: i64, /// Per-category counts, indexed by [`ALL_CATEGORIES`] position. by_category: std::collections::HashMap<&'static str, i64>, + /// Walked file paths relative to the analyzed root (walk order) — the + /// reference `PerFileRetainer` key set consumed by the `--per-file` + /// section enrichment. Composition is a raw-file analyzer, so EVERY + /// classified file (non-UAST files included) gets an entry. + files: Vec, } impl Counts { @@ -82,7 +87,7 @@ fn composition_counts_opts(root_path: &str, opts: &Options) -> Option { let classifier = Classifier::new(); let mut counts = Counts::default(); - walk(root, &classifier, opts, &mut counts); + walk(root, root_path, &classifier, opts, &mut counts); Some(counts) } @@ -101,8 +106,17 @@ pub fn composition_raw_report_value(root_path: &str, opts: &Options) -> Option Option> { - let counts = composition_counts(root_path)?; - let report = build_json_report(&counts); + composition_report_flags(root_path, false) +} + +/// [`composition_report`] with the `--per-file` flag applied: `per_file` +/// enables the reference implementation's section enrichment +/// (`StaticService.enrichWithPerFileData` → `JSONReport.EnrichWithPerFileData` +/// over the `PerFileRetainer` snapshots — one `JSONFileEntry` per WALKED file, +/// non-UAST files included since composition is a raw-file analyzer). +#[must_use] +pub fn composition_report_flags(root_path: &str, per_file: bool) -> Option> { + let report = composition_report_value_flags(root_path, per_file)?; let bytes = Encoder::indented(" ") .with_trailing_newline(true) .encode_to_vec(&report); @@ -114,8 +128,52 @@ pub fn composition_report(root_path: &str) -> Option> { /// multi-analyzer static-JSON merge. `None` when the path cannot be walked. #[must_use] pub fn composition_report_value(root_path: &str) -> Option { + composition_report_value_flags(root_path, false) +} + +/// [`composition_report_value`] with the `--per-file` section enrichment. +/// +/// The reference per-file composition entry is DEGENERATE: the retained +/// per-file report is the raw-file classify result (`file_category`, no +/// `total_files`/`breakdown`), so `NewReportSection` reads every aggregate key +/// as zero — each entry carries the empty-report section values +/// (`No files analyzed`, all-zero metrics, info-only score) and only +/// `file_path` varies. Reproduced byte-for-byte. +#[must_use] +pub fn composition_report_value_flags(root_path: &str, per_file: bool) -> Option { let counts = composition_counts(root_path)?; - Some(build_json_report(&counts)) + let file_entries: Option> = if per_file { + Some( + counts + .files + .iter() + .map(|rel| build_file_entry(rel)) + .collect(), + ) + } else { + None + }; + Some(build_json_report_files(&counts, file_entries)) +} + +/// Builds one degenerate composition `renderer.JSONFileEntry` (see +/// [`composition_report_value_flags`]). +fn build_file_entry(rel_path: &str) -> GoValue { + let metrics = vec![ + metric(METRIC_TOTAL_FILES, "0"), + metric(METRIC_SOURCE, "0"), + metric(METRIC_SOURCE_PCT, &format_percent(0.0)), + ]; + + let mut entry = GoMap::new(MapOrigin::Struct); + entry.push("file_path", GoValue::Str(rel_path.to_string())); + entry.push("score_label", GoValue::Str(SCORE_LABEL_INFO.to_string())); + entry.push("status", GoValue::Str(STATUS_EMPTY.to_string())); + entry.push("metrics", GoValue::Array(metrics)); + // Distribution/issues are empty for the zero-value report. + entry.push("issues", GoValue::Array(Vec::new())); + entry.push("score", GoValue::Float(SCORE_INFO_ONLY)); + GoValue::Map(entry) } /// Builds the `static/composition --format bin` report bytes for `root_path`, @@ -190,7 +248,7 @@ fn build_raw_report(counts: &Counts) -> GoValue { /// Recursively walks `dir` in lexical order, mirroring `filepath.WalkDir`: /// directories are recursed (except `.git`), files are filtered through the /// path policy and classified. -fn walk(dir: &Path, classifier: &Classifier, opts: &Options, counts: &mut Counts) { +fn walk(dir: &Path, root_path: &str, classifier: &Classifier, opts: &Options, counts: &mut Counts) { let Ok(read) = fs::read_dir(dir) else { // Permission / not-exist errors are skipped. return; @@ -211,7 +269,7 @@ fn walk(dir: &Path, classifier: &Classifier, opts: &Options, counts: &mut Counts if super::should_skip_walk_dir(&entry.path(), &entry.file_name()) { continue; // filepath.SkipDir on .git } - walk(&path, classifier, opts, counts); + walk(&path, root_path, classifier, opts, counts); continue; } @@ -228,6 +286,19 @@ fn walk(dir: &Path, classifier: &Classifier, opts: &Options, counts: &mut Counts let category = classifier.classify(&path_str, &header); counts.total_files += 1; *counts.by_category.entry(category.as_str()).or_insert(0) += 1; + counts.files.push(make_relative_path(&path_str, root_path)); + } +} + +/// The reference `filepath.Rel(rootPath, filePath)` (flat repos → path under +/// the root) — the `StampSourceFile` path stamped onto per-file reports. +fn make_relative_path(file_path: &str, root_path: &str) -> String { + if root_path.is_empty() { + return file_path.to_string(); + } + match Path::new(file_path).strip_prefix(Path::new(root_path)) { + Ok(rel) => rel.to_string_lossy().into_owned(), + Err(_) => file_path.to_string(), } } @@ -253,15 +324,19 @@ fn read_header(path: &Path) -> Vec { /// Field order mirrors the reference structs exactly (struct-origin maps keep /// declaration order): `overall_score_label`, `sections`, `overall_score`; and /// per section `title`, `score_label`, `status`, `metrics`, `distribution` -/// (omitted when empty), `issues`, `score`. -fn build_json_report(counts: &Counts) -> GoValue { +/// (omitted when empty), `issues`, `score`. `file_entries` is the `--per-file` +/// enrichment (`None` without the flag). +fn build_json_report_files(counts: &Counts, file_entries: Option>) -> GoValue { // Single info-only section → overall score is ScoreInfoOnly (-1), label Info. let mut report = GoMap::new(MapOrigin::Struct); report.push( "overall_score_label", GoValue::Str(SCORE_LABEL_INFO.to_string()), ); - report.push("sections", GoValue::Array(vec![build_section(counts)])); + report.push( + "sections", + GoValue::Array(vec![build_section_files(counts, file_entries)]), + ); report.push("overall_score", GoValue::Float(SCORE_INFO_ONLY)); GoValue::Map(report) } @@ -269,7 +344,7 @@ fn build_json_report(counts: &Counts) -> GoValue { /// Builds the single composition `renderer.JSONSection` GoValue (reference: /// `SectionToJSON`). Field order mirrors the reference struct: `title`, `score_label`, /// `status`, `metrics`, `distribution` (omitted when empty), `issues`, `score`. -fn build_section(counts: &Counts) -> GoValue { +fn build_section_files(counts: &Counts, file_entries: Option>) -> GoValue { let total = counts.total_files; let source_count = counts.get(Category::Source); @@ -337,7 +412,12 @@ fn build_section(counts: &Counts) -> GoValue { section.push("distribution", GoValue::Array(dist_items)); } section.push("issues", GoValue::Array(issue_items)); - // `files` is a nil `*[]JSONFileEntry` (omitempty) → omitted (no per-file). + // --per-file enrichment: `files` sits between `issues` and `score` + // (renderer.JSONSection field order; a nil `*[]JSONFileEntry` — omitempty — + // without the flag). + if let Some(entries) = file_entries { + section.push("files", GoValue::Array(entries)); + } section.push("score", GoValue::Float(SCORE_INFO_ONLY)); GoValue::Map(section) @@ -369,3 +449,52 @@ fn severity_for(cat: Category) -> &'static str { _ => SEVERITY_INFO, } } + +#[cfg(test)] +mod per_file_tests { + use super::*; + + /// Fixture: one UAST-supported file and one plain-text file — composition + /// is a raw-file analyzer, so BOTH get per-file entries. + fn fixture() -> tempfile::TempDir { + let dir = tempfile::tempdir().expect("tempdir"); + std::fs::write(dir.path().join("a.go"), "package main\n\nfunc main() {}\n").unwrap(); + std::fs::write(dir.path().join("notes.txt"), "just text\n").unwrap(); + dir + } + + #[test] + fn per_file_flag_emits_files_entries() { + let dir = fixture(); + let bytes = composition_report_flags(dir.path().to_str().unwrap(), true).unwrap(); + let json = String::from_utf8(bytes).unwrap(); + assert!(json.contains("\"files\""), "files key missing:\n{json}"); + assert!( + json.contains("\"file_path\": \"a.go\""), + "a.go entry missing:\n{json}" + ); + // Raw-file analyzer: non-UAST files get entries too. + assert!( + json.contains("\"file_path\": \"notes.txt\""), + "notes.txt entry missing:\n{json}" + ); + // The per-file report lacks the aggregate keys, so every entry carries + // the reference empty-report section values. + assert!( + json.contains("\"status\": \"No files analyzed\""), + "degenerate per-file status missing:\n{json}" + ); + } + + #[test] + fn no_per_file_flag_omits_files() { + let dir = fixture(); + let bytes = composition_report_flags(dir.path().to_str().unwrap(), false).unwrap(); + let json = String::from_utf8(bytes).unwrap(); + assert!( + !json.contains("\"files\""), + "files key must be omitted:\n{json}" + ); + assert!(!json.contains("\"file_path\"")); + } +} diff --git a/crates/cf-commands/src/lib.rs b/crates/cf-commands/src/lib.rs index 09713fff..1da3b995 100644 --- a/crates/cf-commands/src/lib.rs +++ b/crates/cf-commands/src/lib.rs @@ -386,7 +386,9 @@ fn run_subcommand(sub: &clap::ArgMatches) -> i32 { .all(|a| handlers::is_static_id_or_glob(a)) && handlers::static_json_selects_multiple(&analyzer_strs) { - if let Some(bytes) = handlers::static_multi_json(&analyzer_strs, &ctx.path) { + if let Some(bytes) = + handlers::static_multi_json(&analyzer_strs, &ctx.path, ctx.matches.get_flag("per-file")) + { std::io::stdout().write_all(&bytes).expect("write stdout"); return 0; }