dart-decimate 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +572 -0
- package/Cargo.toml +41 -0
- package/LICENSE +21 -0
- package/README.md +617 -0
- package/assets/dart-decimate-banner.png +0 -0
- package/npm/bin/dart-decimate-mcp.js +48 -0
- package/npm/bin/dart-decimate.js +48 -0
- package/npm/scripts/postinstall.js +46 -0
- package/npm/scripts/test-npx-local.js +54 -0
- package/npm/scripts/test-npx-mcp-local.js +56 -0
- package/package.json +45 -0
- package/src/baseline.rs +643 -0
- package/src/bin/dart-decimate-mcp.rs +11 -0
- package/src/boundaries/tests.rs +60 -0
- package/src/boundaries.rs +317 -0
- package/src/changed_scope.rs +117 -0
- package/src/ci_template.rs +399 -0
- package/src/cli/analyze.rs +462 -0
- package/src/cli/analyzer_options.rs +82 -0
- package/src/cli/audit_run.rs +327 -0
- package/src/cli/boundary_args.rs +44 -0
- package/src/cli/ci_template_run.rs +196 -0
- package/src/cli/command_name.rs +20 -0
- package/src/cli/common_args.rs +167 -0
- package/src/cli/config_run.rs +78 -0
- package/src/cli/coverage_run.rs +393 -0
- package/src/cli/decision_surface_run.rs +99 -0
- package/src/cli/default_command.rs +69 -0
- package/src/cli/dupes_args.rs +150 -0
- package/src/cli/entry_points.rs +121 -0
- package/src/cli/error.rs +149 -0
- package/src/cli/error_output.rs +68 -0
- package/src/cli/explain_run.rs +72 -0
- package/src/cli/fix_run.rs +162 -0
- package/src/cli/flags_args.rs +23 -0
- package/src/cli/health_args.rs +200 -0
- package/src/cli/hooks_run.rs +191 -0
- package/src/cli/impact_run.rs +95 -0
- package/src/cli/init_run.rs +49 -0
- package/src/cli/inspect_args.rs +20 -0
- package/src/cli/inspect_run.rs +66 -0
- package/src/cli/issue_filter_args.rs +240 -0
- package/src/cli/list_run.rs +311 -0
- package/src/cli/mode_args.rs +72 -0
- package/src/cli/output_format.rs +62 -0
- package/src/cli/regression_args.rs +156 -0
- package/src/cli/request_args.rs +175 -0
- package/src/cli/schema_commands.rs +72 -0
- package/src/cli/scope_args.rs +174 -0
- package/src/cli/security_args.rs +175 -0
- package/src/cli/security_gate_run.rs +66 -0
- package/src/cli/security_summary_run.rs +43 -0
- package/src/cli/summary_args.rs +12 -0
- package/src/cli/tests.rs +516 -0
- package/src/cli/trace_args.rs +67 -0
- package/src/cli/trace_run.rs +131 -0
- package/src/cli/trace_tests.rs +235 -0
- package/src/cli/unsupported_run.rs +116 -0
- package/src/cli/watch_run.rs +153 -0
- package/src/cli.rs +603 -0
- package/src/config/boundary_config.rs +235 -0
- package/src/config/cache.rs +25 -0
- package/src/config/dependencies.rs +114 -0
- package/src/config/health.rs +237 -0
- package/src/config/jsonc.rs +66 -0
- package/src/config/policy.rs +91 -0
- package/src/config/rule_aliases.rs +487 -0
- package/src/config/rules.rs +339 -0
- package/src/config.rs +635 -0
- package/src/coverage.rs +469 -0
- package/src/dart_parser.rs +673 -0
- package/src/decision_surface.rs +405 -0
- package/src/dependencies/analyze.rs +218 -0
- package/src/dependencies/codegen.rs +118 -0
- package/src/dependencies/discovery.rs +60 -0
- package/src/dependencies/overrides.rs +76 -0
- package/src/dependencies/private_src_imports.rs +32 -0
- package/src/dependencies/pubspec_document.rs +86 -0
- package/src/dependencies/pubspec_entry.rs +96 -0
- package/src/dependencies/tests.rs +544 -0
- package/src/dependencies/usage.rs +33 -0
- package/src/dependencies.rs +553 -0
- package/src/dependency_scripts.rs +91 -0
- package/src/dupes/lex.rs +236 -0
- package/src/dupes/tests.rs +119 -0
- package/src/dupes.rs +625 -0
- package/src/explain.rs +692 -0
- package/src/extract/directives.rs +252 -0
- package/src/extract/error_tests.rs +25 -0
- package/src/extract/members.rs +210 -0
- package/src/extract/primary_constructors.rs +421 -0
- package/src/extract/references.rs +151 -0
- package/src/extract/routes.rs +691 -0
- package/src/extract/signatures.rs +266 -0
- package/src/extract/string_tests.rs +18 -0
- package/src/extract/strings.rs +107 -0
- package/src/extract/tests.rs +697 -0
- package/src/extract.rs +680 -0
- package/src/feature_flags/tests.rs +76 -0
- package/src/feature_flags.rs +453 -0
- package/src/finding_identity.rs +60 -0
- package/src/fix.rs +551 -0
- package/src/generated.rs +53 -0
- package/src/graph/augment_tests.rs +212 -0
- package/src/graph/conditionals.rs +74 -0
- package/src/graph/package_config_tests.rs +100 -0
- package/src/graph/parts.rs +248 -0
- package/src/graph/pubspec_tests.rs +119 -0
- package/src/graph/tests.rs +837 -0
- package/src/graph/uri_tests.rs +102 -0
- package/src/graph.rs +644 -0
- package/src/health/coverage.rs +309 -0
- package/src/health/ownership.rs +175 -0
- package/src/health/runtime_coverage.rs +616 -0
- package/src/health/runtime_intelligence.rs +511 -0
- package/src/health/scores.rs +296 -0
- package/src/health/tests.rs +240 -0
- package/src/health/threshold_types.rs +163 -0
- package/src/health/thresholds.rs +229 -0
- package/src/health/types.rs +636 -0
- package/src/health.rs +597 -0
- package/src/hooks/agent_settings.rs +175 -0
- package/src/hooks/templates.rs +51 -0
- package/src/hooks.rs +569 -0
- package/src/impact.rs +287 -0
- package/src/init.rs +237 -0
- package/src/inspect.rs +142 -0
- package/src/intelligence/tests.rs +232 -0
- package/src/intelligence.rs +305 -0
- package/src/lib.rs +198 -0
- package/src/main.rs +5 -0
- package/src/manifest/mcp.rs +565 -0
- package/src/manifest.rs +552 -0
- package/src/mcp/cli_args/value_args.rs +175 -0
- package/src/mcp/cli_args.rs +658 -0
- package/src/mcp/cli_args_tests.rs +522 -0
- package/src/mcp/code_execute.rs +671 -0
- package/src/mcp/tools.rs +660 -0
- package/src/mcp.rs +325 -0
- package/src/output/audit_risk.rs +135 -0
- package/src/output/dependency_findings.rs +327 -0
- package/src/output/duplication_findings.rs +114 -0
- package/src/output/feature_flag_findings.rs +122 -0
- package/src/output/format.rs +31 -0
- package/src/output/graph_findings.rs +461 -0
- package/src/output/health_findings.rs +612 -0
- package/src/output/human.rs +27 -0
- package/src/output/next_steps.rs +92 -0
- package/src/output/route_findings.rs +76 -0
- package/src/output/runtime_coverage.rs +515 -0
- package/src/output/scope.rs +183 -0
- package/src/output/security_findings.rs +390 -0
- package/src/output/security_sarif.rs +150 -0
- package/src/output/suppressions.rs +323 -0
- package/src/output/symbol_findings.rs +257 -0
- package/src/output/tests.rs +114 -0
- package/src/output/types.rs +604 -0
- package/src/output/widget_findings.rs +213 -0
- package/src/output.rs +682 -0
- package/src/package_map.rs +495 -0
- package/src/policy/packs.rs +232 -0
- package/src/policy/tests.rs +134 -0
- package/src/policy.rs +640 -0
- package/src/project_list.rs +376 -0
- package/src/report_schema/inventory.rs +418 -0
- package/src/report_schema.rs +488 -0
- package/src/routes.rs +354 -0
- package/src/scan/tests.rs +250 -0
- package/src/scan.rs +275 -0
- package/src/security/detect.rs +639 -0
- package/src/security/tests.rs +178 -0
- package/src/security.rs +373 -0
- package/src/security_gate.rs +555 -0
- package/src/symbols/path_filters.rs +27 -0
- package/src/symbols/private_type_leaks.rs +125 -0
- package/src/symbols/riverpod.rs +113 -0
- package/src/symbols/tests.rs +692 -0
- package/src/symbols.rs +693 -0
- package/src/trace/tests.rs +225 -0
- package/src/trace.rs +690 -0
- package/src/unsupported.rs +47 -0
- package/src/widgets/lifecycle.rs +484 -0
- package/src/widgets/params.rs +236 -0
- package/src/widgets/tests.rs +430 -0
- package/src/widgets/top_level.rs +110 -0
- package/src/widgets/unrendered.rs +374 -0
- package/src/widgets.rs +590 -0
- package/src/workspace_scope.rs +358 -0
package/src/dupes.rs
ADDED
|
@@ -0,0 +1,625 @@
|
|
|
1
|
+
use std::collections::{BTreeMap, BTreeSet};
|
|
2
|
+
use std::fs;
|
|
3
|
+
use std::path::{Path, PathBuf};
|
|
4
|
+
|
|
5
|
+
use serde::de::{Error as DeError, Visitor};
|
|
6
|
+
use serde::ser::Serializer;
|
|
7
|
+
use serde::{Deserialize, Serialize};
|
|
8
|
+
use thiserror::Error;
|
|
9
|
+
|
|
10
|
+
use crate::ScannedProject;
|
|
11
|
+
use crate::generated::is_generated_dart_path;
|
|
12
|
+
use crate::graph::normalize_against;
|
|
13
|
+
use crate::output::TRACE_SCHEMA_VERSION;
|
|
14
|
+
|
|
15
|
+
mod lex;
|
|
16
|
+
use lex::normalized_lines;
|
|
17
|
+
|
|
18
|
+
/// Dart duplicate-code detection mode.
|
|
19
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
20
|
+
#[serde(rename_all = "kebab-case")]
|
|
21
|
+
pub enum DuplicateMode {
|
|
22
|
+
/// Exact token match after comments and whitespace are removed.
|
|
23
|
+
Strict,
|
|
24
|
+
/// Default mode. Equivalent to strict for the current Dart tokenizer.
|
|
25
|
+
Mild,
|
|
26
|
+
/// Normalize string literal values.
|
|
27
|
+
Weak,
|
|
28
|
+
/// Normalize string literals, numeric literals, and non-keyword identifiers.
|
|
29
|
+
Semantic,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
impl DuplicateMode {
|
|
33
|
+
/// Parse a CLI mode value.
|
|
34
|
+
#[must_use]
|
|
35
|
+
pub fn parse(value: &str) -> Option<Self> {
|
|
36
|
+
match value {
|
|
37
|
+
"strict" => Some(Self::Strict),
|
|
38
|
+
"mild" => Some(Self::Mild),
|
|
39
|
+
"weak" => Some(Self::Weak),
|
|
40
|
+
"semantic" => Some(Self::Semantic),
|
|
41
|
+
_ => None,
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/// Duplicate-code detector options.
|
|
47
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
48
|
+
pub struct DuplicateOptions {
|
|
49
|
+
/// Detection mode.
|
|
50
|
+
pub mode: DuplicateMode,
|
|
51
|
+
/// Minimum token count per clone.
|
|
52
|
+
pub min_tokens: usize,
|
|
53
|
+
/// Minimum line count per clone.
|
|
54
|
+
pub min_lines: usize,
|
|
55
|
+
/// Minimum occurrences before a clone group is reported.
|
|
56
|
+
pub min_occurrences: usize,
|
|
57
|
+
/// Report only clones that cross directory boundaries.
|
|
58
|
+
pub skip_local: bool,
|
|
59
|
+
/// Ignore import/export/part/augment directives.
|
|
60
|
+
pub ignore_imports: bool,
|
|
61
|
+
/// Limit output to the N largest clone groups.
|
|
62
|
+
pub top: Option<usize>,
|
|
63
|
+
/// Fail when duplicated lines exceed this percentage.
|
|
64
|
+
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
65
|
+
pub threshold: Option<DuplicationThreshold>,
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
impl Default for DuplicateOptions {
|
|
69
|
+
fn default() -> Self {
|
|
70
|
+
Self {
|
|
71
|
+
mode: DuplicateMode::Mild,
|
|
72
|
+
min_tokens: 50,
|
|
73
|
+
min_lines: 5,
|
|
74
|
+
min_occurrences: 2,
|
|
75
|
+
skip_local: false,
|
|
76
|
+
ignore_imports: true,
|
|
77
|
+
top: None,
|
|
78
|
+
threshold: None,
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/// Duplicate percentage threshold represented as percentage basis points.
|
|
84
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
85
|
+
pub struct DuplicationThreshold(u32);
|
|
86
|
+
|
|
87
|
+
impl DuplicationThreshold {
|
|
88
|
+
/// Build a threshold from a human percentage in the inclusive range `0..=100`.
|
|
89
|
+
///
|
|
90
|
+
/// # Errors
|
|
91
|
+
///
|
|
92
|
+
/// Returns an error when the value is not finite or is outside `0..=100`.
|
|
93
|
+
pub fn from_percent(value: f64) -> Result<Self, String> {
|
|
94
|
+
if !value.is_finite() || !(0.0..=100.0).contains(&value) {
|
|
95
|
+
return Err("threshold must be a percentage from 0 to 100".to_owned());
|
|
96
|
+
}
|
|
97
|
+
let scaled = (value * 100.0).round();
|
|
98
|
+
let basis_points = scaled
|
|
99
|
+
.to_string()
|
|
100
|
+
.parse::<u32>()
|
|
101
|
+
.map_err(|_| "threshold must be a percentage from 0 to 100".to_owned())?;
|
|
102
|
+
Ok(Self(basis_points))
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
fn from_percent_u64(value: u64) -> Result<Self, String> {
|
|
106
|
+
let percent = u32::try_from(value)
|
|
107
|
+
.ok()
|
|
108
|
+
.filter(|value| *value <= 100)
|
|
109
|
+
.ok_or_else(|| "threshold must be a percentage from 0 to 100".to_owned())?;
|
|
110
|
+
Ok(Self(percent * 100))
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
fn from_percent_i64(value: i64) -> Result<Self, String> {
|
|
114
|
+
let value = u64::try_from(value)
|
|
115
|
+
.map_err(|_| "threshold must be a percentage from 0 to 100".to_owned())?;
|
|
116
|
+
Self::from_percent_u64(value)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/// Return the underlying percentage basis points.
|
|
120
|
+
#[must_use]
|
|
121
|
+
pub const fn basis_points(self) -> u32 {
|
|
122
|
+
self.0
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
fn as_percent(self) -> f64 {
|
|
126
|
+
f64::from(self.0) / 100.0
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
fn is_exceeded_by(self, percentage_basis_points: u32) -> bool {
|
|
130
|
+
percentage_basis_points > self.0
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
impl Serialize for DuplicationThreshold {
|
|
135
|
+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
136
|
+
where
|
|
137
|
+
S: Serializer,
|
|
138
|
+
{
|
|
139
|
+
serializer.serialize_f64(self.as_percent())
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
impl<'de> Deserialize<'de> for DuplicationThreshold {
|
|
144
|
+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
145
|
+
where
|
|
146
|
+
D: serde::Deserializer<'de>,
|
|
147
|
+
{
|
|
148
|
+
struct ThresholdVisitor;
|
|
149
|
+
|
|
150
|
+
impl Visitor<'_> for ThresholdVisitor {
|
|
151
|
+
type Value = DuplicationThreshold;
|
|
152
|
+
|
|
153
|
+
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
154
|
+
formatter.write_str("a percentage from 0 to 100")
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
|
|
158
|
+
where
|
|
159
|
+
E: DeError,
|
|
160
|
+
{
|
|
161
|
+
DuplicationThreshold::from_percent_u64(value).map_err(E::custom)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
|
|
165
|
+
where
|
|
166
|
+
E: DeError,
|
|
167
|
+
{
|
|
168
|
+
DuplicationThreshold::from_percent_i64(value).map_err(E::custom)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
fn visit_f64<E>(self, value: f64) -> Result<Self::Value, E>
|
|
172
|
+
where
|
|
173
|
+
E: DeError,
|
|
174
|
+
{
|
|
175
|
+
DuplicationThreshold::from_percent(value).map_err(E::custom)
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
deserializer.deserialize_any(ThresholdVisitor)
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/// Aggregate duplicate-code statistics.
|
|
184
|
+
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
|
185
|
+
pub struct DuplicateStats {
|
|
186
|
+
/// Dart source lines included in duplicate analysis.
|
|
187
|
+
pub analyzed_lines: usize,
|
|
188
|
+
/// Unique source lines covered by at least one reported clone instance.
|
|
189
|
+
pub duplicated_lines: usize,
|
|
190
|
+
/// Duplicated line percentage in percentage basis points.
|
|
191
|
+
pub duplication_percentage_basis_points: u32,
|
|
192
|
+
/// Whether the configured duplicate threshold was exceeded.
|
|
193
|
+
pub threshold_exceeded: bool,
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/// Code duplication report.
|
|
197
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
198
|
+
pub struct DuplicateCodeReport {
|
|
199
|
+
/// Options used to compute the report.
|
|
200
|
+
pub options: DuplicateOptions,
|
|
201
|
+
/// Aggregate duplicate-code statistics.
|
|
202
|
+
pub stats: DuplicateStats,
|
|
203
|
+
/// Reported clone groups.
|
|
204
|
+
pub clone_groups: Vec<CodeClone>,
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/// One duplicated Dart code block.
|
|
208
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
209
|
+
pub struct CodeClone {
|
|
210
|
+
/// Stable clone fingerprint.
|
|
211
|
+
pub fingerprint: String,
|
|
212
|
+
/// Matching block instances.
|
|
213
|
+
pub instances: Vec<CodeCloneInstance>,
|
|
214
|
+
/// Lines in the duplicated block.
|
|
215
|
+
pub line_count: usize,
|
|
216
|
+
/// Tokens in the duplicated block.
|
|
217
|
+
pub token_count: usize,
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/// One clone group instance.
|
|
221
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
222
|
+
pub struct CodeCloneInstance {
|
|
223
|
+
/// Dart file path.
|
|
224
|
+
pub path: PathBuf,
|
|
225
|
+
/// 1-based start line.
|
|
226
|
+
pub start_line: usize,
|
|
227
|
+
/// 1-based end line.
|
|
228
|
+
pub end_line: usize,
|
|
229
|
+
/// 0-based byte column.
|
|
230
|
+
pub column: usize,
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/// Trace-clone JSON envelope.
|
|
234
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
235
|
+
pub struct CloneTraceReport {
|
|
236
|
+
/// Schema identifier.
|
|
237
|
+
pub schema_version: String,
|
|
238
|
+
/// Typed JSON envelope discriminator.
|
|
239
|
+
pub kind: String,
|
|
240
|
+
/// Tool name.
|
|
241
|
+
pub tool: String,
|
|
242
|
+
/// Trace command name.
|
|
243
|
+
pub command: String,
|
|
244
|
+
/// Trace selector supplied by the caller.
|
|
245
|
+
pub trace: String,
|
|
246
|
+
/// Whether at least one clone group matched.
|
|
247
|
+
pub found: bool,
|
|
248
|
+
/// Matching clone groups.
|
|
249
|
+
pub clone_groups: Vec<TraceCloneGroup>,
|
|
250
|
+
/// Short trace interpretation.
|
|
251
|
+
pub reason: String,
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/// Clone group included in trace output.
|
|
255
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
256
|
+
pub struct TraceCloneGroup {
|
|
257
|
+
/// Stable clone fingerprint.
|
|
258
|
+
pub fingerprint: String,
|
|
259
|
+
/// Lines in the duplicated block.
|
|
260
|
+
pub line_count: usize,
|
|
261
|
+
/// Tokens in the duplicated block.
|
|
262
|
+
pub token_count: usize,
|
|
263
|
+
/// Matching block instances.
|
|
264
|
+
pub instances: Vec<TraceCloneInstance>,
|
|
265
|
+
/// Suggested extraction target.
|
|
266
|
+
pub suggestion: String,
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/// Clone instance included in trace output.
|
|
270
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
271
|
+
pub struct TraceCloneInstance {
|
|
272
|
+
/// Dart file path, root-relative where possible.
|
|
273
|
+
pub path: String,
|
|
274
|
+
/// 1-based start line.
|
|
275
|
+
pub start_line: usize,
|
|
276
|
+
/// 1-based end line.
|
|
277
|
+
pub end_line: usize,
|
|
278
|
+
/// 0-based byte column.
|
|
279
|
+
pub column: usize,
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/// Errors returned while detecting duplicated code.
|
|
283
|
+
#[derive(Debug, Error)]
|
|
284
|
+
pub enum DuplicateCodeError {
|
|
285
|
+
/// A Dart file could not be read.
|
|
286
|
+
#[error("failed to read Dart file {path}: {source}")]
|
|
287
|
+
ReadFile {
|
|
288
|
+
/// File path.
|
|
289
|
+
path: PathBuf,
|
|
290
|
+
/// Underlying IO error.
|
|
291
|
+
source: std::io::Error,
|
|
292
|
+
},
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
296
|
+
struct CloneOccurrence {
|
|
297
|
+
path: PathBuf,
|
|
298
|
+
start_line: usize,
|
|
299
|
+
end_line: usize,
|
|
300
|
+
column: usize,
|
|
301
|
+
parent: PathBuf,
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/// Detect duplicated Dart code blocks.
|
|
305
|
+
///
|
|
306
|
+
/// # Errors
|
|
307
|
+
///
|
|
308
|
+
/// Returns [`DuplicateCodeError`] if a scanned Dart file cannot be read.
|
|
309
|
+
pub fn detect_duplicates(
|
|
310
|
+
project: &ScannedProject,
|
|
311
|
+
options: &DuplicateOptions,
|
|
312
|
+
) -> Result<DuplicateCodeReport, DuplicateCodeError> {
|
|
313
|
+
let mut by_fingerprint = BTreeMap::<String, Vec<(CloneOccurrence, usize)>>::new();
|
|
314
|
+
let mut analyzed_lines = 0usize;
|
|
315
|
+
|
|
316
|
+
for file in &project.files {
|
|
317
|
+
let path = normalize_against(&project.root, &file.path);
|
|
318
|
+
if !path.starts_with(&project.root) || is_ignored_path(&path) {
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
let source = fs::read_to_string(&path).map_err(|source| DuplicateCodeError::ReadFile {
|
|
322
|
+
path: path.clone(),
|
|
323
|
+
source,
|
|
324
|
+
})?;
|
|
325
|
+
analyzed_lines += source.lines().count();
|
|
326
|
+
let lines = normalized_lines(&source, options);
|
|
327
|
+
if lines.len() < options.min_lines {
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
for window in lines.windows(options.min_lines) {
|
|
332
|
+
let token_count = window.iter().map(|line| line.token_count).sum::<usize>();
|
|
333
|
+
if token_count < options.min_tokens {
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
let text = window
|
|
337
|
+
.iter()
|
|
338
|
+
.map(|line| line.text.as_str())
|
|
339
|
+
.collect::<Vec<_>>()
|
|
340
|
+
.join("\n");
|
|
341
|
+
let Some(first) = window.first() else {
|
|
342
|
+
continue;
|
|
343
|
+
};
|
|
344
|
+
let Some(last) = window.last() else {
|
|
345
|
+
continue;
|
|
346
|
+
};
|
|
347
|
+
let fingerprint = fingerprint(&text);
|
|
348
|
+
by_fingerprint.entry(fingerprint).or_default().push((
|
|
349
|
+
CloneOccurrence {
|
|
350
|
+
parent: path.parent().map_or_else(PathBuf::new, Path::to_path_buf),
|
|
351
|
+
path: path.clone(),
|
|
352
|
+
start_line: first.line,
|
|
353
|
+
end_line: last.line,
|
|
354
|
+
column: first.column,
|
|
355
|
+
},
|
|
356
|
+
token_count,
|
|
357
|
+
));
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
let mut clone_groups = by_fingerprint
|
|
362
|
+
.into_iter()
|
|
363
|
+
.filter_map(|(fingerprint, occurrences)| {
|
|
364
|
+
clone_group_from_occurrences(&fingerprint, occurrences, options)
|
|
365
|
+
})
|
|
366
|
+
.collect::<Vec<_>>();
|
|
367
|
+
|
|
368
|
+
clone_groups.sort_by(|left, right| {
|
|
369
|
+
(
|
|
370
|
+
std::cmp::Reverse(left.instances.len()),
|
|
371
|
+
std::cmp::Reverse(left.line_count),
|
|
372
|
+
&left.instances[0].path,
|
|
373
|
+
left.instances[0].start_line,
|
|
374
|
+
&left.fingerprint,
|
|
375
|
+
)
|
|
376
|
+
.cmp(&(
|
|
377
|
+
std::cmp::Reverse(right.instances.len()),
|
|
378
|
+
std::cmp::Reverse(right.line_count),
|
|
379
|
+
&right.instances[0].path,
|
|
380
|
+
right.instances[0].start_line,
|
|
381
|
+
&right.fingerprint,
|
|
382
|
+
))
|
|
383
|
+
});
|
|
384
|
+
clone_groups = collapse_overlapping_groups(clone_groups);
|
|
385
|
+
let stats = duplicate_stats(analyzed_lines, &clone_groups, options.threshold);
|
|
386
|
+
if let Some(top) = options.top {
|
|
387
|
+
clone_groups.truncate(top);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
Ok(DuplicateCodeReport {
|
|
391
|
+
options: options.clone(),
|
|
392
|
+
stats,
|
|
393
|
+
clone_groups,
|
|
394
|
+
})
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/// Trace clone groups by fingerprint or `FILE:LINE`.
|
|
398
|
+
#[must_use]
|
|
399
|
+
pub fn trace_clone(
|
|
400
|
+
project: &ScannedProject,
|
|
401
|
+
report: &DuplicateCodeReport,
|
|
402
|
+
trace: &str,
|
|
403
|
+
) -> CloneTraceReport {
|
|
404
|
+
let clone_groups = report
|
|
405
|
+
.clone_groups
|
|
406
|
+
.iter()
|
|
407
|
+
.filter(|group| clone_matches(project, group, trace))
|
|
408
|
+
.map(|group| trace_group(&project.root, group))
|
|
409
|
+
.collect::<Vec<_>>();
|
|
410
|
+
let found = !clone_groups.is_empty();
|
|
411
|
+
|
|
412
|
+
CloneTraceReport {
|
|
413
|
+
schema_version: TRACE_SCHEMA_VERSION.to_owned(),
|
|
414
|
+
kind: "trace-clone".to_owned(),
|
|
415
|
+
tool: "dart-decimate".to_owned(),
|
|
416
|
+
command: "trace-clone".to_owned(),
|
|
417
|
+
trace: trace.to_owned(),
|
|
418
|
+
found,
|
|
419
|
+
reason: if found {
|
|
420
|
+
"clone trace matched one or more duplicate code groups"
|
|
421
|
+
} else {
|
|
422
|
+
"clone trace did not match any duplicate code group"
|
|
423
|
+
}
|
|
424
|
+
.to_owned(),
|
|
425
|
+
clone_groups,
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/// Render a concise human clone trace.
|
|
430
|
+
#[must_use]
|
|
431
|
+
pub fn render_clone_trace(report: &CloneTraceReport) -> String {
|
|
432
|
+
format!(
|
|
433
|
+
"trace-clone {}: found={} groups={}\n{}\n",
|
|
434
|
+
report.trace,
|
|
435
|
+
report.found,
|
|
436
|
+
report.clone_groups.len(),
|
|
437
|
+
report.reason
|
|
438
|
+
)
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
fn collapse_overlapping_groups(groups: Vec<CodeClone>) -> Vec<CodeClone> {
|
|
442
|
+
let mut collapsed = Vec::new();
|
|
443
|
+
for group in groups {
|
|
444
|
+
if !collapsed
|
|
445
|
+
.iter()
|
|
446
|
+
.any(|existing| groups_overlap(existing, &group))
|
|
447
|
+
{
|
|
448
|
+
collapsed.push(group);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
collapsed
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
fn groups_overlap(left: &CodeClone, right: &CodeClone) -> bool {
|
|
455
|
+
right.instances.iter().all(|right_instance| {
|
|
456
|
+
left.instances.iter().any(|left_instance| {
|
|
457
|
+
left_instance.path == right_instance.path
|
|
458
|
+
&& ranges_overlap(
|
|
459
|
+
left_instance.start_line,
|
|
460
|
+
left_instance.end_line,
|
|
461
|
+
right_instance.start_line,
|
|
462
|
+
right_instance.end_line,
|
|
463
|
+
)
|
|
464
|
+
})
|
|
465
|
+
})
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
fn ranges_overlap(
|
|
469
|
+
left_start: usize,
|
|
470
|
+
left_end: usize,
|
|
471
|
+
right_start: usize,
|
|
472
|
+
right_end: usize,
|
|
473
|
+
) -> bool {
|
|
474
|
+
left_start <= right_end && right_start <= left_end
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
fn duplicate_stats(
|
|
478
|
+
analyzed_lines: usize,
|
|
479
|
+
clone_groups: &[CodeClone],
|
|
480
|
+
threshold: Option<DuplicationThreshold>,
|
|
481
|
+
) -> DuplicateStats {
|
|
482
|
+
let mut duplicated = BTreeSet::<(PathBuf, usize)>::new();
|
|
483
|
+
for group in clone_groups {
|
|
484
|
+
for instance in &group.instances {
|
|
485
|
+
for line in instance.start_line..=instance.end_line {
|
|
486
|
+
duplicated.insert((instance.path.clone(), line));
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
let duplicated_lines = duplicated.len();
|
|
491
|
+
let duplication_percentage_basis_points = if analyzed_lines == 0 {
|
|
492
|
+
0
|
|
493
|
+
} else {
|
|
494
|
+
let raw = ((duplicated_lines as u128) * 10_000) / (analyzed_lines as u128);
|
|
495
|
+
u32::try_from(raw).unwrap_or(u32::MAX)
|
|
496
|
+
};
|
|
497
|
+
let threshold_exceeded = threshold
|
|
498
|
+
.is_some_and(|threshold| threshold.is_exceeded_by(duplication_percentage_basis_points));
|
|
499
|
+
|
|
500
|
+
DuplicateStats {
|
|
501
|
+
analyzed_lines,
|
|
502
|
+
duplicated_lines,
|
|
503
|
+
duplication_percentage_basis_points,
|
|
504
|
+
threshold_exceeded,
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
fn clone_group_from_occurrences(
|
|
509
|
+
fingerprint: &str,
|
|
510
|
+
occurrences: Vec<(CloneOccurrence, usize)>,
|
|
511
|
+
options: &DuplicateOptions,
|
|
512
|
+
) -> Option<CodeClone> {
|
|
513
|
+
let mut seen = BTreeSet::<(PathBuf, usize, usize)>::new();
|
|
514
|
+
let mut instances = Vec::new();
|
|
515
|
+
let mut token_count = 0;
|
|
516
|
+
let mut parents = BTreeSet::new();
|
|
517
|
+
|
|
518
|
+
for (occurrence, tokens) in occurrences {
|
|
519
|
+
if !seen.insert((
|
|
520
|
+
occurrence.path.clone(),
|
|
521
|
+
occurrence.start_line,
|
|
522
|
+
occurrence.end_line,
|
|
523
|
+
)) {
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
parents.insert(occurrence.parent);
|
|
527
|
+
token_count = token_count.max(tokens);
|
|
528
|
+
instances.push(CodeCloneInstance {
|
|
529
|
+
path: occurrence.path,
|
|
530
|
+
start_line: occurrence.start_line,
|
|
531
|
+
end_line: occurrence.end_line,
|
|
532
|
+
column: occurrence.column,
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
if instances.len() < options.min_occurrences || (options.skip_local && parents.len() < 2) {
|
|
537
|
+
return None;
|
|
538
|
+
}
|
|
539
|
+
instances.sort_by(|left, right| {
|
|
540
|
+
(&left.path, left.start_line, left.end_line).cmp(&(
|
|
541
|
+
&right.path,
|
|
542
|
+
right.start_line,
|
|
543
|
+
right.end_line,
|
|
544
|
+
))
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
Some(CodeClone {
|
|
548
|
+
fingerprint: fingerprint.to_owned(),
|
|
549
|
+
instances,
|
|
550
|
+
line_count: options.min_lines,
|
|
551
|
+
token_count,
|
|
552
|
+
})
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
fn is_ignored_path(path: &Path) -> bool {
|
|
556
|
+
if is_generated_dart_path(path) {
|
|
557
|
+
return true;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
path.components().any(|component| {
|
|
561
|
+
matches!(
|
|
562
|
+
component.as_os_str().to_str(),
|
|
563
|
+
Some("test" | "integration_test" | "test_driver" | "__tests__" | "__mocks__")
|
|
564
|
+
)
|
|
565
|
+
})
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
fn fingerprint(text: &str) -> String {
|
|
569
|
+
let mut hash = 0xcbf2_9ce4_8422_2325_u64;
|
|
570
|
+
for byte in text.as_bytes() {
|
|
571
|
+
hash ^= u64::from(*byte);
|
|
572
|
+
hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
|
|
573
|
+
}
|
|
574
|
+
format!("dup:{:08x}", hash & 0xffff_ffff)
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
fn clone_matches(project: &ScannedProject, group: &CodeClone, trace: &str) -> bool {
|
|
578
|
+
if trace == group.fingerprint {
|
|
579
|
+
return true;
|
|
580
|
+
}
|
|
581
|
+
let Some((path, line)) = parse_file_line(trace) else {
|
|
582
|
+
return false;
|
|
583
|
+
};
|
|
584
|
+
let path = normalize_against(&project.root, &path);
|
|
585
|
+
group.instances.iter().any(|instance| {
|
|
586
|
+
instance.path == path && line >= instance.start_line && line <= instance.end_line
|
|
587
|
+
})
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
fn parse_file_line(trace: &str) -> Option<(PathBuf, usize)> {
|
|
591
|
+
let (path, line) = trace.rsplit_once(':')?;
|
|
592
|
+
Some((PathBuf::from(path), line.parse().ok()?))
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
fn trace_group(root: &Path, group: &CodeClone) -> TraceCloneGroup {
|
|
596
|
+
TraceCloneGroup {
|
|
597
|
+
fingerprint: group.fingerprint.clone(),
|
|
598
|
+
line_count: group.line_count,
|
|
599
|
+
token_count: group.token_count,
|
|
600
|
+
instances: group
|
|
601
|
+
.instances
|
|
602
|
+
.iter()
|
|
603
|
+
.map(|instance| TraceCloneInstance {
|
|
604
|
+
path: display_path(root, &instance.path),
|
|
605
|
+
start_line: instance.start_line,
|
|
606
|
+
end_line: instance.end_line,
|
|
607
|
+
column: instance.column,
|
|
608
|
+
})
|
|
609
|
+
.collect(),
|
|
610
|
+
suggestion: format!(
|
|
611
|
+
"Extract the {} duplicated lines into one shared function, method, widget, or mixin owner",
|
|
612
|
+
group.line_count
|
|
613
|
+
),
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
fn display_path(root: &Path, path: &Path) -> String {
|
|
618
|
+
path.strip_prefix(root)
|
|
619
|
+
.unwrap_or(path)
|
|
620
|
+
.to_string_lossy()
|
|
621
|
+
.replace('\\', "/")
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
#[cfg(test)]
|
|
625
|
+
mod tests;
|