dart-decimate 0.0.9 → 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 +41 -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/packages.rs +71 -0
- package/src/dupes/tests.rs +27 -0
- package/src/dupes.rs +6 -1
- 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 +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.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)]
|
|
@@ -313,7 +315,8 @@ struct CloneWindow {
|
|
|
313
315
|
/// Detect duplicated Dart code blocks.
|
|
314
316
|
///
|
|
315
317
|
/// Clone windows satisfy both `min_lines` and `min_tokens`; sparse duplicated
|
|
316
|
-
/// blocks can therefore span more than `min_lines`.
|
|
318
|
+
/// blocks can therefore span more than `min_lines`. Identical matches across
|
|
319
|
+
/// copied local Pub packages with the same package name are ignored.
|
|
317
320
|
///
|
|
318
321
|
/// # Errors
|
|
319
322
|
///
|
|
@@ -392,6 +395,8 @@ pub fn detect_duplicates(
|
|
|
392
395
|
))
|
|
393
396
|
});
|
|
394
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));
|
|
395
400
|
let stats = duplicate_stats(analyzed_lines, &clone_groups, options.threshold);
|
|
396
401
|
if let Some(top) = options.top {
|
|
397
402
|
clone_groups.truncate(top);
|
|
@@ -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;
|
package/src/generated.rs
CHANGED
|
@@ -10,9 +10,12 @@ pub(crate) const GENERATED_DART_SUFFIXES: &[&str] = &[
|
|
|
10
10
|
|
|
11
11
|
#[must_use]
|
|
12
12
|
pub(crate) fn is_generated_dart_path(path: &Path) -> bool {
|
|
13
|
-
path.file_name()
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
let Some(file_name) = path.file_name().and_then(|name| name.to_str()) else {
|
|
14
|
+
return false;
|
|
15
|
+
};
|
|
16
|
+
is_generated_dart_file_name(file_name)
|
|
17
|
+
|| is_flutter_gen_l10n_path(path, file_name)
|
|
18
|
+
|| is_flutterfire_options_path(path)
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
#[must_use]
|
|
@@ -22,6 +25,65 @@ pub(crate) fn is_generated_dart_file_name(name: &str) -> bool {
|
|
|
22
25
|
.any(|suffix| name.ends_with(suffix))
|
|
23
26
|
}
|
|
24
27
|
|
|
28
|
+
fn is_flutter_gen_l10n_path(path: &Path, file_name: &str) -> bool {
|
|
29
|
+
(file_name == "app_localizations.dart"
|
|
30
|
+
|| file_name
|
|
31
|
+
.strip_prefix("app_localizations_")
|
|
32
|
+
.is_some_and(is_flutter_gen_l10n_locale_file))
|
|
33
|
+
&& path.components().any(|component| {
|
|
34
|
+
matches!(
|
|
35
|
+
component.as_os_str().to_str(),
|
|
36
|
+
Some("l10n" | "gen_l10n" | "generated")
|
|
37
|
+
)
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
fn is_flutter_gen_l10n_locale_file(name: &str) -> bool {
|
|
42
|
+
let Some(locale) = name.strip_suffix(".dart") else {
|
|
43
|
+
return false;
|
|
44
|
+
};
|
|
45
|
+
let parts = locale.split('_').collect::<Vec<_>>();
|
|
46
|
+
match parts.as_slice() {
|
|
47
|
+
[language] => is_language_subtag(language),
|
|
48
|
+
[language, script_or_region] => {
|
|
49
|
+
is_language_subtag(language)
|
|
50
|
+
&& (is_script_subtag(script_or_region) || is_region_subtag(script_or_region))
|
|
51
|
+
}
|
|
52
|
+
[language, script, region] => {
|
|
53
|
+
is_language_subtag(language) && is_script_subtag(script) && is_region_subtag(region)
|
|
54
|
+
}
|
|
55
|
+
_ => false,
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
fn is_language_subtag(part: &str) -> bool {
|
|
60
|
+
matches!(part.len(), 2 | 3) && part.bytes().all(|byte| byte.is_ascii_lowercase())
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
fn is_script_subtag(part: &str) -> bool {
|
|
64
|
+
part.len() == 4
|
|
65
|
+
&& part.bytes().enumerate().all(|(index, byte)| match index {
|
|
66
|
+
0 => byte.is_ascii_uppercase(),
|
|
67
|
+
_ => byte.is_ascii_lowercase(),
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
fn is_region_subtag(part: &str) -> bool {
|
|
72
|
+
(part.len() == 2 && part.bytes().all(|byte| byte.is_ascii_uppercase()))
|
|
73
|
+
|| (part.len() == 3 && part.bytes().all(|byte| byte.is_ascii_digit()))
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
#[must_use]
|
|
77
|
+
pub(crate) fn is_flutterfire_options_path(path: &Path) -> bool {
|
|
78
|
+
path.file_name()
|
|
79
|
+
.and_then(|name| name.to_str())
|
|
80
|
+
.is_some_and(|file_name| file_name == "firebase_options.dart")
|
|
81
|
+
&& path
|
|
82
|
+
.parent()
|
|
83
|
+
.and_then(|parent| parent.file_name())
|
|
84
|
+
.is_some_and(|parent| parent == "lib")
|
|
85
|
+
}
|
|
86
|
+
|
|
25
87
|
#[cfg(test)]
|
|
26
88
|
mod tests {
|
|
27
89
|
use std::path::Path;
|
|
@@ -50,4 +112,35 @@ mod tests {
|
|
|
50
112
|
assert!(!is_generated_dart_file_name(file_name), "{file_name}");
|
|
51
113
|
}
|
|
52
114
|
}
|
|
115
|
+
|
|
116
|
+
#[test]
|
|
117
|
+
fn recognizes_flutter_generated_entry_files() {
|
|
118
|
+
for path in [
|
|
119
|
+
Path::new("lib/l10n/app_localizations.dart"),
|
|
120
|
+
Path::new("lib/l10n/app_localizations_en.dart"),
|
|
121
|
+
Path::new("lib/l10n/app_localizations_en_US.dart"),
|
|
122
|
+
Path::new("lib/l10n/app_localizations_zh_Hant.dart"),
|
|
123
|
+
Path::new("lib/l10n/app_localizations_sr_Latn_RS.dart"),
|
|
124
|
+
Path::new("lib/l10n/app_localizations_es_419.dart"),
|
|
125
|
+
Path::new("lib/gen_l10n/app_localizations_es.dart"),
|
|
126
|
+
Path::new("lib/firebase_options.dart"),
|
|
127
|
+
Path::new("packages/foo/lib/firebase_options.dart"),
|
|
128
|
+
] {
|
|
129
|
+
assert!(is_generated_dart_path(path), "{}", path.display());
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
#[test]
|
|
134
|
+
fn rejects_handwritten_app_localizations_sources() {
|
|
135
|
+
for path in [
|
|
136
|
+
Path::new("lib/l10n/app_localizations_repository.dart"),
|
|
137
|
+
Path::new("lib/l10n/app_localizations_en_US_helper.dart"),
|
|
138
|
+
Path::new("lib/l10n/app_localizations_.dart"),
|
|
139
|
+
Path::new("lib/l10n/app_localizations_en-us.dart"),
|
|
140
|
+
Path::new("lib/src/firebase_options.dart"),
|
|
141
|
+
Path::new("packages/foo/lib/config/firebase_options.dart"),
|
|
142
|
+
] {
|
|
143
|
+
assert!(!is_generated_dart_path(path), "{}", path.display());
|
|
144
|
+
}
|
|
145
|
+
}
|
|
53
146
|
}
|