dart-decimate 0.0.29 → 0.0.31
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 +36 -60
- package/npm/bin/runner.js +26 -14
- package/npm/scripts/postinstall.js +89 -63
- package/package.json +3 -2
- package/src/baseline.rs +38 -0
- package/src/cli/health_args.rs +36 -7
- package/src/cli/html_open.rs +4 -0
- package/src/cli/issue_filter_args.rs +1 -0
- package/src/cli/security_summary_run.rs +1 -0
- package/src/config/health.rs +22 -2
- package/src/config/rule_aliases.rs +43 -0
- package/src/config/rules.rs +46 -1
- package/src/config.rs +2 -1
- package/src/dart_parser/test_support.rs +51 -0
- package/src/dart_parser.rs +8 -0
- package/src/dependencies/analyze.rs +3 -1
- package/src/dependencies/tests.rs +111 -7
- package/src/dependencies/usage.rs +5 -1
- package/src/dependencies.rs +7 -3
- package/src/explain/flutter_style.rs +43 -0
- package/src/explain/misc.rs +72 -0
- package/src/explain.rs +23 -67
- package/src/flutter_style/scanner.rs +883 -0
- package/src/flutter_style/types.rs +90 -0
- package/src/flutter_style/usage.rs +349 -0
- package/src/flutter_style.rs +72 -0
- package/src/generated.rs +154 -0
- package/src/health/large_units.rs +49 -0
- package/src/health/tests.rs +212 -1
- package/src/health/threshold_types.rs +31 -1
- package/src/health/thresholds.rs +49 -0
- package/src/health/types.rs +35 -0
- package/src/health.rs +72 -6
- package/src/inspect.rs +8 -0
- package/src/lib.rs +17 -5
- package/src/manifest.rs +7 -3
- package/src/output/dependency_findings.rs +14 -1
- package/src/output/flutter_style.rs +189 -0
- package/src/output/health_findings.rs +93 -10
- package/src/output/health_inventory.rs +121 -0
- package/src/output/html/tests.rs +20 -0
- package/src/output/html.rs +10 -1
- package/src/output/human.rs +85 -1
- package/src/output/human_details.rs +93 -12
- package/src/output/scope.rs +62 -2
- package/src/output/security_findings.rs +101 -76
- package/src/output/security_sarif.rs +24 -13
- package/src/output/suppressions.rs +4 -0
- package/src/output/types.rs +38 -2
- package/src/output/verdict.rs +12 -0
- package/src/output.rs +77 -74
- package/src/report_schema/inventory.rs +268 -4
- package/src/report_schema.rs +94 -60
- package/src/security/catalogue.rs +103 -0
- package/src/security/detect/weak_randomness.rs +762 -0
- package/src/security/detect.rs +1535 -221
- package/src/security/security_matchers.toml +172 -0
- package/src/security/tests.rs +336 -2
- package/src/security.rs +292 -56
- package/src/security_gate.rs +28 -0
- package/src/semantic.rs +276 -0
- package/src/symbols/semantic_evidence.rs +342 -0
- package/src/symbols.rs +20 -10
- package/src/trace/rendering.rs +54 -0
- package/src/trace/semantic.rs +126 -0
- package/src/trace.rs +54 -62
- package/src/widgets/inheritance.rs +63 -0
- package/src/widgets.rs +33 -3
|
@@ -0,0 +1,883 @@
|
|
|
1
|
+
use std::collections::{BTreeMap, BTreeSet};
|
|
2
|
+
|
|
3
|
+
use rayon::prelude::*;
|
|
4
|
+
use tree_sitter::Node;
|
|
5
|
+
|
|
6
|
+
use super::types::{
|
|
7
|
+
FlutterStyleFinding, FlutterStyleFindingKind, FlutterStyleReport, FlutterStyleValueKind,
|
|
8
|
+
ThemeTokenEvidence,
|
|
9
|
+
};
|
|
10
|
+
use super::usage::used_extension_properties;
|
|
11
|
+
use super::{ParsedSourceUnit, SourceUnit, style_parse_error};
|
|
12
|
+
use crate::HealthError;
|
|
13
|
+
|
|
14
|
+
const NEAR_DUPLICATE_DISTANCE_SQUARED: u32 = 4;
|
|
15
|
+
type ExtensionFields = (BTreeMap<String, Vec<ThemeTokenEvidence>>, BTreeSet<String>);
|
|
16
|
+
|
|
17
|
+
pub(super) fn analyze(units: &[SourceUnit]) -> Result<FlutterStyleReport, HealthError> {
|
|
18
|
+
let parsed_units = parse_units(units)?;
|
|
19
|
+
let (extension_fields, ambiguous_extension_names) = extension_fields(&parsed_units);
|
|
20
|
+
let extension_names = extension_fields.keys().cloned().collect::<BTreeSet<_>>();
|
|
21
|
+
let theme_owner_names = theme_owner_names(&parsed_units);
|
|
22
|
+
let mut tokens = extension_fields
|
|
23
|
+
.values()
|
|
24
|
+
.flatten()
|
|
25
|
+
.cloned()
|
|
26
|
+
.collect::<Vec<_>>();
|
|
27
|
+
tokens.extend(static_theme_owner_tokens(&parsed_units, &theme_owner_names));
|
|
28
|
+
let used_properties = used_extension_properties(&parsed_units, &extension_names);
|
|
29
|
+
|
|
30
|
+
for parsed_unit in &parsed_units {
|
|
31
|
+
let unit = parsed_unit.unit;
|
|
32
|
+
let parsed = &parsed_unit.parsed;
|
|
33
|
+
walk(parsed.tree().root_node(), &mut |node| {
|
|
34
|
+
collect_builtin_tokens(node, parsed.source(), &unit.path, &mut tokens);
|
|
35
|
+
populate_extension_values(
|
|
36
|
+
node,
|
|
37
|
+
parsed.source(),
|
|
38
|
+
&unit.path,
|
|
39
|
+
&extension_names,
|
|
40
|
+
&ambiguous_extension_names,
|
|
41
|
+
&mut tokens,
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
deduplicate_tokens(&mut tokens);
|
|
46
|
+
|
|
47
|
+
let mut findings =
|
|
48
|
+
raw_value_findings(&parsed_units, &extension_names, &theme_owner_names, &tokens);
|
|
49
|
+
findings.extend(near_duplicate_findings(&tokens));
|
|
50
|
+
findings.extend(unused_token_findings(&tokens, &used_properties));
|
|
51
|
+
sort_findings(&mut findings);
|
|
52
|
+
Ok(FlutterStyleReport { findings })
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
fn extension_fields(units: &[ParsedSourceUnit<'_>]) -> ExtensionFields {
|
|
56
|
+
let mut extensions = BTreeMap::<String, Vec<ThemeTokenEvidence>>::new();
|
|
57
|
+
let mut ambiguous = BTreeSet::new();
|
|
58
|
+
for parsed_unit in units {
|
|
59
|
+
let unit = parsed_unit.unit;
|
|
60
|
+
let parsed = &parsed_unit.parsed;
|
|
61
|
+
walk(parsed.tree().root_node(), &mut |node| {
|
|
62
|
+
if node.kind() != "class_declaration"
|
|
63
|
+
|| !node_text(node, parsed.source()).contains("ThemeExtension<")
|
|
64
|
+
{
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
let Some(name) = field_text(node, "name", parsed.source()) else {
|
|
68
|
+
return;
|
|
69
|
+
};
|
|
70
|
+
let mut fields = Vec::new();
|
|
71
|
+
walk(node, &mut |candidate| {
|
|
72
|
+
if candidate.kind() != "initialized_identifier"
|
|
73
|
+
|| !is_direct_class_field(candidate, node)
|
|
74
|
+
{
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
let Some(field_name) = field_text(candidate, "name", parsed.source()) else {
|
|
78
|
+
return;
|
|
79
|
+
};
|
|
80
|
+
let declaration_prefix = declaration_prefix(candidate, parsed.source());
|
|
81
|
+
let value_kind = if declaration_prefix.contains("TextStyle") {
|
|
82
|
+
FlutterStyleValueKind::TextStyle
|
|
83
|
+
} else if declaration_prefix.contains("Color") {
|
|
84
|
+
FlutterStyleValueKind::Color
|
|
85
|
+
} else {
|
|
86
|
+
return;
|
|
87
|
+
};
|
|
88
|
+
fields.push(ThemeTokenEvidence {
|
|
89
|
+
name: field_name,
|
|
90
|
+
path: unit.path.clone(),
|
|
91
|
+
location: candidate.start_position().into(),
|
|
92
|
+
value_kind,
|
|
93
|
+
value: None,
|
|
94
|
+
custom: true,
|
|
95
|
+
argb: None,
|
|
96
|
+
owner: Some(name.clone()),
|
|
97
|
+
declared_field: true,
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
if extensions.contains_key(&name) {
|
|
101
|
+
ambiguous.insert(name.clone());
|
|
102
|
+
}
|
|
103
|
+
extensions.entry(name).or_default().extend(fields);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
(extensions, ambiguous)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
fn collect_builtin_tokens(
|
|
110
|
+
node: Node<'_>,
|
|
111
|
+
source: &str,
|
|
112
|
+
path: &std::path::Path,
|
|
113
|
+
tokens: &mut Vec<ThemeTokenEvidence>,
|
|
114
|
+
) {
|
|
115
|
+
if !is_constructor_node(node) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
let Some(constructor) = constructor_type(node, source) else {
|
|
119
|
+
return;
|
|
120
|
+
};
|
|
121
|
+
let value_kind = match constructor.as_str() {
|
|
122
|
+
"ColorScheme" => FlutterStyleValueKind::Color,
|
|
123
|
+
"TextTheme" => FlutterStyleValueKind::TextStyle,
|
|
124
|
+
_ => return,
|
|
125
|
+
};
|
|
126
|
+
for (name, value_node) in named_arguments(node, source) {
|
|
127
|
+
let value = node_text(value_node, source);
|
|
128
|
+
if style_value_kind_node(value_node, source) != Some(value_kind) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
let argb = (value_kind == FlutterStyleValueKind::Color)
|
|
132
|
+
.then(|| parse_argb(value))
|
|
133
|
+
.flatten();
|
|
134
|
+
tokens.push(ThemeTokenEvidence {
|
|
135
|
+
name: format!(
|
|
136
|
+
"{}.{name}",
|
|
137
|
+
if value_kind == FlutterStyleValueKind::Color {
|
|
138
|
+
"colorScheme"
|
|
139
|
+
} else {
|
|
140
|
+
"textTheme"
|
|
141
|
+
}
|
|
142
|
+
),
|
|
143
|
+
path: path.to_path_buf(),
|
|
144
|
+
location: value_node.start_position().into(),
|
|
145
|
+
value_kind,
|
|
146
|
+
value: normalized_value(value_kind, value),
|
|
147
|
+
custom: false,
|
|
148
|
+
argb,
|
|
149
|
+
owner: None,
|
|
150
|
+
declared_field: false,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
fn populate_extension_values(
|
|
156
|
+
node: Node<'_>,
|
|
157
|
+
source: &str,
|
|
158
|
+
path: &std::path::Path,
|
|
159
|
+
extension_names: &BTreeSet<String>,
|
|
160
|
+
ambiguous_extension_names: &BTreeSet<String>,
|
|
161
|
+
tokens: &mut Vec<ThemeTokenEvidence>,
|
|
162
|
+
) {
|
|
163
|
+
if !is_constructor_node(node) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
let Some(owner) = constructor_type(node, source) else {
|
|
167
|
+
return;
|
|
168
|
+
};
|
|
169
|
+
if !extension_names.contains(&owner) || ambiguous_extension_names.contains(&owner) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
for (name, value_node) in named_arguments(node, source) {
|
|
173
|
+
let Some(template) = tokens.iter().find(|token| {
|
|
174
|
+
token.declared_field
|
|
175
|
+
&& token.owner.as_deref() == Some(owner.as_str())
|
|
176
|
+
&& token.name == name
|
|
177
|
+
}) else {
|
|
178
|
+
continue;
|
|
179
|
+
};
|
|
180
|
+
let value = node_text(value_node, source);
|
|
181
|
+
if style_value_kind_node(value_node, source) != Some(template.value_kind) {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
let mut token = template.clone();
|
|
185
|
+
token.path = path.to_path_buf();
|
|
186
|
+
token.location = value_node.start_position().into();
|
|
187
|
+
token.value = normalized_value(token.value_kind, value);
|
|
188
|
+
token.argb = (token.value_kind == FlutterStyleValueKind::Color)
|
|
189
|
+
.then(|| parse_argb(value))
|
|
190
|
+
.flatten();
|
|
191
|
+
token.declared_field = false;
|
|
192
|
+
tokens.push(token);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
fn raw_value_findings(
|
|
197
|
+
units: &[ParsedSourceUnit<'_>],
|
|
198
|
+
extension_names: &BTreeSet<String>,
|
|
199
|
+
theme_owner_names: &BTreeSet<String>,
|
|
200
|
+
tokens: &[ThemeTokenEvidence],
|
|
201
|
+
) -> Vec<FlutterStyleFinding> {
|
|
202
|
+
let mut findings = Vec::new();
|
|
203
|
+
for parsed_unit in units {
|
|
204
|
+
let unit = parsed_unit.unit;
|
|
205
|
+
let parsed = &parsed_unit.parsed;
|
|
206
|
+
walk(parsed.tree().root_node(), &mut |node| {
|
|
207
|
+
if (!is_constructor_node(node) && !is_colors_constant(node, parsed.source()))
|
|
208
|
+
|| is_theme_definer(node, parsed.source(), extension_names, theme_owner_names)
|
|
209
|
+
{
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
let Some(value_kind) = style_value_kind_node(node, parsed.source()) else {
|
|
213
|
+
return;
|
|
214
|
+
};
|
|
215
|
+
let source_value = node_text(node, parsed.source());
|
|
216
|
+
let argb = (value_kind == FlutterStyleValueKind::Color)
|
|
217
|
+
.then(|| parse_argb(source_value))
|
|
218
|
+
.flatten();
|
|
219
|
+
let normalized = normalized_value(value_kind, source_value);
|
|
220
|
+
let Some((nearest, distance)) =
|
|
221
|
+
nearest_token(value_kind, argb, normalized.as_deref(), tokens)
|
|
222
|
+
else {
|
|
223
|
+
return;
|
|
224
|
+
};
|
|
225
|
+
findings.push(FlutterStyleFinding {
|
|
226
|
+
kind: FlutterStyleFindingKind::RawFlutterStyleValue,
|
|
227
|
+
path: unit.path.clone(),
|
|
228
|
+
location: node.start_position().into(),
|
|
229
|
+
value_kind,
|
|
230
|
+
value: normalized,
|
|
231
|
+
token: None,
|
|
232
|
+
nearest_token: Some(nearest.clone()),
|
|
233
|
+
distance,
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
findings
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
fn near_duplicate_findings(tokens: &[ThemeTokenEvidence]) -> Vec<FlutterStyleFinding> {
|
|
241
|
+
let custom_colors = tokens
|
|
242
|
+
.iter()
|
|
243
|
+
.filter(|token| {
|
|
244
|
+
token.custom
|
|
245
|
+
&& !token.declared_field
|
|
246
|
+
&& token.value_kind == FlutterStyleValueKind::Color
|
|
247
|
+
})
|
|
248
|
+
.collect::<Vec<_>>();
|
|
249
|
+
let mut findings = Vec::new();
|
|
250
|
+
for (index, left) in custom_colors.iter().enumerate() {
|
|
251
|
+
for right in custom_colors.iter().skip(index + 1) {
|
|
252
|
+
let (Some(left_argb), Some(right_argb)) = (left.argb, right.argb) else {
|
|
253
|
+
continue;
|
|
254
|
+
};
|
|
255
|
+
let squared = argb_distance_squared(left_argb, right_argb);
|
|
256
|
+
if squared == 0 || squared > NEAR_DUPLICATE_DISTANCE_SQUARED {
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
findings.push(FlutterStyleFinding {
|
|
260
|
+
kind: FlutterStyleFindingKind::NearDuplicateThemeToken,
|
|
261
|
+
path: left.path.clone(),
|
|
262
|
+
location: left.location,
|
|
263
|
+
value_kind: FlutterStyleValueKind::Color,
|
|
264
|
+
value: left.value.clone(),
|
|
265
|
+
token: Some((*left).clone()),
|
|
266
|
+
nearest_token: Some((*right).clone()),
|
|
267
|
+
distance: Some(format_distance(squared)),
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
findings
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
fn unused_token_findings(
|
|
275
|
+
tokens: &[ThemeTokenEvidence],
|
|
276
|
+
used_properties: &BTreeSet<(String, String)>,
|
|
277
|
+
) -> Vec<FlutterStyleFinding> {
|
|
278
|
+
tokens
|
|
279
|
+
.iter()
|
|
280
|
+
.filter(|token| {
|
|
281
|
+
token.custom
|
|
282
|
+
&& token.declared_field
|
|
283
|
+
&& token.owner.as_ref().is_none_or(|owner| {
|
|
284
|
+
!used_properties.contains(&(owner.clone(), token.name.clone()))
|
|
285
|
+
})
|
|
286
|
+
})
|
|
287
|
+
.map(|token| FlutterStyleFinding {
|
|
288
|
+
kind: FlutterStyleFindingKind::UnusedThemeExtensionToken,
|
|
289
|
+
path: token.path.clone(),
|
|
290
|
+
location: token.location,
|
|
291
|
+
value_kind: token.value_kind,
|
|
292
|
+
value: token.value.clone(),
|
|
293
|
+
token: Some(token.clone()),
|
|
294
|
+
nearest_token: None,
|
|
295
|
+
distance: None,
|
|
296
|
+
})
|
|
297
|
+
.collect()
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
fn nearest_token<'tokens>(
|
|
301
|
+
value_kind: FlutterStyleValueKind,
|
|
302
|
+
argb: Option<u32>,
|
|
303
|
+
normalized: Option<&str>,
|
|
304
|
+
tokens: &'tokens [ThemeTokenEvidence],
|
|
305
|
+
) -> Option<(&'tokens ThemeTokenEvidence, Option<String>)> {
|
|
306
|
+
let candidates = tokens
|
|
307
|
+
.iter()
|
|
308
|
+
.filter(|token| token.value_kind == value_kind && token.value.is_some());
|
|
309
|
+
if value_kind == FlutterStyleValueKind::TextStyle {
|
|
310
|
+
return candidates
|
|
311
|
+
.min_by_key(|token| (&token.name, &token.path, token.location.line))
|
|
312
|
+
.map(|token| (token, None));
|
|
313
|
+
}
|
|
314
|
+
let Some(argb) = argb else {
|
|
315
|
+
return candidates
|
|
316
|
+
.filter(|token| token.value.as_deref() == normalized)
|
|
317
|
+
.min_by_key(|token| (&token.name, &token.path, token.location.line))
|
|
318
|
+
.map(|token| (token, Some("0.00".to_owned())));
|
|
319
|
+
};
|
|
320
|
+
candidates
|
|
321
|
+
.filter_map(|token| {
|
|
322
|
+
token
|
|
323
|
+
.argb
|
|
324
|
+
.map(|candidate| (argb_distance_squared(argb, candidate), token))
|
|
325
|
+
})
|
|
326
|
+
.min_by_key(|(distance, token)| {
|
|
327
|
+
(
|
|
328
|
+
*distance,
|
|
329
|
+
&token.name,
|
|
330
|
+
&token.path,
|
|
331
|
+
token.location.line,
|
|
332
|
+
token.location.column,
|
|
333
|
+
)
|
|
334
|
+
})
|
|
335
|
+
.map(|(distance, token)| (token, Some(format_distance(distance))))
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
fn theme_owner_names(units: &[ParsedSourceUnit<'_>]) -> BTreeSet<String> {
|
|
339
|
+
let mut names = BTreeSet::new();
|
|
340
|
+
for parsed_unit in units {
|
|
341
|
+
let parsed = &parsed_unit.parsed;
|
|
342
|
+
walk(parsed.tree().root_node(), &mut |node| {
|
|
343
|
+
if node.kind() != "class_declaration" {
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
let Some(name) = field_text(node, "name", parsed.source()) else {
|
|
347
|
+
return;
|
|
348
|
+
};
|
|
349
|
+
if !is_theme_owner_name(&name) || is_widget_class(node, parsed.source()) {
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
let mut declares_static_style_token = false;
|
|
353
|
+
walk(node, &mut |candidate| {
|
|
354
|
+
if !matches!(
|
|
355
|
+
candidate.kind(),
|
|
356
|
+
"initialized_identifier" | "static_final_declaration"
|
|
357
|
+
) || !is_direct_class_field(candidate, node)
|
|
358
|
+
{
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
declares_static_style_token |=
|
|
362
|
+
is_static_style_declaration(candidate, node, parsed.source());
|
|
363
|
+
});
|
|
364
|
+
if declares_static_style_token {
|
|
365
|
+
names.insert(name);
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
names
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
fn static_theme_owner_tokens(
|
|
373
|
+
units: &[ParsedSourceUnit<'_>],
|
|
374
|
+
theme_owner_names: &BTreeSet<String>,
|
|
375
|
+
) -> Vec<ThemeTokenEvidence> {
|
|
376
|
+
let mut tokens = Vec::new();
|
|
377
|
+
for parsed_unit in units {
|
|
378
|
+
let unit = parsed_unit.unit;
|
|
379
|
+
let parsed = &parsed_unit.parsed;
|
|
380
|
+
walk(parsed.tree().root_node(), &mut |class| {
|
|
381
|
+
if class.kind() != "class_declaration" {
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
let Some(owner) = field_text(class, "name", parsed.source()) else {
|
|
385
|
+
return;
|
|
386
|
+
};
|
|
387
|
+
if !theme_owner_names.contains(&owner) {
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
walk(class, &mut |candidate| {
|
|
391
|
+
if !matches!(
|
|
392
|
+
candidate.kind(),
|
|
393
|
+
"initialized_identifier"
|
|
394
|
+
| "initialized_variable_definition"
|
|
395
|
+
| "static_final_declaration"
|
|
396
|
+
) || !is_direct_class_field(candidate, class)
|
|
397
|
+
|| !is_static_style_declaration(candidate, class, parsed.source())
|
|
398
|
+
{
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
let Some(name) = field_text(candidate, "name", parsed.source()) else {
|
|
402
|
+
return;
|
|
403
|
+
};
|
|
404
|
+
let Some(value_node) = candidate.child_by_field_name("value") else {
|
|
405
|
+
return;
|
|
406
|
+
};
|
|
407
|
+
let Some(value_kind) = style_value_kind_node(value_node, parsed.source()) else {
|
|
408
|
+
return;
|
|
409
|
+
};
|
|
410
|
+
let value = node_text(value_node, parsed.source());
|
|
411
|
+
tokens.push(ThemeTokenEvidence {
|
|
412
|
+
name: format!("{owner}.{name}"),
|
|
413
|
+
path: unit.path.clone(),
|
|
414
|
+
location: value_node.start_position().into(),
|
|
415
|
+
value_kind,
|
|
416
|
+
value: normalized_value(value_kind, value),
|
|
417
|
+
custom: true,
|
|
418
|
+
argb: (value_kind == FlutterStyleValueKind::Color)
|
|
419
|
+
.then(|| parse_argb(value))
|
|
420
|
+
.flatten(),
|
|
421
|
+
owner: Some(owner.clone()),
|
|
422
|
+
declared_field: false,
|
|
423
|
+
});
|
|
424
|
+
});
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
tokens
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
fn is_theme_owner_name(name: &str) -> bool {
|
|
431
|
+
let name = name.to_ascii_lowercase();
|
|
432
|
+
["theme", "tokens", "palette", "colors", "styles"]
|
|
433
|
+
.iter()
|
|
434
|
+
.any(|suffix| name.ends_with(suffix))
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
fn is_static_style_declaration(node: Node<'_>, class: Node<'_>, source: &str) -> bool {
|
|
438
|
+
let mut declaration = Some(node);
|
|
439
|
+
while let Some(candidate) = declaration {
|
|
440
|
+
if candidate.id() == class.id() {
|
|
441
|
+
return false;
|
|
442
|
+
}
|
|
443
|
+
let text = node_text(candidate, source);
|
|
444
|
+
if text.contains("static") && (text.contains("Color") || text.contains("TextStyle")) {
|
|
445
|
+
return true;
|
|
446
|
+
}
|
|
447
|
+
declaration = candidate.parent();
|
|
448
|
+
}
|
|
449
|
+
false
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
fn is_widget_class(node: Node<'_>, source: &str) -> bool {
|
|
453
|
+
let header = node_text(node, source)
|
|
454
|
+
.split_once('{')
|
|
455
|
+
.map_or_else(|| node_text(node, source), |(header, _)| header);
|
|
456
|
+
["StatelessWidget", "StatefulWidget", "ConsumerWidget"]
|
|
457
|
+
.iter()
|
|
458
|
+
.any(|widget| header.contains(widget))
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
fn is_theme_definer(
|
|
462
|
+
node: Node<'_>,
|
|
463
|
+
source: &str,
|
|
464
|
+
extension_names: &BTreeSet<String>,
|
|
465
|
+
theme_owner_names: &BTreeSet<String>,
|
|
466
|
+
) -> bool {
|
|
467
|
+
let mut ancestor = node.parent();
|
|
468
|
+
while let Some(parent) = ancestor {
|
|
469
|
+
if parent.kind() == "class_declaration"
|
|
470
|
+
&& field_text(parent, "name", source).is_some_and(|name| {
|
|
471
|
+
extension_names.contains(&name) || theme_owner_names.contains(&name)
|
|
472
|
+
})
|
|
473
|
+
{
|
|
474
|
+
return true;
|
|
475
|
+
}
|
|
476
|
+
if is_constructor_node(parent)
|
|
477
|
+
&& constructor_type(parent, source).is_some_and(|name| {
|
|
478
|
+
matches!(name.as_str(), "ThemeData" | "ColorScheme" | "TextTheme")
|
|
479
|
+
|| extension_names.contains(&name)
|
|
480
|
+
})
|
|
481
|
+
{
|
|
482
|
+
return true;
|
|
483
|
+
}
|
|
484
|
+
ancestor = parent.parent();
|
|
485
|
+
}
|
|
486
|
+
false
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
fn is_direct_class_field(node: Node<'_>, class: Node<'_>) -> bool {
|
|
490
|
+
let mut ancestor = node.parent();
|
|
491
|
+
while let Some(parent) = ancestor {
|
|
492
|
+
if parent.id() == class.id() {
|
|
493
|
+
return true;
|
|
494
|
+
}
|
|
495
|
+
if parent.kind().contains("function")
|
|
496
|
+
|| parent.kind().contains("method")
|
|
497
|
+
|| parent.kind().contains("constructor")
|
|
498
|
+
|| parent.kind().contains("parameter")
|
|
499
|
+
{
|
|
500
|
+
return false;
|
|
501
|
+
}
|
|
502
|
+
ancestor = parent.parent();
|
|
503
|
+
}
|
|
504
|
+
false
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
fn declaration_prefix<'source>(node: Node<'_>, source: &'source str) -> &'source str {
|
|
508
|
+
let declaration = node
|
|
509
|
+
.parent()
|
|
510
|
+
.and_then(|parent| parent.parent())
|
|
511
|
+
.unwrap_or(node);
|
|
512
|
+
source
|
|
513
|
+
.get(declaration.start_byte()..node.start_byte())
|
|
514
|
+
.unwrap_or_default()
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
fn named_arguments<'tree>(node: Node<'tree>, source: &str) -> Vec<(String, Node<'tree>)> {
|
|
518
|
+
let Some(arguments) = node.child_by_field_name("arguments") else {
|
|
519
|
+
return Vec::new();
|
|
520
|
+
};
|
|
521
|
+
let mut result = Vec::new();
|
|
522
|
+
let mut cursor = arguments.walk();
|
|
523
|
+
for argument in arguments.named_children(&mut cursor) {
|
|
524
|
+
if argument.kind() != "named_argument" {
|
|
525
|
+
continue;
|
|
526
|
+
}
|
|
527
|
+
let mut children_cursor = argument.walk();
|
|
528
|
+
let children = argument
|
|
529
|
+
.named_children(&mut children_cursor)
|
|
530
|
+
.collect::<Vec<_>>();
|
|
531
|
+
let Some(label) = children.iter().find(|child| child.kind() == "label") else {
|
|
532
|
+
continue;
|
|
533
|
+
};
|
|
534
|
+
let Some(value) = children.iter().find(|child| child.kind() != "label") else {
|
|
535
|
+
continue;
|
|
536
|
+
};
|
|
537
|
+
result.push((
|
|
538
|
+
node_text(*label, source).trim_end_matches(':').to_owned(),
|
|
539
|
+
*value,
|
|
540
|
+
));
|
|
541
|
+
}
|
|
542
|
+
result
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
fn constructor_kind(node: Node<'_>, source: &str) -> Option<FlutterStyleValueKind> {
|
|
546
|
+
if !is_constructor_node(node) {
|
|
547
|
+
return None;
|
|
548
|
+
}
|
|
549
|
+
match constructor_type(node, source)?.as_str() {
|
|
550
|
+
"Color" => Some(FlutterStyleValueKind::Color),
|
|
551
|
+
"TextStyle" => Some(FlutterStyleValueKind::TextStyle),
|
|
552
|
+
_ => None,
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
fn style_value_kind_node(node: Node<'_>, source: &str) -> Option<FlutterStyleValueKind> {
|
|
557
|
+
constructor_kind(node, source)
|
|
558
|
+
.or_else(|| is_colors_constant(node, source).then_some(FlutterStyleValueKind::Color))
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
fn is_colors_constant(node: Node<'_>, source: &str) -> bool {
|
|
562
|
+
node.kind() == "member_expression"
|
|
563
|
+
&& field_text(node, "object", source)
|
|
564
|
+
.is_some_and(|object| object.rsplit('.').next().map(str::trim) == Some("Colors"))
|
|
565
|
+
&& field_text(node, "property", source).is_some()
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
fn constructor_type(node: Node<'_>, source: &str) -> Option<String> {
|
|
569
|
+
let value = match node.kind() {
|
|
570
|
+
"call_expression" => field_text(node, "function", source)?,
|
|
571
|
+
"const_object_expression" | "constructor_invocation" => field_text(node, "type", source)?,
|
|
572
|
+
_ => return None,
|
|
573
|
+
};
|
|
574
|
+
value
|
|
575
|
+
.split('.')
|
|
576
|
+
.map(str::trim)
|
|
577
|
+
.find(|name| {
|
|
578
|
+
name.chars()
|
|
579
|
+
.next()
|
|
580
|
+
.is_some_and(|character| character.is_ascii_uppercase())
|
|
581
|
+
})
|
|
582
|
+
.map(str::to_owned)
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
fn is_constructor_node(node: Node<'_>) -> bool {
|
|
586
|
+
matches!(
|
|
587
|
+
node.kind(),
|
|
588
|
+
"call_expression" | "const_object_expression" | "constructor_invocation"
|
|
589
|
+
)
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
fn normalized_value(kind: FlutterStyleValueKind, source: &str) -> Option<String> {
|
|
593
|
+
match kind {
|
|
594
|
+
FlutterStyleValueKind::Color => parse_argb(source)
|
|
595
|
+
.map(|argb| format!("0x{argb:08X}"))
|
|
596
|
+
.or_else(|| is_colors_source(source).then(|| source.trim().to_owned())),
|
|
597
|
+
FlutterStyleValueKind::TextStyle => Some(source.split_whitespace().collect()),
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
fn parse_argb(source: &str) -> Option<u32> {
|
|
602
|
+
if let Some(arguments) = constructor_arguments(source, "fromARGB") {
|
|
603
|
+
let channels = positional_numbers(arguments)?;
|
|
604
|
+
if channels.len() != 4 {
|
|
605
|
+
return None;
|
|
606
|
+
}
|
|
607
|
+
return Some(
|
|
608
|
+
u32::from(to_byte(channels[0])?) << 24
|
|
609
|
+
| u32::from(to_byte(channels[1])?) << 16
|
|
610
|
+
| u32::from(to_byte(channels[2])?) << 8
|
|
611
|
+
| u32::from(to_byte(channels[3])?),
|
|
612
|
+
);
|
|
613
|
+
}
|
|
614
|
+
if let Some(arguments) = constructor_arguments(source, "fromRGBO") {
|
|
615
|
+
let channels = positional_numbers(arguments)?;
|
|
616
|
+
if channels.len() != 4 {
|
|
617
|
+
return None;
|
|
618
|
+
}
|
|
619
|
+
return Some(
|
|
620
|
+
u32::from(to_opacity_byte(channels[3])?) << 24
|
|
621
|
+
| u32::from(to_byte(channels[0])?) << 16
|
|
622
|
+
| u32::from(to_byte(channels[1])?) << 8
|
|
623
|
+
| u32::from(to_byte(channels[2])?),
|
|
624
|
+
);
|
|
625
|
+
}
|
|
626
|
+
if let Some(arguments) = constructor_arguments(source, "from") {
|
|
627
|
+
let values = arguments
|
|
628
|
+
.split(',')
|
|
629
|
+
.filter_map(|argument| argument.split_once(':'))
|
|
630
|
+
.map(|(name, value)| (name.trim(), parse_number(value.trim())))
|
|
631
|
+
.collect::<BTreeMap<_, _>>();
|
|
632
|
+
let alpha = to_opacity_byte(values.get("alpha").copied().flatten()?)?;
|
|
633
|
+
let red = to_opacity_byte(values.get("red").copied().flatten()?)?;
|
|
634
|
+
let green = to_opacity_byte(values.get("green").copied().flatten()?)?;
|
|
635
|
+
let blue = to_opacity_byte(values.get("blue").copied().flatten()?)?;
|
|
636
|
+
return Some(
|
|
637
|
+
u32::from(alpha) << 24 | u32::from(red) << 16 | u32::from(green) << 8 | u32::from(blue),
|
|
638
|
+
);
|
|
639
|
+
}
|
|
640
|
+
let arguments = color_constructor_arguments(source, None)?;
|
|
641
|
+
let value = parse_number(arguments.trim())?;
|
|
642
|
+
(value.denominator == 1)
|
|
643
|
+
.then(|| u32::try_from(value.numerator.rem_euclid(1_i128 << 32)).ok())
|
|
644
|
+
.flatten()
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
fn is_colors_source(source: &str) -> bool {
|
|
648
|
+
source
|
|
649
|
+
.trim()
|
|
650
|
+
.rsplit_once('.')
|
|
651
|
+
.is_some_and(|(object, _)| object.rsplit('.').next() == Some("Colors"))
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
fn constructor_arguments<'source>(source: &'source str, constructor: &str) -> Option<&'source str> {
|
|
655
|
+
color_constructor_arguments(source, Some(constructor))
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
fn color_constructor_arguments<'source>(
|
|
659
|
+
source: &'source str,
|
|
660
|
+
constructor: Option<&str>,
|
|
661
|
+
) -> Option<&'source str> {
|
|
662
|
+
let color = source.find("Color")?;
|
|
663
|
+
let mut remainder = source[color + "Color".len()..].trim_start();
|
|
664
|
+
match constructor {
|
|
665
|
+
Some(constructor) => {
|
|
666
|
+
remainder = remainder.strip_prefix('.')?.trim_start();
|
|
667
|
+
remainder = remainder.strip_prefix(constructor)?.trim_start();
|
|
668
|
+
}
|
|
669
|
+
None => {
|
|
670
|
+
if let Some(after_new) = remainder.strip_prefix(".new") {
|
|
671
|
+
remainder = after_new.trim_start();
|
|
672
|
+
} else if remainder.starts_with('.') {
|
|
673
|
+
return None;
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
remainder
|
|
678
|
+
.strip_prefix('(')?
|
|
679
|
+
.rsplit_once(')')
|
|
680
|
+
.map(|(arguments, _)| arguments)
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
#[derive(Clone, Copy)]
|
|
684
|
+
struct Rational {
|
|
685
|
+
numerator: i128,
|
|
686
|
+
denominator: i128,
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
fn positional_numbers(arguments: &str) -> Option<Vec<Rational>> {
|
|
690
|
+
arguments.split(',').map(parse_number).collect()
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
fn parse_number(source: &str) -> Option<Rational> {
|
|
694
|
+
if let Some((numerator, denominator)) = source.split_once('/') {
|
|
695
|
+
let numerator = parse_number(numerator)?;
|
|
696
|
+
let denominator = parse_number(denominator)?;
|
|
697
|
+
if denominator.numerator == 0 {
|
|
698
|
+
return None;
|
|
699
|
+
}
|
|
700
|
+
let divided = Rational {
|
|
701
|
+
numerator: numerator.numerator.checked_mul(denominator.denominator)?,
|
|
702
|
+
denominator: numerator.denominator.checked_mul(denominator.numerator)?,
|
|
703
|
+
};
|
|
704
|
+
return if divided.denominator < 0 {
|
|
705
|
+
Some(Rational {
|
|
706
|
+
numerator: divided.numerator.checked_neg()?,
|
|
707
|
+
denominator: divided.denominator.checked_neg()?,
|
|
708
|
+
})
|
|
709
|
+
} else {
|
|
710
|
+
Some(divided)
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
parse_numeric_literal(source)
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
fn parse_numeric_literal(source: &str) -> Option<Rational> {
|
|
717
|
+
let normalized = source.trim().replace('_', "");
|
|
718
|
+
let (negative, normalized) = if let Some(unsigned) = normalized.strip_prefix('-') {
|
|
719
|
+
(true, unsigned)
|
|
720
|
+
} else {
|
|
721
|
+
(false, normalized.strip_prefix('+').unwrap_or(&normalized))
|
|
722
|
+
};
|
|
723
|
+
if normalized.is_empty() {
|
|
724
|
+
return None;
|
|
725
|
+
}
|
|
726
|
+
if let Some(hex) = normalized
|
|
727
|
+
.strip_prefix("0x")
|
|
728
|
+
.or_else(|| normalized.strip_prefix("0X"))
|
|
729
|
+
{
|
|
730
|
+
let numerator = i128::from_str_radix(hex, 16).ok()?;
|
|
731
|
+
return Some(Rational {
|
|
732
|
+
numerator: if negative {
|
|
733
|
+
numerator.checked_neg()?
|
|
734
|
+
} else {
|
|
735
|
+
numerator
|
|
736
|
+
},
|
|
737
|
+
denominator: 1,
|
|
738
|
+
});
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
let (mantissa, exponent) = if let Some(position) = normalized.find(['e', 'E']) {
|
|
742
|
+
(
|
|
743
|
+
&normalized[..position],
|
|
744
|
+
normalized[position + 1..].parse::<i32>().ok()?,
|
|
745
|
+
)
|
|
746
|
+
} else {
|
|
747
|
+
(normalized, 0)
|
|
748
|
+
};
|
|
749
|
+
let (whole, fraction) = mantissa
|
|
750
|
+
.split_once('.')
|
|
751
|
+
.map_or((mantissa, ""), |parts| parts);
|
|
752
|
+
if fraction.contains('.') || (whole.is_empty() && fraction.is_empty()) {
|
|
753
|
+
return None;
|
|
754
|
+
}
|
|
755
|
+
let digits = format!("{whole}{fraction}");
|
|
756
|
+
let mut numerator = digits.parse::<i128>().ok()?;
|
|
757
|
+
if negative {
|
|
758
|
+
numerator = numerator.checked_neg()?;
|
|
759
|
+
}
|
|
760
|
+
let mut denominator = 10_i128.checked_pow(u32::try_from(fraction.len()).ok()?)?;
|
|
761
|
+
if exponent >= 0 {
|
|
762
|
+
numerator = numerator.checked_mul(10_i128.checked_pow(exponent.unsigned_abs())?)?;
|
|
763
|
+
} else {
|
|
764
|
+
denominator = denominator.checked_mul(10_i128.checked_pow(exponent.unsigned_abs())?)?;
|
|
765
|
+
}
|
|
766
|
+
Some(Rational {
|
|
767
|
+
numerator,
|
|
768
|
+
denominator,
|
|
769
|
+
})
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
fn to_byte(value: Rational) -> Option<u8> {
|
|
773
|
+
(value.denominator != 0 && value.numerator % value.denominator == 0)
|
|
774
|
+
.then(|| {
|
|
775
|
+
u8::try_from((value.numerator / value.denominator).rem_euclid(i128::from(u8::MAX) + 1))
|
|
776
|
+
.ok()
|
|
777
|
+
})
|
|
778
|
+
.flatten()
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
fn to_opacity_byte(value: Rational) -> Option<u8> {
|
|
782
|
+
if value.denominator <= 0 {
|
|
783
|
+
return None;
|
|
784
|
+
}
|
|
785
|
+
if value.numerator <= 0 {
|
|
786
|
+
return Some(0);
|
|
787
|
+
}
|
|
788
|
+
if value.numerator >= value.denominator {
|
|
789
|
+
return Some(u8::MAX);
|
|
790
|
+
}
|
|
791
|
+
let scaled = value.numerator.checked_mul(255)?;
|
|
792
|
+
let rounded = scaled.checked_add(value.denominator / 2)? / value.denominator;
|
|
793
|
+
u8::try_from(rounded).ok()
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
fn argb_distance_squared(left: u32, right: u32) -> u32 {
|
|
797
|
+
[24, 16, 8, 0]
|
|
798
|
+
.into_iter()
|
|
799
|
+
.map(|shift| {
|
|
800
|
+
let left = i64::from((left >> shift) & 0xff);
|
|
801
|
+
let right = i64::from((right >> shift) & 0xff);
|
|
802
|
+
u32::try_from((left - right).pow(2)).unwrap_or(u32::MAX)
|
|
803
|
+
})
|
|
804
|
+
.sum()
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
fn format_distance(squared: u32) -> String {
|
|
808
|
+
format!("{:.2}", f64::from(squared).sqrt())
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
fn deduplicate_tokens(tokens: &mut Vec<ThemeTokenEvidence>) {
|
|
812
|
+
tokens.sort_by(|left, right| {
|
|
813
|
+
(
|
|
814
|
+
&left.name,
|
|
815
|
+
&left.path,
|
|
816
|
+
left.location.line,
|
|
817
|
+
left.location.column,
|
|
818
|
+
)
|
|
819
|
+
.cmp(&(
|
|
820
|
+
&right.name,
|
|
821
|
+
&right.path,
|
|
822
|
+
right.location.line,
|
|
823
|
+
right.location.column,
|
|
824
|
+
))
|
|
825
|
+
});
|
|
826
|
+
tokens.dedup_by(|left, right| {
|
|
827
|
+
left.name == right.name
|
|
828
|
+
&& left.path == right.path
|
|
829
|
+
&& left.location == right.location
|
|
830
|
+
&& left.value_kind == right.value_kind
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
fn sort_findings(findings: &mut [FlutterStyleFinding]) {
|
|
835
|
+
findings.sort_by(|left, right| {
|
|
836
|
+
(
|
|
837
|
+
left.kind,
|
|
838
|
+
&left.path,
|
|
839
|
+
left.location.line,
|
|
840
|
+
left.location.column,
|
|
841
|
+
&left.value,
|
|
842
|
+
)
|
|
843
|
+
.cmp(&(
|
|
844
|
+
right.kind,
|
|
845
|
+
&right.path,
|
|
846
|
+
right.location.line,
|
|
847
|
+
right.location.column,
|
|
848
|
+
&right.value,
|
|
849
|
+
))
|
|
850
|
+
});
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
fn parse_units(units: &[SourceUnit]) -> Result<Vec<ParsedSourceUnit<'_>>, HealthError> {
|
|
854
|
+
units
|
|
855
|
+
.par_iter()
|
|
856
|
+
.map(|unit| {
|
|
857
|
+
crate::dart_parser::parse_dart_source_lossy(&unit.path, &unit.source)
|
|
858
|
+
.map(|parsed| ParsedSourceUnit { unit, parsed })
|
|
859
|
+
.map_err(style_parse_error)
|
|
860
|
+
})
|
|
861
|
+
.collect::<Vec<_>>()
|
|
862
|
+
.into_iter()
|
|
863
|
+
.collect()
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
fn walk(node: Node<'_>, visit: &mut impl FnMut(Node<'_>)) {
|
|
867
|
+
visit(node);
|
|
868
|
+
let mut cursor = node.walk();
|
|
869
|
+
for child in node.named_children(&mut cursor) {
|
|
870
|
+
walk(child, visit);
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
fn node_text<'source>(node: Node<'_>, source: &'source str) -> &'source str {
|
|
875
|
+
source
|
|
876
|
+
.get(node.start_byte()..node.end_byte())
|
|
877
|
+
.unwrap_or_default()
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
fn field_text(node: Node<'_>, field: &str, source: &str) -> Option<String> {
|
|
881
|
+
node.child_by_field_name(field)
|
|
882
|
+
.map(|child| node_text(child, source).to_owned())
|
|
883
|
+
}
|