dart-decimate 0.0.10 → 0.0.12
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 +3 -3
- package/Cargo.toml +1 -1
- package/README.md +13 -4
- package/package.json +1 -1
- package/src/dupes/declarations.rs +512 -0
- package/src/dupes/packages.rs +143 -1
- package/src/dupes/tests.rs +170 -0
- package/src/dupes.rs +54 -21
- package/src/output/graph_findings.rs +51 -17
- package/src/output/route_cycle/aliases.rs +875 -0
- package/src/output/route_cycle/navigation.rs +1005 -0
- package/src/output/route_cycle/receivers.rs +41 -0
- package/src/output/route_cycle/state_context.rs +209 -0
- package/src/output/route_cycle/tests.rs +175 -0
- package/src/output/route_cycle.rs +730 -0
- package/src/output.rs +2 -1
- package/src/widgets/forwarding.rs +684 -0
- package/src/widgets/patterns.rs +2595 -0
- package/src/widgets/reads.rs +616 -0
- package/src/widgets/tests.rs +1837 -55
- package/src/widgets/unrendered.rs +150 -0
- package/src/widgets.rs +25 -93
|
@@ -141,6 +141,10 @@ fn is_object_constructor(node: Node<'_>) -> bool {
|
|
|
141
141
|
}
|
|
142
142
|
|
|
143
143
|
fn constructor_type_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
144
|
+
if let Some(constructor) = function_expression_body_constructor_name(node, source) {
|
|
145
|
+
return Some(constructor);
|
|
146
|
+
}
|
|
147
|
+
|
|
144
148
|
let arguments = node.child_by_field_name("arguments")?;
|
|
145
149
|
let prefix = source.get(node.start_byte()..arguments.start_byte())?;
|
|
146
150
|
let constructor = prefix
|
|
@@ -155,6 +159,92 @@ fn constructor_type_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
|
155
159
|
(!constructor.is_empty()).then_some(constructor)
|
|
156
160
|
}
|
|
157
161
|
|
|
162
|
+
fn function_expression_body_constructor_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
163
|
+
if node.kind() != "call_expression" {
|
|
164
|
+
return None;
|
|
165
|
+
}
|
|
166
|
+
let function = node.child_by_field_name("function")?;
|
|
167
|
+
if function.kind() != "function_expression" {
|
|
168
|
+
return None;
|
|
169
|
+
}
|
|
170
|
+
let body = function.child_by_field_name("body")?;
|
|
171
|
+
let expression = body.named_child(0)?;
|
|
172
|
+
constructor_call_type_name(expression, source)
|
|
173
|
+
.or_else(|| split_closure_body_constructor_name(node, expression, source))
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
fn constructor_call_type_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
177
|
+
if !is_object_constructor(node) {
|
|
178
|
+
return None;
|
|
179
|
+
}
|
|
180
|
+
let arguments = node.child_by_field_name("arguments")?;
|
|
181
|
+
let prefix = source.get(node.start_byte()..arguments.start_byte())?;
|
|
182
|
+
let constructor = prefix
|
|
183
|
+
.trim()
|
|
184
|
+
.strip_prefix("const ")
|
|
185
|
+
.or_else(|| prefix.trim().strip_prefix("new "))
|
|
186
|
+
.unwrap_or(prefix.trim())
|
|
187
|
+
.split('<')
|
|
188
|
+
.next()
|
|
189
|
+
.unwrap_or("")
|
|
190
|
+
.replace(' ', "");
|
|
191
|
+
(!constructor_name_candidates(&constructor).is_empty()).then_some(constructor)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
fn split_closure_body_constructor_name(
|
|
195
|
+
call: Node<'_>,
|
|
196
|
+
expression: Node<'_>,
|
|
197
|
+
source: &str,
|
|
198
|
+
) -> Option<String> {
|
|
199
|
+
call.child_by_field_name("arguments")?;
|
|
200
|
+
if !expression_is_constructor_call_name(expression, source) {
|
|
201
|
+
return None;
|
|
202
|
+
}
|
|
203
|
+
expression
|
|
204
|
+
.utf8_text(source.as_bytes())
|
|
205
|
+
.ok()
|
|
206
|
+
.map(str::to_owned)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
fn expression_is_constructor_call_name(expression: Node<'_>, source: &str) -> bool {
|
|
210
|
+
if !matches!(expression.kind(), "identifier" | "type_identifier") {
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
let Some(suffix) = source.get(expression.end_byte()..) else {
|
|
214
|
+
return false;
|
|
215
|
+
};
|
|
216
|
+
let suffix = suffix.trim_start();
|
|
217
|
+
if suffix.starts_with('(') {
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
if suffix.starts_with('<')
|
|
221
|
+
&& let Some(after_type_arguments) =
|
|
222
|
+
matching_enclosed_end(suffix, '<', '>').and_then(|end| suffix.get(end + 1..))
|
|
223
|
+
{
|
|
224
|
+
return after_type_arguments.trim_start().starts_with('(');
|
|
225
|
+
}
|
|
226
|
+
false
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
fn matching_enclosed_end(text: &str, open: char, close: char) -> Option<usize> {
|
|
230
|
+
let mut chars = text.char_indices();
|
|
231
|
+
if chars.next().is_none_or(|(_, character)| character != open) {
|
|
232
|
+
return None;
|
|
233
|
+
}
|
|
234
|
+
let mut depth = 0usize;
|
|
235
|
+
for (index, character) in text.char_indices() {
|
|
236
|
+
if character == open {
|
|
237
|
+
depth += 1;
|
|
238
|
+
} else if character == close {
|
|
239
|
+
depth = depth.checked_sub(1)?;
|
|
240
|
+
if depth == 0 {
|
|
241
|
+
return Some(index);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
None
|
|
246
|
+
}
|
|
247
|
+
|
|
158
248
|
fn is_arrow_body_constructor_identifier(node: Node<'_>, source: &str) -> bool {
|
|
159
249
|
if !matches!(node.kind(), "identifier" | "type_identifier")
|
|
160
250
|
|| node
|
|
@@ -339,6 +429,7 @@ void main() {
|
|
|
339
429
|
ui.PrefixedCard();
|
|
340
430
|
DeadCard.named();
|
|
341
431
|
DeadCard.route;
|
|
432
|
+
(() => BareIifeCard).call();
|
|
342
433
|
}
|
|
343
434
|
";
|
|
344
435
|
let parsed = parse_tree(Path::new("lib/widgets.dart"), source)?;
|
|
@@ -348,9 +439,44 @@ void main() {
|
|
|
348
439
|
names,
|
|
349
440
|
vec!["LiveCard", "LegacyCard", "PrefixedCard", "DeadCard"]
|
|
350
441
|
);
|
|
442
|
+
assert!(
|
|
443
|
+
!names.iter().any(|name| name == "BareIifeCard"),
|
|
444
|
+
"{names:?}"
|
|
445
|
+
);
|
|
446
|
+
Ok(())
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
#[test]
|
|
450
|
+
fn split_closure_body_constructor_name_rejects_member_references()
|
|
451
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
452
|
+
let source = r"
|
|
453
|
+
void main(BuildContext context) {
|
|
454
|
+
print(DeadCard.route);
|
|
455
|
+
}
|
|
456
|
+
";
|
|
457
|
+
let parsed = parse_tree(Path::new("lib/widgets.dart"), source)?;
|
|
458
|
+
let root = parsed.tree().root_node();
|
|
459
|
+
let call = first_named_node(root, "call_expression")
|
|
460
|
+
.ok_or_else(|| std::io::Error::other("missing call_expression"))?;
|
|
461
|
+
let member = first_named_node(root, "member_expression")
|
|
462
|
+
.ok_or_else(|| std::io::Error::other("missing member_expression"))?;
|
|
463
|
+
|
|
464
|
+
assert_eq!(
|
|
465
|
+
split_closure_body_constructor_name(call, member, parsed.source()),
|
|
466
|
+
None
|
|
467
|
+
);
|
|
351
468
|
Ok(())
|
|
352
469
|
}
|
|
353
470
|
|
|
471
|
+
fn first_named_node<'tree>(node: Node<'tree>, kind: &str) -> Option<Node<'tree>> {
|
|
472
|
+
if node.kind() == kind {
|
|
473
|
+
return Some(node);
|
|
474
|
+
}
|
|
475
|
+
let mut cursor = node.walk();
|
|
476
|
+
node.named_children(&mut cursor)
|
|
477
|
+
.find_map(|child| first_named_node(child, kind))
|
|
478
|
+
}
|
|
479
|
+
|
|
354
480
|
#[test]
|
|
355
481
|
fn object_constructor_names_include_material_page_route_builders()
|
|
356
482
|
-> Result<(), Box<dyn std::error::Error>> {
|
|
@@ -370,4 +496,28 @@ void open(BuildContext context) {
|
|
|
370
496
|
assert!(names.iter().any(|name| name == "CartScreen"));
|
|
371
497
|
Ok(())
|
|
372
498
|
}
|
|
499
|
+
|
|
500
|
+
#[test]
|
|
501
|
+
fn object_constructor_names_include_generic_builder_children()
|
|
502
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
503
|
+
let source = r"
|
|
504
|
+
class RoutineConceptFlow extends StatelessWidget {
|
|
505
|
+
Widget build(BuildContext context) => BentoList<RoutineConceptItem>(
|
|
506
|
+
items: const [],
|
|
507
|
+
itemBuilder: (context, item, _) => RoutineConceptCard(
|
|
508
|
+
title: item.title,
|
|
509
|
+
),
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
";
|
|
513
|
+
let parsed = parse_tree(Path::new("lib/routine_concept_flow.dart"), source)?;
|
|
514
|
+
let names = object_constructor_names(parsed.tree().root_node(), parsed.source());
|
|
515
|
+
|
|
516
|
+
assert!(names.iter().any(|name| name == "BentoList"), "{names:?}");
|
|
517
|
+
assert!(
|
|
518
|
+
names.iter().any(|name| name == "RoutineConceptCard"),
|
|
519
|
+
"{names:?}"
|
|
520
|
+
);
|
|
521
|
+
Ok(())
|
|
522
|
+
}
|
|
373
523
|
}
|
package/src/widgets.rs
CHANGED
|
@@ -11,14 +11,20 @@ use crate::generated::is_generated_dart_path;
|
|
|
11
11
|
use crate::graph::normalize_against;
|
|
12
12
|
use crate::{DeadCodeReport, Location, ScannedProject};
|
|
13
13
|
|
|
14
|
+
mod forwarding;
|
|
14
15
|
mod lifecycle;
|
|
15
16
|
mod params;
|
|
17
|
+
mod patterns;
|
|
18
|
+
mod reads;
|
|
16
19
|
mod top_level;
|
|
17
20
|
mod unrendered;
|
|
18
21
|
|
|
22
|
+
use forwarding::{forwarded_param_used, widget_forwarded_param_uses};
|
|
19
23
|
pub use lifecycle::MissingContextMountedAfterAwait;
|
|
20
24
|
use lifecycle::lifecycle_findings;
|
|
21
25
|
use params::constructor_params;
|
|
26
|
+
use patterns::object_pattern_field_reads_for_widget;
|
|
27
|
+
use reads::{state_body_uses_param, widget_body_uses_param};
|
|
22
28
|
use top_level::top_level_widget_functions;
|
|
23
29
|
use unrendered::unrendered_widgets;
|
|
24
30
|
|
|
@@ -317,6 +323,7 @@ fn findings_in_source(path: &Path, root: Node<'_>, source: &str) -> FileWidgetFi
|
|
|
317
323
|
let mut classes = Vec::new();
|
|
318
324
|
collect_class_declarations(root, &mut classes);
|
|
319
325
|
let states = state_classes_by_widget(&classes, source);
|
|
326
|
+
let forwarded_uses = widget_forwarded_param_uses(&classes, source);
|
|
320
327
|
let mut findings = FileWidgetFindings::default();
|
|
321
328
|
let has_widget_class = classes
|
|
322
329
|
.iter()
|
|
@@ -346,13 +353,28 @@ fn findings_in_source(path: &Path, root: Node<'_>, source: &str) -> FileWidgetFi
|
|
|
346
353
|
let Some(body) = class.child_by_field_name("body") else {
|
|
347
354
|
continue;
|
|
348
355
|
};
|
|
356
|
+
let object_pattern_reads = object_pattern_field_reads_for_widget(
|
|
357
|
+
root,
|
|
358
|
+
&widget_class,
|
|
359
|
+
body,
|
|
360
|
+
states.get(&widget_class),
|
|
361
|
+
source,
|
|
362
|
+
);
|
|
349
363
|
for param in constructor_params(class, &widget_class, source) {
|
|
350
364
|
if widget_body_uses_param(body, ¶m.field_name, source)
|
|
365
|
+
|| object_pattern_reads.contains(¶m.field_name)
|
|
351
366
|
|| states.get(&widget_class).is_some_and(|state_bodies| {
|
|
352
367
|
state_bodies.iter().any(|state_body| {
|
|
353
368
|
state_body_uses_param(*state_body, ¶m.field_name, source)
|
|
354
369
|
})
|
|
355
370
|
})
|
|
371
|
+
|| forwarded_param_used(
|
|
372
|
+
forwarded_uses.get(&widget_class),
|
|
373
|
+
body,
|
|
374
|
+
states.get(&widget_class),
|
|
375
|
+
¶m.field_name,
|
|
376
|
+
source,
|
|
377
|
+
)
|
|
356
378
|
{
|
|
357
379
|
continue;
|
|
358
380
|
}
|
|
@@ -461,86 +483,9 @@ fn strip_whitespace(text: &str) -> String {
|
|
|
461
483
|
}
|
|
462
484
|
|
|
463
485
|
pub(super) fn simple_type_name(text: &str) -> String {
|
|
464
|
-
text.trim_end_matches('?')
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
.unwrap_or(text)
|
|
468
|
-
.to_owned()
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
fn widget_body_uses_param(body: Node<'_>, name: &str, source: &str) -> bool {
|
|
472
|
-
let mut found = false;
|
|
473
|
-
visit_named(body, &mut |node| {
|
|
474
|
-
if !found && is_body_identifier_use(node, name, source) {
|
|
475
|
-
found = true;
|
|
476
|
-
}
|
|
477
|
-
});
|
|
478
|
-
found
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
fn is_body_identifier_use(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
482
|
-
if !matches!(node.kind(), "identifier" | "identifier_dollar_escaped") {
|
|
483
|
-
return false;
|
|
484
|
-
}
|
|
485
|
-
if node.utf8_text(source.as_bytes()).ok() != Some(name) {
|
|
486
|
-
return false;
|
|
487
|
-
}
|
|
488
|
-
if has_ancestor_kind(node, BODY_USAGE_SKIP_ANCESTORS) {
|
|
489
|
-
return false;
|
|
490
|
-
}
|
|
491
|
-
let Some(parent) = node.parent() else {
|
|
492
|
-
return true;
|
|
493
|
-
};
|
|
494
|
-
if parent.kind() == "label" || name_field_of(parent, node) {
|
|
495
|
-
return false;
|
|
496
|
-
}
|
|
497
|
-
!(parent.kind() == "identifier_list" && has_ancestor_kind(parent, &["declaration"]))
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
const BODY_USAGE_SKIP_ANCESTORS: &[&str] = &[
|
|
501
|
-
"constructor_signature",
|
|
502
|
-
"constant_constructor_signature",
|
|
503
|
-
"factory_constructor_signature",
|
|
504
|
-
"redirecting_factory_constructor_signature",
|
|
505
|
-
"constructor_param",
|
|
506
|
-
"super_formal_parameter",
|
|
507
|
-
"initializers",
|
|
508
|
-
"initializer_list_entry",
|
|
509
|
-
"field_initializer",
|
|
510
|
-
"type",
|
|
511
|
-
"typed_identifier",
|
|
512
|
-
];
|
|
513
|
-
|
|
514
|
-
fn state_body_uses_param(body: Node<'_>, name: &str, source: &str) -> bool {
|
|
515
|
-
let mut found = false;
|
|
516
|
-
visit_named(body, &mut |node| {
|
|
517
|
-
if !found && is_widget_member_access(node, name, source) {
|
|
518
|
-
found = true;
|
|
519
|
-
}
|
|
520
|
-
});
|
|
521
|
-
found
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
fn is_widget_member_access(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
525
|
-
if !matches!(
|
|
526
|
-
node.kind(),
|
|
527
|
-
"member_expression" | "null_aware_member_expression" | "assignable_expression"
|
|
528
|
-
) {
|
|
529
|
-
return false;
|
|
530
|
-
}
|
|
531
|
-
let Some(property) = node.child_by_field_name("property") else {
|
|
532
|
-
return false;
|
|
533
|
-
};
|
|
534
|
-
if property.utf8_text(source.as_bytes()).ok() != Some(name) {
|
|
535
|
-
return false;
|
|
536
|
-
}
|
|
537
|
-
let Some(object) = node.child_by_field_name("object") else {
|
|
538
|
-
return false;
|
|
539
|
-
};
|
|
540
|
-
matches!(
|
|
541
|
-
object.utf8_text(source.as_bytes()).ok(),
|
|
542
|
-
Some("widget" | "oldWidget")
|
|
543
|
-
)
|
|
486
|
+
let text = text.trim_end_matches('?');
|
|
487
|
+
let text = text.split('<').next().unwrap_or(text).trim();
|
|
488
|
+
text.rsplit('.').next().unwrap_or(text).to_owned()
|
|
544
489
|
}
|
|
545
490
|
|
|
546
491
|
fn visit_named(node: Node<'_>, visitor: &mut impl FnMut(Node<'_>)) {
|
|
@@ -551,13 +496,6 @@ fn visit_named(node: Node<'_>, visitor: &mut impl FnMut(Node<'_>)) {
|
|
|
551
496
|
}
|
|
552
497
|
}
|
|
553
498
|
|
|
554
|
-
fn name_field_of(parent: Node<'_>, child: Node<'_>) -> bool {
|
|
555
|
-
let mut cursor = parent.walk();
|
|
556
|
-
parent
|
|
557
|
-
.children_by_field_name("name", &mut cursor)
|
|
558
|
-
.any(|field| same_node(field, child))
|
|
559
|
-
}
|
|
560
|
-
|
|
561
499
|
fn has_ancestor_kind(node: Node<'_>, kinds: &[&str]) -> bool {
|
|
562
500
|
let mut parent = node.parent();
|
|
563
501
|
while let Some(ancestor) = parent {
|
|
@@ -569,12 +507,6 @@ fn has_ancestor_kind(node: Node<'_>, kinds: &[&str]) -> bool {
|
|
|
569
507
|
false
|
|
570
508
|
}
|
|
571
509
|
|
|
572
|
-
fn same_node(left: Node<'_>, right: Node<'_>) -> bool {
|
|
573
|
-
left.kind() == right.kind()
|
|
574
|
-
&& left.start_byte() == right.start_byte()
|
|
575
|
-
&& left.end_byte() == right.end_byte()
|
|
576
|
-
}
|
|
577
|
-
|
|
578
510
|
fn is_test_path(path: &Path) -> bool {
|
|
579
511
|
path.file_name()
|
|
580
512
|
.and_then(|name| name.to_str())
|