dart-decimate 0.0.8 → 0.0.10
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 +43 -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/config.rs +3 -2
- 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/packages.rs +71 -0
- package/src/dupes/tests.rs +60 -0
- package/src/dupes.rs +56 -13
- 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/duplication_findings.rs +2 -2
- package/src/output/feature_flag_findings.rs +63 -1
- package/src/output/graph_findings.rs +23 -21
- package/src/output/security_findings.rs +15 -2
- package/src/output/tests.rs +104 -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/dupes/tests.rs
CHANGED
|
@@ -75,6 +75,66 @@ 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
|
+
|
|
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
|
+
|
|
78
138
|
#[test]
|
|
79
139
|
fn trace_clone_matches_fingerprint_and_source_line() -> Result<(), Box<dyn std::error::Error>> {
|
|
80
140
|
let fixture = tempfile::tempdir()?;
|
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 packages;
|
|
18
|
+
use packages::CopiedPackageFilter;
|
|
17
19
|
|
|
18
20
|
/// Dart duplicate-code detection mode.
|
|
19
21
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
@@ -48,9 +50,10 @@ impl DuplicateMode {
|
|
|
48
50
|
pub struct DuplicateOptions {
|
|
49
51
|
/// Detection mode.
|
|
50
52
|
pub mode: DuplicateMode,
|
|
51
|
-
/// Minimum token count per clone.
|
|
53
|
+
/// Minimum normalized token count per clone window.
|
|
52
54
|
pub min_tokens: usize,
|
|
53
|
-
/// Minimum line count per clone
|
|
55
|
+
/// Minimum normalized line count per clone window; sparse windows may grow
|
|
56
|
+
/// past this to satisfy `min_tokens`.
|
|
54
57
|
pub min_lines: usize,
|
|
55
58
|
/// Minimum occurrences before a clone group is reported.
|
|
56
59
|
pub min_occurrences: usize,
|
|
@@ -211,9 +214,9 @@ pub struct CodeClone {
|
|
|
211
214
|
pub fingerprint: String,
|
|
212
215
|
/// Matching block instances.
|
|
213
216
|
pub instances: Vec<CodeCloneInstance>,
|
|
214
|
-
///
|
|
217
|
+
/// Normalized lines in the matched duplicated block.
|
|
215
218
|
pub line_count: usize,
|
|
216
|
-
///
|
|
219
|
+
/// Normalized tokens in the matched duplicated block.
|
|
217
220
|
pub token_count: usize,
|
|
218
221
|
}
|
|
219
222
|
|
|
@@ -256,9 +259,9 @@ pub struct CloneTraceReport {
|
|
|
256
259
|
pub struct TraceCloneGroup {
|
|
257
260
|
/// Stable clone fingerprint.
|
|
258
261
|
pub fingerprint: String,
|
|
259
|
-
///
|
|
262
|
+
/// Normalized lines in the matched duplicated block.
|
|
260
263
|
pub line_count: usize,
|
|
261
|
-
///
|
|
264
|
+
/// Normalized tokens in the matched duplicated block.
|
|
262
265
|
pub token_count: usize,
|
|
263
266
|
/// Matching block instances.
|
|
264
267
|
pub instances: Vec<TraceCloneInstance>,
|
|
@@ -299,10 +302,22 @@ struct CloneOccurrence {
|
|
|
299
302
|
end_line: usize,
|
|
300
303
|
column: usize,
|
|
301
304
|
parent: PathBuf,
|
|
305
|
+
line_count: usize,
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
309
|
+
struct CloneWindow {
|
|
310
|
+
start: usize,
|
|
311
|
+
end: usize,
|
|
312
|
+
token_count: usize,
|
|
302
313
|
}
|
|
303
314
|
|
|
304
315
|
/// Detect duplicated Dart code blocks.
|
|
305
316
|
///
|
|
317
|
+
/// 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
|
+
///
|
|
306
321
|
/// # Errors
|
|
307
322
|
///
|
|
308
323
|
/// Returns [`DuplicateCodeError`] if a scanned Dart file cannot be read.
|
|
@@ -328,11 +343,8 @@ pub fn detect_duplicates(
|
|
|
328
343
|
continue;
|
|
329
344
|
}
|
|
330
345
|
|
|
331
|
-
for
|
|
332
|
-
let
|
|
333
|
-
if token_count < options.min_tokens {
|
|
334
|
-
continue;
|
|
335
|
-
}
|
|
346
|
+
for clone_window in clone_windows(&lines, options) {
|
|
347
|
+
let window = &lines[clone_window.start..=clone_window.end];
|
|
336
348
|
let text = window
|
|
337
349
|
.iter()
|
|
338
350
|
.map(|line| line.text.as_str())
|
|
@@ -352,8 +364,9 @@ pub fn detect_duplicates(
|
|
|
352
364
|
start_line: first.line,
|
|
353
365
|
end_line: last.line,
|
|
354
366
|
column: first.column,
|
|
367
|
+
line_count: window.len(),
|
|
355
368
|
},
|
|
356
|
-
token_count,
|
|
369
|
+
clone_window.token_count,
|
|
357
370
|
));
|
|
358
371
|
}
|
|
359
372
|
}
|
|
@@ -382,6 +395,8 @@ pub fn detect_duplicates(
|
|
|
382
395
|
))
|
|
383
396
|
});
|
|
384
397
|
clone_groups = collapse_overlapping_groups(clone_groups);
|
|
398
|
+
let mut copied_packages = CopiedPackageFilter::new(&project.root);
|
|
399
|
+
clone_groups.retain(|group| !copied_packages.is_copied_package_clone(group));
|
|
385
400
|
let stats = duplicate_stats(analyzed_lines, &clone_groups, options.threshold);
|
|
386
401
|
if let Some(top) = options.top {
|
|
387
402
|
clone_groups.truncate(top);
|
|
@@ -474,6 +489,32 @@ fn ranges_overlap(
|
|
|
474
489
|
left_start <= right_end && right_start <= left_end
|
|
475
490
|
}
|
|
476
491
|
|
|
492
|
+
fn clone_windows(lines: &[lex::NormalizedLine], options: &DuplicateOptions) -> Vec<CloneWindow> {
|
|
493
|
+
let min_lines = options.min_lines.max(1);
|
|
494
|
+
let mut windows = Vec::new();
|
|
495
|
+
let mut end = 0;
|
|
496
|
+
let mut token_count = 0;
|
|
497
|
+
|
|
498
|
+
for start in 0..lines.len() {
|
|
499
|
+
while end < lines.len() && (end - start < min_lines || token_count < options.min_tokens) {
|
|
500
|
+
token_count += lines[end].token_count;
|
|
501
|
+
end += 1;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
if end - start >= min_lines && token_count >= options.min_tokens {
|
|
505
|
+
windows.push(CloneWindow {
|
|
506
|
+
start,
|
|
507
|
+
end: end - 1,
|
|
508
|
+
token_count,
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
token_count -= lines[start].token_count;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
windows
|
|
516
|
+
}
|
|
517
|
+
|
|
477
518
|
fn duplicate_stats(
|
|
478
519
|
analyzed_lines: usize,
|
|
479
520
|
clone_groups: &[CodeClone],
|
|
@@ -513,6 +554,7 @@ fn clone_group_from_occurrences(
|
|
|
513
554
|
let mut seen = BTreeSet::<(PathBuf, usize, usize)>::new();
|
|
514
555
|
let mut instances = Vec::new();
|
|
515
556
|
let mut token_count = 0;
|
|
557
|
+
let mut line_count = 0;
|
|
516
558
|
let mut parents = BTreeSet::new();
|
|
517
559
|
|
|
518
560
|
for (occurrence, tokens) in occurrences {
|
|
@@ -525,6 +567,7 @@ fn clone_group_from_occurrences(
|
|
|
525
567
|
}
|
|
526
568
|
parents.insert(occurrence.parent);
|
|
527
569
|
token_count = token_count.max(tokens);
|
|
570
|
+
line_count = line_count.max(occurrence.line_count);
|
|
528
571
|
instances.push(CodeCloneInstance {
|
|
529
572
|
path: occurrence.path,
|
|
530
573
|
start_line: occurrence.start_line,
|
|
@@ -547,7 +590,7 @@ fn clone_group_from_occurrences(
|
|
|
547
590
|
Some(CodeClone {
|
|
548
591
|
fingerprint: fingerprint.to_owned(),
|
|
549
592
|
instances,
|
|
550
|
-
line_count
|
|
593
|
+
line_count,
|
|
551
594
|
token_count,
|
|
552
595
|
})
|
|
553
596
|
}
|
|
@@ -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
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
use tree_sitter::Node;
|
|
2
2
|
|
|
3
|
-
use super::IdentifierReference;
|
|
3
|
+
use super::{IdentifierReference, Location};
|
|
4
4
|
|
|
5
5
|
pub(super) fn extract_identifier_references(
|
|
6
6
|
root: Node<'_>,
|
|
@@ -16,6 +16,10 @@ fn collect_identifier_references(
|
|
|
16
16
|
source: &str,
|
|
17
17
|
references: &mut Vec<IdentifierReference>,
|
|
18
18
|
) {
|
|
19
|
+
if node.kind() == "string_literal" {
|
|
20
|
+
collect_string_interpolation_references(node, source, references);
|
|
21
|
+
}
|
|
22
|
+
|
|
19
23
|
if is_reference_identifier(node, source) {
|
|
20
24
|
if let Ok(name) = node.utf8_text(source.as_bytes()) {
|
|
21
25
|
references.push(IdentifierReference {
|
|
@@ -26,11 +30,269 @@ fn collect_identifier_references(
|
|
|
26
30
|
}
|
|
27
31
|
|
|
28
32
|
let mut cursor = node.walk();
|
|
29
|
-
for child in node.
|
|
33
|
+
for child in node.children(&mut cursor) {
|
|
30
34
|
collect_identifier_references(child, source, references);
|
|
31
35
|
}
|
|
32
36
|
}
|
|
33
37
|
|
|
38
|
+
fn collect_string_interpolation_references(
|
|
39
|
+
node: Node<'_>,
|
|
40
|
+
source: &str,
|
|
41
|
+
references: &mut Vec<IdentifierReference>,
|
|
42
|
+
) {
|
|
43
|
+
let mut cursor = node.walk();
|
|
44
|
+
let segments = node
|
|
45
|
+
.children(&mut cursor)
|
|
46
|
+
.filter(|child| is_string_literal_segment(child.kind()))
|
|
47
|
+
.collect::<Vec<_>>();
|
|
48
|
+
if !segments.is_empty() {
|
|
49
|
+
for segment in segments {
|
|
50
|
+
if is_raw_string_literal_segment(segment.kind()) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
let Ok(text) = segment.utf8_text(source.as_bytes()) else {
|
|
54
|
+
continue;
|
|
55
|
+
};
|
|
56
|
+
collect_string_segment_interpolation_references(
|
|
57
|
+
text,
|
|
58
|
+
segment.start_byte(),
|
|
59
|
+
source,
|
|
60
|
+
references,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let Ok(text) = node.utf8_text(source.as_bytes()) else {
|
|
67
|
+
return;
|
|
68
|
+
};
|
|
69
|
+
if is_raw_string_literal(text) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
collect_string_segment_interpolation_references(text, node.start_byte(), source, references);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
fn collect_string_segment_interpolation_references(
|
|
76
|
+
text: &str,
|
|
77
|
+
base_byte: usize,
|
|
78
|
+
source: &str,
|
|
79
|
+
references: &mut Vec<IdentifierReference>,
|
|
80
|
+
) {
|
|
81
|
+
let bytes = text.as_bytes();
|
|
82
|
+
let mut index = 0;
|
|
83
|
+
while let Some(relative) = bytes[index..].iter().position(|byte| *byte == b'$') {
|
|
84
|
+
let dollar = index + relative;
|
|
85
|
+
index = dollar + 1;
|
|
86
|
+
if is_escaped_dollar(bytes, dollar) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
let interpolation_start = dollar + 1;
|
|
90
|
+
if bytes.get(interpolation_start) == Some(&b'{') {
|
|
91
|
+
let Some(end) = interpolation_body_end(text, interpolation_start + 1) else {
|
|
92
|
+
continue;
|
|
93
|
+
};
|
|
94
|
+
index = end + 1;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if let Some((name, start)) = interpolation_identifier(text, interpolation_start) {
|
|
98
|
+
references.push(IdentifierReference {
|
|
99
|
+
name: name.to_owned(),
|
|
100
|
+
location: location_at(source, base_byte + start),
|
|
101
|
+
});
|
|
102
|
+
index = start + name.len();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
fn is_string_literal_segment(kind: &str) -> bool {
|
|
108
|
+
kind.starts_with("raw_string_literal") || kind.starts_with("string_literal_")
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
fn is_raw_string_literal_segment(kind: &str) -> bool {
|
|
112
|
+
kind.starts_with("raw_string_literal")
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
fn is_raw_string_literal(text: &str) -> bool {
|
|
116
|
+
text.trim_start().starts_with("r'")
|
|
117
|
+
|| text.trim_start().starts_with("r\"")
|
|
118
|
+
|| text.trim_start().starts_with("R'")
|
|
119
|
+
|| text.trim_start().starts_with("R\"")
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
fn is_escaped_dollar(bytes: &[u8], dollar: usize) -> bool {
|
|
123
|
+
let mut slash_count = 0;
|
|
124
|
+
let mut index = dollar;
|
|
125
|
+
while index > 0 && bytes[index - 1] == b'\\' {
|
|
126
|
+
slash_count += 1;
|
|
127
|
+
index -= 1;
|
|
128
|
+
}
|
|
129
|
+
slash_count % 2 == 1
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
fn interpolation_identifier(text: &str, start: usize) -> Option<(&str, usize)> {
|
|
133
|
+
let bytes = text.as_bytes();
|
|
134
|
+
let end = identifier_end(bytes, start)?;
|
|
135
|
+
Some((&text[start..end], start))
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
fn interpolation_body_end(text: &str, start: usize) -> Option<usize> {
|
|
139
|
+
let bytes = text.as_bytes();
|
|
140
|
+
let mut index = start;
|
|
141
|
+
let mut brace_depth = 0;
|
|
142
|
+
while index < bytes.len() {
|
|
143
|
+
if line_comment_start(bytes, index) {
|
|
144
|
+
index = line_comment_end(bytes, index);
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if block_comment_start(bytes, index) {
|
|
148
|
+
index = block_comment_end(bytes, index);
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
match bytes[index] {
|
|
152
|
+
b'}' if brace_depth == 0 => return Some(index),
|
|
153
|
+
b'}' => brace_depth -= 1,
|
|
154
|
+
b'{' => brace_depth += 1,
|
|
155
|
+
b'r' | b'R' if raw_quoted_segment_start(bytes, index).is_some() => {
|
|
156
|
+
index = raw_quoted_segment_end(bytes, index);
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
b'\'' | b'"' => {
|
|
160
|
+
index = quoted_segment_end(bytes, index);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
_ => {}
|
|
164
|
+
}
|
|
165
|
+
index += 1;
|
|
166
|
+
}
|
|
167
|
+
None
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
fn line_comment_start(bytes: &[u8], start: usize) -> bool {
|
|
171
|
+
bytes.get(start) == Some(&b'/') && bytes.get(start + 1) == Some(&b'/')
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
fn line_comment_end(bytes: &[u8], start: usize) -> usize {
|
|
175
|
+
bytes[start + 2..]
|
|
176
|
+
.iter()
|
|
177
|
+
.position(|byte| *byte == b'\n')
|
|
178
|
+
.map_or(bytes.len(), |relative| start + 2 + relative + 1)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
fn block_comment_start(bytes: &[u8], start: usize) -> bool {
|
|
182
|
+
bytes.get(start) == Some(&b'/') && bytes.get(start + 1) == Some(&b'*')
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
fn block_comment_end(bytes: &[u8], start: usize) -> usize {
|
|
186
|
+
let mut index = start + 2;
|
|
187
|
+
let mut depth = 1;
|
|
188
|
+
while index + 1 < bytes.len() {
|
|
189
|
+
if block_comment_start(bytes, index) {
|
|
190
|
+
depth += 1;
|
|
191
|
+
index += 2;
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
if bytes[index] == b'*' && bytes[index + 1] == b'/' {
|
|
195
|
+
depth -= 1;
|
|
196
|
+
index += 2;
|
|
197
|
+
if depth == 0 {
|
|
198
|
+
return index;
|
|
199
|
+
}
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
index += 1;
|
|
203
|
+
}
|
|
204
|
+
bytes.len()
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
fn raw_quoted_segment_start(bytes: &[u8], start: usize) -> Option<usize> {
|
|
208
|
+
let quote_start = start + 1;
|
|
209
|
+
matches!(bytes.get(quote_start), Some(b'\'' | b'"')).then_some(quote_start)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
fn raw_quoted_segment_end(bytes: &[u8], start: usize) -> usize {
|
|
213
|
+
let Some(quote_start) = raw_quoted_segment_start(bytes, start) else {
|
|
214
|
+
return start + 1;
|
|
215
|
+
};
|
|
216
|
+
let quote = bytes[quote_start];
|
|
217
|
+
let is_triple =
|
|
218
|
+
bytes.get(quote_start + 1) == Some("e) && bytes.get(quote_start + 2) == Some("e);
|
|
219
|
+
let mut index = quote_start + if is_triple { 3 } else { 1 };
|
|
220
|
+
while index < bytes.len() {
|
|
221
|
+
if is_triple {
|
|
222
|
+
if bytes[index] == quote
|
|
223
|
+
&& bytes.get(index + 1) == Some("e)
|
|
224
|
+
&& bytes.get(index + 2) == Some("e)
|
|
225
|
+
{
|
|
226
|
+
return index + 3;
|
|
227
|
+
}
|
|
228
|
+
} else if bytes[index] == quote {
|
|
229
|
+
return index + 1;
|
|
230
|
+
}
|
|
231
|
+
index += 1;
|
|
232
|
+
}
|
|
233
|
+
bytes.len()
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
fn quoted_segment_end(bytes: &[u8], start: usize) -> usize {
|
|
237
|
+
let quote = bytes[start];
|
|
238
|
+
let is_triple = bytes.get(start + 1) == Some("e) && bytes.get(start + 2) == Some("e);
|
|
239
|
+
let mut index = start + if is_triple { 3 } else { 1 };
|
|
240
|
+
while index < bytes.len() {
|
|
241
|
+
if bytes[index] == b'\\' {
|
|
242
|
+
index += 2;
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
if is_triple {
|
|
246
|
+
if bytes[index] == quote
|
|
247
|
+
&& bytes.get(index + 1) == Some("e)
|
|
248
|
+
&& bytes.get(index + 2) == Some("e)
|
|
249
|
+
{
|
|
250
|
+
return index + 3;
|
|
251
|
+
}
|
|
252
|
+
} else if bytes[index] == quote {
|
|
253
|
+
return index + 1;
|
|
254
|
+
}
|
|
255
|
+
index += 1;
|
|
256
|
+
}
|
|
257
|
+
bytes.len()
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
fn identifier_end(bytes: &[u8], start: usize) -> Option<usize> {
|
|
261
|
+
if !bytes
|
|
262
|
+
.get(start)
|
|
263
|
+
.is_some_and(|byte| is_identifier_start(*byte))
|
|
264
|
+
{
|
|
265
|
+
return None;
|
|
266
|
+
}
|
|
267
|
+
let mut end = start + 1;
|
|
268
|
+
while bytes.get(end).is_some_and(|byte| is_identifier_part(*byte)) {
|
|
269
|
+
end += 1;
|
|
270
|
+
}
|
|
271
|
+
Some(end)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const fn is_identifier_start(byte: u8) -> bool {
|
|
275
|
+
byte == b'_' || byte.is_ascii_alphabetic()
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const fn is_identifier_part(byte: u8) -> bool {
|
|
279
|
+
is_identifier_start(byte) || byte.is_ascii_digit()
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
fn location_at(source: &str, byte_offset: usize) -> Location {
|
|
283
|
+
let mut line = 1;
|
|
284
|
+
let mut column = 0;
|
|
285
|
+
for byte in source[..byte_offset].bytes() {
|
|
286
|
+
if byte == b'\n' {
|
|
287
|
+
line += 1;
|
|
288
|
+
column = 0;
|
|
289
|
+
} else {
|
|
290
|
+
column += 1;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
Location { line, column }
|
|
294
|
+
}
|
|
295
|
+
|
|
34
296
|
fn is_reference_identifier(node: Node<'_>, source: &str) -> bool {
|
|
35
297
|
if !matches!(node.kind(), "identifier" | "type_identifier") {
|
|
36
298
|
return false;
|
package/src/extract/tests.rs
CHANGED
|
@@ -600,6 +600,31 @@ void boot(Config config) {
|
|
|
600
600
|
Ok(())
|
|
601
601
|
}
|
|
602
602
|
|
|
603
|
+
#[test]
|
|
604
|
+
fn extracts_interpolation_references_without_raw_string_prefixes() -> Result<(), ExtractError> {
|
|
605
|
+
let source = r#"
|
|
606
|
+
void boot(String suffix, String escaped) {
|
|
607
|
+
print('$suffix ${suffix.toUpperCase()} ${r"raw literal"} \$escaped');
|
|
608
|
+
}
|
|
609
|
+
"#;
|
|
610
|
+
|
|
611
|
+
let extracted = extract_dart_source("lib/interpolation.dart", source)?;
|
|
612
|
+
let references = extracted
|
|
613
|
+
.references
|
|
614
|
+
.iter()
|
|
615
|
+
.map(|reference| reference.name.as_str())
|
|
616
|
+
.collect::<Vec<_>>();
|
|
617
|
+
|
|
618
|
+
assert!(references.contains(&"suffix"));
|
|
619
|
+
assert!(references.contains(&"toUpperCase"));
|
|
620
|
+
assert!(!references.contains(&"r"));
|
|
621
|
+
assert!(!references.contains(&"raw"));
|
|
622
|
+
assert!(!references.contains(&"literal"));
|
|
623
|
+
assert!(!references.contains(&"escaped"));
|
|
624
|
+
|
|
625
|
+
Ok(())
|
|
626
|
+
}
|
|
627
|
+
|
|
603
628
|
#[test]
|
|
604
629
|
fn extracts_references_from_annotations_generics_and_patterns() -> Result<(), ExtractError> {
|
|
605
630
|
let source = "\
|
package/src/extract.rs
CHANGED
|
@@ -82,8 +82,8 @@ pub struct DartFile {
|
|
|
82
82
|
pub declarations: Vec<TopLevelDeclaration>,
|
|
83
83
|
/// Class-like member declarations in source order.
|
|
84
84
|
pub members: Vec<MemberDeclaration>,
|
|
85
|
-
/// Identifier uses in source order,
|
|
86
|
-
/// obvious declaration-name positions.
|
|
85
|
+
/// Identifier uses in source order, including non-raw string interpolation,
|
|
86
|
+
/// excluding import/export metadata and obvious declaration-name positions.
|
|
87
87
|
pub references: Vec<IdentifierReference>,
|
|
88
88
|
/// Type references from public declaration signatures, excluding bodies.
|
|
89
89
|
pub signature_references: Vec<SignatureReference>,
|
|
@@ -212,7 +212,8 @@ pub struct MemberDeclaration {
|
|
|
212
212
|
pub location: Location,
|
|
213
213
|
}
|
|
214
214
|
|
|
215
|
-
/// A syntactic identifier or type identifier use
|
|
215
|
+
/// A syntactic identifier or type identifier use, including references found in
|
|
216
|
+
/// non-raw string interpolation.
|
|
216
217
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
217
218
|
pub struct IdentifierReference {
|
|
218
219
|
/// Referenced identifier text.
|
|
@@ -675,6 +676,8 @@ fn find_first_named_descendant_with_cursor<'tree>(
|
|
|
675
676
|
#[cfg(test)]
|
|
676
677
|
mod error_tests;
|
|
677
678
|
#[cfg(test)]
|
|
679
|
+
mod reference_tests;
|
|
680
|
+
#[cfg(test)]
|
|
678
681
|
mod string_tests;
|
|
679
682
|
#[cfg(test)]
|
|
680
683
|
mod tests;
|