1use std::path::{Path, PathBuf};
2
3use fallow_config::{EmailMode, OutputFormat};
4use fallow_core::results::AnalysisResults;
5use serde::Serialize;
6
7use crate::check::{CheckOptions, IssueFilters, TraceOptions};
8use crate::dupes::{DupesMode, DupesOptions};
9use crate::health::{HealthOptions, SortBy};
10use crate::health_types::EffortEstimate;
11use crate::report::ci::diff_filter::{DiffIndex, LoadedDiff, MAX_DIFF_BYTES};
12use crate::report::{build_duplication_json, build_health_json};
13
14pub const COMMON_ANALYSIS_OPTION_FLAGS: &[&str] = &[
15 "root",
16 "config",
17 "no-cache",
18 "threads",
19 "changed-since",
20 "diff-file",
21 "production",
22 "workspace",
23 "changed-workspaces",
24 "explain",
25 "legacy-envelope",
26];
27
28#[derive(Debug, Clone, Serialize)]
30pub struct ProgrammaticError {
31 pub message: String,
32 pub exit_code: u8,
33 pub code: Option<String>,
34 pub help: Option<String>,
35 pub context: Option<String>,
36}
37
38impl ProgrammaticError {
39 #[must_use]
40 pub fn new(message: impl Into<String>, exit_code: u8) -> Self {
41 Self {
42 message: message.into(),
43 exit_code,
44 code: None,
45 help: None,
46 context: None,
47 }
48 }
49
50 #[must_use]
51 pub fn with_help(mut self, help: impl Into<String>) -> Self {
52 self.help = Some(help.into());
53 self
54 }
55
56 #[must_use]
57 pub fn with_code(mut self, code: impl Into<String>) -> Self {
58 self.code = Some(code.into());
59 self
60 }
61
62 #[must_use]
63 pub fn with_context(mut self, context: impl Into<String>) -> Self {
64 self.context = Some(context.into());
65 self
66 }
67}
68
69impl std::fmt::Display for ProgrammaticError {
70 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71 write!(f, "{}", self.message)
72 }
73}
74
75impl std::error::Error for ProgrammaticError {}
76
77type ProgrammaticResult<T> = Result<T, ProgrammaticError>;
78
79#[derive(Debug, Clone, Default)]
81pub struct AnalysisOptions {
82 pub root: Option<PathBuf>,
83 pub config_path: Option<PathBuf>,
84 pub no_cache: bool,
85 pub threads: Option<usize>,
86 pub diff_file: Option<PathBuf>,
87 pub production: bool,
90 pub production_override: Option<bool>,
93 pub changed_since: Option<String>,
94 pub workspace: Option<Vec<String>>,
95 pub changed_workspaces: Option<String>,
96 pub explain: bool,
97 pub legacy_envelope: bool,
99}
100
101#[derive(Debug, Clone, Default)]
103pub struct DeadCodeFilters {
104 pub unused_files: bool,
105 pub unused_exports: bool,
106 pub unused_deps: bool,
107 pub unused_types: bool,
108 pub private_type_leaks: bool,
109 pub unused_enum_members: bool,
110 pub unused_class_members: bool,
111 pub unused_store_members: bool,
112 pub unprovided_injects: bool,
113 pub unrendered_components: bool,
114 pub unused_component_props: bool,
115 pub unused_component_emits: bool,
116 pub unused_component_inputs: bool,
117 pub unused_component_outputs: bool,
118 pub unused_svelte_events: bool,
119 pub unused_server_actions: bool,
120 pub unused_load_data_keys: bool,
121 pub unresolved_imports: bool,
122 pub unlisted_deps: bool,
123 pub duplicate_exports: bool,
124 pub circular_deps: bool,
125 pub re_export_cycles: bool,
126 pub boundary_violations: bool,
127 pub policy_violations: bool,
128 pub stale_suppressions: bool,
129 pub unused_catalog_entries: bool,
130 pub empty_catalog_groups: bool,
131 pub unresolved_catalog_references: bool,
132 pub unused_dependency_overrides: bool,
133 pub misconfigured_dependency_overrides: bool,
134}
135
136#[derive(Debug, Clone, Default)]
138pub struct DeadCodeOptions {
139 pub analysis: AnalysisOptions,
140 pub filters: DeadCodeFilters,
141 pub files: Vec<PathBuf>,
142 pub include_entry_exports: bool,
143}
144
145#[derive(Debug, Clone, Copy, Default)]
147pub enum DuplicationMode {
148 Strict,
149 #[default]
150 Mild,
151 Weak,
152 Semantic,
153}
154
155impl DuplicationMode {
156 const fn to_cli(self) -> DupesMode {
157 match self {
158 Self::Strict => DupesMode::Strict,
159 Self::Mild => DupesMode::Mild,
160 Self::Weak => DupesMode::Weak,
161 Self::Semantic => DupesMode::Semantic,
162 }
163 }
164}
165
166#[derive(Debug, Clone)]
168pub struct DuplicationOptions {
169 pub analysis: AnalysisOptions,
170 pub mode: DuplicationMode,
171 pub min_tokens: usize,
172 pub min_lines: usize,
173 pub min_occurrences: usize,
180 pub threshold: f64,
181 pub skip_local: bool,
182 pub cross_language: bool,
183 pub ignore_imports: Option<bool>,
187 pub top: Option<usize>,
188}
189
190impl Default for DuplicationOptions {
191 fn default() -> Self {
192 Self {
193 analysis: AnalysisOptions::default(),
194 mode: DuplicationMode::Mild,
195 min_tokens: 50,
196 min_lines: 5,
197 min_occurrences: 2,
198 threshold: 0.0,
199 skip_local: false,
200 cross_language: false,
201 ignore_imports: None,
202 top: None,
203 }
204 }
205}
206
207#[derive(Debug, Clone, Copy, Default)]
209pub enum ComplexitySort {
210 #[default]
211 Cyclomatic,
212 Cognitive,
213 Lines,
214 Severity,
215}
216
217impl ComplexitySort {
218 const fn to_cli(self) -> SortBy {
219 match self {
220 Self::Severity => SortBy::Severity,
221 Self::Cyclomatic => SortBy::Cyclomatic,
222 Self::Cognitive => SortBy::Cognitive,
223 Self::Lines => SortBy::Lines,
224 }
225 }
226}
227
228#[derive(Debug, Clone, Copy, Default)]
230pub enum OwnershipEmailMode {
231 Raw,
232 #[default]
233 Handle,
234 Anonymized,
235 Hash,
237}
238
239impl OwnershipEmailMode {
240 const fn to_config(self) -> EmailMode {
241 match self {
242 Self::Raw => EmailMode::Raw,
243 Self::Handle => EmailMode::Handle,
244 Self::Anonymized => EmailMode::Anonymized,
245 Self::Hash => EmailMode::Hash,
246 }
247 }
248}
249
250#[derive(Debug, Clone, Copy)]
252pub enum TargetEffort {
253 Low,
254 Medium,
255 High,
256}
257
258impl TargetEffort {
259 const fn to_cli(self) -> EffortEstimate {
260 match self {
261 Self::Low => EffortEstimate::Low,
262 Self::Medium => EffortEstimate::Medium,
263 Self::High => EffortEstimate::High,
264 }
265 }
266}
267
268#[derive(Debug, Clone, Default)]
270pub struct ComplexityOptions {
271 pub analysis: AnalysisOptions,
272 pub max_cyclomatic: Option<u16>,
273 pub max_cognitive: Option<u16>,
274 pub max_crap: Option<f64>,
275 pub top: Option<usize>,
276 pub sort: ComplexitySort,
277 pub complexity: bool,
278 pub file_scores: bool,
279 pub coverage_gaps: bool,
280 pub hotspots: bool,
281 pub ownership: bool,
282 pub ownership_emails: Option<OwnershipEmailMode>,
283 pub targets: bool,
284 pub css: bool,
285 pub effort: Option<TargetEffort>,
286 pub score: bool,
287 pub since: Option<String>,
288 pub min_commits: Option<u32>,
289 pub coverage: Option<PathBuf>,
290 pub coverage_root: Option<PathBuf>,
291}
292
293struct ResolvedAnalysisOptions {
294 root: PathBuf,
295 config_path: Option<PathBuf>,
296 no_cache: bool,
297 threads: usize,
298 pool: rayon::ThreadPool,
299 diff: Option<LoadedDiff>,
300 production_override: Option<bool>,
301 changed_since: Option<String>,
302 workspace: Option<Vec<String>>,
303 changed_workspaces: Option<String>,
304 explain: bool,
305 legacy_envelope: bool,
306}
307
308impl AnalysisOptions {
309 fn resolve(&self) -> ProgrammaticResult<ResolvedAnalysisOptions> {
310 validate_analysis_option_shape(self)?;
311 let root = resolve_analysis_root(self.root.as_deref())?;
312 validate_analysis_config_path(self.config_path.as_deref())?;
313
314 let threads = self.threads.unwrap_or_else(default_threads);
315 let pool = build_analysis_thread_pool(threads)?;
316 let diff = self
317 .diff_file
318 .as_deref()
319 .map(|path| load_explicit_diff_file(path, &root))
320 .transpose()?;
321 let production_override = self
322 .production_override
323 .or_else(|| self.production.then_some(true));
324
325 Ok(ResolvedAnalysisOptions {
326 root,
327 config_path: self.config_path.clone(),
328 no_cache: self.no_cache,
329 threads,
330 pool,
331 diff,
332 production_override,
333 changed_since: self.changed_since.clone(),
334 workspace: self.workspace.clone(),
335 changed_workspaces: self.changed_workspaces.clone(),
336 explain: self.explain,
337 legacy_envelope: self.legacy_envelope,
338 })
339 }
340}
341
342fn validate_analysis_option_shape(options: &AnalysisOptions) -> ProgrammaticResult<()> {
343 if options.threads == Some(0) {
344 return Err(
345 ProgrammaticError::new("`threads` must be greater than 0", 2)
346 .with_code("FALLOW_INVALID_THREADS")
347 .with_context("analysis.threads"),
348 );
349 }
350 if options.workspace.is_some() && options.changed_workspaces.is_some() {
351 return Err(ProgrammaticError::new(
352 "`workspace` and `changed_workspaces` are mutually exclusive",
353 2,
354 )
355 .with_code("FALLOW_MUTUALLY_EXCLUSIVE_OPTIONS")
356 .with_context("analysis.workspace"));
357 }
358
359 Ok(())
360}
361
362fn resolve_analysis_root(root: Option<&Path>) -> ProgrammaticResult<PathBuf> {
363 let root = match root {
364 Some(root) => root.to_path_buf(),
365 None => std::env::current_dir().map_err(|err| {
366 ProgrammaticError::new(
367 format!("failed to resolve current working directory: {err}"),
368 2,
369 )
370 .with_code("FALLOW_CWD_UNAVAILABLE")
371 .with_context("analysis.root")
372 })?,
373 };
374
375 if !root.exists() {
376 return Err(ProgrammaticError::new(
377 format!("analysis root does not exist: {}", root.display()),
378 2,
379 )
380 .with_code("FALLOW_INVALID_ROOT")
381 .with_context("analysis.root"));
382 }
383 if !root.is_dir() {
384 return Err(ProgrammaticError::new(
385 format!("analysis root is not a directory: {}", root.display()),
386 2,
387 )
388 .with_code("FALLOW_INVALID_ROOT")
389 .with_context("analysis.root"));
390 }
391
392 Ok(root)
393}
394
395fn validate_analysis_config_path(config_path: Option<&Path>) -> ProgrammaticResult<()> {
396 if let Some(config_path) = config_path
397 && !config_path.exists()
398 {
399 return Err(ProgrammaticError::new(
400 format!("config file does not exist: {}", config_path.display()),
401 2,
402 )
403 .with_code("FALLOW_INVALID_CONFIG_PATH")
404 .with_context("analysis.configPath"));
405 }
406
407 Ok(())
408}
409
410fn build_analysis_thread_pool(threads: usize) -> ProgrammaticResult<rayon::ThreadPool> {
411 crate::rayon_pool::build_thread_pool(threads).map_err(|err| {
412 ProgrammaticError::new(format!("failed to build analysis thread pool: {err}"), 2)
413 .with_code("FALLOW_THREAD_POOL_INIT_FAILED")
414 .with_context("analysis.threads")
415 })
416}
417
418impl ResolvedAnalysisOptions {
419 fn install<R: Send>(&self, f: impl FnOnce() -> R + Send) -> R {
420 self.pool.install(f)
421 }
422
423 fn diff_index(&self) -> Option<&DiffIndex> {
424 self.diff.as_ref().map(|loaded| &loaded.index)
425 }
426}
427
428fn default_threads() -> usize {
429 std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get)
430}
431
432fn load_explicit_diff_file(path: &Path, root: &Path) -> ProgrammaticResult<LoadedDiff> {
433 if path == Path::new("-") {
434 return Err(ProgrammaticError::new(
435 "`diff_file` does not support stdin; pass a file path",
436 2,
437 )
438 .with_code("FALLOW_INVALID_DIFF_FILE")
439 .with_context("analysis.diffFile"));
440 }
441
442 let abs = if crate::path_util::is_absolute_path_any_platform(path) {
443 path.to_path_buf()
444 } else {
445 root.join(path)
446 };
447
448 let meta = std::fs::metadata(&abs).map_err(|err| {
449 ProgrammaticError::new(
450 format!(
451 "diff file does not exist or cannot be read: {} ({err})",
452 abs.display()
453 ),
454 2,
455 )
456 .with_code("FALLOW_INVALID_DIFF_FILE")
457 .with_context("analysis.diffFile")
458 })?;
459 if !meta.is_file() {
460 return Err(ProgrammaticError::new(
461 format!("diff path is not a file: {}", abs.display()),
462 2,
463 )
464 .with_code("FALLOW_INVALID_DIFF_FILE")
465 .with_context("analysis.diffFile"));
466 }
467 if meta.len() > MAX_DIFF_BYTES {
468 return Err(ProgrammaticError::new(
469 format!(
470 "diff file is {} bytes, above the {MAX_DIFF_BYTES} byte limit: {}",
471 meta.len(),
472 abs.display()
473 ),
474 2,
475 )
476 .with_code("FALLOW_INVALID_DIFF_FILE")
477 .with_context("analysis.diffFile"));
478 }
479
480 let text = std::fs::read_to_string(&abs).map_err(|err| {
481 ProgrammaticError::new(
482 format!("failed to read diff file {}: {err}", abs.display()),
483 2,
484 )
485 .with_code("FALLOW_INVALID_DIFF_FILE")
486 .with_context("analysis.diffFile")
487 })?;
488
489 Ok(LoadedDiff {
490 index: DiffIndex::from_unified_diff(&text),
491 })
492}
493
494fn insert_meta(output: &mut serde_json::Value, meta: serde_json::Value) {
495 if let serde_json::Value::Object(map) = output {
496 let telemetry = map
497 .get("_meta")
498 .and_then(|existing| existing.get("telemetry"))
499 .cloned();
500 let mut meta = meta;
501 if let (Some(telemetry), Some(meta_map)) = (telemetry, meta.as_object_mut()) {
502 meta_map.insert("telemetry".to_string(), telemetry);
503 }
504 map.insert("_meta".to_string(), meta);
505 }
506}
507
508fn apply_programmatic_envelope_options(
509 output: &mut serde_json::Value,
510 resolved: &ResolvedAnalysisOptions,
511) {
512 if resolved.legacy_envelope {
513 crate::output_envelope::remove_root_kind(output);
514 }
515}
516
517fn build_dead_code_json(
518 results: &AnalysisResults,
519 root: &Path,
520 elapsed: std::time::Duration,
521 explain: bool,
522 config_fixable: bool,
523) -> ProgrammaticResult<serde_json::Value> {
524 let mut output =
525 crate::report::build_json_with_config_fixable(results, root, elapsed, config_fixable)
526 .map_err(|err| {
527 ProgrammaticError::new(format!("failed to serialize dead-code report: {err}"), 2)
528 .with_code("FALLOW_SERIALIZE_DEAD_CODE_REPORT")
529 .with_context("dead-code")
530 })?;
531 if explain {
532 insert_meta(&mut output, crate::explain::check_meta());
533 }
534 Ok(output)
537}
538
539fn to_issue_filters(filters: &DeadCodeFilters) -> IssueFilters {
540 IssueFilters {
541 unused_files: filters.unused_files,
542 unused_exports: filters.unused_exports,
543 unused_deps: filters.unused_deps,
544 unused_types: filters.unused_types,
545 private_type_leaks: filters.private_type_leaks,
546 unused_enum_members: filters.unused_enum_members,
547 unused_class_members: filters.unused_class_members,
548 unused_store_members: filters.unused_store_members,
549 unprovided_injects: filters.unprovided_injects,
550 unrendered_components: filters.unrendered_components,
551 unused_component_props: filters.unused_component_props,
552 unused_component_emits: filters.unused_component_emits,
553 unused_component_inputs: filters.unused_component_inputs,
554 unused_component_outputs: filters.unused_component_outputs,
555 unused_svelte_events: filters.unused_svelte_events,
556 unused_server_actions: filters.unused_server_actions,
557 unused_load_data_keys: filters.unused_load_data_keys,
558 unresolved_imports: filters.unresolved_imports,
559 unlisted_deps: filters.unlisted_deps,
560 duplicate_exports: filters.duplicate_exports,
561 circular_deps: filters.circular_deps,
562 re_export_cycles: filters.re_export_cycles,
563 boundary_violations: filters.boundary_violations,
564 policy_violations: filters.policy_violations,
565 stale_suppressions: filters.stale_suppressions,
566 unused_catalog_entries: filters.unused_catalog_entries,
567 empty_catalog_groups: filters.empty_catalog_groups,
568 unresolved_catalog_references: filters.unresolved_catalog_references,
569 unused_dependency_overrides: filters.unused_dependency_overrides,
570 misconfigured_dependency_overrides: filters.misconfigured_dependency_overrides,
571 invalid_client_exports: false,
574 mixed_client_server_barrels: false,
577 misplaced_directives: false,
580 route_collisions: false,
584 dynamic_segment_name_conflicts: false,
585 }
586}
587
588fn generic_analysis_error(command: &str) -> ProgrammaticError {
589 let code = format!(
590 "FALLOW_{}_FAILED",
591 command.replace('-', "_").to_ascii_uppercase()
592 );
593 ProgrammaticError::new(format!("{command} failed"), 2)
594 .with_code(code)
595 .with_context(format!("fallow {command}"))
596 .with_help(format!(
597 "Re-run `fallow {command} --format json --quiet` in the target project for CLI diagnostics"
598 ))
599}
600
601fn build_check_options<'a>(
602 resolved: &'a ResolvedAnalysisOptions,
603 options: &'a DeadCodeOptions,
604 filters: &'a IssueFilters,
605 trace_opts: &'a TraceOptions,
606) -> CheckOptions<'a> {
607 CheckOptions {
608 root: &resolved.root,
609 config_path: &resolved.config_path,
610 output: OutputFormat::Human,
611 no_cache: resolved.no_cache,
612 threads: resolved.threads,
613 quiet: true,
614 fail_on_issues: false,
615 filters,
616 changed_since: resolved.changed_since.as_deref(),
617 diff_index: resolved.diff_index(),
618 use_shared_diff_index: false,
619 baseline: None,
620 save_baseline: None,
621 sarif_file: None,
622 production: resolved.production_override.unwrap_or(false),
623 production_override: resolved.production_override,
624 workspace: resolved.workspace.as_deref(),
625 changed_workspaces: resolved.changed_workspaces.as_deref(),
626 group_by: None,
627 include_dupes: false,
628 trace_opts,
629 explain: resolved.explain,
630 top: None,
631 file: &options.files,
632 include_entry_exports: options.include_entry_exports,
633 summary: false,
634 regression_opts: crate::regression::RegressionOpts {
635 fail_on_regression: false,
636 tolerance: crate::regression::Tolerance::Absolute(0),
637 regression_baseline_file: None,
638 save_target: crate::regression::SaveRegressionTarget::None,
639 scoped: false,
640 quiet: true,
641 output: fallow_config::OutputFormat::Json,
642 },
643 retain_modules_for_health: false,
644 defer_performance: false,
645 }
646}
647
648fn filter_for_circular_dependencies(results: &AnalysisResults) -> AnalysisResults {
649 let mut filtered = results.clone();
650 filtered.unused_files.clear();
651 filtered.unused_exports.clear();
652 filtered.unused_types.clear();
653 filtered.private_type_leaks.clear();
654 filtered.unused_dependencies.clear();
655 filtered.unused_dev_dependencies.clear();
656 filtered.unused_optional_dependencies.clear();
657 filtered.unused_enum_members.clear();
658 filtered.unused_class_members.clear();
659 filtered.unused_store_members.clear();
660 filtered.unprovided_injects.clear();
661 filtered.unrendered_components.clear();
662 filtered.unused_component_props.clear();
663 filtered.unused_component_emits.clear();
664 filtered.unused_component_inputs.clear();
665 filtered.unused_component_outputs.clear();
666 filtered.unused_svelte_events.clear();
667 filtered.unused_server_actions.clear();
668 filtered.unused_load_data_keys.clear();
669 filtered.unresolved_imports.clear();
670 filtered.unlisted_dependencies.clear();
671 filtered.duplicate_exports.clear();
672 filtered.type_only_dependencies.clear();
673 filtered.test_only_dependencies.clear();
674 filtered.boundary_violations.clear();
675 filtered.boundary_coverage_violations.clear();
676 filtered.boundary_call_violations.clear();
677 filtered.policy_violations.clear();
678 filtered.stale_suppressions.clear();
679 filtered
680}
681
682fn filter_for_boundary_violations(results: &AnalysisResults) -> AnalysisResults {
683 let mut filtered = results.clone();
684 filtered.unused_files.clear();
685 filtered.unused_exports.clear();
686 filtered.unused_types.clear();
687 filtered.private_type_leaks.clear();
688 filtered.unused_dependencies.clear();
689 filtered.unused_dev_dependencies.clear();
690 filtered.unused_optional_dependencies.clear();
691 filtered.unused_enum_members.clear();
692 filtered.unused_class_members.clear();
693 filtered.unused_store_members.clear();
694 filtered.unprovided_injects.clear();
695 filtered.unrendered_components.clear();
696 filtered.unused_component_props.clear();
697 filtered.unused_component_emits.clear();
698 filtered.unused_component_inputs.clear();
699 filtered.unused_component_outputs.clear();
700 filtered.unused_svelte_events.clear();
701 filtered.unused_server_actions.clear();
702 filtered.unused_load_data_keys.clear();
703 filtered.unresolved_imports.clear();
704 filtered.unlisted_dependencies.clear();
705 filtered.duplicate_exports.clear();
706 filtered.type_only_dependencies.clear();
707 filtered.test_only_dependencies.clear();
708 filtered.circular_dependencies.clear();
709 filtered.stale_suppressions.clear();
710 filtered
711}
712
713pub fn detect_dead_code(options: &DeadCodeOptions) -> ProgrammaticResult<serde_json::Value> {
715 let resolved = options.analysis.resolve()?;
716 resolved.install(|| {
717 let filters = to_issue_filters(&options.filters);
718 let trace_opts = TraceOptions {
719 trace_export: None,
720 trace_file: None,
721 trace_dependency: None,
722 performance: false,
723 };
724 let check_options = build_check_options(&resolved, options, &filters, &trace_opts);
725 let result = crate::check::execute_check(&check_options)
726 .map_err(|_| generic_analysis_error("dead-code"))?;
727 let mut output = build_dead_code_json(
728 &result.results,
729 &result.config.root,
730 result.elapsed,
731 resolved.explain,
732 result.config_fixable,
733 )?;
734 apply_programmatic_envelope_options(&mut output, &resolved);
735 Ok(output)
736 })
737}
738
739pub fn detect_circular_dependencies(
742 options: &DeadCodeOptions,
743) -> ProgrammaticResult<serde_json::Value> {
744 let resolved = options.analysis.resolve()?;
745 resolved.install(|| {
746 let filters = to_issue_filters(&options.filters);
747 let trace_opts = TraceOptions {
748 trace_export: None,
749 trace_file: None,
750 trace_dependency: None,
751 performance: false,
752 };
753 let check_options = build_check_options(&resolved, options, &filters, &trace_opts);
754 let result = crate::check::execute_check(&check_options)
755 .map_err(|_| generic_analysis_error("dead-code"))?;
756 let filtered = filter_for_circular_dependencies(&result.results);
757 let mut output = build_dead_code_json(
758 &filtered,
759 &result.config.root,
760 result.elapsed,
761 resolved.explain,
762 result.config_fixable,
763 )?;
764 apply_programmatic_envelope_options(&mut output, &resolved);
765 Ok(output)
766 })
767}
768
769pub fn detect_boundary_violations(
773 options: &DeadCodeOptions,
774) -> ProgrammaticResult<serde_json::Value> {
775 let resolved = options.analysis.resolve()?;
776 resolved.install(|| {
777 let filters = to_issue_filters(&options.filters);
778 let trace_opts = TraceOptions {
779 trace_export: None,
780 trace_file: None,
781 trace_dependency: None,
782 performance: false,
783 };
784 let check_options = build_check_options(&resolved, options, &filters, &trace_opts);
785 let result = crate::check::execute_check(&check_options)
786 .map_err(|_| generic_analysis_error("dead-code"))?;
787 let filtered = filter_for_boundary_violations(&result.results);
788 let mut output = build_dead_code_json(
789 &filtered,
790 &result.config.root,
791 result.elapsed,
792 resolved.explain,
793 result.config_fixable,
794 )?;
795 apply_programmatic_envelope_options(&mut output, &resolved);
796 Ok(output)
797 })
798}
799
800pub fn detect_duplication(options: &DuplicationOptions) -> ProgrammaticResult<serde_json::Value> {
802 let resolved = options.analysis.resolve()?;
803 resolved.install(|| {
804 let dupes_options = DupesOptions {
805 root: &resolved.root,
806 config_path: &resolved.config_path,
807 output: OutputFormat::Human,
808 no_cache: resolved.no_cache,
809 threads: resolved.threads,
810 quiet: true,
811 mode: Some(options.mode.to_cli()),
812 min_tokens: Some(options.min_tokens),
813 min_lines: Some(options.min_lines),
814 min_occurrences: Some(options.min_occurrences),
815 threshold: Some(options.threshold),
816 skip_local: options.skip_local,
817 cross_language: options.cross_language,
818 ignore_imports: options.ignore_imports,
819 top: options.top,
820 baseline_path: None,
821 save_baseline_path: None,
822 production: resolved.production_override.unwrap_or(false),
823 production_override: resolved.production_override,
824 trace: None,
825 changed_since: resolved.changed_since.as_deref(),
826 diff_index: resolved.diff_index(),
827 use_shared_diff_index: false,
828 changed_files: None,
829 workspace: resolved.workspace.as_deref(),
830 changed_workspaces: resolved.changed_workspaces.as_deref(),
831 explain: resolved.explain,
832 explain_skipped: false,
833 summary: false,
834 group_by: None,
835 performance: false,
836 };
837 let result = crate::dupes::execute_dupes(&dupes_options)
838 .map_err(|_| generic_analysis_error("dupes"))?;
839 let mut output = build_duplication_json(
840 &result.report,
841 &result.config.root,
842 result.elapsed,
843 resolved.explain,
844 )
845 .map_err(|err| {
846 ProgrammaticError::new(format!("failed to serialize duplication report: {err}"), 2)
847 .with_code("FALLOW_SERIALIZE_DUPLICATION_REPORT")
848 .with_context("dupes")
849 })?;
850 apply_programmatic_envelope_options(&mut output, &resolved);
851 Ok(output)
852 })
853}
854
855fn build_complexity_options<'a>(
856 resolved: &'a ResolvedAnalysisOptions,
857 options: &'a ComplexityOptions,
858) -> HealthOptions<'a> {
859 let state = derived_complexity_options(options);
860
861 HealthOptions {
862 root: &resolved.root,
863 config_path: &resolved.config_path,
864 output: OutputFormat::Human,
865 no_cache: resolved.no_cache,
866 threads: resolved.threads,
867 quiet: true,
868 max_cyclomatic: options.max_cyclomatic,
869 max_cognitive: options.max_cognitive,
870 max_crap: options.max_crap,
871 top: options.top,
872 sort: options.sort.to_cli(),
873 production: resolved.production_override.unwrap_or(false),
874 production_override: resolved.production_override,
875 changed_since: resolved.changed_since.as_deref(),
876 diff_index: resolved.diff_index(),
877 use_shared_diff_index: false,
878 workspace: resolved.workspace.as_deref(),
879 changed_workspaces: resolved.changed_workspaces.as_deref(),
880 baseline: None,
881 save_baseline: None,
882 complexity: state.complexity,
883 complexity_breakdown: false,
884 file_scores: state.file_scores,
885 coverage_gaps: state.coverage_gaps,
886 config_activates_coverage_gaps: !state.any_section,
887 hotspots: state.hotspots,
888 ownership: state.ownership,
889 ownership_emails: options.ownership_emails.map(OwnershipEmailMode::to_config),
890 targets: state.targets,
891 css: options.css,
892 force_full: state.force_full,
893 score_only_output: state.score_only_output,
894 enforce_coverage_gap_gate: true,
895 effort: options.effort.map(TargetEffort::to_cli),
896 score: state.score,
897 min_score: None,
898 since: options.since.as_deref(),
899 min_commits: options.min_commits,
900 explain: resolved.explain,
901 summary: false,
902 save_snapshot: None,
903 trend: false,
904 group_by: None,
905 coverage: options.coverage.as_deref(),
906 coverage_root: options.coverage_root.as_deref(),
907 performance: false,
908 min_severity: None,
909 report_only: false,
910 runtime_coverage: None,
911 churn_file: None,
914 }
915}
916
917struct DerivedComplexityOptions {
918 any_section: bool,
919 complexity: bool,
920 file_scores: bool,
921 coverage_gaps: bool,
922 hotspots: bool,
923 ownership: bool,
924 targets: bool,
925 force_full: bool,
926 score_only_output: bool,
927 score: bool,
928}
929
930fn derived_complexity_options(options: &ComplexityOptions) -> DerivedComplexityOptions {
931 let ownership = options.ownership || options.ownership_emails.is_some();
932 let requested_hotspots = options.hotspots || ownership;
933 let requested_targets = options.targets || options.effort.is_some();
934 let any_section = options.complexity
935 || options.file_scores
936 || options.coverage_gaps
937 || requested_hotspots
938 || requested_targets
939 || options.score;
940 let score = if any_section { options.score } else { true };
941 let hotspots = if any_section {
942 requested_hotspots
943 } else {
944 true
945 };
946
947 DerivedComplexityOptions {
948 any_section,
949 complexity: if any_section {
950 options.complexity
951 } else {
952 true
953 },
954 file_scores: effective_file_scores(options, any_section, score),
955 coverage_gaps: if any_section {
956 options.coverage_gaps
957 } else {
958 false
959 },
960 hotspots,
961 ownership: ownership && hotspots,
962 targets: if any_section { requested_targets } else { true },
963 force_full: score,
964 score_only_output: is_score_only_output(options, requested_hotspots, requested_targets),
965 score,
966 }
967}
968
969fn effective_file_scores(options: &ComplexityOptions, any_section: bool, force_full: bool) -> bool {
970 (if any_section {
971 options.file_scores
972 } else {
973 true
974 }) || force_full
975}
976
977fn is_score_only_output(options: &ComplexityOptions, hotspots: bool, targets: bool) -> bool {
978 options.score
979 && !options.complexity
980 && !options.file_scores
981 && !options.coverage_gaps
982 && !hotspots
983 && !targets
984}
985
986pub fn compute_complexity(options: &ComplexityOptions) -> ProgrammaticResult<serde_json::Value> {
988 let resolved = options.analysis.resolve()?;
989 if let Some(path) = &options.coverage
990 && !path.exists()
991 {
992 return Err(ProgrammaticError::new(
993 format!("coverage path does not exist: {}", path.display()),
994 2,
995 )
996 .with_code("FALLOW_INVALID_COVERAGE_PATH")
997 .with_context("health.coverage"));
998 }
999 if let Err(message) =
1000 crate::health::scoring::validate_coverage_root_absolute(options.coverage_root.as_deref())
1001 {
1002 return Err(ProgrammaticError::new(message, 2)
1003 .with_code("FALLOW_INVALID_COVERAGE_ROOT")
1004 .with_context("health.coverage_root"));
1005 }
1006
1007 resolved.install(|| {
1008 let health_options = build_complexity_options(&resolved, options);
1009 let result = crate::health::execute_health(&health_options)
1010 .map_err(|_| generic_analysis_error("health"))?;
1011 let mut output = build_health_json(
1012 &result.report,
1013 &result.config.root,
1014 result.elapsed,
1015 resolved.explain,
1016 )
1017 .map_err(|err| {
1018 ProgrammaticError::new(format!("failed to serialize health report: {err}"), 2)
1019 .with_code("FALLOW_SERIALIZE_HEALTH_REPORT")
1020 .with_context("health")
1021 })?;
1022 apply_programmatic_envelope_options(&mut output, &resolved);
1023 Ok(output)
1024 })
1025}
1026
1027pub fn compute_health(options: &ComplexityOptions) -> ProgrammaticResult<serde_json::Value> {
1029 compute_complexity(options)
1030}
1031
1032#[cfg(test)]
1033mod tests {
1034 use super::*;
1035 use crate::report::test_helpers::sample_results;
1036 use std::process::Command;
1037
1038 const SHARED_DIFF_CHILD_ENV: &str = "FALLOW_PROGRAMMATIC_SHARED_DIFF_CHILD";
1039 const SHARED_DIFF_CHILD_TEST: &str =
1040 "programmatic::tests::programmatic_without_diff_file_ignores_shared_diff_cache";
1041
1042 #[test]
1043 fn circular_dependency_filter_clears_other_issue_types() {
1044 let root = PathBuf::from("/project");
1045 let results = sample_results(&root);
1046 let filtered = filter_for_circular_dependencies(&results);
1047 let json = build_dead_code_json(&filtered, &root, std::time::Duration::ZERO, false, false)
1048 .expect("should serialize");
1049
1050 assert_eq!(json["kind"], "dead-code");
1051 assert_eq!(json["circular_dependencies"].as_array().unwrap().len(), 1);
1052 assert_eq!(json["boundary_violations"].as_array().unwrap().len(), 0);
1053 assert_eq!(json["unused_files"].as_array().unwrap().len(), 0);
1054 assert_eq!(json["summary"]["total_issues"], serde_json::Value::from(1));
1055 }
1056
1057 #[test]
1058 fn boundary_violation_filter_clears_other_issue_types() {
1059 let root = PathBuf::from("/project");
1060 let results = sample_results(&root);
1061 let filtered = filter_for_boundary_violations(&results);
1062 let json = build_dead_code_json(&filtered, &root, std::time::Duration::ZERO, false, false)
1063 .expect("should serialize");
1064
1065 assert_eq!(json["kind"], "dead-code");
1066 assert_eq!(json["boundary_violations"].as_array().unwrap().len(), 1);
1067 assert_eq!(json["circular_dependencies"].as_array().unwrap().len(), 0);
1068 assert_eq!(json["unused_exports"].as_array().unwrap().len(), 0);
1069 assert_eq!(json["summary"]["total_issues"], serde_json::Value::from(1));
1070 }
1071
1072 #[test]
1073 fn dead_code_without_production_override_uses_per_analysis_config() {
1074 let dir = tempfile::tempdir().expect("temp dir");
1075 let root = dir.path();
1076 std::fs::create_dir_all(root.join("src")).unwrap();
1077 std::fs::write(
1078 root.join("package.json"),
1079 r#"{"name":"programmatic-production","main":"src/index.ts"}"#,
1080 )
1081 .unwrap();
1082 std::fs::write(root.join("src/index.ts"), "export const ok = 1;\n").unwrap();
1083 std::fs::write(root.join("src/utils.test.ts"), "export const dead = 1;\n").unwrap();
1084 std::fs::write(
1085 root.join(".fallowrc.json"),
1086 r#"{"production":{"deadCode":true,"health":false,"dupes":false}}"#,
1087 )
1088 .unwrap();
1089
1090 let options = DeadCodeOptions {
1091 analysis: AnalysisOptions {
1092 root: Some(root.to_path_buf()),
1093 ..AnalysisOptions::default()
1094 },
1095 ..DeadCodeOptions::default()
1096 };
1097 let json = detect_dead_code(&options).expect("analysis should succeed");
1098 let paths = unused_file_paths(&json);
1099
1100 assert!(
1101 !paths.iter().any(|path| path.ends_with("utils.test.ts")),
1102 "omitted production option should defer to production.deadCode=true config: {paths:?}"
1103 );
1104 }
1105
1106 #[test]
1107 fn dead_code_legacy_envelope_removes_root_kind() {
1108 let dir = tempfile::tempdir().expect("temp dir");
1109 let root = dir.path();
1110 std::fs::create_dir_all(root.join("src")).unwrap();
1111 std::fs::write(
1112 root.join("package.json"),
1113 r#"{"name":"programmatic-legacy","main":"src/index.ts"}"#,
1114 )
1115 .unwrap();
1116 std::fs::write(root.join("src/index.ts"), "export const ok = 1;\n").unwrap();
1117
1118 let options = DeadCodeOptions {
1119 analysis: AnalysisOptions {
1120 root: Some(root.to_path_buf()),
1121 legacy_envelope: true,
1122 ..AnalysisOptions::default()
1123 },
1124 ..DeadCodeOptions::default()
1125 };
1126 let json = detect_dead_code(&options).expect("analysis should succeed");
1127
1128 assert!(json.get("kind").is_none());
1129 assert_eq!(json["schema_version"], crate::report::SCHEMA_VERSION);
1130 }
1131
1132 #[test]
1133 fn dead_code_explicit_production_false_overrides_config() {
1134 let dir = tempfile::tempdir().expect("temp dir");
1135 let root = dir.path();
1136 std::fs::create_dir_all(root.join("src")).unwrap();
1137 std::fs::write(
1138 root.join("package.json"),
1139 r#"{"name":"programmatic-production","main":"src/index.ts"}"#,
1140 )
1141 .unwrap();
1142 std::fs::write(root.join("src/index.ts"), "export const ok = 1;\n").unwrap();
1143 std::fs::write(root.join("src/utils.test.ts"), "export const dead = 1;\n").unwrap();
1144 std::fs::write(
1145 root.join(".fallowrc.json"),
1146 r#"{"production":{"deadCode":true,"health":false,"dupes":false}}"#,
1147 )
1148 .unwrap();
1149
1150 let options = DeadCodeOptions {
1151 analysis: AnalysisOptions {
1152 root: Some(root.to_path_buf()),
1153 production_override: Some(false),
1154 ..AnalysisOptions::default()
1155 },
1156 ..DeadCodeOptions::default()
1157 };
1158 let json = detect_dead_code(&options).expect("analysis should succeed");
1159 let paths = unused_file_paths(&json);
1160
1161 assert!(
1162 paths.iter().any(|path| path.ends_with("utils.test.ts")),
1163 "explicit production=false should include test files despite config: {paths:?}"
1164 );
1165 }
1166
1167 #[test]
1168 fn analysis_resolve_uses_per_call_thread_pool() {
1169 let dir = tempfile::tempdir().expect("temp dir");
1170 let root = dir.path();
1171
1172 let one = AnalysisOptions {
1173 root: Some(root.to_path_buf()),
1174 threads: Some(1),
1175 ..AnalysisOptions::default()
1176 }
1177 .resolve()
1178 .expect("one-thread options should resolve");
1179 let two = AnalysisOptions {
1180 root: Some(root.to_path_buf()),
1181 threads: Some(2),
1182 ..AnalysisOptions::default()
1183 }
1184 .resolve()
1185 .expect("two-thread options should resolve");
1186
1187 assert_eq!(one.install(rayon::current_num_threads), 1);
1188 assert_eq!(two.install(rayon::current_num_threads), 2);
1189 }
1190
1191 #[test]
1192 fn explicit_diff_file_scopes_dead_code_per_call() {
1193 let dir = tempfile::tempdir().expect("temp dir");
1194 let root = dir.path();
1195 std::fs::create_dir_all(root.join("src")).unwrap();
1196 std::fs::write(
1197 root.join("package.json"),
1198 r#"{"name":"programmatic-diff","main":"src/index.ts"}"#,
1199 )
1200 .unwrap();
1201 std::fs::write(
1202 root.join("src/index.ts"),
1203 "import { used } from './used';\nimport './a';\nimport './b';\nconsole.log(used);\n",
1204 )
1205 .unwrap();
1206 std::fs::write(root.join("src/used.ts"), "export const used = 1;\n").unwrap();
1207 std::fs::write(root.join("src/a.ts"), "export const deadA = 1;\n").unwrap();
1208 std::fs::write(root.join("src/b.ts"), "export const deadB = 1;\n").unwrap();
1209 std::fs::write(
1210 root.join("a.diff"),
1211 diff_for("src/a.ts", "export const deadA = 1;\n"),
1212 )
1213 .unwrap();
1214 std::fs::write(
1215 root.join("b.diff"),
1216 diff_for("src/b.ts", "export const deadB = 1;\n"),
1217 )
1218 .unwrap();
1219
1220 let filters = DeadCodeFilters {
1221 unused_exports: true,
1222 ..DeadCodeFilters::default()
1223 };
1224
1225 let a_json = detect_dead_code(&DeadCodeOptions {
1226 analysis: AnalysisOptions {
1227 root: Some(root.to_path_buf()),
1228 diff_file: Some(PathBuf::from("a.diff")),
1229 ..AnalysisOptions::default()
1230 },
1231 filters: filters.clone(),
1232 ..DeadCodeOptions::default()
1233 })
1234 .expect("a-scoped analysis should succeed");
1235 let b_json = detect_dead_code(&DeadCodeOptions {
1236 analysis: AnalysisOptions {
1237 root: Some(root.to_path_buf()),
1238 diff_file: Some(PathBuf::from("b.diff")),
1239 ..AnalysisOptions::default()
1240 },
1241 filters,
1242 ..DeadCodeOptions::default()
1243 })
1244 .expect("b-scoped analysis should succeed");
1245
1246 assert_eq!(unused_export_names(&a_json), vec!["deadA"]);
1247 assert_eq!(unused_export_names(&b_json), vec!["deadB"]);
1248 }
1249
1250 #[test]
1251 fn programmatic_without_diff_file_ignores_shared_diff_cache() {
1252 if std::env::var_os(SHARED_DIFF_CHILD_ENV).is_some() {
1253 run_programmatic_shared_diff_child();
1254 return;
1255 }
1256
1257 let current_exe = std::env::current_exe().expect("current test binary should be known");
1258 let output = Command::new(current_exe)
1259 .arg("--exact")
1260 .arg(SHARED_DIFF_CHILD_TEST)
1261 .arg("--nocapture")
1262 .env(SHARED_DIFF_CHILD_ENV, "1")
1263 .output()
1264 .expect("shared diff child should start");
1265
1266 assert!(
1267 output.status.success(),
1268 "shared diff child failed with status {:?}\nstdout:\n{}\nstderr:\n{}",
1269 output.status.code(),
1270 String::from_utf8_lossy(&output.stdout),
1271 String::from_utf8_lossy(&output.stderr)
1272 );
1273 }
1274
1275 fn run_programmatic_shared_diff_child() {
1276 let dir = tempfile::tempdir().expect("temp dir");
1277 let root = dir.path();
1278 std::fs::create_dir_all(root.join("src")).unwrap();
1279 std::fs::write(
1280 root.join("package.json"),
1281 r#"{"name":"programmatic-shared-diff","main":"src/index.ts"}"#,
1282 )
1283 .unwrap();
1284 std::fs::write(
1285 root.join("src/index.ts"),
1286 "import { used } from './used';\nimport './a';\nimport './b';\nconsole.log(used);\n",
1287 )
1288 .unwrap();
1289 std::fs::write(root.join("src/used.ts"), "export const used = 1;\n").unwrap();
1290 std::fs::write(root.join("src/a.ts"), "export const deadA = 1;\n").unwrap();
1291 std::fs::write(root.join("src/b.ts"), "export const deadB = 1;\n").unwrap();
1292 std::fs::write(
1293 root.join("a.diff"),
1294 diff_for("src/a.ts", "export const deadA = 1;\n"),
1295 )
1296 .unwrap();
1297
1298 let source = crate::report::ci::diff_filter::DiffSource::Flag(root.join("a.diff"));
1299 let loaded = crate::report::ci::diff_filter::init_shared_diff(Some(&source), true);
1300 assert!(loaded.is_some(), "shared diff should load in child process");
1301
1302 let json = detect_dead_code(&DeadCodeOptions {
1303 analysis: AnalysisOptions {
1304 root: Some(root.to_path_buf()),
1305 ..AnalysisOptions::default()
1306 },
1307 filters: DeadCodeFilters {
1308 unused_exports: true,
1309 ..DeadCodeFilters::default()
1310 },
1311 ..DeadCodeOptions::default()
1312 })
1313 .expect("analysis without explicit diff should succeed");
1314
1315 assert_eq!(unused_export_names(&json), vec!["deadA", "deadB"]);
1316 }
1317
1318 #[test]
1319 fn explicit_diff_file_rejects_stdin_sentinel() {
1320 let dir = tempfile::tempdir().expect("temp dir");
1321 let Err(error) = AnalysisOptions {
1322 root: Some(dir.path().to_path_buf()),
1323 diff_file: Some(PathBuf::from("-")),
1324 ..AnalysisOptions::default()
1325 }
1326 .resolve() else {
1327 panic!("stdin sentinel is not part of the programmatic API");
1328 };
1329
1330 assert_eq!(error.code.as_deref(), Some("FALLOW_INVALID_DIFF_FILE"));
1331 assert_eq!(error.context.as_deref(), Some("analysis.diffFile"));
1332 }
1333
1334 fn tiny_project() -> tempfile::TempDir {
1336 let dir = tempfile::tempdir().expect("temp dir");
1337 let root = dir.path();
1338 std::fs::create_dir_all(root.join("src")).unwrap();
1339 std::fs::write(
1340 root.join("package.json"),
1341 r#"{"name":"prog-e2e","main":"src/index.ts"}"#,
1342 )
1343 .unwrap();
1344 std::fs::write(
1345 root.join("src/index.ts"),
1346 "export const ok = 1;\nconsole.log(ok);\n",
1347 )
1348 .unwrap();
1349 dir
1350 }
1351
1352 fn analysis_at(root: &Path) -> AnalysisOptions {
1353 AnalysisOptions {
1354 root: Some(root.to_path_buf()),
1355 ..AnalysisOptions::default()
1356 }
1357 }
1358
1359 #[test]
1360 fn resolve_rejects_zero_threads() {
1361 let err = AnalysisOptions {
1362 threads: Some(0),
1363 ..AnalysisOptions::default()
1364 }
1365 .resolve()
1366 .err()
1367 .expect("zero threads must be rejected");
1368 assert_eq!(err.exit_code, 2);
1369 assert_eq!(err.code.as_deref(), Some("FALLOW_INVALID_THREADS"));
1370 assert_eq!(err.context.as_deref(), Some("analysis.threads"));
1371 }
1372
1373 #[test]
1374 fn resolve_rejects_mutually_exclusive_workspace_flags() {
1375 let err = AnalysisOptions {
1376 workspace: Some(vec!["packages/*".to_owned()]),
1377 changed_workspaces: Some("HEAD~1".to_owned()),
1378 ..AnalysisOptions::default()
1379 }
1380 .resolve()
1381 .err()
1382 .expect("workspace + changed_workspaces must be rejected");
1383 assert_eq!(
1384 err.code.as_deref(),
1385 Some("FALLOW_MUTUALLY_EXCLUSIVE_OPTIONS")
1386 );
1387 assert_eq!(err.context.as_deref(), Some("analysis.workspace"));
1388 }
1389
1390 #[test]
1391 fn resolve_rejects_nonexistent_root() {
1392 let err = AnalysisOptions {
1393 root: Some(PathBuf::from("/definitely/not/a/real/path/xyzzy")),
1394 ..AnalysisOptions::default()
1395 }
1396 .resolve()
1397 .err()
1398 .expect("nonexistent root must be rejected");
1399 assert_eq!(err.code.as_deref(), Some("FALLOW_INVALID_ROOT"));
1400 assert_eq!(err.context.as_deref(), Some("analysis.root"));
1401 }
1402
1403 #[test]
1404 fn resolve_rejects_root_that_is_a_file() {
1405 let dir = tempfile::tempdir().expect("temp dir");
1406 let file = dir.path().join("not-a-dir.txt");
1407 std::fs::write(&file, "x").unwrap();
1408 let err = AnalysisOptions {
1409 root: Some(file),
1410 ..AnalysisOptions::default()
1411 }
1412 .resolve()
1413 .err()
1414 .expect("a file root must be rejected");
1415 assert_eq!(err.code.as_deref(), Some("FALLOW_INVALID_ROOT"));
1416 }
1417
1418 #[test]
1419 fn resolve_rejects_nonexistent_config_path() {
1420 let dir = tempfile::tempdir().expect("temp dir");
1421 let err = AnalysisOptions {
1422 root: Some(dir.path().to_path_buf()),
1423 config_path: Some(dir.path().join("missing.fallowrc.json")),
1424 ..AnalysisOptions::default()
1425 }
1426 .resolve()
1427 .err()
1428 .expect("nonexistent config must be rejected");
1429 assert_eq!(err.code.as_deref(), Some("FALLOW_INVALID_CONFIG_PATH"));
1430 assert_eq!(err.context.as_deref(), Some("analysis.configPath"));
1431 }
1432
1433 #[test]
1434 fn resolve_rejects_missing_diff_file() {
1435 let dir = tempfile::tempdir().expect("temp dir");
1436 let err = AnalysisOptions {
1437 root: Some(dir.path().to_path_buf()),
1438 diff_file: Some(PathBuf::from("nope.diff")),
1439 ..AnalysisOptions::default()
1440 }
1441 .resolve()
1442 .err()
1443 .expect("missing diff file must be rejected");
1444 assert_eq!(err.code.as_deref(), Some("FALLOW_INVALID_DIFF_FILE"));
1445 assert_eq!(err.context.as_deref(), Some("analysis.diffFile"));
1446 }
1447
1448 #[test]
1449 fn resolve_rejects_diff_path_that_is_a_directory() {
1450 let dir = tempfile::tempdir().expect("temp dir");
1451 std::fs::create_dir_all(dir.path().join("a-dir")).unwrap();
1452 let err = AnalysisOptions {
1453 root: Some(dir.path().to_path_buf()),
1454 diff_file: Some(PathBuf::from("a-dir")),
1455 ..AnalysisOptions::default()
1456 }
1457 .resolve()
1458 .err()
1459 .expect("a directory diff path must be rejected");
1460 assert_eq!(err.code.as_deref(), Some("FALLOW_INVALID_DIFF_FILE"));
1461 }
1462
1463 #[test]
1464 fn detect_circular_dependencies_returns_dead_code_envelope() {
1465 let project = tiny_project();
1466 let json = detect_circular_dependencies(&DeadCodeOptions {
1467 analysis: analysis_at(project.path()),
1468 ..DeadCodeOptions::default()
1469 })
1470 .expect("circular-dependency analysis should succeed");
1471 assert_eq!(json["kind"], "dead-code");
1472 assert!(json["circular_dependencies"].is_array());
1473 }
1474
1475 #[test]
1476 fn detect_boundary_violations_returns_dead_code_envelope() {
1477 let project = tiny_project();
1478 let json = detect_boundary_violations(&DeadCodeOptions {
1479 analysis: analysis_at(project.path()),
1480 ..DeadCodeOptions::default()
1481 })
1482 .expect("boundary-violation analysis should succeed");
1483 assert_eq!(json["kind"], "dead-code");
1484 assert!(json["boundary_violations"].is_array());
1485 }
1486
1487 #[test]
1488 fn detect_boundary_violations_includes_boundary_coverage() {
1489 let project = tiny_project();
1490 let root = project.path();
1491 std::fs::write(
1492 root.join(".fallowrc.json"),
1493 r#"{
1494 "boundaries": {
1495 "zones": [
1496 { "name": "domain", "patterns": ["src/domain/**"] }
1497 ],
1498 "coverage": { "requireAllFiles": true }
1499 }
1500 }"#,
1501 )
1502 .unwrap();
1503
1504 let json = detect_boundary_violations(&DeadCodeOptions {
1505 analysis: analysis_at(root),
1506 ..DeadCodeOptions::default()
1507 })
1508 .expect("boundary-violation analysis should succeed");
1509
1510 let coverage = json["boundary_coverage_violations"]
1511 .as_array()
1512 .expect("coverage findings should be an array");
1513 assert_eq!(coverage.len(), 1);
1514 assert_eq!(coverage[0]["path"], "src/index.ts");
1515 assert_eq!(json["summary"]["boundary_coverage_violations"], 1);
1516 }
1517
1518 #[test]
1519 fn detect_boundary_violations_includes_boundary_calls() {
1520 let project = tiny_project();
1521 let root = project.path();
1522 std::fs::write(
1523 root.join("src/index.ts"),
1524 "console.log('hello');\nexport const x = 1;\n",
1525 )
1526 .unwrap();
1527 std::fs::write(
1528 root.join(".fallowrc.json"),
1529 r#"{
1530 "boundaries": {
1531 "zones": [
1532 { "name": "domain", "patterns": ["src/**"] }
1533 ],
1534 "calls": {
1535 "forbidden": [
1536 { "from": "domain", "callee": "console.*" }
1537 ]
1538 }
1539 }
1540 }"#,
1541 )
1542 .unwrap();
1543
1544 let json = detect_boundary_violations(&DeadCodeOptions {
1545 analysis: analysis_at(root),
1546 ..DeadCodeOptions::default()
1547 })
1548 .expect("boundary-violation analysis should succeed");
1549
1550 let calls = json["boundary_call_violations"]
1551 .as_array()
1552 .expect("boundary call findings should be an array");
1553 assert_eq!(calls.len(), 1);
1554 assert_eq!(calls[0]["path"], "src/index.ts");
1555 assert_eq!(calls[0]["zone"], "domain");
1556 assert_eq!(calls[0]["callee"], "console.log");
1557 assert_eq!(calls[0]["pattern"], "console.*");
1558 assert_eq!(json["summary"]["boundary_call_violations"], 1);
1559 }
1560
1561 #[test]
1562 fn detect_duplication_returns_dupes_envelope() {
1563 let project = tiny_project();
1564 let json = detect_duplication(&DuplicationOptions {
1565 analysis: analysis_at(project.path()),
1566 ..DuplicationOptions::default()
1567 })
1568 .expect("duplication analysis should succeed");
1569 assert_eq!(json["kind"], "dupes");
1570 assert!(json["clone_groups"].is_array());
1572 assert!(json["stats"].is_object());
1573 }
1574
1575 #[test]
1576 fn compute_health_returns_health_envelope() {
1577 let project = tiny_project();
1578 let options = ComplexityOptions {
1579 analysis: analysis_at(project.path()),
1580 ..ComplexityOptions::default()
1581 };
1582 let json = compute_health(&options).expect("health analysis should succeed");
1584 assert_eq!(json["kind"], "health");
1585 assert!(json["summary"].is_object());
1587 assert!(json["findings"].is_array());
1588 }
1589
1590 #[test]
1591 fn compute_health_css_option_returns_css_analytics() {
1592 let project = tempfile::tempdir().expect("temp dir");
1593 let root = project.path();
1594 std::fs::create_dir_all(root.join("src")).unwrap();
1595 std::fs::write(
1596 root.join("package.json"),
1597 r#"{"name":"prog-css","main":"src/index.ts","dependencies":{"tailwindcss":"4.0.0"}}"#,
1598 )
1599 .unwrap();
1600 std::fs::write(
1601 root.join("src/index.ts"),
1602 "import './style.css';\nexport const ok = true;\n",
1603 )
1604 .unwrap();
1605 std::fs::write(
1606 root.join("src/style.css"),
1607 r"
1608@theme {
1609 --color-brand: #0055cc;
1610}
1611
1612.used { color: var(--color-brand); }
1613",
1614 )
1615 .unwrap();
1616
1617 let json = compute_health(&ComplexityOptions {
1618 analysis: analysis_at(root),
1619 css: true,
1620 ..ComplexityOptions::default()
1621 })
1622 .expect("CSS health analysis should succeed");
1623
1624 assert_eq!(json["kind"], "health");
1625 assert!(json["css_analytics"].is_object());
1626 }
1627
1628 #[test]
1629 fn compute_complexity_rejects_missing_coverage_path() {
1630 let project = tiny_project();
1631 let err = compute_complexity(&ComplexityOptions {
1632 analysis: analysis_at(project.path()),
1633 coverage: Some(project.path().join("missing-coverage.json")),
1634 ..ComplexityOptions::default()
1635 })
1636 .expect_err("a missing coverage path must be rejected");
1637 assert_eq!(err.code.as_deref(), Some("FALLOW_INVALID_COVERAGE_PATH"));
1638 assert_eq!(err.context.as_deref(), Some("health.coverage"));
1639 }
1640
1641 #[test]
1642 fn compute_complexity_rejects_relative_coverage_root() {
1643 let project = tiny_project();
1644 let err = compute_complexity(&ComplexityOptions {
1645 analysis: analysis_at(project.path()),
1646 coverage_root: Some(PathBuf::from("relative/prefix")),
1647 ..ComplexityOptions::default()
1648 })
1649 .expect_err("a relative coverage_root must be rejected");
1650 assert_eq!(err.code.as_deref(), Some("FALLOW_INVALID_COVERAGE_ROOT"));
1651 assert_eq!(err.context.as_deref(), Some("health.coverage_root"));
1652 }
1653
1654 #[test]
1655 fn programmatic_error_builders_compose_and_display() {
1656 let err = ProgrammaticError::new("boom", 7)
1657 .with_code("FALLOW_X")
1658 .with_help("try again")
1659 .with_context("ctx.path");
1660 assert_eq!(err.message, "boom");
1661 assert_eq!(err.exit_code, 7);
1662 assert_eq!(err.code.as_deref(), Some("FALLOW_X"));
1663 assert_eq!(err.help.as_deref(), Some("try again"));
1664 assert_eq!(err.context.as_deref(), Some("ctx.path"));
1665 assert_eq!(format!("{err}"), "boom");
1667 }
1668
1669 #[test]
1670 fn generic_analysis_error_uppercases_command_into_code() {
1671 let err = generic_analysis_error("dead-code");
1672 assert_eq!(err.code.as_deref(), Some("FALLOW_DEAD_CODE_FAILED"));
1673 assert_eq!(err.exit_code, 2);
1674 assert_eq!(err.context.as_deref(), Some("fallow dead-code"));
1675 assert!(err.help.is_some(), "diagnostics hint should be attached");
1676 }
1677
1678 fn unused_file_paths(json: &serde_json::Value) -> Vec<String> {
1679 json["unused_files"]
1680 .as_array()
1681 .unwrap()
1682 .iter()
1683 .filter_map(|file| file["path"].as_str())
1684 .map(str::to_owned)
1685 .collect()
1686 }
1687
1688 fn unused_export_names(json: &serde_json::Value) -> Vec<String> {
1689 let mut names: Vec<String> = json["unused_exports"]
1690 .as_array()
1691 .unwrap()
1692 .iter()
1693 .filter_map(|export| export["export_name"].as_str())
1694 .map(str::to_owned)
1695 .collect();
1696 names.sort();
1697 names
1698 }
1699
1700 fn diff_for(path: &str, line: &str) -> String {
1701 format!("diff --git a/{path} b/{path}\n--- /dev/null\n+++ b/{path}\n@@ -0,0 +1 @@\n+{line}")
1702 }
1703}