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/Cargo.lock +1 -1
- 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
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
use tree_sitter::Node;
|
|
2
|
+
|
|
3
|
+
use super::{direct_named_child, strip_method_type_arguments};
|
|
4
|
+
|
|
5
|
+
pub(super) fn route_extension_receiver_node<'tree>(
|
|
6
|
+
site: Node<'tree>,
|
|
7
|
+
navigation_name: &str,
|
|
8
|
+
source: &str,
|
|
9
|
+
) -> Option<Node<'tree>> {
|
|
10
|
+
let function = site
|
|
11
|
+
.child_by_field_name("function")
|
|
12
|
+
.or_else(|| direct_named_child(site, "member_expression"))
|
|
13
|
+
.or_else(|| direct_named_child(site, "null_aware_member_expression"))
|
|
14
|
+
.or_else(|| direct_named_child(site, "instantiation_expression"))?;
|
|
15
|
+
navigation_function_receiver(function, navigation_name, source)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
fn navigation_function_receiver<'tree>(
|
|
19
|
+
function: Node<'tree>,
|
|
20
|
+
navigation_name: &str,
|
|
21
|
+
source: &str,
|
|
22
|
+
) -> Option<Node<'tree>> {
|
|
23
|
+
if function.kind() == "instantiation_expression" {
|
|
24
|
+
return function
|
|
25
|
+
.child_by_field_name("function")
|
|
26
|
+
.or_else(|| direct_named_child(function, "member_expression"))
|
|
27
|
+
.and_then(|inner| navigation_function_receiver(inner, navigation_name, source));
|
|
28
|
+
}
|
|
29
|
+
if !matches!(
|
|
30
|
+
function.kind(),
|
|
31
|
+
"member_expression" | "null_aware_member_expression" | "assignable_expression"
|
|
32
|
+
) {
|
|
33
|
+
return None;
|
|
34
|
+
}
|
|
35
|
+
let property = function.child_by_field_name("property")?;
|
|
36
|
+
let property_text = property.utf8_text(source.as_bytes()).ok()?;
|
|
37
|
+
if strip_method_type_arguments(property_text.trim()) != navigation_name {
|
|
38
|
+
return None;
|
|
39
|
+
}
|
|
40
|
+
function.child_by_field_name("object")
|
|
41
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
use std::collections::BTreeSet;
|
|
2
|
+
|
|
3
|
+
use tree_sitter::Node;
|
|
4
|
+
|
|
5
|
+
use super::{is_identifier_character, is_identifier_text, simple_type_name};
|
|
6
|
+
|
|
7
|
+
pub(super) fn class_extends_state(class_body: Node<'_>, source: &str) -> bool {
|
|
8
|
+
let Some(class) = class_body
|
|
9
|
+
.parent()
|
|
10
|
+
.filter(|parent| parent.kind() == "class_declaration")
|
|
11
|
+
else {
|
|
12
|
+
return false;
|
|
13
|
+
};
|
|
14
|
+
let root = root_node(class);
|
|
15
|
+
let mut visited = BTreeSet::new();
|
|
16
|
+
class_or_local_superclass_extends_state(root, class, source, &mut visited)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
fn class_or_local_superclass_extends_state(
|
|
20
|
+
root: Node<'_>,
|
|
21
|
+
class: Node<'_>,
|
|
22
|
+
source: &str,
|
|
23
|
+
visited: &mut BTreeSet<String>,
|
|
24
|
+
) -> bool {
|
|
25
|
+
let Some(superclass) = direct_superclass_type_name(class, source) else {
|
|
26
|
+
return false;
|
|
27
|
+
};
|
|
28
|
+
if framework_state_type_reference(root, &superclass, source) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
if superclass.contains('.') {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
let simple_superclass = simple_type_name(&superclass);
|
|
35
|
+
if matches!(simple_superclass.as_str(), "State" | "ConsumerState")
|
|
36
|
+
&& let Some(declaration) = find_class_declaration(root, &simple_superclass, source)
|
|
37
|
+
{
|
|
38
|
+
if !visited.insert(simple_superclass) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
return class_or_local_superclass_extends_state(root, declaration, source, visited);
|
|
42
|
+
}
|
|
43
|
+
if unprefixed_framework_state_import(root, &simple_superclass, source) {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
if !visited.insert(simple_superclass.clone()) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
find_class_declaration(root, &simple_superclass, source).is_some_and(|declaration| {
|
|
50
|
+
class_or_local_superclass_extends_state(root, declaration, source, visited)
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
fn direct_superclass_type_name(class: Node<'_>, source: &str) -> Option<String> {
|
|
55
|
+
let superclass = class.child_by_field_name("superclass")?;
|
|
56
|
+
let text = superclass.utf8_text(source.as_bytes()).ok()?;
|
|
57
|
+
inherited_type_name(text.trim().strip_prefix("extends").unwrap_or(text))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
fn inherited_type_name(text: &str) -> Option<String> {
|
|
61
|
+
let mut name = String::new();
|
|
62
|
+
for character in text.trim().chars() {
|
|
63
|
+
if character == '<' {
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
if is_identifier_character(character) || character == '.' {
|
|
67
|
+
name.push(character);
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if !name.is_empty() {
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
is_qualified_identifier_text(&name).then_some(name)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
fn find_class_declaration<'tree>(
|
|
78
|
+
node: Node<'tree>,
|
|
79
|
+
name: &str,
|
|
80
|
+
source: &str,
|
|
81
|
+
) -> Option<Node<'tree>> {
|
|
82
|
+
if node.kind() == "class_declaration"
|
|
83
|
+
&& field_text(node, "name", source).as_deref() == Some(name)
|
|
84
|
+
{
|
|
85
|
+
return Some(node);
|
|
86
|
+
}
|
|
87
|
+
let mut cursor = node.walk();
|
|
88
|
+
node.named_children(&mut cursor)
|
|
89
|
+
.find_map(|child| find_class_declaration(child, name, source))
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
fn is_qualified_identifier_text(text: &str) -> bool {
|
|
93
|
+
!text.is_empty() && text.split('.').all(is_identifier_text)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
fn framework_state_type_reference(root: Node<'_>, type_name: &str, source: &str) -> bool {
|
|
97
|
+
let simple = simple_type_name(type_name);
|
|
98
|
+
if !matches!(simple.as_str(), "State" | "ConsumerState") {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
let Some((prefix, _)) = type_name.split_once('.') else {
|
|
102
|
+
return false;
|
|
103
|
+
};
|
|
104
|
+
import_prefix_resolves_to_framework(root, prefix, &simple, source)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
fn unprefixed_framework_state_import(root: Node<'_>, type_name: &str, source: &str) -> bool {
|
|
108
|
+
if !matches!(type_name, "State" | "ConsumerState") {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
let mut found = false;
|
|
112
|
+
visit_named(root, &mut |node| {
|
|
113
|
+
if found || node.kind() != "library_import" {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if import_alias(node, source).is_some() {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if import_uri(node, source).is_some_and(|uri| state_framework_import_uri(&uri, type_name)) {
|
|
120
|
+
found = true;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
found
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
fn import_prefix_resolves_to_framework(
|
|
127
|
+
root: Node<'_>,
|
|
128
|
+
prefix: &str,
|
|
129
|
+
type_name: &str,
|
|
130
|
+
source: &str,
|
|
131
|
+
) -> bool {
|
|
132
|
+
let mut found = false;
|
|
133
|
+
visit_named(root, &mut |node| {
|
|
134
|
+
if found || node.kind() != "library_import" {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if import_alias(node, source).as_deref() != Some(prefix) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if import_uri(node, source).is_some_and(|uri| state_framework_import_uri(&uri, type_name)) {
|
|
141
|
+
found = true;
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
found
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
fn import_alias(node: Node<'_>, source: &str) -> Option<String> {
|
|
148
|
+
find_first_named_descendant(node, "import_specification")
|
|
149
|
+
.and_then(|specification| field_text(specification, "alias", source))
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
fn import_uri(node: Node<'_>, source: &str) -> Option<String> {
|
|
153
|
+
find_first_named_descendant(node, "uri")
|
|
154
|
+
.and_then(|uri| uri.utf8_text(source.as_bytes()).ok())
|
|
155
|
+
.and_then(unquote_dart_string)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
fn unquote_dart_string(text: &str) -> Option<String> {
|
|
159
|
+
let trimmed = text.trim().trim_start_matches('r');
|
|
160
|
+
let quote = trimmed.chars().next()?;
|
|
161
|
+
if quote != '\'' && quote != '"' {
|
|
162
|
+
return None;
|
|
163
|
+
}
|
|
164
|
+
trimmed
|
|
165
|
+
.strip_prefix(quote)
|
|
166
|
+
.and_then(|rest| rest.strip_suffix(quote))
|
|
167
|
+
.map(str::to_owned)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
fn state_framework_import_uri(uri: &str, type_name: &str) -> bool {
|
|
171
|
+
match type_name {
|
|
172
|
+
"State" => uri.starts_with("package:flutter/"),
|
|
173
|
+
"ConsumerState" => {
|
|
174
|
+
uri.starts_with("package:flutter_riverpod/")
|
|
175
|
+
|| uri.starts_with("package:hooks_riverpod/")
|
|
176
|
+
}
|
|
177
|
+
_ => false,
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
fn find_first_named_descendant<'tree>(node: Node<'tree>, kind: &str) -> Option<Node<'tree>> {
|
|
182
|
+
if node.kind() == kind {
|
|
183
|
+
return Some(node);
|
|
184
|
+
}
|
|
185
|
+
let mut cursor = node.walk();
|
|
186
|
+
node.named_children(&mut cursor)
|
|
187
|
+
.find_map(|child| find_first_named_descendant(child, kind))
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
fn root_node(mut node: Node<'_>) -> Node<'_> {
|
|
191
|
+
while let Some(parent) = node.parent() {
|
|
192
|
+
node = parent;
|
|
193
|
+
}
|
|
194
|
+
node
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
fn visit_named(node: Node<'_>, visitor: &mut impl FnMut(Node<'_>)) {
|
|
198
|
+
visitor(node);
|
|
199
|
+
let mut cursor = node.walk();
|
|
200
|
+
for child in node.named_children(&mut cursor) {
|
|
201
|
+
visit_named(child, visitor);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
fn field_text(node: Node<'_>, field: &str, source: &str) -> Option<String> {
|
|
206
|
+
node.child_by_field_name(field)
|
|
207
|
+
.and_then(|child| child.utf8_text(source.as_bytes()).ok())
|
|
208
|
+
.map(str::to_owned)
|
|
209
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
use std::collections::BTreeSet;
|
|
2
|
+
|
|
3
|
+
use super::helper_has_typed_route_navigation_call;
|
|
4
|
+
|
|
5
|
+
fn typed_route_navigation_found(source: &str) -> Result<bool, Box<dyn std::error::Error>> {
|
|
6
|
+
let parsed = crate::dart_parser::parse_dart_source_lossy("helper.dart".as_ref(), source)?;
|
|
7
|
+
let route_classes = BTreeSet::from(["HomeRoute".to_owned()]);
|
|
8
|
+
Ok(helper_has_typed_route_navigation_call(
|
|
9
|
+
parsed.tree().root_node(),
|
|
10
|
+
parsed.source(),
|
|
11
|
+
&route_classes,
|
|
12
|
+
))
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
#[test]
|
|
16
|
+
fn state_context_resolves_through_local_state_base_class() -> Result<(), Box<dyn std::error::Error>>
|
|
17
|
+
{
|
|
18
|
+
let source = r"
|
|
19
|
+
import 'package:flutter/widgets.dart';
|
|
20
|
+
|
|
21
|
+
class BaseState<T> extends State<T> {}
|
|
22
|
+
|
|
23
|
+
class ScreenState extends BaseState<Screen> {
|
|
24
|
+
void open() {
|
|
25
|
+
context.go(const HomeRoute().location);
|
|
26
|
+
const HomeRoute().go(this.context);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
";
|
|
30
|
+
|
|
31
|
+
assert!(typed_route_navigation_found(source)?);
|
|
32
|
+
Ok(())
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
#[test]
|
|
36
|
+
fn state_context_resolves_through_local_consumer_state_base_class()
|
|
37
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
38
|
+
let source = r"
|
|
39
|
+
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
40
|
+
|
|
41
|
+
class BaseState<T> extends ConsumerState<T> {}
|
|
42
|
+
|
|
43
|
+
class ScreenState extends BaseState<Screen> {
|
|
44
|
+
void open() {
|
|
45
|
+
context.go(const HomeRoute().location);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
";
|
|
49
|
+
|
|
50
|
+
assert!(typed_route_navigation_found(source)?);
|
|
51
|
+
Ok(())
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
#[test]
|
|
55
|
+
fn unrelated_local_base_class_does_not_provide_state_context()
|
|
56
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
57
|
+
let source = r"
|
|
58
|
+
class BaseState<T> extends Object {}
|
|
59
|
+
|
|
60
|
+
class ScreenState extends BaseState<Screen> {
|
|
61
|
+
void open() {
|
|
62
|
+
context.go(const HomeRoute().location);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
";
|
|
66
|
+
|
|
67
|
+
assert!(!typed_route_navigation_found(source)?);
|
|
68
|
+
Ok(())
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
#[test]
|
|
72
|
+
fn state_context_resolves_aliased_framework_state() -> Result<(), Box<dyn std::error::Error>> {
|
|
73
|
+
let source = r"
|
|
74
|
+
import 'package:flutter/widgets.dart' as f;
|
|
75
|
+
|
|
76
|
+
class ScreenState extends f.State<Screen> {
|
|
77
|
+
void open() {
|
|
78
|
+
context.go(const HomeRoute().location);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
";
|
|
82
|
+
|
|
83
|
+
assert!(typed_route_navigation_found(source)?);
|
|
84
|
+
Ok(())
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
#[test]
|
|
88
|
+
fn local_state_shadow_does_not_provide_state_context() -> Result<(), Box<dyn std::error::Error>> {
|
|
89
|
+
let source = r"
|
|
90
|
+
class State<T> {}
|
|
91
|
+
|
|
92
|
+
class ScreenState extends State<Screen> {
|
|
93
|
+
void open() {
|
|
94
|
+
context.go(const HomeRoute().location);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
";
|
|
98
|
+
|
|
99
|
+
assert!(!typed_route_navigation_found(source)?);
|
|
100
|
+
Ok(())
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
#[test]
|
|
104
|
+
fn unresolved_prefixed_state_does_not_provide_state_context()
|
|
105
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
106
|
+
let source = r"
|
|
107
|
+
import 'package:not_flutter/fake.dart' as fake;
|
|
108
|
+
|
|
109
|
+
class ScreenState extends fake.State<Screen> {
|
|
110
|
+
void open() {
|
|
111
|
+
context.go(const HomeRoute().location);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
";
|
|
115
|
+
|
|
116
|
+
assert!(!typed_route_navigation_found(source)?);
|
|
117
|
+
Ok(())
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
#[test]
|
|
121
|
+
fn prefixed_state_does_not_fall_back_to_local_state_shadow()
|
|
122
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
123
|
+
let source = r"
|
|
124
|
+
import 'package:flutter/widgets.dart' as f;
|
|
125
|
+
import 'package:not_flutter/fake.dart' as fake;
|
|
126
|
+
|
|
127
|
+
class State<T> extends f.State<T> {}
|
|
128
|
+
|
|
129
|
+
class ScreenState extends fake.State<Screen> {
|
|
130
|
+
void open() {
|
|
131
|
+
context.go(const HomeRoute().location);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
";
|
|
135
|
+
|
|
136
|
+
assert!(!typed_route_navigation_found(source)?);
|
|
137
|
+
Ok(())
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
#[test]
|
|
141
|
+
fn route_location_named_argument_is_not_navigation_target() -> Result<(), Box<dyn std::error::Error>>
|
|
142
|
+
{
|
|
143
|
+
let source = r"
|
|
144
|
+
class Screen {
|
|
145
|
+
void open(BuildContext context) {
|
|
146
|
+
context.go(location: const HomeRoute().location);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
";
|
|
150
|
+
|
|
151
|
+
assert!(!typed_route_navigation_found(source)?);
|
|
152
|
+
Ok(())
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
#[test]
|
|
156
|
+
fn local_route_name_shadow_does_not_match_unqualified_constructor()
|
|
157
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
158
|
+
let source = r"
|
|
159
|
+
import 'app_routes.dart' as routes;
|
|
160
|
+
|
|
161
|
+
class Screen {
|
|
162
|
+
void open(BuildContext context) {
|
|
163
|
+
const HomeRoute().go(context);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
class HomeRoute {
|
|
168
|
+
const HomeRoute();
|
|
169
|
+
void go(BuildContext context) {}
|
|
170
|
+
}
|
|
171
|
+
";
|
|
172
|
+
|
|
173
|
+
assert!(!typed_route_navigation_found(source)?);
|
|
174
|
+
Ok(())
|
|
175
|
+
}
|