dart-decimate 0.0.10 → 0.0.11

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.
@@ -3,7 +3,11 @@ use std::path::{Path, PathBuf};
3
3
 
4
4
  use crate::dependencies::{LocalPubPackage, local_pub_package_from_pubspec};
5
5
 
6
- use super::CodeClone;
6
+ use super::{CodeClone, CodeCloneInstance};
7
+
8
+ type PackageIdentity = (String, PathBuf);
9
+ type InstanceKey = (PathBuf, usize, usize, usize);
10
+ type OccurrenceLayout = Vec<(usize, usize, usize)>;
7
11
 
8
12
  pub(super) struct CopiedPackageFilter {
9
13
  root: PathBuf,
@@ -37,6 +41,7 @@ impl CopiedPackageFilter {
37
41
  relative_path.to_path_buf(),
38
42
  instance.start_line,
39
43
  instance.end_line,
44
+ instance.column,
40
45
  );
41
46
  if identity.is_none() {
42
47
  identity = Some(current);
@@ -47,6 +52,60 @@ impl CopiedPackageFilter {
47
52
  roots.len() > 1
48
53
  }
49
54
 
55
+ pub(super) fn canonicalize_copied_package_instances(&mut self, group: &mut CodeClone) {
56
+ if group.instances.len() < 2 {
57
+ return;
58
+ }
59
+
60
+ let mut instances_by_identity =
61
+ BTreeMap::<PackageIdentity, BTreeMap<PathBuf, Vec<CodeCloneInstance>>>::new();
62
+ for instance in &group.instances {
63
+ let Some(package) = self.owning_package(&instance.path) else {
64
+ continue;
65
+ };
66
+ let Ok(relative_path) = instance.path.strip_prefix(&package.root) else {
67
+ continue;
68
+ };
69
+ instances_by_identity
70
+ .entry((package.name, relative_path.to_path_buf()))
71
+ .or_default()
72
+ .entry(package.root)
73
+ .or_default()
74
+ .push(instance.clone());
75
+ }
76
+
77
+ let canonical_instances = canonical_instances_by_occurrence(instances_by_identity);
78
+ if canonical_instances.is_empty() {
79
+ return;
80
+ }
81
+
82
+ let mut seen = BTreeSet::<(PathBuf, usize, usize, usize)>::new();
83
+ let mut instances = Vec::new();
84
+ for instance in &group.instances {
85
+ let canonical = canonical_instances
86
+ .get(&instance_key(instance))
87
+ .cloned()
88
+ .unwrap_or_else(|| instance.clone());
89
+ if seen.insert((
90
+ canonical.path.clone(),
91
+ canonical.start_line,
92
+ canonical.end_line,
93
+ canonical.column,
94
+ )) {
95
+ instances.push(canonical);
96
+ }
97
+ }
98
+ instances.sort_by(|left, right| {
99
+ (&left.path, left.start_line, left.end_line, left.column).cmp(&(
100
+ &right.path,
101
+ right.start_line,
102
+ right.end_line,
103
+ right.column,
104
+ ))
105
+ });
106
+ group.instances = instances;
107
+ }
108
+
50
109
  fn owning_package(&mut self, path: &Path) -> Option<LocalPubPackage> {
51
110
  let mut current = path.parent();
52
111
  while let Some(dir) = current {
@@ -69,3 +128,86 @@ impl CopiedPackageFilter {
69
128
  None
70
129
  }
71
130
  }
131
+
132
+ fn package_root_order(left: &PathBuf, right: &PathBuf) -> std::cmp::Ordering {
133
+ let left_components = left.components().count();
134
+ let right_components = right.components().count();
135
+ (left_components, left).cmp(&(right_components, right))
136
+ }
137
+
138
+ fn canonical_instances_by_occurrence(
139
+ instances_by_identity: BTreeMap<PackageIdentity, BTreeMap<PathBuf, Vec<CodeCloneInstance>>>,
140
+ ) -> BTreeMap<InstanceKey, CodeCloneInstance> {
141
+ let mut canonical_instances = BTreeMap::new();
142
+ for roots in instances_by_identity.into_values() {
143
+ let mut roots_by_layout =
144
+ BTreeMap::<OccurrenceLayout, BTreeMap<PathBuf, Vec<CodeCloneInstance>>>::new();
145
+ for (root, root_instances) in roots {
146
+ roots_by_layout
147
+ .entry(occurrence_layout(&root_instances))
148
+ .or_default()
149
+ .insert(root, root_instances);
150
+ }
151
+ for roots in roots_by_layout.into_values() {
152
+ if roots.len() < 2 {
153
+ continue;
154
+ }
155
+ let Some((canonical_root, canonical_root_instances)) = roots
156
+ .iter()
157
+ .min_by(|left, right| package_root_order(left.0, right.0))
158
+ else {
159
+ continue;
160
+ };
161
+ let canonical_root_instances = sorted_instances(canonical_root_instances);
162
+ for (root, root_instances) in &roots {
163
+ if root == canonical_root {
164
+ continue;
165
+ }
166
+ for (index, instance) in sorted_instances(root_instances).into_iter().enumerate() {
167
+ let Some(canonical) = canonical_root_instances.get(index) else {
168
+ continue;
169
+ };
170
+ canonical_instances.insert(instance_key(&instance), canonical.clone());
171
+ }
172
+ }
173
+ }
174
+ }
175
+ canonical_instances
176
+ }
177
+
178
+ fn occurrence_layout(instances: &[CodeCloneInstance]) -> OccurrenceLayout {
179
+ let instances = sorted_instances(instances);
180
+ let first_start = instances.first().map_or(0, |instance| instance.start_line);
181
+ instances
182
+ .iter()
183
+ .map(|instance| {
184
+ (
185
+ instance.start_line.saturating_sub(first_start),
186
+ instance.end_line.saturating_sub(instance.start_line),
187
+ instance.column,
188
+ )
189
+ })
190
+ .collect()
191
+ }
192
+
193
+ fn sorted_instances(instances: &[CodeCloneInstance]) -> Vec<CodeCloneInstance> {
194
+ let mut instances = instances.to_vec();
195
+ instances.sort_by(|left, right| {
196
+ (left.start_line, left.end_line, left.column, &left.path).cmp(&(
197
+ right.start_line,
198
+ right.end_line,
199
+ right.column,
200
+ &right.path,
201
+ ))
202
+ });
203
+ instances
204
+ }
205
+
206
+ fn instance_key(instance: &CodeCloneInstance) -> InstanceKey {
207
+ (
208
+ instance.path.clone(),
209
+ instance.start_line,
210
+ instance.end_line,
211
+ instance.column,
212
+ )
213
+ }
@@ -135,6 +135,133 @@ fn copied_package_filter_ignores_unrelated_malformed_pubspec()
135
135
  Ok(())
136
136
  }
137
137
 
138
+ #[test]
139
+ fn top_uses_canonicalized_clone_ranking() -> Result<(), Box<dyn std::error::Error>> {
140
+ let fixture = tempfile::tempdir()?;
141
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
142
+ let mirrored = pagination_clone_source();
143
+ write(&fixture, "functions/shared/pubspec.yaml", "name: shared\n")?;
144
+ write(&fixture, "functions/shared/lib/pagination.dart", mirrored)?;
145
+ for function in ["delete_user", "squad_operations"] {
146
+ write(
147
+ &fixture,
148
+ &format!("functions/{function}/shared/pubspec.yaml"),
149
+ "name: shared\n",
150
+ )?;
151
+ write(
152
+ &fixture,
153
+ &format!("functions/{function}/shared/lib/pagination.dart"),
154
+ mirrored,
155
+ )?;
156
+ }
157
+ let real = "String visibleClone(String owner) {\n final normalized = owner.trim().toLowerCase();\n final words = normalized.split(' ');\n final compact = words.where((word) => word.isNotEmpty).join('-');\n return 'owner:$compact';\n}\n";
158
+ for path in ["lib/alpha.dart", "lib/beta.dart", "lib/gamma.dart"] {
159
+ write(&fixture, path, real)?;
160
+ }
161
+ let project = scan_project(fixture.path())?;
162
+ let mut opts = options(DuplicateMode::Strict, 5, 10);
163
+ opts.top = Some(1);
164
+
165
+ let report = detect_duplicates(&project, &opts)?;
166
+
167
+ assert_eq!(report.clone_groups.len(), 1);
168
+ let clone = &report.clone_groups[0];
169
+ assert_eq!(clone.instances.len(), 3);
170
+ assert!(
171
+ clone
172
+ .instances
173
+ .iter()
174
+ .all(|instance| { instance.path.starts_with(fixture.path().join("lib")) })
175
+ );
176
+
177
+ Ok(())
178
+ }
179
+
180
+ #[test]
181
+ fn copied_package_canonicalization_keeps_unmatched_occurrences()
182
+ -> Result<(), Box<dyn std::error::Error>> {
183
+ let fixture = tempfile::tempdir()?;
184
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
185
+ write(&fixture, "functions/shared/pubspec.yaml", "name: shared\n")?;
186
+ write(
187
+ &fixture,
188
+ "functions/delete_user/shared/pubspec.yaml",
189
+ "name: shared\n",
190
+ )?;
191
+ let clone = "String copiedFormatter(String value) {\n final normalized = value.trim().toLowerCase();\n final compact = normalized.split(' ').where((word) => word.isNotEmpty).join('-');\n final suffix = compact.isEmpty ? 'empty' : compact;\n return 'owner:$suffix';\n}\n";
192
+ write(
193
+ &fixture,
194
+ "functions/shared/lib/pagination.dart",
195
+ &format!(
196
+ "// header 1\n// header 2\n// header 3\n// header 4\n// header 5\n// header 6\n// header 7\n{clone}"
197
+ ),
198
+ )?;
199
+ write(
200
+ &fixture,
201
+ "functions/delete_user/shared/lib/pagination.dart",
202
+ &format!("{clone}\n{clone}"),
203
+ )?;
204
+ let project = scan_project(fixture.path())?;
205
+
206
+ let report = detect_duplicates(&project, &options(DuplicateMode::Strict, 6, 20))?;
207
+
208
+ assert_eq!(report.clone_groups.len(), 1);
209
+ let clone_group = &report.clone_groups[0];
210
+ assert_eq!(clone_group.instances.len(), 3);
211
+ assert!(clone_group.instances.iter().any(|instance| {
212
+ instance
213
+ .path
214
+ .ends_with("functions/delete_user/shared/lib/pagination.dart")
215
+ && instance.start_line == 1
216
+ }));
217
+
218
+ Ok(())
219
+ }
220
+
221
+ #[test]
222
+ fn copied_package_canonicalization_orders_same_line_instances_by_column()
223
+ -> Result<(), Box<dyn std::error::Error>> {
224
+ let fixture = tempfile::tempdir()?;
225
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
226
+ write(&fixture, "functions/shared/pubspec.yaml", "name: shared\n")?;
227
+ write(
228
+ &fixture,
229
+ "functions/delete_user/shared/pubspec.yaml",
230
+ "name: shared\n",
231
+ )?;
232
+ let canonical = fixture.path().join("functions/shared/lib/pagination.dart");
233
+ let mirror = fixture
234
+ .path()
235
+ .join("functions/delete_user/shared/lib/pagination.dart");
236
+ let mut group = CodeClone {
237
+ fingerprint: "dup:test".to_owned(),
238
+ instances: vec![
239
+ instance(&mirror, 10, 12, 8),
240
+ instance(&mirror, 10, 12, 4),
241
+ instance(&canonical, 10, 12, 8),
242
+ instance(&canonical, 10, 12, 4),
243
+ ],
244
+ line_count: 3,
245
+ token_count: 20,
246
+ };
247
+ let mut filter = CopiedPackageFilter::new(fixture.path());
248
+
249
+ filter.canonicalize_copied_package_instances(&mut group);
250
+
251
+ assert_eq!(group.instances.len(), 2);
252
+ assert!(group.instances.iter().all(|clone| clone.path == canonical));
253
+ assert_eq!(
254
+ group
255
+ .instances
256
+ .iter()
257
+ .map(|clone| clone.column)
258
+ .collect::<Vec<_>>(),
259
+ vec![4, 8]
260
+ );
261
+
262
+ Ok(())
263
+ }
264
+
138
265
  #[test]
139
266
  fn trace_clone_matches_fingerprint_and_source_line() -> Result<(), Box<dyn std::error::Error>> {
140
267
  let fixture = tempfile::tempdir()?;
@@ -157,6 +284,35 @@ fn trace_clone_matches_fingerprint_and_source_line() -> Result<(), Box<dyn std::
157
284
  Ok(())
158
285
  }
159
286
 
287
+ fn pagination_clone_source() -> &'static str {
288
+ r"Future<List<String>> fetchAllRowIds(Pages pages) async {
289
+ final values = <String>[];
290
+ var cursor = '';
291
+ do {
292
+ final page = await pages.list(cursor: cursor);
293
+ for (final row in page.rows) {
294
+ values.add(row.id);
295
+ }
296
+ cursor = page.nextCursor;
297
+ } while (cursor.isNotEmpty);
298
+ return values;
299
+ }
300
+
301
+ Future<List<String>> fetchAllFieldValues(Pages pages) async {
302
+ final values = <String>[];
303
+ var cursor = '';
304
+ do {
305
+ final page = await pages.list(cursor: cursor);
306
+ for (final row in page.rows) {
307
+ values.add(row.id);
308
+ }
309
+ cursor = page.nextCursor;
310
+ } while (cursor.isNotEmpty);
311
+ return values;
312
+ }
313
+ "
314
+ }
315
+
160
316
  fn options(mode: DuplicateMode, min_lines: usize, min_tokens: usize) -> DuplicateOptions {
161
317
  DuplicateOptions {
162
318
  mode,
@@ -170,6 +326,20 @@ fn options(mode: DuplicateMode, min_lines: usize, min_tokens: usize) -> Duplicat
170
326
  }
171
327
  }
172
328
 
329
+ fn instance(
330
+ path: &std::path::Path,
331
+ start_line: usize,
332
+ end_line: usize,
333
+ column: usize,
334
+ ) -> CodeCloneInstance {
335
+ CodeCloneInstance {
336
+ path: path.to_path_buf(),
337
+ start_line,
338
+ end_line,
339
+ column,
340
+ }
341
+ }
342
+
173
343
  fn write(fixture: &TempDir, path: &str, source: &str) -> Result<(), std::io::Error> {
174
344
  let path = fixture.path().join(path);
175
345
  if let Some(parent) = path.parent() {
package/src/dupes.rs CHANGED
@@ -14,6 +14,8 @@ use crate::output::TRACE_SCHEMA_VERSION;
14
14
 
15
15
  mod lex;
16
16
  use lex::normalized_lines;
17
+ mod declarations;
18
+ use declarations::DeclarationCloneFilter;
17
19
  mod packages;
18
20
  use packages::CopiedPackageFilter;
19
21
 
@@ -315,8 +317,8 @@ struct CloneWindow {
315
317
  /// Detect duplicated Dart code blocks.
316
318
  ///
317
319
  /// Clone windows satisfy both `min_lines` and `min_tokens`; sparse duplicated
318
- /// blocks can therefore span more than `min_lines`. Identical matches across
319
- /// copied local Pub packages with the same package name are ignored.
320
+ /// blocks can therefore span more than `min_lines`. Declaration-only abstract
321
+ /// contracts and copied local Pub package mirrors are filtered conservatively.
320
322
  ///
321
323
  /// # Errors
322
324
  ///
@@ -378,24 +380,15 @@ pub fn detect_duplicates(
378
380
  })
379
381
  .collect::<Vec<_>>();
380
382
 
381
- clone_groups.sort_by(|left, right| {
382
- (
383
- std::cmp::Reverse(left.instances.len()),
384
- std::cmp::Reverse(left.line_count),
385
- &left.instances[0].path,
386
- left.instances[0].start_line,
387
- &left.fingerprint,
388
- )
389
- .cmp(&(
390
- std::cmp::Reverse(right.instances.len()),
391
- std::cmp::Reverse(right.line_count),
392
- &right.instances[0].path,
393
- right.instances[0].start_line,
394
- &right.fingerprint,
395
- ))
396
- });
397
- clone_groups = collapse_overlapping_groups(clone_groups);
398
383
  let mut copied_packages = CopiedPackageFilter::new(&project.root);
384
+ for group in &mut clone_groups {
385
+ copied_packages.canonicalize_copied_package_instances(group);
386
+ }
387
+ clone_groups.retain(|group| group_satisfies_occurrence_options(group, options));
388
+ let mut declaration_filter = DeclarationCloneFilter::new();
389
+ clone_groups.retain(|group| !declaration_filter.is_declaration_only_clone(group));
390
+ sort_clone_groups(&mut clone_groups);
391
+ clone_groups = collapse_overlapping_groups(clone_groups);
399
392
  clone_groups.retain(|group| !copied_packages.is_copied_package_clone(group));
400
393
  let stats = duplicate_stats(analyzed_lines, &clone_groups, options.threshold);
401
394
  if let Some(top) = options.top {
@@ -453,6 +446,25 @@ pub fn render_clone_trace(report: &CloneTraceReport) -> String {
453
446
  )
454
447
  }
455
448
 
449
+ fn sort_clone_groups(clone_groups: &mut [CodeClone]) {
450
+ clone_groups.sort_by(|left, right| {
451
+ (
452
+ std::cmp::Reverse(left.instances.len()),
453
+ std::cmp::Reverse(left.line_count),
454
+ &left.instances[0].path,
455
+ left.instances[0].start_line,
456
+ &left.fingerprint,
457
+ )
458
+ .cmp(&(
459
+ std::cmp::Reverse(right.instances.len()),
460
+ std::cmp::Reverse(right.line_count),
461
+ &right.instances[0].path,
462
+ right.instances[0].start_line,
463
+ &right.fingerprint,
464
+ ))
465
+ });
466
+ }
467
+
456
468
  fn collapse_overlapping_groups(groups: Vec<CodeClone>) -> Vec<CodeClone> {
457
469
  let mut collapsed = Vec::new();
458
470
  for group in groups {
@@ -466,6 +478,25 @@ fn collapse_overlapping_groups(groups: Vec<CodeClone>) -> Vec<CodeClone> {
466
478
  collapsed
467
479
  }
468
480
 
481
+ fn group_satisfies_occurrence_options(group: &CodeClone, options: &DuplicateOptions) -> bool {
482
+ group.instances.len() >= options.min_occurrences
483
+ && (!options.skip_local || clone_parent_count(group) >= 2)
484
+ }
485
+
486
+ fn clone_parent_count(group: &CodeClone) -> usize {
487
+ group
488
+ .instances
489
+ .iter()
490
+ .map(|instance| {
491
+ instance
492
+ .path
493
+ .parent()
494
+ .map_or_else(PathBuf::new, Path::to_path_buf)
495
+ })
496
+ .collect::<BTreeSet<_>>()
497
+ .len()
498
+ }
499
+
469
500
  fn groups_overlap(left: &CodeClone, right: &CodeClone) -> bool {
470
501
  right.instances.iter().all(|right_instance| {
471
502
  left.instances.iter().any(|left_instance| {
@@ -551,7 +582,7 @@ fn clone_group_from_occurrences(
551
582
  occurrences: Vec<(CloneOccurrence, usize)>,
552
583
  options: &DuplicateOptions,
553
584
  ) -> Option<CodeClone> {
554
- let mut seen = BTreeSet::<(PathBuf, usize, usize)>::new();
585
+ let mut seen = BTreeSet::<(PathBuf, usize, usize, usize)>::new();
555
586
  let mut instances = Vec::new();
556
587
  let mut token_count = 0;
557
588
  let mut line_count = 0;
@@ -562,6 +593,7 @@ fn clone_group_from_occurrences(
562
593
  occurrence.path.clone(),
563
594
  occurrence.start_line,
564
595
  occurrence.end_line,
596
+ occurrence.column,
565
597
  )) {
566
598
  continue;
567
599
  }
@@ -580,10 +612,11 @@ fn clone_group_from_occurrences(
580
612
  return None;
581
613
  }
582
614
  instances.sort_by(|left, right| {
583
- (&left.path, left.start_line, left.end_line).cmp(&(
615
+ (&left.path, left.start_line, left.end_line, left.column).cmp(&(
584
616
  &right.path,
585
617
  right.start_line,
586
618
  right.end_line,
619
+ right.column,
587
620
  ))
588
621
  });
589
622
 
@@ -1,4 +1,5 @@
1
1
  use super::format::{dependency_kind, display_path};
2
+ use super::route_cycle::is_typed_go_router_registry_cycle;
2
3
  use super::{Finding, FindingAction, FindingEdge, FindingKind, Severity};
3
4
  use crate::{
4
5
  BoundaryCallViolation, BoundaryCoverageGap, BoundaryViolation, DeadCodeReport, DependencyCycle,
@@ -70,23 +71,24 @@ pub(super) fn add_dead_code_findings(
70
71
  }
71
72
 
72
73
  pub(super) fn add_cycle_findings(
73
- root: &std::path::Path,
74
+ project: &ScannedProject,
74
75
  cycles: &[DependencyCycle],
75
76
  findings: &mut Vec<Finding>,
76
77
  ) {
77
78
  for cycle in cycles {
79
+ let classification = cycle_classification(project, cycle);
78
80
  let files = cycle
79
81
  .files
80
82
  .iter()
81
- .map(|path| display_path(root, path))
83
+ .map(|path| display_path(&project.root, path))
82
84
  .collect::<Vec<_>>();
83
85
  let path = files.first().cloned().unwrap_or_default();
84
86
  findings.push(Finding {
85
87
  rule_id: "dart-decimate/circular-dependency".to_owned(),
86
88
  fingerprint: None,
87
89
  kind: FindingKind::CircularDependency,
88
- severity: Severity::Error,
89
- message: format!("Circular dependency spans {} Dart files", files.len()),
90
+ severity: classification.severity,
91
+ message: classification.message(files.len()),
90
92
  path: path.clone(),
91
93
  line: 1,
92
94
  column: 0,
@@ -94,24 +96,56 @@ pub(super) fn add_cycle_findings(
94
96
  files,
95
97
  edge: None,
96
98
  actions: vec![
97
- FindingAction::new(
98
- "break-cycle",
99
- "Inspect the cycle edge; split barrels or move shared ownership before expanding imports",
100
- false,
101
- )
102
- .with_target_path(path.clone())
103
- .with_dart_decimate_args([
104
- "inspect",
105
- "--format",
106
- "json",
107
- "--file",
108
- path.as_str(),
109
- ]),
99
+ FindingAction::new(classification.action, classification.description, false)
100
+ .with_target_path(path.clone())
101
+ .with_dart_decimate_args([
102
+ "inspect",
103
+ "--format",
104
+ "json",
105
+ "--file",
106
+ path.as_str(),
107
+ ]),
110
108
  ],
111
109
  });
112
110
  }
113
111
  }
114
112
 
113
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
114
+ struct CycleClassification {
115
+ severity: Severity,
116
+ action: &'static str,
117
+ description: &'static str,
118
+ route_registry: bool,
119
+ }
120
+
121
+ impl CycleClassification {
122
+ fn message(self, file_count: usize) -> String {
123
+ if self.route_registry {
124
+ format!("Typed GoRouter route registry cycle spans {file_count} Dart files")
125
+ } else {
126
+ format!("Circular dependency spans {file_count} Dart files")
127
+ }
128
+ }
129
+ }
130
+
131
+ fn cycle_classification(project: &ScannedProject, cycle: &DependencyCycle) -> CycleClassification {
132
+ if is_typed_go_router_registry_cycle(project, cycle) {
133
+ return CycleClassification {
134
+ severity: Severity::Warning,
135
+ action: "review-typed-route-cycle",
136
+ description: "Keep typed routes if this is only the route registry to screen navigation helper cycle; split unrelated imports out of the cycle",
137
+ route_registry: true,
138
+ };
139
+ }
140
+
141
+ CycleClassification {
142
+ severity: Severity::Error,
143
+ action: "break-cycle",
144
+ description: "Inspect the cycle edge; split barrels or move shared ownership before expanding imports",
145
+ route_registry: false,
146
+ }
147
+ }
148
+
115
149
  pub(super) fn add_re_export_cycle_findings(
116
150
  root: &std::path::Path,
117
151
  cycles: &[ReExportCycle],