1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//! Smells command - Detect code smells
//!
//! Identifies common code smells like God Class, Long Method, etc.
//! Auto-routes through daemon when available for ~35x speedup.
use std::path::PathBuf;
use anyhow::Result;
use clap::Args;
use tldr_core::{
analyze_smells_aggregated_with_walker_opts, detect_smells_with_walker_opts, Language,
SmellType, SmellsReport, SmellsWalkerOpts, ThresholdPreset,
};
use crate::commands::daemon_router::{params_for_smells, try_daemon_route};
use crate::output::{format_smells_text, OutputFormat, OutputWriter};
/// Detect code smells
#[derive(Debug, Args)]
pub struct SmellsArgs {
/// Path to analyze (file or directory)
#[arg(default_value = ".")]
pub path: PathBuf,
/// Programming language to filter by (auto-detected if omitted)
#[arg(long, short = 'l')]
pub lang: Option<Language>,
/// Threshold preset
#[arg(long, short = 't', default_value = "default")]
pub threshold: ThresholdPresetArg,
/// Filter by smell type
#[arg(long, short = 's')]
pub smell_type: Option<SmellTypeArg>,
/// Include suggestions for fixing
#[arg(long)]
pub suggest: bool,
/// Deep analysis: aggregate findings from cohesion, coupling, dead code,
/// similarity, and cognitive complexity analyzers in addition to the
/// standard smell detectors
#[arg(long)]
pub deep: bool,
/// Walk vendored/build dirs (node_modules, target, dist, etc.) that would normally be skipped.
#[arg(long)]
pub no_default_ignore: bool,
/// Limit the scan to specific files (repeatable; EXACT-PATH-ONLY, no glob expansion).
/// Each entry is validated via `validate_file_path` (rejects path traversal /
/// non-existent files). When set, the path argument becomes a project-root
/// anchor for output ordering only and the walker is bypassed. Implies
/// `--include-tests` (caller picked the list).
#[arg(long)]
pub files: Vec<PathBuf>,
/// Include findings from test files. Default: test-file findings are excluded
/// (PR-review default). Implicit `true` when `--files` is non-empty.
#[arg(long)]
pub include_tests: bool,
}
/// CLI wrapper for threshold preset
#[derive(Debug, Clone, Copy, Default, clap::ValueEnum)]
pub enum ThresholdPresetArg {
/// Strict thresholds for high-quality codebases
Strict,
/// Default thresholds (recommended)
#[default]
Default,
/// Relaxed thresholds for legacy code
Relaxed,
}
impl From<ThresholdPresetArg> for ThresholdPreset {
fn from(arg: ThresholdPresetArg) -> Self {
match arg {
ThresholdPresetArg::Strict => ThresholdPreset::Strict,
ThresholdPresetArg::Default => ThresholdPreset::Default,
ThresholdPresetArg::Relaxed => ThresholdPreset::Relaxed,
}
}
}
/// CLI wrapper for smell type
#[derive(Debug, Clone, Copy, clap::ValueEnum)]
pub enum SmellTypeArg {
/// God Class (>20 methods or >500 LOC)
GodClass,
/// Long Method (>50 LOC or cyclomatic >10)
LongMethod,
/// Long Parameter List (>5 parameters)
LongParameterList,
/// Feature Envy
FeatureEnvy,
/// Data Clumps
DataClumps,
/// Low Cohesion (LCOM4 >= 2) -- requires --deep
LowCohesion,
/// Tight Coupling (score >= 0.6) -- requires --deep
TightCoupling,
/// Dead Code (unreachable functions) -- requires --deep
DeadCode,
/// Code Clone (similar functions) -- requires --deep
CodeClone,
/// High Cognitive Complexity (>= 15) -- requires --deep
HighCognitiveComplexity,
/// Deep Nesting (nesting depth >= 5)
DeepNesting,
/// Data Class (many fields, few/no methods)
DataClass,
/// Lazy Element (class with only 1 method and 0-1 fields)
LazyElement,
/// Message Chain (long method call chains > 3)
MessageChain,
/// Primitive Obsession (many primitive-typed parameters)
PrimitiveObsession,
/// Middle Man (>60% delegation) -- requires --deep
MiddleMan,
/// Refused Bequest (<33% inherited usage) -- requires --deep
RefusedBequest,
/// Inappropriate Intimacy (bidirectional coupling) -- requires --deep
InappropriateIntimacy,
}
impl From<SmellTypeArg> for SmellType {
fn from(arg: SmellTypeArg) -> Self {
match arg {
SmellTypeArg::GodClass => SmellType::GodClass,
SmellTypeArg::LongMethod => SmellType::LongMethod,
SmellTypeArg::LongParameterList => SmellType::LongParameterList,
SmellTypeArg::FeatureEnvy => SmellType::FeatureEnvy,
SmellTypeArg::DataClumps => SmellType::DataClumps,
SmellTypeArg::LowCohesion => SmellType::LowCohesion,
SmellTypeArg::TightCoupling => SmellType::TightCoupling,
SmellTypeArg::DeadCode => SmellType::DeadCode,
SmellTypeArg::CodeClone => SmellType::CodeClone,
SmellTypeArg::HighCognitiveComplexity => SmellType::HighCognitiveComplexity,
SmellTypeArg::DeepNesting => SmellType::DeepNesting,
SmellTypeArg::DataClass => SmellType::DataClass,
SmellTypeArg::LazyElement => SmellType::LazyElement,
SmellTypeArg::MessageChain => SmellType::MessageChain,
SmellTypeArg::PrimitiveObsession => SmellType::PrimitiveObsession,
SmellTypeArg::MiddleMan => SmellType::MiddleMan,
SmellTypeArg::RefusedBequest => SmellType::RefusedBequest,
SmellTypeArg::InappropriateIntimacy => SmellType::InappropriateIntimacy,
}
}
}
impl SmellsArgs {
/// Run the smells command
pub fn run(&self, format: OutputFormat, quiet: bool) -> Result<()> {
let writer = OutputWriter::new(format, quiet);
// BUG-11: validate path exists BEFORE any analysis. Without this
// check, a missing path silently slipped through: `is_dir()` returned
// false, the file branch ran with no files to scan, and the command
// returned exit 0 with empty results. Now: missing path => exit 1
// (matches `health`, `structure`, `deps`, `vuln`).
if !self.path.exists() {
anyhow::bail!("Path not found: {}", self.path.display());
}
// v0.2.3 (#1.D): when `--files` is non-empty, the caller explicitly named
// each path. Trust them and force `include_tests=true` so user-listed
// test files are not silently filtered.
let include_tests = self.include_tests || !self.files.is_empty();
// v0.2.3 (#1.D): each `--files` entry MUST go through the CORE
// validator (`tldr_core::validation::validate_file_path`) — same one
// the daemon uses. We pass the smells `path` argument as the project
// root so path-traversal attempts (`/etc/passwd`, `../../etc/...`)
// produce a hard error rather than a silent skip. Failures bubble up
// as a CLI error (non-zero exit), NOT a silent skip.
let project_root = if self.path.is_dir() {
// Try to canonicalize the path (so traversal checks work). Fall
// back to the literal path on canonicalize error (e.g. tmpdir
// shenanigans on macOS where /var -> /private/var).
dunce::canonicalize(&self.path).unwrap_or_else(|_| self.path.clone())
} else {
// For file paths, use the parent dir (or "." if none).
self.path
.parent()
.map(|p| dunce::canonicalize(p).unwrap_or_else(|_| p.to_path_buf()))
.unwrap_or_else(|| PathBuf::from("."))
};
let mut validated_files: Vec<PathBuf> = Vec::with_capacity(self.files.len());
for f in &self.files {
let f_str = f.to_str().ok_or_else(|| {
anyhow::anyhow!("--files entry contains non-UTF8 bytes: {:?}", f)
})?;
let canonical =
tldr_core::validation::validate_file_path(f_str, Some(&project_root))
.map_err(|e| anyhow::anyhow!("--files {}: {}", f.display(), e))?;
validated_files.push(canonical);
}
// Try daemon first for cached result
if let Some(report) = try_daemon_route::<SmellsReport>(
&self.path,
"smells",
params_for_smells(Some(&self.path), &validated_files, include_tests),
) {
// Output based on format
if writer.is_text() {
let text = format_smells_text(&report);
writer.write_text(&text)?;
return Ok(());
} else {
writer.write(&report)?;
return Ok(());
}
}
// determinism-and-stderr-hygiene-v1 (BUG-18): the M14
// (med-cleanup-bundle-v1) deep-only-smells hint used to be
// unconditionally written to stderr via `eprintln!`, which
// broke the JSON-mode contract (`tldr smells <path> 2>err >
// out.json` always produced a non-empty stderr stream).
//
// Relocate the same advisory into `SmellsReport.warnings` so
// BOTH JSON consumers (introspectable via `report.warnings[]`)
// AND text consumers (rendered to stdout by the text
// formatter — see `format_smells_text`) still see it. Skip
// injection when `--quiet`, when the user asked for a single
// smell type via `--smell-type` (warning would be misleading),
// or when `--deep` is set (the analyzers ARE running).
let deep_only_warning: Option<String> =
(!self.deep && !quiet && self.smell_type.is_none()).then(|| {
const DEEP_ONLY_SMELLS: &[&str] = &[
"low_cohesion",
"tight_coupling",
"dead_code",
"code_clone",
"high_cognitive_complexity",
"middle_man",
"refused_bequest",
"inappropriate_intimacy",
];
format!(
"Note: {} smell analyzers require --deep flag. Run with --deep for: {}",
DEEP_ONLY_SMELLS.len(),
DEEP_ONLY_SMELLS.join(", ")
)
});
// Fallback to direct compute
writer.progress(&format!(
"Scanning for code smells in {}{}...",
self.path.display(),
if self.deep { " (deep analysis)" } else { "" }
));
// Detect smells - use aggregated analysis when --deep is set
let walker_opts = SmellsWalkerOpts {
no_default_ignore: self.no_default_ignore,
lang: self.lang,
files: validated_files,
include_tests,
};
let mut report = if self.deep {
analyze_smells_aggregated_with_walker_opts(
&self.path,
self.threshold.into(),
self.smell_type.map(|s| s.into()),
self.suggest,
walker_opts,
)?
} else {
detect_smells_with_walker_opts(
&self.path,
self.threshold.into(),
self.smell_type.map(|s| s.into()),
self.suggest,
walker_opts,
)?
};
if let Some(msg) = deep_only_warning {
report.warnings.push(msg);
}
// Output based on format
if writer.is_text() {
let text = format_smells_text(&report);
writer.write_text(&text)?;
} else {
writer.write(&report)?;
}
Ok(())
}
}