dart-decimate 0.0.42 → 0.0.43
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/package.json +1 -1
- package/src/dupes/tests.rs +86 -0
- package/src/dupes.rs +23 -14
package/Cargo.lock
CHANGED
package/Cargo.toml
CHANGED
package/package.json
CHANGED
package/src/dupes/tests.rs
CHANGED
|
@@ -284,6 +284,25 @@ fn trace_clone_matches_fingerprint_and_source_line() -> Result<(), Box<dyn std::
|
|
|
284
284
|
Ok(())
|
|
285
285
|
}
|
|
286
286
|
|
|
287
|
+
#[test]
|
|
288
|
+
fn does_not_group_distinct_windows_with_a_colliding_fingerprint()
|
|
289
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
290
|
+
assert_fingerprint_collision_is_not_a_clone(
|
|
291
|
+
"lib/builder.dart",
|
|
292
|
+
"lib/validation.dart",
|
|
293
|
+
"void build4sh0() {\n final palette4sh0 = select(red, green, blue, amber, ivory, slate, navy, mint);\n final surface4sh0 = compose(palette4sh0, red, green, blue, amber, ivory, slate);\n final result4sh0 = create(surface4sh0, red, green, blue, amber, ivory, slate);\n report(result4sh0, red, green, blue, amber, ivory, slate);\n}",
|
|
294
|
+
"bool verify9zz() {\n final record9zz = collect(one, two, three, four, five, six, seven, eight);\n if (record9zz == one && record9zz == two && record9zz == three && record9zz == four) {\n return record9zz == five || record9zz == six || record9zz == seven || record9zz == eight;\n }\n return false;\n}",
|
|
295
|
+
)?;
|
|
296
|
+
assert_fingerprint_collision_is_not_a_clone(
|
|
297
|
+
"lib/invariant.dart",
|
|
298
|
+
"lib/catalog.dart",
|
|
299
|
+
"Record parse4o3l(Map values) {\n final stamp = values['stamp'] == null ? null : DateTime.parse(values['stamp']).toUtc();\n if (values['phase'] == 'sent' && stamp == null) throw StateError('missing');\n if (values['phase'] == 'ready' && stamp != null) throw StateError('unexpected');\n return Record(stamp: stamp, phase: values['phase']);\n}",
|
|
300
|
+
"Record localizedhed() {\n final state = Record(label: 'alpha', detail: 'beta', title: 'gamma', note: 'delta');\n if (state.label == 'alpha' && state.detail == 'beta') return state;\n if (state.title == 'gamma' || state.note == 'delta') return Record(label: 'next', detail: 'value');\n return state;\n}",
|
|
301
|
+
)?;
|
|
302
|
+
|
|
303
|
+
Ok(())
|
|
304
|
+
}
|
|
305
|
+
|
|
287
306
|
fn pagination_clone_source() -> &'static str {
|
|
288
307
|
r"Future<List<String>> fetchAllRowIds(Pages pages) async {
|
|
289
308
|
final values = <String>[];
|
|
@@ -327,6 +346,73 @@ fn options(mode: DuplicateMode, min_lines: usize, min_tokens: usize) -> Duplicat
|
|
|
327
346
|
}
|
|
328
347
|
}
|
|
329
348
|
|
|
349
|
+
fn assert_fingerprint_collision_is_not_a_clone(
|
|
350
|
+
first_path: &str,
|
|
351
|
+
second_path: &str,
|
|
352
|
+
first_source: &str,
|
|
353
|
+
second_source: &str,
|
|
354
|
+
) -> Result<(), Box<dyn std::error::Error>> {
|
|
355
|
+
let fixture = tempfile::tempdir()?;
|
|
356
|
+
let options = options(DuplicateMode::Mild, 5, 50);
|
|
357
|
+
write(&fixture, "pubspec.yaml", "name: app\n")?;
|
|
358
|
+
write(&fixture, first_path, first_source)?;
|
|
359
|
+
write(&fixture, second_path, second_source)?;
|
|
360
|
+
|
|
361
|
+
let first_window = first_clone_window_text(first_source, &options)?;
|
|
362
|
+
let second_window = first_clone_window_text(second_source, &options)?;
|
|
363
|
+
assert_ne!(first_window, second_window);
|
|
364
|
+
assert_eq!(
|
|
365
|
+
super::fingerprint(&first_window),
|
|
366
|
+
super::fingerprint(&second_window)
|
|
367
|
+
);
|
|
368
|
+
|
|
369
|
+
let project = scan_project(fixture.path())?;
|
|
370
|
+
let report = detect_duplicates(&project, &options)?;
|
|
371
|
+
assert!(report.clone_groups.is_empty());
|
|
372
|
+
|
|
373
|
+
write(&fixture, "lib/exact_copy.dart", first_source)?;
|
|
374
|
+
let project = scan_project(fixture.path())?;
|
|
375
|
+
let report = detect_duplicates(&project, &options)?;
|
|
376
|
+
assert_eq!(report.clone_groups.len(), 1);
|
|
377
|
+
assert_eq!(report.clone_groups[0].instances.len(), 2);
|
|
378
|
+
assert!(
|
|
379
|
+
report.clone_groups[0]
|
|
380
|
+
.instances
|
|
381
|
+
.iter()
|
|
382
|
+
.any(|instance| instance.path.ends_with(first_path))
|
|
383
|
+
);
|
|
384
|
+
assert!(
|
|
385
|
+
report.clone_groups[0]
|
|
386
|
+
.instances
|
|
387
|
+
.iter()
|
|
388
|
+
.any(|instance| instance.path.ends_with("lib/exact_copy.dart"))
|
|
389
|
+
);
|
|
390
|
+
assert!(
|
|
391
|
+
report.clone_groups[0]
|
|
392
|
+
.instances
|
|
393
|
+
.iter()
|
|
394
|
+
.all(|instance| !instance.path.ends_with(second_path))
|
|
395
|
+
);
|
|
396
|
+
|
|
397
|
+
Ok(())
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
fn first_clone_window_text(
|
|
401
|
+
source: &str,
|
|
402
|
+
options: &DuplicateOptions,
|
|
403
|
+
) -> Result<String, std::io::Error> {
|
|
404
|
+
let lines = super::lex::normalized_lines(source, options);
|
|
405
|
+
let window = super::clone_windows(&lines, options)
|
|
406
|
+
.into_iter()
|
|
407
|
+
.next()
|
|
408
|
+
.ok_or_else(|| std::io::Error::other("fixture contains a clone window"))?;
|
|
409
|
+
Ok(lines[window.start..=window.end]
|
|
410
|
+
.iter()
|
|
411
|
+
.map(|line| line.text.as_str())
|
|
412
|
+
.collect::<Vec<_>>()
|
|
413
|
+
.join("\n"))
|
|
414
|
+
}
|
|
415
|
+
|
|
330
416
|
fn instance(
|
|
331
417
|
path: &std::path::Path,
|
|
332
418
|
start_line: usize,
|
package/src/dupes.rs
CHANGED
|
@@ -337,7 +337,9 @@ pub fn detect_duplicates(
|
|
|
337
337
|
project: &ScannedProject,
|
|
338
338
|
options: &DuplicateOptions,
|
|
339
339
|
) -> Result<DuplicateCodeReport, DuplicateCodeError> {
|
|
340
|
-
|
|
340
|
+
// Fingerprints identify report groups, but exact normalized text defines a clone.
|
|
341
|
+
let mut by_fingerprint =
|
|
342
|
+
BTreeMap::<String, BTreeMap<String, Vec<(CloneOccurrence, usize)>>>::new();
|
|
341
343
|
let mut analyzed_lines = 0usize;
|
|
342
344
|
|
|
343
345
|
for file in &project.files {
|
|
@@ -369,24 +371,31 @@ pub fn detect_duplicates(
|
|
|
369
371
|
continue;
|
|
370
372
|
};
|
|
371
373
|
let fingerprint = fingerprint(&text);
|
|
372
|
-
by_fingerprint
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
374
|
+
by_fingerprint
|
|
375
|
+
.entry(fingerprint)
|
|
376
|
+
.or_default()
|
|
377
|
+
.entry(text)
|
|
378
|
+
.or_default()
|
|
379
|
+
.push((
|
|
380
|
+
CloneOccurrence {
|
|
381
|
+
parent: path.parent().map_or_else(PathBuf::new, Path::to_path_buf),
|
|
382
|
+
path: path.clone(),
|
|
383
|
+
start_line: first.line,
|
|
384
|
+
end_line: last.line,
|
|
385
|
+
column: first.column,
|
|
386
|
+
line_count: window.len(),
|
|
387
|
+
},
|
|
388
|
+
clone_window.token_count,
|
|
389
|
+
));
|
|
383
390
|
}
|
|
384
391
|
}
|
|
385
392
|
|
|
386
393
|
let mut clone_groups = by_fingerprint
|
|
387
394
|
.into_iter()
|
|
388
|
-
.
|
|
389
|
-
|
|
395
|
+
.flat_map(|(fingerprint, by_text)| {
|
|
396
|
+
by_text.into_values().filter_map(move |occurrences| {
|
|
397
|
+
clone_group_from_occurrences(&fingerprint, occurrences, options)
|
|
398
|
+
})
|
|
390
399
|
})
|
|
391
400
|
.collect::<Vec<_>>();
|
|
392
401
|
|