dart-decimate 0.0.8 → 0.0.9
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 +1 -1
- package/Cargo.toml +1 -1
- package/README.md +4 -2
- package/package.json +1 -1
- package/src/config.rs +3 -2
- package/src/dupes/tests.rs +33 -0
- package/src/dupes.rs +51 -13
- package/src/output/duplication_findings.rs +2 -2
package/Cargo.lock
CHANGED
package/Cargo.toml
CHANGED
package/README.md
CHANGED
|
@@ -91,7 +91,7 @@ Add this to `package.json` if you want a short project command:
|
|
|
91
91
|
"dart-decimate": "dart-decimate json ."
|
|
92
92
|
},
|
|
93
93
|
"devDependencies": {
|
|
94
|
-
"dart-decimate": "^0.0.
|
|
94
|
+
"dart-decimate": "^0.0.9"
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
97
|
```
|
|
@@ -245,6 +245,8 @@ Duplication means the same Dart code appears in more than one place.
|
|
|
245
245
|
|
|
246
246
|
Dart Decimate finds exact and semantic clone groups. Each clone group gets a stable
|
|
247
247
|
fingerprint like `dup:abc12345`, so agents can trace it before touching code.
|
|
248
|
+
Clone windows must meet both line and token thresholds; sparse duplicated blocks
|
|
249
|
+
can span more than `--min-lines`, and `line_count` reports the actual match.
|
|
248
250
|
|
|
249
251
|
Useful commands:
|
|
250
252
|
|
|
@@ -670,7 +672,7 @@ This repository forbids `unsafe_code`.
|
|
|
670
672
|
|
|
671
673
|
## Release Flow
|
|
672
674
|
|
|
673
|
-
Current version: `0.0.
|
|
675
|
+
Current version: `0.0.9`.
|
|
674
676
|
|
|
675
677
|
After the first public release, changes should go through pull requests. Every
|
|
676
678
|
PR to `main` must bump both `Cargo.toml` and `package.json` above the base
|
package/package.json
CHANGED
package/src/config.rs
CHANGED
|
@@ -94,10 +94,11 @@ pub struct DuplicateConfig {
|
|
|
94
94
|
/// Detection mode.
|
|
95
95
|
#[serde(default)]
|
|
96
96
|
pub mode: Option<DuplicateMode>,
|
|
97
|
-
/// Minimum tokens per clone.
|
|
97
|
+
/// Minimum normalized tokens per clone window.
|
|
98
98
|
#[serde(default, alias = "minTokens")]
|
|
99
99
|
pub min_tokens: Option<usize>,
|
|
100
|
-
/// Minimum lines per clone
|
|
100
|
+
/// Minimum normalized lines per clone window; sparse windows may grow past
|
|
101
|
+
/// this to satisfy `min_tokens`.
|
|
101
102
|
#[serde(default, alias = "minLines")]
|
|
102
103
|
pub min_lines: Option<usize>,
|
|
103
104
|
/// Minimum clone instances per group.
|
package/src/dupes/tests.rs
CHANGED
|
@@ -75,6 +75,39 @@ fn filters_short_blocks_and_generated_files() -> Result<(), Box<dyn std::error::
|
|
|
75
75
|
Ok(())
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
#[test]
|
|
79
|
+
fn detects_sparse_duplicate_blocks_that_meet_default_tokens_as_a_whole()
|
|
80
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
81
|
+
let fixture = tempfile::tempdir()?;
|
|
82
|
+
write(&fixture, "pubspec.yaml", "name: app\n")?;
|
|
83
|
+
let helper = " static String formatLabel({\n required String value,\n required String fallback,\n required bool useFallback,\n required String prefix,\n }) {\n if (useFallback) {\n return '$prefix ${fallback.trim()}';\n }\n\n final normalized = value.trim().toLowerCase();\n return '$prefix $normalized';\n }\n";
|
|
84
|
+
write(
|
|
85
|
+
&fixture,
|
|
86
|
+
"lib/a.dart",
|
|
87
|
+
&format!("class AFormatter {{\n{helper} int get aOnly => 1;\n}}\n"),
|
|
88
|
+
)?;
|
|
89
|
+
write(
|
|
90
|
+
&fixture,
|
|
91
|
+
"lib/b.dart",
|
|
92
|
+
&format!("class BFormatter {{\n{helper} int get bOnly => 2;\n}}\n"),
|
|
93
|
+
)?;
|
|
94
|
+
let project = scan_project(fixture.path())?;
|
|
95
|
+
|
|
96
|
+
let report = detect_duplicates(&project, &DuplicateOptions::default())?;
|
|
97
|
+
|
|
98
|
+
assert_eq!(report.clone_groups.len(), 1);
|
|
99
|
+
let clone = &report.clone_groups[0];
|
|
100
|
+
assert!(clone.line_count > DuplicateOptions::default().min_lines);
|
|
101
|
+
assert!(clone.token_count >= DuplicateOptions::default().min_tokens);
|
|
102
|
+
assert_eq!(clone.instances.len(), 2);
|
|
103
|
+
assert_eq!(clone.instances[0].path, fixture.path().join("lib/a.dart"));
|
|
104
|
+
assert_eq!(clone.instances[0].start_line, 2);
|
|
105
|
+
assert_eq!(clone.instances[1].path, fixture.path().join("lib/b.dart"));
|
|
106
|
+
assert_eq!(clone.instances[1].start_line, 2);
|
|
107
|
+
|
|
108
|
+
Ok(())
|
|
109
|
+
}
|
|
110
|
+
|
|
78
111
|
#[test]
|
|
79
112
|
fn trace_clone_matches_fingerprint_and_source_line() -> Result<(), Box<dyn std::error::Error>> {
|
|
80
113
|
let fixture = tempfile::tempdir()?;
|
package/src/dupes.rs
CHANGED
|
@@ -48,9 +48,10 @@ impl DuplicateMode {
|
|
|
48
48
|
pub struct DuplicateOptions {
|
|
49
49
|
/// Detection mode.
|
|
50
50
|
pub mode: DuplicateMode,
|
|
51
|
-
/// Minimum token count per clone.
|
|
51
|
+
/// Minimum normalized token count per clone window.
|
|
52
52
|
pub min_tokens: usize,
|
|
53
|
-
/// Minimum line count per clone
|
|
53
|
+
/// Minimum normalized line count per clone window; sparse windows may grow
|
|
54
|
+
/// past this to satisfy `min_tokens`.
|
|
54
55
|
pub min_lines: usize,
|
|
55
56
|
/// Minimum occurrences before a clone group is reported.
|
|
56
57
|
pub min_occurrences: usize,
|
|
@@ -211,9 +212,9 @@ pub struct CodeClone {
|
|
|
211
212
|
pub fingerprint: String,
|
|
212
213
|
/// Matching block instances.
|
|
213
214
|
pub instances: Vec<CodeCloneInstance>,
|
|
214
|
-
///
|
|
215
|
+
/// Normalized lines in the matched duplicated block.
|
|
215
216
|
pub line_count: usize,
|
|
216
|
-
///
|
|
217
|
+
/// Normalized tokens in the matched duplicated block.
|
|
217
218
|
pub token_count: usize,
|
|
218
219
|
}
|
|
219
220
|
|
|
@@ -256,9 +257,9 @@ pub struct CloneTraceReport {
|
|
|
256
257
|
pub struct TraceCloneGroup {
|
|
257
258
|
/// Stable clone fingerprint.
|
|
258
259
|
pub fingerprint: String,
|
|
259
|
-
///
|
|
260
|
+
/// Normalized lines in the matched duplicated block.
|
|
260
261
|
pub line_count: usize,
|
|
261
|
-
///
|
|
262
|
+
/// Normalized tokens in the matched duplicated block.
|
|
262
263
|
pub token_count: usize,
|
|
263
264
|
/// Matching block instances.
|
|
264
265
|
pub instances: Vec<TraceCloneInstance>,
|
|
@@ -299,10 +300,21 @@ struct CloneOccurrence {
|
|
|
299
300
|
end_line: usize,
|
|
300
301
|
column: usize,
|
|
301
302
|
parent: PathBuf,
|
|
303
|
+
line_count: usize,
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
307
|
+
struct CloneWindow {
|
|
308
|
+
start: usize,
|
|
309
|
+
end: usize,
|
|
310
|
+
token_count: usize,
|
|
302
311
|
}
|
|
303
312
|
|
|
304
313
|
/// Detect duplicated Dart code blocks.
|
|
305
314
|
///
|
|
315
|
+
/// Clone windows satisfy both `min_lines` and `min_tokens`; sparse duplicated
|
|
316
|
+
/// blocks can therefore span more than `min_lines`.
|
|
317
|
+
///
|
|
306
318
|
/// # Errors
|
|
307
319
|
///
|
|
308
320
|
/// Returns [`DuplicateCodeError`] if a scanned Dart file cannot be read.
|
|
@@ -328,11 +340,8 @@ pub fn detect_duplicates(
|
|
|
328
340
|
continue;
|
|
329
341
|
}
|
|
330
342
|
|
|
331
|
-
for
|
|
332
|
-
let
|
|
333
|
-
if token_count < options.min_tokens {
|
|
334
|
-
continue;
|
|
335
|
-
}
|
|
343
|
+
for clone_window in clone_windows(&lines, options) {
|
|
344
|
+
let window = &lines[clone_window.start..=clone_window.end];
|
|
336
345
|
let text = window
|
|
337
346
|
.iter()
|
|
338
347
|
.map(|line| line.text.as_str())
|
|
@@ -352,8 +361,9 @@ pub fn detect_duplicates(
|
|
|
352
361
|
start_line: first.line,
|
|
353
362
|
end_line: last.line,
|
|
354
363
|
column: first.column,
|
|
364
|
+
line_count: window.len(),
|
|
355
365
|
},
|
|
356
|
-
token_count,
|
|
366
|
+
clone_window.token_count,
|
|
357
367
|
));
|
|
358
368
|
}
|
|
359
369
|
}
|
|
@@ -474,6 +484,32 @@ fn ranges_overlap(
|
|
|
474
484
|
left_start <= right_end && right_start <= left_end
|
|
475
485
|
}
|
|
476
486
|
|
|
487
|
+
fn clone_windows(lines: &[lex::NormalizedLine], options: &DuplicateOptions) -> Vec<CloneWindow> {
|
|
488
|
+
let min_lines = options.min_lines.max(1);
|
|
489
|
+
let mut windows = Vec::new();
|
|
490
|
+
let mut end = 0;
|
|
491
|
+
let mut token_count = 0;
|
|
492
|
+
|
|
493
|
+
for start in 0..lines.len() {
|
|
494
|
+
while end < lines.len() && (end - start < min_lines || token_count < options.min_tokens) {
|
|
495
|
+
token_count += lines[end].token_count;
|
|
496
|
+
end += 1;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
if end - start >= min_lines && token_count >= options.min_tokens {
|
|
500
|
+
windows.push(CloneWindow {
|
|
501
|
+
start,
|
|
502
|
+
end: end - 1,
|
|
503
|
+
token_count,
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
token_count -= lines[start].token_count;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
windows
|
|
511
|
+
}
|
|
512
|
+
|
|
477
513
|
fn duplicate_stats(
|
|
478
514
|
analyzed_lines: usize,
|
|
479
515
|
clone_groups: &[CodeClone],
|
|
@@ -513,6 +549,7 @@ fn clone_group_from_occurrences(
|
|
|
513
549
|
let mut seen = BTreeSet::<(PathBuf, usize, usize)>::new();
|
|
514
550
|
let mut instances = Vec::new();
|
|
515
551
|
let mut token_count = 0;
|
|
552
|
+
let mut line_count = 0;
|
|
516
553
|
let mut parents = BTreeSet::new();
|
|
517
554
|
|
|
518
555
|
for (occurrence, tokens) in occurrences {
|
|
@@ -525,6 +562,7 @@ fn clone_group_from_occurrences(
|
|
|
525
562
|
}
|
|
526
563
|
parents.insert(occurrence.parent);
|
|
527
564
|
token_count = token_count.max(tokens);
|
|
565
|
+
line_count = line_count.max(occurrence.line_count);
|
|
528
566
|
instances.push(CodeCloneInstance {
|
|
529
567
|
path: occurrence.path,
|
|
530
568
|
start_line: occurrence.start_line,
|
|
@@ -547,7 +585,7 @@ fn clone_group_from_occurrences(
|
|
|
547
585
|
Some(CodeClone {
|
|
548
586
|
fingerprint: fingerprint.to_owned(),
|
|
549
587
|
instances,
|
|
550
|
-
line_count
|
|
588
|
+
line_count,
|
|
551
589
|
token_count,
|
|
552
590
|
})
|
|
553
591
|
}
|
|
@@ -13,9 +13,9 @@ pub struct JsonCloneGroup {
|
|
|
13
13
|
pub fingerprint: String,
|
|
14
14
|
/// Matching clone instances.
|
|
15
15
|
pub instances: Vec<JsonCloneInstance>,
|
|
16
|
-
///
|
|
16
|
+
/// Normalized lines in the matched duplicated block.
|
|
17
17
|
pub line_count: usize,
|
|
18
|
-
///
|
|
18
|
+
/// Normalized tokens in the matched duplicated block.
|
|
19
19
|
pub token_count: usize,
|
|
20
20
|
}
|
|
21
21
|
|