dart-decimate 0.0.9 → 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.
- package/Cargo.lock +1 -1
- package/Cargo.toml +1 -1
- package/README.md +50 -58
- package/package.json +1 -1
- package/src/cli/analyze.rs +6 -3
- package/src/cli/entry_points.rs +18 -2
- package/src/cli/list_run.rs +1 -0
- package/src/cli/tests.rs +74 -0
- package/src/config/rules.rs +50 -13
- package/src/dependencies/analyze.rs +7 -1
- package/src/dependencies/tests.rs +29 -0
- package/src/dependencies.rs +19 -0
- package/src/dependency_scripts.rs +2 -0
- package/src/dupes/declarations.rs +512 -0
- package/src/dupes/packages.rs +213 -0
- package/src/dupes/tests.rs +197 -0
- package/src/dupes.rs +57 -19
- package/src/extract/reference_tests.rs +106 -0
- package/src/extract/references.rs +264 -2
- package/src/extract/tests.rs +25 -0
- package/src/extract.rs +6 -3
- package/src/generated.rs +96 -3
- package/src/graph/package_config_tests.rs +123 -0
- package/src/graph/tests.rs +125 -0
- package/src/graph.rs +6 -5
- package/src/output/feature_flag_findings.rs +63 -1
- package/src/output/graph_findings.rs +73 -37
- package/src/output/route_cycle/aliases.rs +875 -0
- package/src/output/route_cycle/navigation.rs +1005 -0
- package/src/output/route_cycle/receivers.rs +41 -0
- package/src/output/route_cycle/state_context.rs +209 -0
- package/src/output/route_cycle/tests.rs +175 -0
- package/src/output/route_cycle.rs +730 -0
- package/src/output/security_findings.rs +15 -2
- package/src/output/tests.rs +104 -1
- package/src/output.rs +2 -1
- package/src/package_map.rs +220 -28
- package/src/routes.rs +6 -1
- package/src/scan.rs +15 -5
- package/src/security/detect.rs +140 -6
- package/src/security/tests.rs +96 -0
- package/src/trace.rs +1 -1
- package/src/widgets/forwarding.rs +684 -0
- package/src/widgets/patterns.rs +2595 -0
- package/src/widgets/reads.rs +616 -0
- package/src/widgets/tests.rs +1837 -55
- package/src/widgets/unrendered.rs +150 -0
- package/src/widgets.rs +25 -93
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
use std::collections::{BTreeMap, BTreeSet};
|
|
2
|
+
use std::path::{Path, PathBuf};
|
|
3
|
+
|
|
4
|
+
use crate::dependencies::{LocalPubPackage, local_pub_package_from_pubspec};
|
|
5
|
+
|
|
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)>;
|
|
11
|
+
|
|
12
|
+
pub(super) struct CopiedPackageFilter {
|
|
13
|
+
root: PathBuf,
|
|
14
|
+
package_cache: BTreeMap<PathBuf, Option<LocalPubPackage>>,
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
impl CopiedPackageFilter {
|
|
18
|
+
pub(super) fn new(root: &Path) -> Self {
|
|
19
|
+
Self {
|
|
20
|
+
root: root.to_path_buf(),
|
|
21
|
+
package_cache: BTreeMap::new(),
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
pub(super) fn is_copied_package_clone(&mut self, group: &CodeClone) -> bool {
|
|
26
|
+
if group.instances.len() < 2 {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
let mut roots = BTreeSet::new();
|
|
30
|
+
let mut identity = None;
|
|
31
|
+
for instance in &group.instances {
|
|
32
|
+
let Some(package) = self.owning_package(&instance.path) else {
|
|
33
|
+
return false;
|
|
34
|
+
};
|
|
35
|
+
let Ok(relative_path) = instance.path.strip_prefix(&package.root) else {
|
|
36
|
+
return false;
|
|
37
|
+
};
|
|
38
|
+
roots.insert(package.root.clone());
|
|
39
|
+
let current = (
|
|
40
|
+
package.name.clone(),
|
|
41
|
+
relative_path.to_path_buf(),
|
|
42
|
+
instance.start_line,
|
|
43
|
+
instance.end_line,
|
|
44
|
+
instance.column,
|
|
45
|
+
);
|
|
46
|
+
if identity.is_none() {
|
|
47
|
+
identity = Some(current);
|
|
48
|
+
} else if identity != Some(current) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
roots.len() > 1
|
|
53
|
+
}
|
|
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
|
+
|
|
109
|
+
fn owning_package(&mut self, path: &Path) -> Option<LocalPubPackage> {
|
|
110
|
+
let mut current = path.parent();
|
|
111
|
+
while let Some(dir) = current {
|
|
112
|
+
if !dir.starts_with(&self.root) {
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
let pubspec_path = dir.join("pubspec.yaml");
|
|
116
|
+
if pubspec_path.is_file() {
|
|
117
|
+
return self
|
|
118
|
+
.package_cache
|
|
119
|
+
.entry(pubspec_path.clone())
|
|
120
|
+
.or_insert_with(|| local_pub_package_from_pubspec(&pubspec_path).ok().flatten())
|
|
121
|
+
.clone();
|
|
122
|
+
}
|
|
123
|
+
if dir == self.root {
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
current = dir.parent();
|
|
127
|
+
}
|
|
128
|
+
None
|
|
129
|
+
}
|
|
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
|
+
}
|
package/src/dupes/tests.rs
CHANGED
|
@@ -108,6 +108,160 @@ fn detects_sparse_duplicate_blocks_that_meet_default_tokens_as_a_whole()
|
|
|
108
108
|
Ok(())
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
+
#[test]
|
|
112
|
+
fn copied_package_filter_ignores_unrelated_malformed_pubspec()
|
|
113
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
114
|
+
let fixture = tempfile::tempdir()?;
|
|
115
|
+
write(&fixture, "pubspec.yaml", "name: app\n")?;
|
|
116
|
+
write(&fixture, "fixtures/bad/pubspec.yaml", "name: [\n")?;
|
|
117
|
+
for package in ["copy_a", "copy_b"] {
|
|
118
|
+
write(
|
|
119
|
+
&fixture,
|
|
120
|
+
&format!("{package}/pubspec.yaml"),
|
|
121
|
+
"name: copied_package\n",
|
|
122
|
+
)?;
|
|
123
|
+
write(
|
|
124
|
+
&fixture,
|
|
125
|
+
&format!("{package}/lib/shared.dart"),
|
|
126
|
+
"String sharedHttp() {\n final headers = <String, String>{\n 'accept': 'application/json',\n 'content-type': 'application/json',\n 'x-client': 'function-client',\n };\n final values = ['alpha', 'beta', 'gamma', headers.keys.join('|')];\n final normalized = values.map((value) => value.trim().toLowerCase()).where((value) => value.isNotEmpty).toList();\n return normalized.join(',');\n}\n",
|
|
127
|
+
)?;
|
|
128
|
+
}
|
|
129
|
+
let project = scan_project(fixture.path())?;
|
|
130
|
+
|
|
131
|
+
let report = detect_duplicates(&project, &DuplicateOptions::default())?;
|
|
132
|
+
|
|
133
|
+
assert!(report.clone_groups.is_empty());
|
|
134
|
+
|
|
135
|
+
Ok(())
|
|
136
|
+
}
|
|
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
|
+
|
|
111
265
|
#[test]
|
|
112
266
|
fn trace_clone_matches_fingerprint_and_source_line() -> Result<(), Box<dyn std::error::Error>> {
|
|
113
267
|
let fixture = tempfile::tempdir()?;
|
|
@@ -130,6 +284,35 @@ fn trace_clone_matches_fingerprint_and_source_line() -> Result<(), Box<dyn std::
|
|
|
130
284
|
Ok(())
|
|
131
285
|
}
|
|
132
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
|
+
|
|
133
316
|
fn options(mode: DuplicateMode, min_lines: usize, min_tokens: usize) -> DuplicateOptions {
|
|
134
317
|
DuplicateOptions {
|
|
135
318
|
mode,
|
|
@@ -143,6 +326,20 @@ fn options(mode: DuplicateMode, min_lines: usize, min_tokens: usize) -> Duplicat
|
|
|
143
326
|
}
|
|
144
327
|
}
|
|
145
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
|
+
|
|
146
343
|
fn write(fixture: &TempDir, path: &str, source: &str) -> Result<(), std::io::Error> {
|
|
147
344
|
let path = fixture.path().join(path);
|
|
148
345
|
if let Some(parent) = path.parent() {
|
package/src/dupes.rs
CHANGED
|
@@ -14,6 +14,10 @@ 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;
|
|
19
|
+
mod packages;
|
|
20
|
+
use packages::CopiedPackageFilter;
|
|
17
21
|
|
|
18
22
|
/// Dart duplicate-code detection mode.
|
|
19
23
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
@@ -313,7 +317,8 @@ struct CloneWindow {
|
|
|
313
317
|
/// Detect duplicated Dart code blocks.
|
|
314
318
|
///
|
|
315
319
|
/// Clone windows satisfy both `min_lines` and `min_tokens`; sparse duplicated
|
|
316
|
-
/// blocks can therefore span more than `min_lines`.
|
|
320
|
+
/// blocks can therefore span more than `min_lines`. Declaration-only abstract
|
|
321
|
+
/// contracts and copied local Pub package mirrors are filtered conservatively.
|
|
317
322
|
///
|
|
318
323
|
/// # Errors
|
|
319
324
|
///
|
|
@@ -375,23 +380,16 @@ pub fn detect_duplicates(
|
|
|
375
380
|
})
|
|
376
381
|
.collect::<Vec<_>>();
|
|
377
382
|
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
.cmp(&(
|
|
387
|
-
std::cmp::Reverse(right.instances.len()),
|
|
388
|
-
std::cmp::Reverse(right.line_count),
|
|
389
|
-
&right.instances[0].path,
|
|
390
|
-
right.instances[0].start_line,
|
|
391
|
-
&right.fingerprint,
|
|
392
|
-
))
|
|
393
|
-
});
|
|
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);
|
|
394
391
|
clone_groups = collapse_overlapping_groups(clone_groups);
|
|
392
|
+
clone_groups.retain(|group| !copied_packages.is_copied_package_clone(group));
|
|
395
393
|
let stats = duplicate_stats(analyzed_lines, &clone_groups, options.threshold);
|
|
396
394
|
if let Some(top) = options.top {
|
|
397
395
|
clone_groups.truncate(top);
|
|
@@ -448,6 +446,25 @@ pub fn render_clone_trace(report: &CloneTraceReport) -> String {
|
|
|
448
446
|
)
|
|
449
447
|
}
|
|
450
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
|
+
|
|
451
468
|
fn collapse_overlapping_groups(groups: Vec<CodeClone>) -> Vec<CodeClone> {
|
|
452
469
|
let mut collapsed = Vec::new();
|
|
453
470
|
for group in groups {
|
|
@@ -461,6 +478,25 @@ fn collapse_overlapping_groups(groups: Vec<CodeClone>) -> Vec<CodeClone> {
|
|
|
461
478
|
collapsed
|
|
462
479
|
}
|
|
463
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
|
+
|
|
464
500
|
fn groups_overlap(left: &CodeClone, right: &CodeClone) -> bool {
|
|
465
501
|
right.instances.iter().all(|right_instance| {
|
|
466
502
|
left.instances.iter().any(|left_instance| {
|
|
@@ -546,7 +582,7 @@ fn clone_group_from_occurrences(
|
|
|
546
582
|
occurrences: Vec<(CloneOccurrence, usize)>,
|
|
547
583
|
options: &DuplicateOptions,
|
|
548
584
|
) -> Option<CodeClone> {
|
|
549
|
-
let mut seen = BTreeSet::<(PathBuf, usize, usize)>::new();
|
|
585
|
+
let mut seen = BTreeSet::<(PathBuf, usize, usize, usize)>::new();
|
|
550
586
|
let mut instances = Vec::new();
|
|
551
587
|
let mut token_count = 0;
|
|
552
588
|
let mut line_count = 0;
|
|
@@ -557,6 +593,7 @@ fn clone_group_from_occurrences(
|
|
|
557
593
|
occurrence.path.clone(),
|
|
558
594
|
occurrence.start_line,
|
|
559
595
|
occurrence.end_line,
|
|
596
|
+
occurrence.column,
|
|
560
597
|
)) {
|
|
561
598
|
continue;
|
|
562
599
|
}
|
|
@@ -575,10 +612,11 @@ fn clone_group_from_occurrences(
|
|
|
575
612
|
return None;
|
|
576
613
|
}
|
|
577
614
|
instances.sort_by(|left, right| {
|
|
578
|
-
(&left.path, left.start_line, left.end_line).cmp(&(
|
|
615
|
+
(&left.path, left.start_line, left.end_line, left.column).cmp(&(
|
|
579
616
|
&right.path,
|
|
580
617
|
right.start_line,
|
|
581
618
|
right.end_line,
|
|
619
|
+
right.column,
|
|
582
620
|
))
|
|
583
621
|
});
|
|
584
622
|
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
use super::{ExtractError, extract_dart_source};
|
|
2
|
+
|
|
3
|
+
#[test]
|
|
4
|
+
fn raw_adjacent_string_segment_does_not_suppress_later_interpolation() -> Result<(), ExtractError> {
|
|
5
|
+
let source = "\
|
|
6
|
+
void boot(String _ignored, String _used) {
|
|
7
|
+
print(r'$_ignored' '$_used');
|
|
8
|
+
}
|
|
9
|
+
";
|
|
10
|
+
|
|
11
|
+
let extracted = extract_dart_source("lib/interpolation.dart", source)?;
|
|
12
|
+
let references = extracted
|
|
13
|
+
.references
|
|
14
|
+
.iter()
|
|
15
|
+
.map(|reference| reference.name.as_str())
|
|
16
|
+
.collect::<Vec<_>>();
|
|
17
|
+
|
|
18
|
+
assert!(references.contains(&"_used"));
|
|
19
|
+
assert!(!references.contains(&"_ignored"));
|
|
20
|
+
|
|
21
|
+
Ok(())
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
#[test]
|
|
25
|
+
fn braced_interpolation_references_are_not_duplicated() -> Result<(), ExtractError> {
|
|
26
|
+
let source = "\
|
|
27
|
+
void boot(String suffix) {
|
|
28
|
+
print('${suffix.toUpperCase()} $suffix');
|
|
29
|
+
}
|
|
30
|
+
";
|
|
31
|
+
|
|
32
|
+
let extracted = extract_dart_source("lib/interpolation.dart", source)?;
|
|
33
|
+
let references = extracted
|
|
34
|
+
.references
|
|
35
|
+
.iter()
|
|
36
|
+
.map(|reference| reference.name.as_str())
|
|
37
|
+
.collect::<Vec<_>>();
|
|
38
|
+
|
|
39
|
+
assert_eq!(
|
|
40
|
+
references
|
|
41
|
+
.iter()
|
|
42
|
+
.filter(|reference| **reference == "suffix")
|
|
43
|
+
.count(),
|
|
44
|
+
2
|
|
45
|
+
);
|
|
46
|
+
assert_eq!(
|
|
47
|
+
references
|
|
48
|
+
.iter()
|
|
49
|
+
.filter(|reference| **reference == "toUpperCase")
|
|
50
|
+
.count(),
|
|
51
|
+
1
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
Ok(())
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
#[test]
|
|
58
|
+
fn braced_interpolation_ignores_comment_identifiers() -> Result<(), ExtractError> {
|
|
59
|
+
let source = "\
|
|
60
|
+
void boot(String value, String _block, String _line) {
|
|
61
|
+
print('''${/* _block */ value} ${value // _line
|
|
62
|
+
}''');
|
|
63
|
+
}
|
|
64
|
+
";
|
|
65
|
+
|
|
66
|
+
let extracted = extract_dart_source("lib/interpolation.dart", source)?;
|
|
67
|
+
let references = extracted
|
|
68
|
+
.references
|
|
69
|
+
.iter()
|
|
70
|
+
.map(|reference| reference.name.as_str())
|
|
71
|
+
.collect::<Vec<_>>();
|
|
72
|
+
|
|
73
|
+
assert_eq!(
|
|
74
|
+
references
|
|
75
|
+
.iter()
|
|
76
|
+
.filter(|reference| **reference == "value")
|
|
77
|
+
.count(),
|
|
78
|
+
2
|
|
79
|
+
);
|
|
80
|
+
assert!(!references.contains(&"_block"));
|
|
81
|
+
assert!(!references.contains(&"_line"));
|
|
82
|
+
|
|
83
|
+
Ok(())
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
#[test]
|
|
87
|
+
fn braced_interpolation_ignores_declaration_identifiers() -> Result<(), ExtractError> {
|
|
88
|
+
let source = "\
|
|
89
|
+
void boot(String value, String arg, String _unused) {
|
|
90
|
+
print('${((String _unused) => value)(arg)}');
|
|
91
|
+
}
|
|
92
|
+
";
|
|
93
|
+
|
|
94
|
+
let extracted = extract_dart_source("lib/interpolation.dart", source)?;
|
|
95
|
+
let references = extracted
|
|
96
|
+
.references
|
|
97
|
+
.iter()
|
|
98
|
+
.map(|reference| reference.name.as_str())
|
|
99
|
+
.collect::<Vec<_>>();
|
|
100
|
+
|
|
101
|
+
assert!(references.contains(&"value"));
|
|
102
|
+
assert!(references.contains(&"arg"));
|
|
103
|
+
assert!(!references.contains(&"_unused"));
|
|
104
|
+
|
|
105
|
+
Ok(())
|
|
106
|
+
}
|