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,875 @@
|
|
|
1
|
+
use std::collections::BTreeSet;
|
|
2
|
+
|
|
3
|
+
use tree_sitter::Node;
|
|
4
|
+
|
|
5
|
+
use super::{
|
|
6
|
+
direct_constructor_call_text, direct_named_child, is_identifier_character,
|
|
7
|
+
route_class_locally_shadowed, route_constructor_receiver, strip_whitespace,
|
|
8
|
+
unwrap_parenthesized_text,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
pub(super) fn route_alias_receiver_node(
|
|
12
|
+
node: Node<'_>,
|
|
13
|
+
aliases: &BTreeSet<String>,
|
|
14
|
+
source: &str,
|
|
15
|
+
) -> bool {
|
|
16
|
+
node.utf8_text(source.as_bytes())
|
|
17
|
+
.ok()
|
|
18
|
+
.is_some_and(|text| route_alias_receiver_text(text, aliases))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
pub(super) fn route_alias_receiver_text(text: &str, aliases: &BTreeSet<String>) -> bool {
|
|
22
|
+
let compact = strip_whitespace(text);
|
|
23
|
+
let receiver = unwrap_parenthesized_text(&compact)
|
|
24
|
+
.unwrap_or(&compact)
|
|
25
|
+
.trim_end_matches(['?', '!']);
|
|
26
|
+
aliases.contains(receiver)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
pub(super) fn route_aliases_at(
|
|
30
|
+
root: Node<'_>,
|
|
31
|
+
site: Node<'_>,
|
|
32
|
+
route_classes: &BTreeSet<String>,
|
|
33
|
+
source: &str,
|
|
34
|
+
) -> BTreeSet<String> {
|
|
35
|
+
let mut aliases = RouteAliasSet::default();
|
|
36
|
+
let mut steps = Vec::new();
|
|
37
|
+
let mut path_child = site;
|
|
38
|
+
let mut parent = site.parent();
|
|
39
|
+
while let Some(scope) = parent {
|
|
40
|
+
steps.push((scope, path_child));
|
|
41
|
+
if same_node(scope, root) {
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
path_child = scope;
|
|
45
|
+
parent = scope.parent();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
for (scope, child) in steps.into_iter().rev() {
|
|
49
|
+
if scope.kind() == "class_body" {
|
|
50
|
+
collect_route_member_aliases(root, scope, &mut aliases, route_classes, source);
|
|
51
|
+
}
|
|
52
|
+
remove_aliases_shadowed_by_callable_parameters(scope, &mut aliases, source);
|
|
53
|
+
remove_aliases_shadowed_by_header_scope(
|
|
54
|
+
scope,
|
|
55
|
+
child,
|
|
56
|
+
site.start_byte(),
|
|
57
|
+
&mut aliases,
|
|
58
|
+
source,
|
|
59
|
+
);
|
|
60
|
+
collect_prior_route_aliases(root, scope, child, &mut aliases, route_classes, source);
|
|
61
|
+
}
|
|
62
|
+
aliases.names()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
#[derive(Default)]
|
|
66
|
+
struct RouteAliasSet {
|
|
67
|
+
member: BTreeSet<String>,
|
|
68
|
+
local: BTreeSet<String>,
|
|
69
|
+
shadowed_unqualified: BTreeSet<String>,
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
#[derive(Clone)]
|
|
73
|
+
struct RouteAliasEntry {
|
|
74
|
+
text: String,
|
|
75
|
+
origin: RouteAliasOrigin,
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
#[derive(Clone, Copy, Eq, PartialEq)]
|
|
79
|
+
enum RouteAliasOrigin {
|
|
80
|
+
Member,
|
|
81
|
+
Local,
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
impl RouteAliasSet {
|
|
85
|
+
fn insert_member(&mut self, alias: String) {
|
|
86
|
+
self.member.insert(alias);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
fn insert_local(&mut self, alias: String) {
|
|
90
|
+
self.shadowed_unqualified.insert(alias.clone());
|
|
91
|
+
self.local.insert(alias);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
fn remove_unqualified(&mut self, name: &str) {
|
|
95
|
+
self.member.remove(name);
|
|
96
|
+
self.local.remove(name);
|
|
97
|
+
self.shadowed_unqualified.insert(name.to_owned());
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
fn remove_entry(&mut self, entry: &RouteAliasEntry) {
|
|
101
|
+
match entry.origin {
|
|
102
|
+
RouteAliasOrigin::Member => {
|
|
103
|
+
self.member.remove(&entry.text);
|
|
104
|
+
}
|
|
105
|
+
RouteAliasOrigin::Local => {
|
|
106
|
+
self.local.remove(&entry.text);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
fn unqualified_names(&self) -> BTreeSet<String> {
|
|
112
|
+
self.member
|
|
113
|
+
.iter()
|
|
114
|
+
.chain(self.local.iter())
|
|
115
|
+
.filter(|alias| !alias.contains('.'))
|
|
116
|
+
.cloned()
|
|
117
|
+
.collect()
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
fn entries(&self) -> Vec<RouteAliasEntry> {
|
|
121
|
+
self.member
|
|
122
|
+
.iter()
|
|
123
|
+
.map(|alias| RouteAliasEntry {
|
|
124
|
+
text: alias.clone(),
|
|
125
|
+
origin: RouteAliasOrigin::Member,
|
|
126
|
+
})
|
|
127
|
+
.chain(self.local.iter().map(|alias| RouteAliasEntry {
|
|
128
|
+
text: alias.clone(),
|
|
129
|
+
origin: RouteAliasOrigin::Local,
|
|
130
|
+
}))
|
|
131
|
+
.collect()
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
fn unqualified_shadowed(&self, name: &str) -> bool {
|
|
135
|
+
self.shadowed_unqualified.contains(name)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
fn names(&self) -> BTreeSet<String> {
|
|
139
|
+
self.member
|
|
140
|
+
.iter()
|
|
141
|
+
.chain(self.local.iter())
|
|
142
|
+
.cloned()
|
|
143
|
+
.collect()
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
fn collect_route_member_aliases(
|
|
148
|
+
root: Node<'_>,
|
|
149
|
+
class_body: Node<'_>,
|
|
150
|
+
aliases: &mut RouteAliasSet,
|
|
151
|
+
route_classes: &BTreeSet<String>,
|
|
152
|
+
source: &str,
|
|
153
|
+
) {
|
|
154
|
+
let mut cursor = class_body.walk();
|
|
155
|
+
for member in class_body.named_children(&mut cursor) {
|
|
156
|
+
collect_route_aliases_from_member(root, member, aliases, route_classes, source);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
fn collect_route_aliases_from_member(
|
|
161
|
+
root: Node<'_>,
|
|
162
|
+
node: Node<'_>,
|
|
163
|
+
aliases: &mut RouteAliasSet,
|
|
164
|
+
route_classes: &BTreeSet<String>,
|
|
165
|
+
source: &str,
|
|
166
|
+
) {
|
|
167
|
+
if is_callable_node(node.kind()) || node.kind() == "function_body" {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
collect_route_alias_from_initialized_node(root, node, aliases, route_classes, true, source);
|
|
171
|
+
let mut cursor = node.walk();
|
|
172
|
+
for child in node.named_children(&mut cursor) {
|
|
173
|
+
collect_route_aliases_from_member(root, child, aliases, route_classes, source);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
fn remove_aliases_shadowed_by_callable_parameters(
|
|
178
|
+
scope: Node<'_>,
|
|
179
|
+
aliases: &mut RouteAliasSet,
|
|
180
|
+
source: &str,
|
|
181
|
+
) {
|
|
182
|
+
if !is_callable_node(scope.kind()) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
for parameter in callable_direct_parameter_names(scope, source) {
|
|
186
|
+
aliases.remove_unqualified(¶meter);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
fn remove_aliases_shadowed_by_header_scope(
|
|
191
|
+
scope: Node<'_>,
|
|
192
|
+
path_child: Node<'_>,
|
|
193
|
+
usage_start: usize,
|
|
194
|
+
aliases: &mut RouteAliasSet,
|
|
195
|
+
source: &str,
|
|
196
|
+
) {
|
|
197
|
+
let shadowed = aliases
|
|
198
|
+
.unqualified_names()
|
|
199
|
+
.into_iter()
|
|
200
|
+
.filter(|alias| scoped_header_binding_exists(scope, path_child, usage_start, alias, source))
|
|
201
|
+
.collect::<Vec<_>>();
|
|
202
|
+
for alias in shadowed {
|
|
203
|
+
aliases.remove_unqualified(&alias);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
fn collect_prior_route_aliases(
|
|
208
|
+
root: Node<'_>,
|
|
209
|
+
scope: Node<'_>,
|
|
210
|
+
path_child: Node<'_>,
|
|
211
|
+
aliases: &mut RouteAliasSet,
|
|
212
|
+
route_classes: &BTreeSet<String>,
|
|
213
|
+
source: &str,
|
|
214
|
+
) {
|
|
215
|
+
let mut cursor = scope.walk();
|
|
216
|
+
for sibling in scope.named_children(&mut cursor) {
|
|
217
|
+
if same_node(sibling, path_child) || sibling.start_byte() >= path_child.start_byte() {
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
collect_route_aliases_from_lexical_declaration(
|
|
221
|
+
root,
|
|
222
|
+
sibling,
|
|
223
|
+
aliases,
|
|
224
|
+
route_classes,
|
|
225
|
+
source,
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
fn collect_route_aliases_from_lexical_declaration(
|
|
231
|
+
root: Node<'_>,
|
|
232
|
+
node: Node<'_>,
|
|
233
|
+
aliases: &mut RouteAliasSet,
|
|
234
|
+
route_classes: &BTreeSet<String>,
|
|
235
|
+
source: &str,
|
|
236
|
+
) {
|
|
237
|
+
remove_aliases_shadowed_by_lexical_sibling(node, aliases, source);
|
|
238
|
+
remove_aliases_changed_by_lexical_sibling(node, aliases, source);
|
|
239
|
+
match node.kind() {
|
|
240
|
+
"local_variable_declaration" | "pattern_variable_declaration" => {
|
|
241
|
+
collect_route_aliases_from_declaration_shape(
|
|
242
|
+
root,
|
|
243
|
+
node,
|
|
244
|
+
aliases,
|
|
245
|
+
route_classes,
|
|
246
|
+
source,
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
"expression_statement" | "statement" => {
|
|
250
|
+
let mut cursor = node.walk();
|
|
251
|
+
for child in node.named_children(&mut cursor) {
|
|
252
|
+
if matches!(
|
|
253
|
+
child.kind(),
|
|
254
|
+
"local_variable_declaration" | "pattern_variable_declaration"
|
|
255
|
+
) {
|
|
256
|
+
collect_route_aliases_from_declaration_shape(
|
|
257
|
+
root,
|
|
258
|
+
child,
|
|
259
|
+
aliases,
|
|
260
|
+
route_classes,
|
|
261
|
+
source,
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
_ => {}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
fn remove_aliases_shadowed_by_lexical_sibling(
|
|
271
|
+
node: Node<'_>,
|
|
272
|
+
aliases: &mut RouteAliasSet,
|
|
273
|
+
source: &str,
|
|
274
|
+
) {
|
|
275
|
+
if node.kind() == "local_function_declaration"
|
|
276
|
+
&& let Some(name) = local_function_name(node, source)
|
|
277
|
+
{
|
|
278
|
+
aliases.remove_unqualified(&name);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
fn remove_aliases_changed_by_lexical_sibling(
|
|
283
|
+
node: Node<'_>,
|
|
284
|
+
aliases: &mut RouteAliasSet,
|
|
285
|
+
source: &str,
|
|
286
|
+
) {
|
|
287
|
+
let changed = aliases
|
|
288
|
+
.entries()
|
|
289
|
+
.into_iter()
|
|
290
|
+
.filter(|entry| lexical_sibling_changes_alias(node, entry, aliases, source))
|
|
291
|
+
.collect::<Vec<_>>();
|
|
292
|
+
for entry in changed {
|
|
293
|
+
aliases.remove_entry(&entry);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
fn lexical_sibling_changes_alias(
|
|
298
|
+
node: Node<'_>,
|
|
299
|
+
alias: &RouteAliasEntry,
|
|
300
|
+
aliases: &RouteAliasSet,
|
|
301
|
+
source: &str,
|
|
302
|
+
) -> bool {
|
|
303
|
+
if is_callable_node(node.kind()) || node.kind() == "function_body" {
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
lexical_node_changes_alias(node, alias, aliases, false, source)
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
fn lexical_node_changes_alias(
|
|
310
|
+
node: Node<'_>,
|
|
311
|
+
alias: &RouteAliasEntry,
|
|
312
|
+
aliases: &RouteAliasSet,
|
|
313
|
+
unqualified_shadowed: bool,
|
|
314
|
+
source: &str,
|
|
315
|
+
) -> bool {
|
|
316
|
+
if is_callable_node(node.kind()) || node.kind() == "function_body" {
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
let alias_name = unqualified_alias_name(&alias.text);
|
|
320
|
+
let mut shadowed = unqualified_shadowed || lexical_node_binds_name(node, alias_name, source);
|
|
321
|
+
if assignment_or_update_changes_alias(node, alias, aliases, shadowed, source) {
|
|
322
|
+
return true;
|
|
323
|
+
}
|
|
324
|
+
let mut cursor = node.walk();
|
|
325
|
+
for child in node.named_children(&mut cursor) {
|
|
326
|
+
let child_shadowed = shadowed
|
|
327
|
+
|| lexical_child_binds_name_for_following_siblings(
|
|
328
|
+
node.kind(),
|
|
329
|
+
child,
|
|
330
|
+
alias_name,
|
|
331
|
+
source,
|
|
332
|
+
);
|
|
333
|
+
if lexical_node_changes_alias(child, alias, aliases, child_shadowed, source) {
|
|
334
|
+
return true;
|
|
335
|
+
}
|
|
336
|
+
if lexical_child_binds_name_for_following_siblings(node.kind(), child, alias_name, source) {
|
|
337
|
+
shadowed = true;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
false
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
fn unqualified_alias_name(alias: &str) -> &str {
|
|
344
|
+
alias.strip_prefix("this.").unwrap_or(alias)
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
fn lexical_node_binds_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
348
|
+
if catch_clause_binds_name(node, name, source) {
|
|
349
|
+
return true;
|
|
350
|
+
}
|
|
351
|
+
matches!(
|
|
352
|
+
node.kind(),
|
|
353
|
+
"local_variable_declaration"
|
|
354
|
+
| "pattern_variable_declaration"
|
|
355
|
+
| "local_function_declaration"
|
|
356
|
+
) && declaration_binds_name(node, name, source)
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
fn lexical_child_binds_name_for_following_siblings(
|
|
360
|
+
scope_kind: &str,
|
|
361
|
+
child: Node<'_>,
|
|
362
|
+
name: &str,
|
|
363
|
+
source: &str,
|
|
364
|
+
) -> bool {
|
|
365
|
+
if child.kind() == "local_function_declaration"
|
|
366
|
+
&& local_function_name(child, source).as_deref() == Some(name)
|
|
367
|
+
{
|
|
368
|
+
return true;
|
|
369
|
+
}
|
|
370
|
+
if matches!(
|
|
371
|
+
child.kind(),
|
|
372
|
+
"local_variable_declaration" | "pattern_variable_declaration"
|
|
373
|
+
) && declaration_binds_name(child, name, source)
|
|
374
|
+
{
|
|
375
|
+
return true;
|
|
376
|
+
}
|
|
377
|
+
header_binding_child(scope_kind, child) && declaration_binds_name(child, name, source)
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
fn assignment_or_update_changes_alias(
|
|
381
|
+
node: Node<'_>,
|
|
382
|
+
alias: &RouteAliasEntry,
|
|
383
|
+
aliases: &RouteAliasSet,
|
|
384
|
+
unqualified_shadowed: bool,
|
|
385
|
+
source: &str,
|
|
386
|
+
) -> bool {
|
|
387
|
+
let assigned = match node.kind() {
|
|
388
|
+
"assignment_expression" => node
|
|
389
|
+
.child_by_field_name("left")
|
|
390
|
+
.and_then(|left| assigned_target_from_assignable(left, source)),
|
|
391
|
+
"postfix_expression" => node
|
|
392
|
+
.child_by_field_name("argument")
|
|
393
|
+
.and_then(|argument| assigned_target_from_assignable(argument, source)),
|
|
394
|
+
"unary_expression" if unary_update_expression(node, source) => {
|
|
395
|
+
direct_named_child(node, "assignable_expression")
|
|
396
|
+
.and_then(|argument| assigned_target_from_assignable(argument, source))
|
|
397
|
+
}
|
|
398
|
+
_ => None,
|
|
399
|
+
};
|
|
400
|
+
assigned.is_some_and(|assigned| match assigned {
|
|
401
|
+
AssignedTarget::Name(name) => {
|
|
402
|
+
if unqualified_shadowed {
|
|
403
|
+
return false;
|
|
404
|
+
}
|
|
405
|
+
alias.text == name
|
|
406
|
+
|| (alias.origin == RouteAliasOrigin::Member
|
|
407
|
+
&& alias.text.strip_prefix("this.") == Some(name.as_str())
|
|
408
|
+
&& !aliases.unqualified_shadowed(&name))
|
|
409
|
+
}
|
|
410
|
+
AssignedTarget::ThisMember(name) => {
|
|
411
|
+
alias.origin == RouteAliasOrigin::Member
|
|
412
|
+
&& (alias.text == name || alias.text.strip_prefix("this.") == Some(name.as_str()))
|
|
413
|
+
}
|
|
414
|
+
})
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
fn unary_update_expression(node: Node<'_>, source: &str) -> bool {
|
|
418
|
+
node.utf8_text(source.as_bytes())
|
|
419
|
+
.ok()
|
|
420
|
+
.map(str::trim_start)
|
|
421
|
+
.is_some_and(|text| text.starts_with("++") || text.starts_with("--"))
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
enum AssignedTarget {
|
|
425
|
+
Name(String),
|
|
426
|
+
ThisMember(String),
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
fn assigned_target_from_assignable(node: Node<'_>, source: &str) -> Option<AssignedTarget> {
|
|
430
|
+
let text = node
|
|
431
|
+
.utf8_text(source.as_bytes())
|
|
432
|
+
.ok()
|
|
433
|
+
.map(strip_whitespace)?;
|
|
434
|
+
if is_identifier_text(&text) {
|
|
435
|
+
return Some(AssignedTarget::Name(text));
|
|
436
|
+
}
|
|
437
|
+
let property = field_text(node, "property", source)?;
|
|
438
|
+
let object = node
|
|
439
|
+
.child_by_field_name("object")?
|
|
440
|
+
.utf8_text(source.as_bytes())
|
|
441
|
+
.ok()
|
|
442
|
+
.map(strip_whitespace)?;
|
|
443
|
+
(object == "this").then_some(AssignedTarget::ThisMember(property))
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
fn collect_route_aliases_from_declaration_shape(
|
|
447
|
+
root: Node<'_>,
|
|
448
|
+
node: Node<'_>,
|
|
449
|
+
aliases: &mut RouteAliasSet,
|
|
450
|
+
route_classes: &BTreeSet<String>,
|
|
451
|
+
source: &str,
|
|
452
|
+
) {
|
|
453
|
+
let shadowed = aliases
|
|
454
|
+
.unqualified_names()
|
|
455
|
+
.into_iter()
|
|
456
|
+
.filter(|alias| declaration_binds_name(node, alias, source))
|
|
457
|
+
.collect::<Vec<_>>();
|
|
458
|
+
for alias in shadowed {
|
|
459
|
+
aliases.remove_unqualified(&alias);
|
|
460
|
+
}
|
|
461
|
+
collect_route_alias_from_initialized_node(root, node, aliases, route_classes, false, source);
|
|
462
|
+
let mut cursor = node.walk();
|
|
463
|
+
for child in node.named_children(&mut cursor) {
|
|
464
|
+
if matches!(
|
|
465
|
+
child.kind(),
|
|
466
|
+
"initialized_identifier"
|
|
467
|
+
| "initialized_identifier_list"
|
|
468
|
+
| "initialized_variable_definition"
|
|
469
|
+
| "variable_declaration_list"
|
|
470
|
+
) {
|
|
471
|
+
collect_route_aliases_from_declaration_shape(
|
|
472
|
+
root,
|
|
473
|
+
child,
|
|
474
|
+
aliases,
|
|
475
|
+
route_classes,
|
|
476
|
+
source,
|
|
477
|
+
);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
fn collect_route_alias_from_initialized_node(
|
|
483
|
+
root: Node<'_>,
|
|
484
|
+
node: Node<'_>,
|
|
485
|
+
aliases: &mut RouteAliasSet,
|
|
486
|
+
route_classes: &BTreeSet<String>,
|
|
487
|
+
include_this: bool,
|
|
488
|
+
source: &str,
|
|
489
|
+
) {
|
|
490
|
+
let Some(alias) = route_alias_from_initialized_node(root, node, route_classes, source) else {
|
|
491
|
+
return;
|
|
492
|
+
};
|
|
493
|
+
if include_this {
|
|
494
|
+
aliases.insert_member(format!("this.{alias}"));
|
|
495
|
+
aliases.insert_member(alias);
|
|
496
|
+
} else {
|
|
497
|
+
aliases.insert_local(alias);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
fn route_alias_from_initialized_node(
|
|
502
|
+
root: Node<'_>,
|
|
503
|
+
node: Node<'_>,
|
|
504
|
+
route_classes: &BTreeSet<String>,
|
|
505
|
+
source: &str,
|
|
506
|
+
) -> Option<String> {
|
|
507
|
+
if !matches!(
|
|
508
|
+
node.kind(),
|
|
509
|
+
"initialized_identifier" | "initialized_variable_definition"
|
|
510
|
+
) {
|
|
511
|
+
return None;
|
|
512
|
+
}
|
|
513
|
+
let text = node.utf8_text(source.as_bytes()).ok()?;
|
|
514
|
+
let (left, right) = text.split_once('=')?;
|
|
515
|
+
let is_route_constructor = node
|
|
516
|
+
.child_by_field_name("value")
|
|
517
|
+
.is_some_and(|value| route_constructor_receiver(root, value, route_classes, source))
|
|
518
|
+
|| route_constructor_expression_text(root, right, route_classes, source);
|
|
519
|
+
if !is_route_constructor {
|
|
520
|
+
return None;
|
|
521
|
+
}
|
|
522
|
+
field_text(node, "name", source).or_else(|| identifier_before_equals(left))
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
fn route_constructor_expression_text(
|
|
526
|
+
root: Node<'_>,
|
|
527
|
+
text: &str,
|
|
528
|
+
route_classes: &BTreeSet<String>,
|
|
529
|
+
source: &str,
|
|
530
|
+
) -> bool {
|
|
531
|
+
let expression = text.trim().trim_end_matches([',', ';']).trim();
|
|
532
|
+
let unwrapped = unwrap_parenthesized_text(expression).unwrap_or(expression);
|
|
533
|
+
let without_keyword = strip_constructor_keyword_prefix(unwrapped).unwrap_or(unwrapped);
|
|
534
|
+
let receiver = strip_whitespace(without_keyword);
|
|
535
|
+
route_classes.iter().any(|route_class| {
|
|
536
|
+
direct_constructor_call_text(&receiver, route_class)
|
|
537
|
+
&& !route_class_locally_shadowed(root, route_class, source)
|
|
538
|
+
})
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
fn strip_constructor_keyword_prefix(text: &str) -> Option<&str> {
|
|
542
|
+
for keyword in ["const", "new"] {
|
|
543
|
+
let Some(rest) = text.strip_prefix(keyword) else {
|
|
544
|
+
continue;
|
|
545
|
+
};
|
|
546
|
+
if rest.chars().next().is_some_and(char::is_whitespace) {
|
|
547
|
+
return Some(rest.trim_start());
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
None
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
fn scoped_header_binding_exists(
|
|
554
|
+
scope: Node<'_>,
|
|
555
|
+
path_child: Node<'_>,
|
|
556
|
+
usage_start: usize,
|
|
557
|
+
name: &str,
|
|
558
|
+
source: &str,
|
|
559
|
+
) -> bool {
|
|
560
|
+
if catch_header_binding_exists(scope, path_child, usage_start, name, source) {
|
|
561
|
+
return true;
|
|
562
|
+
}
|
|
563
|
+
if !matches!(
|
|
564
|
+
scope.kind(),
|
|
565
|
+
"for_element"
|
|
566
|
+
| "for_statement"
|
|
567
|
+
| "if_element"
|
|
568
|
+
| "if_statement"
|
|
569
|
+
| "switch_expression_case"
|
|
570
|
+
| "switch_statement_case"
|
|
571
|
+
) {
|
|
572
|
+
return false;
|
|
573
|
+
}
|
|
574
|
+
if header_field_binding_exists(scope, path_child, usage_start, name, source) {
|
|
575
|
+
return true;
|
|
576
|
+
}
|
|
577
|
+
let mut cursor = scope.walk();
|
|
578
|
+
for child in scope.named_children(&mut cursor) {
|
|
579
|
+
if same_node(child, path_child) || child.start_byte() >= path_child.start_byte() {
|
|
580
|
+
break;
|
|
581
|
+
}
|
|
582
|
+
if header_binding_child(scope.kind(), child)
|
|
583
|
+
&& child.end_byte() <= usage_start
|
|
584
|
+
&& declaration_binds_name(child, name, source)
|
|
585
|
+
{
|
|
586
|
+
return true;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
false
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
fn catch_header_binding_exists(
|
|
593
|
+
scope: Node<'_>,
|
|
594
|
+
path_child: Node<'_>,
|
|
595
|
+
usage_start: usize,
|
|
596
|
+
name: &str,
|
|
597
|
+
source: &str,
|
|
598
|
+
) -> bool {
|
|
599
|
+
if scope.kind() == "catch_clause" {
|
|
600
|
+
return scope.start_byte() < path_child.start_byte()
|
|
601
|
+
&& catch_clause_binds_name(scope, name, source);
|
|
602
|
+
}
|
|
603
|
+
if scope.kind() == "try_statement" {
|
|
604
|
+
let mut previous: Option<Node<'_>> = None;
|
|
605
|
+
let mut cursor = scope.walk();
|
|
606
|
+
for child in scope.named_children(&mut cursor) {
|
|
607
|
+
if same_node(child, path_child) || child.start_byte() >= path_child.start_byte() {
|
|
608
|
+
return previous.is_some_and(|header| {
|
|
609
|
+
header.kind() == "catch_clause"
|
|
610
|
+
&& catch_body_child(path_child)
|
|
611
|
+
&& header.end_byte() <= usage_start
|
|
612
|
+
&& !source_between_contains_finally(header, path_child, source)
|
|
613
|
+
&& catch_clause_binds_name(header, name, source)
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
previous = Some(child);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
false
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
fn catch_body_child(node: Node<'_>) -> bool {
|
|
623
|
+
matches!(node.kind(), "block" | "function_body")
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
fn source_between_contains_finally(left: Node<'_>, right: Node<'_>, source: &str) -> bool {
|
|
627
|
+
source
|
|
628
|
+
.get(left.end_byte()..right.start_byte())
|
|
629
|
+
.is_some_and(|text| text.contains("finally"))
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
fn catch_clause_binds_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
633
|
+
field_text(node, "exception", source).as_deref() == Some(name)
|
|
634
|
+
|| field_text(node, "stack_trace", source).as_deref() == Some(name)
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
fn header_field_binding_exists(
|
|
638
|
+
scope: Node<'_>,
|
|
639
|
+
path_child: Node<'_>,
|
|
640
|
+
usage_start: usize,
|
|
641
|
+
name: &str,
|
|
642
|
+
source: &str,
|
|
643
|
+
) -> bool {
|
|
644
|
+
if !matches!(scope.kind(), "for_element" | "for_statement") {
|
|
645
|
+
return false;
|
|
646
|
+
}
|
|
647
|
+
let mut cursor = scope.walk();
|
|
648
|
+
scope
|
|
649
|
+
.children_by_field_name("name", &mut cursor)
|
|
650
|
+
.any(|field| {
|
|
651
|
+
field.end_byte() <= usage_start
|
|
652
|
+
&& field.start_byte() < path_child.start_byte()
|
|
653
|
+
&& field.utf8_text(source.as_bytes()).ok() == Some(name)
|
|
654
|
+
})
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
fn header_binding_child(scope_kind: &str, child: Node<'_>) -> bool {
|
|
658
|
+
if matches!(scope_kind, "for_element" | "for_statement") {
|
|
659
|
+
return !is_body_statement_child(child.kind())
|
|
660
|
+
|| child.kind() == "local_variable_declaration";
|
|
661
|
+
}
|
|
662
|
+
!is_body_statement_child(child.kind()) && node_can_contain_pattern_binding(child)
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
fn node_can_contain_pattern_binding(node: Node<'_>) -> bool {
|
|
666
|
+
let mut found = false;
|
|
667
|
+
visit_named(node, &mut |candidate| {
|
|
668
|
+
if !found
|
|
669
|
+
&& matches!(
|
|
670
|
+
candidate.kind(),
|
|
671
|
+
"list_pattern"
|
|
672
|
+
| "local_variable_declaration"
|
|
673
|
+
| "map_pattern"
|
|
674
|
+
| "object_pattern"
|
|
675
|
+
| "pattern_variable_declaration"
|
|
676
|
+
| "record_pattern"
|
|
677
|
+
| "variable_pattern"
|
|
678
|
+
)
|
|
679
|
+
{
|
|
680
|
+
found = true;
|
|
681
|
+
}
|
|
682
|
+
});
|
|
683
|
+
found
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
fn is_body_statement_child(kind: &str) -> bool {
|
|
687
|
+
matches!(
|
|
688
|
+
kind,
|
|
689
|
+
"assert_statement"
|
|
690
|
+
| "block"
|
|
691
|
+
| "break_statement"
|
|
692
|
+
| "continue_statement"
|
|
693
|
+
| "declaration"
|
|
694
|
+
| "do_statement"
|
|
695
|
+
| "empty_statement"
|
|
696
|
+
| "expression_statement"
|
|
697
|
+
| "for_statement"
|
|
698
|
+
| "if_statement"
|
|
699
|
+
| "local_function_declaration"
|
|
700
|
+
| "local_variable_declaration"
|
|
701
|
+
| "return_statement"
|
|
702
|
+
| "switch_statement"
|
|
703
|
+
| "try_statement"
|
|
704
|
+
| "while_statement"
|
|
705
|
+
| "yield_statement"
|
|
706
|
+
)
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
fn local_function_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
710
|
+
direct_named_child(node, "function_signature")
|
|
711
|
+
.and_then(|signature| signature.child_by_field_name("name"))
|
|
712
|
+
.or_else(|| node.child_by_field_name("name"))
|
|
713
|
+
.and_then(|name| name.utf8_text(source.as_bytes()).ok())
|
|
714
|
+
.map(str::to_owned)
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
fn declaration_binds_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
718
|
+
if binding_name(node, source).as_deref() == Some(name) {
|
|
719
|
+
return true;
|
|
720
|
+
}
|
|
721
|
+
if is_callable_node(node.kind()) || node.kind() == "function_body" {
|
|
722
|
+
return false;
|
|
723
|
+
}
|
|
724
|
+
let mut cursor = node.walk();
|
|
725
|
+
node.named_children(&mut cursor)
|
|
726
|
+
.any(|child| declaration_binds_name(child, name, source))
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
fn binding_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
730
|
+
if !matches!(
|
|
731
|
+
node.kind(),
|
|
732
|
+
"formal_parameter"
|
|
733
|
+
| "initialized_identifier"
|
|
734
|
+
| "initialized_variable_definition"
|
|
735
|
+
| "typed_identifier"
|
|
736
|
+
| "variable_pattern"
|
|
737
|
+
) {
|
|
738
|
+
return None;
|
|
739
|
+
}
|
|
740
|
+
field_text(node, "name", source)
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
fn callable_direct_parameter_names(node: Node<'_>, source: &str) -> Vec<String> {
|
|
744
|
+
direct_parameter_lists(node)
|
|
745
|
+
.into_iter()
|
|
746
|
+
.flat_map(|parameters| direct_parameter_names(parameters, source))
|
|
747
|
+
.collect()
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
fn direct_parameter_lists(node: Node<'_>) -> Vec<Node<'_>> {
|
|
751
|
+
match node.kind() {
|
|
752
|
+
"function_expression" => parameter_field_lists(node),
|
|
753
|
+
"function_declaration" | "local_function_declaration" | "method_declaration" => {
|
|
754
|
+
helper_signature(node)
|
|
755
|
+
.and_then(parameter_list)
|
|
756
|
+
.into_iter()
|
|
757
|
+
.collect()
|
|
758
|
+
}
|
|
759
|
+
_ => Vec::new(),
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
fn parameter_field_lists(node: Node<'_>) -> Vec<Node<'_>> {
|
|
764
|
+
let mut cursor = node.walk();
|
|
765
|
+
node.children_by_field_name("parameters", &mut cursor)
|
|
766
|
+
.filter_map(formal_parameter_list)
|
|
767
|
+
.collect()
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
fn helper_signature(node: Node<'_>) -> Option<Node<'_>> {
|
|
771
|
+
node.child_by_field_name("signature")
|
|
772
|
+
.or_else(|| direct_named_child(node, "function_signature"))
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
fn parameter_list(node: Node<'_>) -> Option<Node<'_>> {
|
|
776
|
+
node.child_by_field_name("parameters")
|
|
777
|
+
.or_else(|| direct_named_child(node, "formal_parameter_list"))
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
fn formal_parameter_list(node: Node<'_>) -> Option<Node<'_>> {
|
|
781
|
+
if node.kind() == "formal_parameter_list" {
|
|
782
|
+
return Some(node);
|
|
783
|
+
}
|
|
784
|
+
let mut cursor = node.walk();
|
|
785
|
+
node.named_children(&mut cursor)
|
|
786
|
+
.find(|child| child.kind() == "formal_parameter_list")
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
fn direct_parameter_names(parameters: Node<'_>, source: &str) -> Vec<String> {
|
|
790
|
+
let mut names = Vec::new();
|
|
791
|
+
let mut cursor = parameters.walk();
|
|
792
|
+
for candidate in parameters.named_children(&mut cursor) {
|
|
793
|
+
match candidate.kind() {
|
|
794
|
+
"formal_parameter" => {
|
|
795
|
+
if let Some(name) = formal_parameter_name(candidate, source) {
|
|
796
|
+
names.push(name);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
"optional_formal_parameters" => {
|
|
800
|
+
let mut optional_cursor = candidate.walk();
|
|
801
|
+
names.extend(
|
|
802
|
+
candidate
|
|
803
|
+
.named_children(&mut optional_cursor)
|
|
804
|
+
.filter(|child| child.kind() == "formal_parameter")
|
|
805
|
+
.filter_map(|child| formal_parameter_name(child, source)),
|
|
806
|
+
);
|
|
807
|
+
}
|
|
808
|
+
_ => {}
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
names
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
fn formal_parameter_name(param: Node<'_>, source: &str) -> Option<String> {
|
|
815
|
+
field_text(param, "name", source).or_else(|| last_identifier_child(param, source))
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
fn last_identifier_child(node: Node<'_>, source: &str) -> Option<String> {
|
|
819
|
+
let mut cursor = node.walk();
|
|
820
|
+
node.named_children(&mut cursor)
|
|
821
|
+
.filter(|child| {
|
|
822
|
+
matches!(
|
|
823
|
+
child.kind(),
|
|
824
|
+
"identifier" | "identifier_dollar_escaped" | "type_identifier"
|
|
825
|
+
) && child.utf8_text(source.as_bytes()).ok() != Some("key")
|
|
826
|
+
})
|
|
827
|
+
.filter_map(|child| child.utf8_text(source.as_bytes()).ok())
|
|
828
|
+
.last()
|
|
829
|
+
.map(str::to_owned)
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
fn field_text(node: Node<'_>, field: &str, source: &str) -> Option<String> {
|
|
833
|
+
node.child_by_field_name(field)
|
|
834
|
+
.and_then(|child| child.utf8_text(source.as_bytes()).ok())
|
|
835
|
+
.map(str::to_owned)
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
fn visit_named(node: Node<'_>, visitor: &mut impl FnMut(Node<'_>)) {
|
|
839
|
+
visitor(node);
|
|
840
|
+
let mut cursor = node.walk();
|
|
841
|
+
for child in node.named_children(&mut cursor) {
|
|
842
|
+
visit_named(child, visitor);
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
fn identifier_before_equals(text: &str) -> Option<String> {
|
|
847
|
+
text.split(|character: char| !is_identifier_character(character))
|
|
848
|
+
.rev()
|
|
849
|
+
.find(|part| !part.is_empty())
|
|
850
|
+
.map(str::to_owned)
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
fn is_identifier_text(text: &str) -> bool {
|
|
854
|
+
let mut chars = text.chars();
|
|
855
|
+
chars
|
|
856
|
+
.next()
|
|
857
|
+
.is_some_and(|first| first == '_' || first == '$' || first.is_ascii_alphabetic())
|
|
858
|
+
&& chars.all(is_identifier_character)
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
fn is_callable_node(kind: &str) -> bool {
|
|
862
|
+
matches!(
|
|
863
|
+
kind,
|
|
864
|
+
"function_declaration"
|
|
865
|
+
| "function_expression"
|
|
866
|
+
| "local_function_declaration"
|
|
867
|
+
| "method_declaration"
|
|
868
|
+
)
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
fn same_node(left: Node<'_>, right: Node<'_>) -> bool {
|
|
872
|
+
left.kind() == right.kind()
|
|
873
|
+
&& left.start_byte() == right.start_byte()
|
|
874
|
+
&& left.end_byte() == right.end_byte()
|
|
875
|
+
}
|