dart-decimate 0.0.10 → 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/src/output.rs CHANGED
@@ -11,6 +11,7 @@ mod html;
11
11
  mod human;
12
12
  mod human_details;
13
13
  mod next_steps;
14
+ mod route_cycle;
14
15
  mod route_findings;
15
16
  mod runtime_coverage;
16
17
  mod scope;
@@ -297,7 +298,7 @@ fn report_findings(
297
298
  add_symbol_findings(&project.root, symbols, &mut findings);
298
299
  }
299
300
 
300
- add_cycle_findings(&project.root, &results.cycles, &mut findings);
301
+ add_cycle_findings(project, &results.cycles, &mut findings);
301
302
  add_re_export_cycle_findings(&project.root, &results.re_export_cycles, &mut findings);
302
303
  add_boundary_findings(&project.root, &results.boundary_violations, &mut findings);
303
304
  graph_findings::add_boundary_coverage_findings(
@@ -0,0 +1,684 @@
1
+ use std::collections::{BTreeMap, BTreeSet};
2
+
3
+ use tree_sitter::Node;
4
+
5
+ use super::patterns::{
6
+ object_pattern_field_reads_in_body, remove_roots_shadowed_by_enclosing_callable,
7
+ root_aliases_at, top_level_name_shadowed_at,
8
+ };
9
+ use super::reads::direct_identifier_shadowed_before;
10
+
11
+ #[derive(Debug, Clone, PartialEq, Eq)]
12
+ pub(super) struct WidgetParamForwarder {
13
+ name: String,
14
+ parameter: ForwardedParameter,
15
+ fields: BTreeSet<String>,
16
+ }
17
+
18
+ #[derive(Debug, Clone, PartialEq, Eq)]
19
+ struct ForwardedParameter {
20
+ name: String,
21
+ positional_index: Option<usize>,
22
+ }
23
+
24
+ pub(super) fn forwarded_param_used(
25
+ forwarders: Option<&Vec<WidgetParamForwarder>>,
26
+ widget_body: Node<'_>,
27
+ state_bodies: Option<&Vec<Node<'_>>>,
28
+ field_name: &str,
29
+ source: &str,
30
+ ) -> bool {
31
+ let Some(forwarders) = forwarders else {
32
+ return false;
33
+ };
34
+ forwarders.iter().any(|forwarder| {
35
+ forwarder.fields.contains(field_name)
36
+ && (body_calls_forwarder_with_roots(
37
+ widget_body,
38
+ &forwarder.name,
39
+ &forwarder.parameter,
40
+ &["this"],
41
+ source,
42
+ ) || state_bodies.is_some_and(|state_bodies| {
43
+ state_bodies.iter().any(|state_body| {
44
+ body_calls_forwarder_with_roots(
45
+ *state_body,
46
+ &forwarder.name,
47
+ &forwarder.parameter,
48
+ &["widget", "oldWidget"],
49
+ source,
50
+ )
51
+ })
52
+ }))
53
+ })
54
+ }
55
+
56
+ pub(super) fn widget_forwarded_param_uses(
57
+ classes: &[Node<'_>],
58
+ source: &str,
59
+ ) -> BTreeMap<String, Vec<WidgetParamForwarder>> {
60
+ let mut uses = BTreeMap::<String, Vec<WidgetParamForwarder>>::new();
61
+ for class in classes {
62
+ let mut declarations = Vec::new();
63
+ collect_nodes_in(
64
+ *class,
65
+ &["declaration", "method_declaration"],
66
+ &mut declarations,
67
+ );
68
+ for declaration in declarations {
69
+ let Some(signature) = declaration_constructor_signature(declaration) else {
70
+ continue;
71
+ };
72
+ let Some(name) = constructor_qualified_name(*class, signature, source) else {
73
+ continue;
74
+ };
75
+ for parameter in constructor_widget_params(signature, source) {
76
+ let mut fields = member_fields_read_from(declaration, &parameter.name, source);
77
+ let scan_root = constructor_scan_root(signature, declaration);
78
+ if !same_node(scan_root, declaration) {
79
+ fields.extend(member_fields_read_from(scan_root, &parameter.name, source));
80
+ }
81
+ fields.extend(object_pattern_field_reads_in_body(
82
+ declaration,
83
+ &parameter.widget_class,
84
+ &[&parameter.name],
85
+ source,
86
+ ));
87
+ if let Some(body) = constructor_body(scan_root) {
88
+ fields.extend(object_pattern_field_reads_in_body(
89
+ body,
90
+ &parameter.widget_class,
91
+ &[&parameter.name],
92
+ source,
93
+ ));
94
+ }
95
+ if fields.is_empty() {
96
+ continue;
97
+ }
98
+ uses.entry(parameter.widget_class.clone())
99
+ .or_default()
100
+ .push(WidgetParamForwarder {
101
+ name: name.clone(),
102
+ parameter: ForwardedParameter {
103
+ name: parameter.name,
104
+ positional_index: parameter.positional_index,
105
+ },
106
+ fields,
107
+ });
108
+ }
109
+ }
110
+ }
111
+ uses
112
+ }
113
+
114
+ const CONSTRUCTOR_SIGNATURES: &[&str] = &[
115
+ "constructor_signature",
116
+ "constant_constructor_signature",
117
+ "factory_constructor_signature",
118
+ "redirecting_factory_constructor_signature",
119
+ ];
120
+
121
+ fn declaration_constructor_signature(declaration: Node<'_>) -> Option<Node<'_>> {
122
+ if CONSTRUCTOR_SIGNATURES.contains(&declaration.kind()) {
123
+ return Some(declaration);
124
+ }
125
+ let mut cursor = declaration.walk();
126
+ declaration
127
+ .named_children(&mut cursor)
128
+ .find_map(declaration_constructor_signature)
129
+ }
130
+
131
+ fn constructor_scan_root<'tree>(signature: Node<'tree>, fallback: Node<'tree>) -> Node<'tree> {
132
+ let mut root = fallback;
133
+ let mut current = signature;
134
+ while let Some(parent) = current.parent() {
135
+ if matches!(parent.kind(), "class_body" | "class_declaration") {
136
+ break;
137
+ }
138
+ if matches!(
139
+ parent.kind(),
140
+ "class_member" | "declaration" | "method_declaration"
141
+ ) {
142
+ root = parent;
143
+ }
144
+ current = parent;
145
+ }
146
+ root
147
+ }
148
+
149
+ fn constructor_body(node: Node<'_>) -> Option<Node<'_>> {
150
+ if node.kind() == "function_body" {
151
+ return Some(node);
152
+ }
153
+ let mut cursor = node.walk();
154
+ node.named_children(&mut cursor).find_map(constructor_body)
155
+ }
156
+
157
+ fn constructor_qualified_name(
158
+ class: Node<'_>,
159
+ signature: Node<'_>,
160
+ source: &str,
161
+ ) -> Option<String> {
162
+ let owner = class
163
+ .child_by_field_name("name")
164
+ .and_then(|name| name.utf8_text(source.as_bytes()).ok())
165
+ .map(str::to_owned)?;
166
+ let mut cursor = signature.walk();
167
+ let mut parts = signature
168
+ .children_by_field_name("name", &mut cursor)
169
+ .filter(|child| matches!(child.kind(), "identifier" | "new"))
170
+ .filter_map(|child| child.utf8_text(source.as_bytes()).ok())
171
+ .map(str::to_owned);
172
+ let first = parts.next()?;
173
+ let member = member_name_without_owner(&parts.last().unwrap_or(first), &owner);
174
+ if member == owner || member == "new" {
175
+ Some(owner)
176
+ } else {
177
+ Some(format!("{owner}.{member}"))
178
+ }
179
+ }
180
+
181
+ fn member_name_without_owner(name: &str, owner: &str) -> String {
182
+ name.strip_prefix(owner)
183
+ .and_then(|rest| rest.strip_prefix('.'))
184
+ .unwrap_or(name)
185
+ .to_owned()
186
+ }
187
+
188
+ struct ConstructorWidgetParam {
189
+ name: String,
190
+ widget_class: String,
191
+ positional_index: Option<usize>,
192
+ }
193
+
194
+ fn constructor_widget_params(signature: Node<'_>, source: &str) -> Vec<ConstructorWidgetParam> {
195
+ let Some(parameters) = signature.child_by_field_name("parameters") else {
196
+ return Vec::new();
197
+ };
198
+ let formal_parameters = direct_formal_parameters(parameters);
199
+ let mut positional_index = 0usize;
200
+ let mut widget_params = Vec::new();
201
+ for param in formal_parameters {
202
+ let is_named = is_named_parameter(param, source);
203
+ let current_positional_index = (!is_named).then_some(positional_index);
204
+ if !is_named {
205
+ positional_index += 1;
206
+ }
207
+ let Some(name) = formal_parameter_name(param, source) else {
208
+ continue;
209
+ };
210
+ let Some(widget_class) = formal_parameter_type(param, &name, source) else {
211
+ continue;
212
+ };
213
+ widget_params.push(ConstructorWidgetParam {
214
+ name,
215
+ widget_class,
216
+ positional_index: current_positional_index,
217
+ });
218
+ }
219
+ widget_params
220
+ }
221
+
222
+ fn direct_formal_parameters(parameters: Node<'_>) -> Vec<Node<'_>> {
223
+ let mut formal_parameters = Vec::new();
224
+ let mut cursor = parameters.walk();
225
+ for candidate in parameters.named_children(&mut cursor) {
226
+ if candidate.kind() == "optional_formal_parameters" {
227
+ let mut optional_cursor = candidate.walk();
228
+ formal_parameters.extend(candidate.named_children(&mut optional_cursor));
229
+ } else {
230
+ formal_parameters.push(candidate);
231
+ }
232
+ }
233
+ formal_parameters
234
+ }
235
+
236
+ fn is_named_parameter(param: Node<'_>, source: &str) -> bool {
237
+ let Some(parent) = param.parent() else {
238
+ return false;
239
+ };
240
+ parent.kind() == "optional_formal_parameters"
241
+ && parent
242
+ .utf8_text(source.as_bytes())
243
+ .ok()
244
+ .is_some_and(|text| text.trim_start().starts_with('{'))
245
+ }
246
+
247
+ fn formal_parameter_name(param: Node<'_>, source: &str) -> Option<String> {
248
+ param
249
+ .child_by_field_name("name")
250
+ .or_else(|| last_identifier_child(param, source))
251
+ .and_then(|name| name.utf8_text(source.as_bytes()).ok())
252
+ .map(str::to_owned)
253
+ }
254
+
255
+ fn formal_parameter_type(param: Node<'_>, name: &str, source: &str) -> Option<String> {
256
+ let text = param.utf8_text(source.as_bytes()).ok()?;
257
+ let name_index = text.rfind(name)?;
258
+ let before_name = text[..name_index].trim();
259
+ let type_name = before_name
260
+ .split_whitespace()
261
+ .rfind(|part| !matches!(*part, "required" | "covariant" | "final" | "var"))?;
262
+ Some(simple_type_name(type_name.trim_end_matches('?')))
263
+ }
264
+
265
+ fn last_identifier_child<'tree>(node: Node<'tree>, source: &str) -> Option<Node<'tree>> {
266
+ let mut cursor = node.walk();
267
+ node.named_children(&mut cursor)
268
+ .filter(|child| {
269
+ matches!(
270
+ child.kind(),
271
+ "identifier" | "identifier_dollar_escaped" | "type_identifier"
272
+ ) && child.utf8_text(source.as_bytes()).ok() != Some("key")
273
+ })
274
+ .last()
275
+ }
276
+
277
+ fn member_fields_read_from(node: Node<'_>, object_name: &str, source: &str) -> BTreeSet<String> {
278
+ let mut fields = BTreeSet::new();
279
+ collect_member_fields_read_from(node, node, object_name, source, &mut fields);
280
+ fields
281
+ }
282
+
283
+ fn collect_member_fields_read_from(
284
+ root: Node<'_>,
285
+ node: Node<'_>,
286
+ object_name: &str,
287
+ source: &str,
288
+ fields: &mut BTreeSet<String>,
289
+ ) {
290
+ if !same_node(root, node) && callable_direct_parameters_bind_name(node, object_name, source) {
291
+ return;
292
+ }
293
+ if let Some(field) = member_property_for_object(root, node, object_name, source) {
294
+ fields.insert(field);
295
+ }
296
+ let mut cursor = node.walk();
297
+ for child in node.named_children(&mut cursor) {
298
+ collect_member_fields_read_from(root, child, object_name, source, fields);
299
+ }
300
+ }
301
+
302
+ fn callable_direct_parameters_bind_name(node: Node<'_>, name: &str, source: &str) -> bool {
303
+ if !matches!(
304
+ node.kind(),
305
+ "function_declaration"
306
+ | "function_expression"
307
+ | "local_function_declaration"
308
+ | "method_declaration"
309
+ ) {
310
+ return false;
311
+ }
312
+ direct_parameter_lists(node)
313
+ .into_iter()
314
+ .any(|parameters| parameter_list_directly_binds_name(parameters, name, source))
315
+ }
316
+
317
+ fn direct_parameter_lists(node: Node<'_>) -> Vec<Node<'_>> {
318
+ match node.kind() {
319
+ "function_expression" => parameter_field_lists(node),
320
+ "function_declaration" | "local_function_declaration" | "method_declaration" => {
321
+ helper_signature(node)
322
+ .and_then(parameter_list)
323
+ .into_iter()
324
+ .collect()
325
+ }
326
+ _ => Vec::new(),
327
+ }
328
+ }
329
+
330
+ fn helper_signature(node: Node<'_>) -> Option<Node<'_>> {
331
+ node.child_by_field_name("signature")
332
+ .or_else(|| direct_named_child(node, "function_signature"))
333
+ }
334
+
335
+ fn parameter_list(node: Node<'_>) -> Option<Node<'_>> {
336
+ node.child_by_field_name("parameters")
337
+ .or_else(|| direct_named_child(node, "formal_parameter_list"))
338
+ }
339
+
340
+ fn parameter_field_lists(node: Node<'_>) -> Vec<Node<'_>> {
341
+ let mut cursor = node.walk();
342
+ node.children_by_field_name("parameters", &mut cursor)
343
+ .filter_map(formal_parameter_list)
344
+ .collect()
345
+ }
346
+
347
+ fn formal_parameter_list(node: Node<'_>) -> Option<Node<'_>> {
348
+ if node.kind() == "formal_parameter_list" {
349
+ return Some(node);
350
+ }
351
+ let mut cursor = node.walk();
352
+ node.named_children(&mut cursor)
353
+ .find(|child| child.kind() == "formal_parameter_list")
354
+ }
355
+
356
+ fn parameter_list_directly_binds_name(parameters: Node<'_>, name: &str, source: &str) -> bool {
357
+ let mut cursor = parameters.walk();
358
+ parameters.named_children(&mut cursor).any(|candidate| {
359
+ if candidate.kind() == "formal_parameter" {
360
+ return formal_parameter_name(candidate, source).as_deref() == Some(name);
361
+ }
362
+ candidate.kind() == "optional_formal_parameters"
363
+ && optional_parameters_directly_bind_name(candidate, name, source)
364
+ })
365
+ }
366
+
367
+ fn optional_parameters_directly_bind_name(parameters: Node<'_>, name: &str, source: &str) -> bool {
368
+ let mut cursor = parameters.walk();
369
+ parameters
370
+ .named_children(&mut cursor)
371
+ .filter(|candidate| candidate.kind() == "formal_parameter")
372
+ .any(|candidate| formal_parameter_name(candidate, source).as_deref() == Some(name))
373
+ }
374
+
375
+ fn member_property_for_object(
376
+ root: Node<'_>,
377
+ node: Node<'_>,
378
+ object_name: &str,
379
+ source: &str,
380
+ ) -> Option<String> {
381
+ if !matches!(
382
+ node.kind(),
383
+ "member_expression" | "null_aware_member_expression" | "assignable_expression"
384
+ ) {
385
+ return None;
386
+ }
387
+ let object = node.child_by_field_name("object")?;
388
+ if object.utf8_text(source.as_bytes()).ok() != Some(object_name) {
389
+ return None;
390
+ }
391
+ if direct_identifier_shadowed_before(root, object, object_name, source) {
392
+ return None;
393
+ }
394
+ node.child_by_field_name("property")
395
+ .and_then(|property| property.utf8_text(source.as_bytes()).ok())
396
+ .map(str::to_owned)
397
+ }
398
+
399
+ fn collect_nodes_in<'tree>(node: Node<'tree>, kinds: &[&str], nodes: &mut Vec<Node<'tree>>) {
400
+ if kinds.contains(&node.kind()) {
401
+ nodes.push(node);
402
+ }
403
+ let mut cursor = node.walk();
404
+ for child in node.named_children(&mut cursor) {
405
+ collect_nodes_in(child, kinds, nodes);
406
+ }
407
+ }
408
+
409
+ fn direct_named_child<'tree>(node: Node<'tree>, kind: &str) -> Option<Node<'tree>> {
410
+ let mut cursor = node.walk();
411
+ node.named_children(&mut cursor)
412
+ .find(|child| child.kind() == kind)
413
+ }
414
+
415
+ fn body_calls_forwarder_with_roots(
416
+ body: Node<'_>,
417
+ forwarder_name: &str,
418
+ parameter: &ForwardedParameter,
419
+ root_names: &[&str],
420
+ source: &str,
421
+ ) -> bool {
422
+ let mut found = false;
423
+ visit_named(body, &mut |node| {
424
+ if found {
425
+ return;
426
+ }
427
+ let Some(invocation_name) = invocation_name(node, source) else {
428
+ return;
429
+ };
430
+ if invocation_name != forwarder_name {
431
+ return;
432
+ }
433
+ if forwarder_owner_shadowed(body, node, &invocation_name, source) {
434
+ return;
435
+ }
436
+ let mut roots = root_aliases_at(body, node, root_names, source);
437
+ remove_roots_shadowed_by_enclosing_callable(body, node, &mut roots, source);
438
+ if !roots.is_empty() && invocation_arguments_pass_roots(node, parameter, &roots, source) {
439
+ found = true;
440
+ }
441
+ });
442
+ found
443
+ }
444
+
445
+ fn forwarder_owner_shadowed(
446
+ body: Node<'_>,
447
+ invocation: Node<'_>,
448
+ forwarder_name: &str,
449
+ source: &str,
450
+ ) -> bool {
451
+ let owner = forwarder_name
452
+ .split_once('.')
453
+ .map_or(forwarder_name, |(owner, _)| owner);
454
+ top_level_name_shadowed_at(body, invocation, owner, source)
455
+ }
456
+
457
+ fn invocation_name(node: Node<'_>, source: &str) -> Option<String> {
458
+ if !matches!(
459
+ node.kind(),
460
+ "call_expression" | "constructor_invocation" | "const_object_expression" | "new_expression"
461
+ ) {
462
+ return None;
463
+ }
464
+ if let Some(name) = constructor_invocation_name(node, source) {
465
+ return Some(name);
466
+ }
467
+ let arguments = node.child_by_field_name("arguments")?;
468
+ source
469
+ .get(node.start_byte()..arguments.start_byte())
470
+ .map(normalized_invocation_prefix)
471
+ }
472
+
473
+ fn constructor_invocation_name(node: Node<'_>, source: &str) -> Option<String> {
474
+ if !matches!(
475
+ node.kind(),
476
+ "constructor_invocation" | "const_object_expression" | "new_expression"
477
+ ) {
478
+ return None;
479
+ }
480
+ let type_name = node
481
+ .child_by_field_name("type")?
482
+ .utf8_text(source.as_bytes())
483
+ .ok()
484
+ .map(strip_whitespace)
485
+ .map(|name| strip_type_arguments(&name))?;
486
+ node.child_by_field_name("constructor")
487
+ .and_then(|constructor| constructor.utf8_text(source.as_bytes()).ok())
488
+ .map_or(Some(type_name.clone()), |constructor| {
489
+ Some(format!("{type_name}.{constructor}"))
490
+ })
491
+ }
492
+
493
+ fn normalized_invocation_prefix(prefix: &str) -> String {
494
+ let prefix = strip_whitespace(
495
+ prefix
496
+ .trim()
497
+ .strip_prefix("const ")
498
+ .or_else(|| prefix.trim().strip_prefix("new "))
499
+ .unwrap_or(prefix.trim()),
500
+ );
501
+ strip_type_arguments(&prefix)
502
+ }
503
+
504
+ fn invocation_arguments_pass_roots(
505
+ node: Node<'_>,
506
+ parameter: &ForwardedParameter,
507
+ roots: &BTreeSet<String>,
508
+ source: &str,
509
+ ) -> bool {
510
+ let Some(arguments) = node.child_by_field_name("arguments") else {
511
+ return false;
512
+ };
513
+ let mut positional_index = 0usize;
514
+ let mut cursor = arguments.walk();
515
+ for argument in arguments.named_children(&mut cursor) {
516
+ if argument.kind() == "named_argument" {
517
+ if named_argument_label(argument, source).as_deref() == Some(parameter.name.as_str())
518
+ && named_argument_matches_roots(argument, roots, source)
519
+ {
520
+ return true;
521
+ }
522
+ continue;
523
+ }
524
+ if parameter.positional_index == Some(positional_index)
525
+ && argument_matches_roots(argument, roots, source)
526
+ {
527
+ return true;
528
+ }
529
+ positional_index += 1;
530
+ }
531
+ argument_texts_pass_roots(arguments, parameter, roots, source)
532
+ }
533
+
534
+ fn named_argument_label(node: Node<'_>, source: &str) -> Option<String> {
535
+ let mut cursor = node.walk();
536
+ node.named_children(&mut cursor)
537
+ .find(|child| child.kind() == "label")
538
+ .and_then(|label| label.utf8_text(source.as_bytes()).ok())
539
+ .map(str::trim)
540
+ .and_then(|label| label.strip_suffix(':'))
541
+ .map(str::to_owned)
542
+ }
543
+
544
+ fn named_argument_value(node: Node<'_>) -> Option<Node<'_>> {
545
+ let mut cursor = node.walk();
546
+ node.named_children(&mut cursor)
547
+ .find(|child| child.kind() != "label")
548
+ }
549
+
550
+ fn named_argument_matches_roots(
551
+ argument: Node<'_>,
552
+ roots: &BTreeSet<String>,
553
+ source: &str,
554
+ ) -> bool {
555
+ named_argument_value(argument).is_some_and(|value| argument_matches_roots(value, roots, source))
556
+ || argument
557
+ .utf8_text(source.as_bytes())
558
+ .ok()
559
+ .and_then(|text| text.split_once(':').map(|(_, value)| value))
560
+ .is_some_and(|value| expression_matches_roots(value, roots))
561
+ }
562
+
563
+ fn argument_matches_roots(argument: Node<'_>, roots: &BTreeSet<String>, source: &str) -> bool {
564
+ argument
565
+ .utf8_text(source.as_bytes())
566
+ .ok()
567
+ .map(strip_whitespace)
568
+ .is_some_and(|text| roots.contains(&text))
569
+ }
570
+
571
+ fn argument_texts_pass_roots(
572
+ arguments: Node<'_>,
573
+ parameter: &ForwardedParameter,
574
+ roots: &BTreeSet<String>,
575
+ source: &str,
576
+ ) -> bool {
577
+ let Some(text) = arguments.utf8_text(source.as_bytes()).ok() else {
578
+ return false;
579
+ };
580
+ let mut positional_index = 0usize;
581
+ for argument in split_argument_texts(text) {
582
+ if let Some((label, value)) = argument.split_once(':') {
583
+ if label.trim() == parameter.name && expression_matches_roots(value, roots) {
584
+ return true;
585
+ }
586
+ continue;
587
+ }
588
+ if parameter.positional_index == Some(positional_index)
589
+ && expression_matches_roots(argument, roots)
590
+ {
591
+ return true;
592
+ }
593
+ positional_index += 1;
594
+ }
595
+ false
596
+ }
597
+
598
+ fn split_argument_texts(text: &str) -> Vec<&str> {
599
+ let inner = text
600
+ .trim()
601
+ .strip_prefix('(')
602
+ .and_then(|text| text.strip_suffix(')'))
603
+ .unwrap_or(text)
604
+ .trim();
605
+ if inner.is_empty() {
606
+ return Vec::new();
607
+ }
608
+ let mut parts = Vec::new();
609
+ let mut start = 0usize;
610
+ let mut depth = 0usize;
611
+ for (index, character) in inner.char_indices() {
612
+ match character {
613
+ '(' | '[' | '{' => depth += 1,
614
+ ')' | ']' | '}' => depth = depth.saturating_sub(1),
615
+ ',' if depth == 0 => {
616
+ parts.push(inner[start..index].trim());
617
+ start = index + 1;
618
+ }
619
+ _ => {}
620
+ }
621
+ }
622
+ parts.push(inner[start..].trim());
623
+ parts
624
+ }
625
+
626
+ fn expression_matches_roots(expression: &str, roots: &BTreeSet<String>) -> bool {
627
+ let expression = normalized_expression(expression);
628
+ roots.contains(&expression)
629
+ }
630
+
631
+ fn normalized_expression(expression: &str) -> String {
632
+ let mut expression = expression
633
+ .split([';', ',', '}'])
634
+ .next()
635
+ .unwrap_or(expression)
636
+ .trim()
637
+ .trim_end_matches(';')
638
+ .trim()
639
+ .to_owned();
640
+ while expression.starts_with('(') && expression.ends_with(')') {
641
+ expression = expression[1..expression.len() - 1].trim().to_owned();
642
+ }
643
+ strip_whitespace(&expression)
644
+ }
645
+
646
+ fn strip_whitespace(text: &str) -> String {
647
+ text.chars()
648
+ .filter(|character| !character.is_whitespace())
649
+ .collect()
650
+ }
651
+
652
+ fn strip_type_arguments(text: &str) -> String {
653
+ let mut stripped = String::new();
654
+ let mut depth = 0usize;
655
+ for character in text.chars() {
656
+ match character {
657
+ '<' => depth += 1,
658
+ '>' => depth = depth.saturating_sub(1),
659
+ _ if depth == 0 => stripped.push(character),
660
+ _ => {}
661
+ }
662
+ }
663
+ stripped
664
+ }
665
+
666
+ fn simple_type_name(text: &str) -> String {
667
+ let text = text.trim_end_matches('?');
668
+ let text = text.split('<').next().unwrap_or(text).trim();
669
+ text.rsplit('.').next().unwrap_or(text).to_owned()
670
+ }
671
+
672
+ fn visit_named(node: Node<'_>, visitor: &mut impl FnMut(Node<'_>)) {
673
+ visitor(node);
674
+ let mut cursor = node.walk();
675
+ for child in node.named_children(&mut cursor) {
676
+ visit_named(child, visitor);
677
+ }
678
+ }
679
+
680
+ fn same_node(left: Node<'_>, right: Node<'_>) -> bool {
681
+ left.kind() == right.kind()
682
+ && left.start_byte() == right.start_byte()
683
+ && left.end_byte() == right.end_byte()
684
+ }