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,616 @@
|
|
|
1
|
+
use tree_sitter::Node;
|
|
2
|
+
|
|
3
|
+
use super::patterns::{declaration_binds_name, pattern_binds_name};
|
|
4
|
+
|
|
5
|
+
pub(super) fn widget_body_uses_param(body: Node<'_>, name: &str, source: &str) -> bool {
|
|
6
|
+
let mut found = false;
|
|
7
|
+
visit_named(body, &mut |node| {
|
|
8
|
+
if !found && is_this_member_access(node, name, source) {
|
|
9
|
+
found = true;
|
|
10
|
+
}
|
|
11
|
+
if !found && is_body_identifier_use(node, name, source) {
|
|
12
|
+
found = true;
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
found
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
fn is_body_identifier_use(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
19
|
+
if !matches!(node.kind(), "identifier" | "identifier_dollar_escaped") {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
if node.utf8_text(source.as_bytes()).ok() != Some(name) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
if has_ancestor_kind(node, BODY_USAGE_SKIP_ANCESTORS) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
let Some(parent) = node.parent() else {
|
|
29
|
+
return true;
|
|
30
|
+
};
|
|
31
|
+
if parent.kind() == "label" || name_field_of(parent, node) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
if property_field_of(parent, node) || has_ancestor_kind(node, PATTERN_ANCESTORS) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
if parent.kind() == "identifier_list" && has_ancestor_kind(parent, &["declaration"]) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
!direct_identifier_shadowed(node, name, source)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const BODY_USAGE_SKIP_ANCESTORS: &[&str] = &[
|
|
44
|
+
"constructor_signature",
|
|
45
|
+
"constant_constructor_signature",
|
|
46
|
+
"factory_constructor_signature",
|
|
47
|
+
"redirecting_factory_constructor_signature",
|
|
48
|
+
"constructor_param",
|
|
49
|
+
"super_formal_parameter",
|
|
50
|
+
"initializers",
|
|
51
|
+
"initializer_list_entry",
|
|
52
|
+
"field_initializer",
|
|
53
|
+
"type",
|
|
54
|
+
"typed_identifier",
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
const PATTERN_ANCESTORS: &[&str] = &[
|
|
58
|
+
"cast_pattern",
|
|
59
|
+
"constant_pattern",
|
|
60
|
+
"list_pattern",
|
|
61
|
+
"map_pattern",
|
|
62
|
+
"null_assert_pattern",
|
|
63
|
+
"null_check_pattern",
|
|
64
|
+
"object_pattern",
|
|
65
|
+
"pattern_variable_declaration",
|
|
66
|
+
"record_pattern",
|
|
67
|
+
"rest_pattern",
|
|
68
|
+
"variable_pattern",
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
pub(super) fn state_body_uses_param(body: Node<'_>, name: &str, source: &str) -> bool {
|
|
72
|
+
let mut found = false;
|
|
73
|
+
visit_named(body, &mut |node| {
|
|
74
|
+
if !found && is_widget_member_access(body, node, name, source) {
|
|
75
|
+
found = true;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
found
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
fn is_widget_member_access(body: Node<'_>, node: Node<'_>, name: &str, source: &str) -> bool {
|
|
82
|
+
if !matches!(
|
|
83
|
+
node.kind(),
|
|
84
|
+
"member_expression" | "null_aware_member_expression" | "assignable_expression"
|
|
85
|
+
) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
let Some(property) = node.child_by_field_name("property") else {
|
|
89
|
+
return false;
|
|
90
|
+
};
|
|
91
|
+
if property.utf8_text(source.as_bytes()).ok() != Some(name) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
let Some(object) = node.child_by_field_name("object") else {
|
|
95
|
+
return false;
|
|
96
|
+
};
|
|
97
|
+
let Some(object_name @ ("widget" | "oldWidget")) = object.utf8_text(source.as_bytes()).ok()
|
|
98
|
+
else {
|
|
99
|
+
return false;
|
|
100
|
+
};
|
|
101
|
+
state_root_receiver_available(body, object, object_name, source)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
fn state_root_receiver_available(
|
|
105
|
+
boundary: Node<'_>,
|
|
106
|
+
node: Node<'_>,
|
|
107
|
+
name: &str,
|
|
108
|
+
source: &str,
|
|
109
|
+
) -> bool {
|
|
110
|
+
let mut child = node;
|
|
111
|
+
let mut parent = node.parent();
|
|
112
|
+
while let Some(ancestor) = parent {
|
|
113
|
+
if same_node(ancestor, boundary) {
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
if matches!(ancestor.kind(), "class_body" | "class_declaration") {
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
if enclosing_parameters_bind_name(ancestor, name, source)
|
|
120
|
+
&& !state_owner_parameter_preserves_root(ancestor, name, source)
|
|
121
|
+
{
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
if earlier_local_binding_exists(ancestor, child, node.start_byte(), name, source) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
child = ancestor;
|
|
128
|
+
parent = ancestor.parent();
|
|
129
|
+
}
|
|
130
|
+
true
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
fn state_owner_parameter_preserves_root(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
134
|
+
name == "oldWidget"
|
|
135
|
+
&& node.kind() == "method_declaration"
|
|
136
|
+
&& method_declaration_name(node, source).as_deref() == Some("didUpdateWidget")
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
fn method_declaration_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
140
|
+
node.child_by_field_name("signature")
|
|
141
|
+
.and_then(|signature| {
|
|
142
|
+
field_text(signature, "name", source)
|
|
143
|
+
.or_else(|| identifier_before_parameters(signature, source))
|
|
144
|
+
})
|
|
145
|
+
.or_else(|| field_text(node, "name", source))
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
fn identifier_before_parameters(node: Node<'_>, source: &str) -> Option<String> {
|
|
149
|
+
let text = node.utf8_text(source.as_bytes()).ok()?;
|
|
150
|
+
let before_parameters = text.split_once('(')?.0;
|
|
151
|
+
before_parameters
|
|
152
|
+
.split(|character: char| {
|
|
153
|
+
!(character == '_' || character == '$' || character.is_ascii_alphanumeric())
|
|
154
|
+
})
|
|
155
|
+
.rfind(|part| !part.is_empty())
|
|
156
|
+
.map(str::to_owned)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
fn is_this_member_access(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
160
|
+
if !matches!(
|
|
161
|
+
node.kind(),
|
|
162
|
+
"member_expression" | "null_aware_member_expression" | "assignable_expression"
|
|
163
|
+
) {
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
let Some(property) = node.child_by_field_name("property") else {
|
|
167
|
+
return false;
|
|
168
|
+
};
|
|
169
|
+
if property.utf8_text(source.as_bytes()).ok() != Some(name) {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
let Some(object) = node.child_by_field_name("object") else {
|
|
173
|
+
return false;
|
|
174
|
+
};
|
|
175
|
+
object.utf8_text(source.as_bytes()).ok() == Some("this")
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
pub(super) fn direct_identifier_shadowed_before(
|
|
179
|
+
boundary: Node<'_>,
|
|
180
|
+
node: Node<'_>,
|
|
181
|
+
name: &str,
|
|
182
|
+
source: &str,
|
|
183
|
+
) -> bool {
|
|
184
|
+
identifier_shadowed_until(Some(boundary), node, name, source)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
fn direct_identifier_shadowed(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
188
|
+
identifier_shadowed_until(None, node, name, source)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
fn identifier_shadowed_until(
|
|
192
|
+
boundary: Option<Node<'_>>,
|
|
193
|
+
node: Node<'_>,
|
|
194
|
+
name: &str,
|
|
195
|
+
source: &str,
|
|
196
|
+
) -> bool {
|
|
197
|
+
let mut child = node;
|
|
198
|
+
let mut parent = node.parent();
|
|
199
|
+
while let Some(ancestor) = parent {
|
|
200
|
+
if boundary.is_some_and(|boundary| same_node(ancestor, boundary)) {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
if matches!(ancestor.kind(), "class_body" | "class_declaration") {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
if enclosing_parameters_bind_name(ancestor, name, source)
|
|
207
|
+
|| earlier_local_binding_exists(ancestor, child, node.start_byte(), name, source)
|
|
208
|
+
{
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
child = ancestor;
|
|
212
|
+
parent = ancestor.parent();
|
|
213
|
+
}
|
|
214
|
+
false
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
fn enclosing_parameters_bind_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
218
|
+
if call_expression_function_parameters_bind_name(node, name, source) {
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
if !matches!(
|
|
222
|
+
node.kind(),
|
|
223
|
+
"declaration" | "function_expression" | "local_function_declaration" | "method_declaration"
|
|
224
|
+
) {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
let parameter_lists = enclosing_parameter_lists(node);
|
|
228
|
+
if parameter_lists.is_empty() {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
for parameters in parameter_lists {
|
|
232
|
+
if parameter_list_directly_binds_name(parameters, name, source) {
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
false
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
fn call_expression_function_parameters_bind_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
240
|
+
if node.kind() != "call_expression" {
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
let Some(function) = node.child_by_field_name("function") else {
|
|
244
|
+
return false;
|
|
245
|
+
};
|
|
246
|
+
if function.kind() != "function_expression" {
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
enclosing_parameters_bind_name(function, name, source)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
fn parameter_list_directly_binds_name(parameters: Node<'_>, name: &str, source: &str) -> bool {
|
|
253
|
+
let mut cursor = parameters.walk();
|
|
254
|
+
parameters.named_children(&mut cursor).any(|candidate| {
|
|
255
|
+
if matches!(candidate.kind(), "identifier" | "identifier_dollar_escaped") {
|
|
256
|
+
return candidate.utf8_text(source.as_bytes()).ok() == Some(name);
|
|
257
|
+
}
|
|
258
|
+
if candidate.kind() == "formal_parameter" {
|
|
259
|
+
return formal_parameter_name(candidate, source).as_deref() == Some(name);
|
|
260
|
+
}
|
|
261
|
+
candidate.kind() == "optional_formal_parameters"
|
|
262
|
+
&& optional_parameters_directly_bind_name(candidate, name, source)
|
|
263
|
+
})
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
fn optional_parameters_directly_bind_name(parameters: Node<'_>, name: &str, source: &str) -> bool {
|
|
267
|
+
let mut cursor = parameters.walk();
|
|
268
|
+
parameters
|
|
269
|
+
.named_children(&mut cursor)
|
|
270
|
+
.filter(|candidate| candidate.kind() == "formal_parameter")
|
|
271
|
+
.any(|candidate| formal_parameter_name(candidate, source).as_deref() == Some(name))
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
fn enclosing_parameter_lists(node: Node<'_>) -> Vec<Node<'_>> {
|
|
275
|
+
match node.kind() {
|
|
276
|
+
"function_expression" => parameter_field_lists(node),
|
|
277
|
+
"method_declaration" => node
|
|
278
|
+
.child_by_field_name("signature")
|
|
279
|
+
.map_or_else(Vec::new, collect_parameter_lists),
|
|
280
|
+
"local_function_declaration" | "declaration" => {
|
|
281
|
+
direct_named_child(node, "function_signature")
|
|
282
|
+
.map_or_else(Vec::new, collect_parameter_lists)
|
|
283
|
+
}
|
|
284
|
+
_ => Vec::new(),
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
fn parameter_field_lists(node: Node<'_>) -> Vec<Node<'_>> {
|
|
289
|
+
let mut cursor = node.walk();
|
|
290
|
+
node.children_by_field_name("parameters", &mut cursor)
|
|
291
|
+
.filter_map(formal_parameter_list)
|
|
292
|
+
.collect()
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
fn collect_parameter_lists(node: Node<'_>) -> Vec<Node<'_>> {
|
|
296
|
+
own_parameter_list(node).into_iter().collect()
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
fn own_parameter_list(node: Node<'_>) -> Option<Node<'_>> {
|
|
300
|
+
if node.kind() == "formal_parameter_list" {
|
|
301
|
+
return Some(node);
|
|
302
|
+
}
|
|
303
|
+
let mut cursor = node.walk();
|
|
304
|
+
node.named_children(&mut cursor)
|
|
305
|
+
.find_map(own_parameter_list)
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
fn formal_parameter_list(node: Node<'_>) -> Option<Node<'_>> {
|
|
309
|
+
if node.kind() == "formal_parameter_list" {
|
|
310
|
+
return Some(node);
|
|
311
|
+
}
|
|
312
|
+
let mut cursor = node.walk();
|
|
313
|
+
node.named_children(&mut cursor)
|
|
314
|
+
.find(|child| child.kind() == "formal_parameter_list")
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
fn earlier_local_binding_exists(
|
|
318
|
+
scope: Node<'_>,
|
|
319
|
+
path_child: Node<'_>,
|
|
320
|
+
usage_start: usize,
|
|
321
|
+
name: &str,
|
|
322
|
+
source: &str,
|
|
323
|
+
) -> bool {
|
|
324
|
+
if scoped_header_binding_exists(scope, path_child, usage_start, name, source) {
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
let mut cursor = scope.walk();
|
|
329
|
+
for sibling in scope.named_children(&mut cursor) {
|
|
330
|
+
if same_node(sibling, path_child) || sibling.start_byte() >= path_child.start_byte() {
|
|
331
|
+
break;
|
|
332
|
+
}
|
|
333
|
+
if lexical_sibling_binds_name(sibling, name, source) {
|
|
334
|
+
return true;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
false
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
pub(super) fn scoped_header_binding_exists(
|
|
341
|
+
scope: Node<'_>,
|
|
342
|
+
path_child: Node<'_>,
|
|
343
|
+
usage_start: usize,
|
|
344
|
+
name: &str,
|
|
345
|
+
source: &str,
|
|
346
|
+
) -> bool {
|
|
347
|
+
if catch_header_binding_exists(scope, path_child, usage_start, name, source) {
|
|
348
|
+
return true;
|
|
349
|
+
}
|
|
350
|
+
if !matches!(
|
|
351
|
+
scope.kind(),
|
|
352
|
+
"for_element"
|
|
353
|
+
| "for_statement"
|
|
354
|
+
| "if_element"
|
|
355
|
+
| "if_statement"
|
|
356
|
+
| "switch_expression_case"
|
|
357
|
+
| "switch_statement_case"
|
|
358
|
+
) {
|
|
359
|
+
return false;
|
|
360
|
+
}
|
|
361
|
+
if header_field_binding_exists(scope, path_child, usage_start, name, source) {
|
|
362
|
+
return true;
|
|
363
|
+
}
|
|
364
|
+
let mut cursor = scope.walk();
|
|
365
|
+
for child in scope.named_children(&mut cursor) {
|
|
366
|
+
if same_node(child, path_child) || child.start_byte() >= path_child.start_byte() {
|
|
367
|
+
break;
|
|
368
|
+
}
|
|
369
|
+
if header_binding_child(scope.kind(), child)
|
|
370
|
+
&& child.end_byte() <= usage_start
|
|
371
|
+
&& node_contains_binding_name(child, name, source)
|
|
372
|
+
{
|
|
373
|
+
return true;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
false
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
fn catch_header_binding_exists(
|
|
380
|
+
scope: Node<'_>,
|
|
381
|
+
path_child: Node<'_>,
|
|
382
|
+
usage_start: usize,
|
|
383
|
+
name: &str,
|
|
384
|
+
source: &str,
|
|
385
|
+
) -> bool {
|
|
386
|
+
if scope.kind() == "catch_clause" {
|
|
387
|
+
return scope.start_byte() < path_child.start_byte()
|
|
388
|
+
&& catch_clause_binds_name(scope, name, source);
|
|
389
|
+
}
|
|
390
|
+
if scope.kind() == "try_statement" {
|
|
391
|
+
let mut previous: Option<Node<'_>> = None;
|
|
392
|
+
let mut cursor = scope.walk();
|
|
393
|
+
for child in scope.named_children(&mut cursor) {
|
|
394
|
+
if same_node(child, path_child) || child.start_byte() >= path_child.start_byte() {
|
|
395
|
+
return previous.is_some_and(|header| {
|
|
396
|
+
header.kind() == "catch_clause"
|
|
397
|
+
&& catch_body_child(path_child)
|
|
398
|
+
&& header.end_byte() <= usage_start
|
|
399
|
+
&& !source_between_contains_finally(header, path_child, source)
|
|
400
|
+
&& catch_clause_binds_name(header, name, source)
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
previous = Some(child);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
false
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
fn catch_body_child(node: Node<'_>) -> bool {
|
|
410
|
+
matches!(node.kind(), "block" | "function_body")
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
fn source_between_contains_finally(left: Node<'_>, right: Node<'_>, source: &str) -> bool {
|
|
414
|
+
source
|
|
415
|
+
.get(left.end_byte()..right.start_byte())
|
|
416
|
+
.is_some_and(|text| text.contains("finally"))
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
fn catch_clause_binds_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
420
|
+
field_text(node, "exception", source).as_deref() == Some(name)
|
|
421
|
+
|| field_text(node, "stack_trace", source).as_deref() == Some(name)
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
fn header_field_binding_exists(
|
|
425
|
+
scope: Node<'_>,
|
|
426
|
+
path_child: Node<'_>,
|
|
427
|
+
usage_start: usize,
|
|
428
|
+
name: &str,
|
|
429
|
+
source: &str,
|
|
430
|
+
) -> bool {
|
|
431
|
+
if !matches!(scope.kind(), "for_element" | "for_statement") {
|
|
432
|
+
return false;
|
|
433
|
+
}
|
|
434
|
+
let mut cursor = scope.walk();
|
|
435
|
+
scope
|
|
436
|
+
.children_by_field_name("name", &mut cursor)
|
|
437
|
+
.any(|field| {
|
|
438
|
+
field.end_byte() <= usage_start
|
|
439
|
+
&& field.start_byte() < path_child.start_byte()
|
|
440
|
+
&& field.utf8_text(source.as_bytes()).ok() == Some(name)
|
|
441
|
+
})
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
fn lexical_sibling_binds_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
445
|
+
if node.kind() == "local_function_declaration" {
|
|
446
|
+
return local_function_name(node, source).as_deref() == Some(name);
|
|
447
|
+
}
|
|
448
|
+
matches!(
|
|
449
|
+
node.kind(),
|
|
450
|
+
"local_variable_declaration" | "pattern_variable_declaration"
|
|
451
|
+
) && declaration_binds_name(node, name, source)
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
fn local_function_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
455
|
+
direct_named_child(node, "function_signature")
|
|
456
|
+
.and_then(|signature| signature.child_by_field_name("name"))
|
|
457
|
+
.or_else(|| node.child_by_field_name("name"))
|
|
458
|
+
.and_then(|name| name.utf8_text(source.as_bytes()).ok())
|
|
459
|
+
.map(str::to_owned)
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
fn pattern_or_local_binding_owner(node: Node<'_>) -> bool {
|
|
463
|
+
matches!(
|
|
464
|
+
node.kind(),
|
|
465
|
+
"list_pattern"
|
|
466
|
+
| "local_variable_declaration"
|
|
467
|
+
| "map_pattern"
|
|
468
|
+
| "object_pattern"
|
|
469
|
+
| "pattern_variable_declaration"
|
|
470
|
+
| "record_pattern"
|
|
471
|
+
| "variable_pattern"
|
|
472
|
+
)
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
fn header_binding_child(scope_kind: &str, child: Node<'_>) -> bool {
|
|
476
|
+
if matches!(scope_kind, "for_element" | "for_statement") {
|
|
477
|
+
return !is_body_statement_child(child.kind())
|
|
478
|
+
|| child.kind() == "local_variable_declaration";
|
|
479
|
+
}
|
|
480
|
+
!is_body_statement_child(child.kind()) && node_can_contain_pattern_binding(child)
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
fn node_can_contain_pattern_binding(node: Node<'_>) -> bool {
|
|
484
|
+
let mut found = false;
|
|
485
|
+
visit_named(node, &mut |candidate| {
|
|
486
|
+
if !found && pattern_or_local_binding_owner(candidate) {
|
|
487
|
+
found = true;
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
found
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
fn is_body_statement_child(kind: &str) -> bool {
|
|
494
|
+
matches!(
|
|
495
|
+
kind,
|
|
496
|
+
"assert_statement"
|
|
497
|
+
| "block"
|
|
498
|
+
| "break_statement"
|
|
499
|
+
| "continue_statement"
|
|
500
|
+
| "declaration"
|
|
501
|
+
| "do_statement"
|
|
502
|
+
| "empty_statement"
|
|
503
|
+
| "expression_statement"
|
|
504
|
+
| "for_statement"
|
|
505
|
+
| "if_statement"
|
|
506
|
+
| "local_function_declaration"
|
|
507
|
+
| "local_variable_declaration"
|
|
508
|
+
| "return_statement"
|
|
509
|
+
| "switch_statement"
|
|
510
|
+
| "try_statement"
|
|
511
|
+
| "while_statement"
|
|
512
|
+
| "yield_statement"
|
|
513
|
+
)
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
fn node_contains_binding_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
517
|
+
let mut found = false;
|
|
518
|
+
visit_named(node, &mut |candidate| {
|
|
519
|
+
if found {
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
if binding_name(candidate, source).as_deref() == Some(name)
|
|
523
|
+
|| pattern_binds_name(candidate, name, source)
|
|
524
|
+
{
|
|
525
|
+
found = true;
|
|
526
|
+
}
|
|
527
|
+
});
|
|
528
|
+
found
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
fn binding_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
532
|
+
if !matches!(
|
|
533
|
+
node.kind(),
|
|
534
|
+
"formal_parameter"
|
|
535
|
+
| "initialized_identifier"
|
|
536
|
+
| "initialized_variable_definition"
|
|
537
|
+
| "typed_identifier"
|
|
538
|
+
| "variable_pattern"
|
|
539
|
+
) {
|
|
540
|
+
return None;
|
|
541
|
+
}
|
|
542
|
+
node.child_by_field_name("name")
|
|
543
|
+
.and_then(|name| name.utf8_text(source.as_bytes()).ok())
|
|
544
|
+
.map(str::to_owned)
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
fn field_text(node: Node<'_>, field_name: &str, source: &str) -> Option<String> {
|
|
548
|
+
node.child_by_field_name(field_name)
|
|
549
|
+
.and_then(|field| field.utf8_text(source.as_bytes()).ok())
|
|
550
|
+
.map(str::to_owned)
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
fn formal_parameter_name(param: Node<'_>, source: &str) -> Option<String> {
|
|
554
|
+
param
|
|
555
|
+
.child_by_field_name("name")
|
|
556
|
+
.or_else(|| last_identifier_child(param, source))
|
|
557
|
+
.and_then(|name| name.utf8_text(source.as_bytes()).ok())
|
|
558
|
+
.map(str::to_owned)
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
fn last_identifier_child<'tree>(node: Node<'tree>, source: &str) -> Option<Node<'tree>> {
|
|
562
|
+
let mut cursor = node.walk();
|
|
563
|
+
node.named_children(&mut cursor)
|
|
564
|
+
.filter(|child| {
|
|
565
|
+
matches!(
|
|
566
|
+
child.kind(),
|
|
567
|
+
"identifier" | "identifier_dollar_escaped" | "type_identifier"
|
|
568
|
+
) && child.utf8_text(source.as_bytes()).ok() != Some("key")
|
|
569
|
+
})
|
|
570
|
+
.last()
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
fn visit_named(node: Node<'_>, visitor: &mut impl FnMut(Node<'_>)) {
|
|
574
|
+
visitor(node);
|
|
575
|
+
let mut cursor = node.walk();
|
|
576
|
+
for child in node.named_children(&mut cursor) {
|
|
577
|
+
visit_named(child, visitor);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
fn direct_named_child<'tree>(node: Node<'tree>, kind: &str) -> Option<Node<'tree>> {
|
|
582
|
+
let mut cursor = node.walk();
|
|
583
|
+
node.named_children(&mut cursor)
|
|
584
|
+
.find(|child| child.kind() == kind)
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
fn name_field_of(parent: Node<'_>, child: Node<'_>) -> bool {
|
|
588
|
+
let mut cursor = parent.walk();
|
|
589
|
+
parent
|
|
590
|
+
.children_by_field_name("name", &mut cursor)
|
|
591
|
+
.any(|field| same_node(field, child))
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
fn property_field_of(parent: Node<'_>, child: Node<'_>) -> bool {
|
|
595
|
+
let mut cursor = parent.walk();
|
|
596
|
+
parent
|
|
597
|
+
.children_by_field_name("property", &mut cursor)
|
|
598
|
+
.any(|field| same_node(field, child))
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
fn has_ancestor_kind(node: Node<'_>, kinds: &[&str]) -> bool {
|
|
602
|
+
let mut parent = node.parent();
|
|
603
|
+
while let Some(ancestor) = parent {
|
|
604
|
+
if kinds.contains(&ancestor.kind()) {
|
|
605
|
+
return true;
|
|
606
|
+
}
|
|
607
|
+
parent = ancestor.parent();
|
|
608
|
+
}
|
|
609
|
+
false
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
fn same_node(left: Node<'_>, right: Node<'_>) -> bool {
|
|
613
|
+
left.kind() == right.kind()
|
|
614
|
+
&& left.start_byte() == right.start_byte()
|
|
615
|
+
&& left.end_byte() == right.end_byte()
|
|
616
|
+
}
|