dart-decimate 0.0.9 → 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 +50 -58
- package/package.json +1 -1
- package/src/cli/analyze.rs +6 -3
- package/src/cli/entry_points.rs +18 -2
- package/src/cli/list_run.rs +1 -0
- package/src/cli/tests.rs +74 -0
- package/src/config/rules.rs +50 -13
- package/src/dependencies/analyze.rs +7 -1
- package/src/dependencies/tests.rs +29 -0
- package/src/dependencies.rs +19 -0
- package/src/dependency_scripts.rs +2 -0
- package/src/dupes/declarations.rs +512 -0
- package/src/dupes/packages.rs +213 -0
- package/src/dupes/tests.rs +197 -0
- package/src/dupes.rs +57 -19
- package/src/extract/reference_tests.rs +106 -0
- package/src/extract/references.rs +264 -2
- package/src/extract/tests.rs +25 -0
- package/src/extract.rs +6 -3
- package/src/generated.rs +96 -3
- package/src/graph/package_config_tests.rs +123 -0
- package/src/graph/tests.rs +125 -0
- package/src/graph.rs +6 -5
- package/src/output/feature_flag_findings.rs +63 -1
- package/src/output/graph_findings.rs +73 -37
- 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/security_findings.rs +15 -2
- package/src/output/tests.rs +104 -1
- package/src/output.rs +2 -1
- package/src/package_map.rs +220 -28
- package/src/routes.rs +6 -1
- package/src/scan.rs +15 -5
- package/src/security/detect.rs +140 -6
- package/src/security/tests.rs +96 -0
- package/src/trace.rs +1 -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,2595 @@
|
|
|
1
|
+
use std::collections::BTreeSet;
|
|
2
|
+
|
|
3
|
+
use tree_sitter::Node;
|
|
4
|
+
|
|
5
|
+
use super::{reads::scoped_header_binding_exists, simple_type_name};
|
|
6
|
+
|
|
7
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
8
|
+
struct ObjectPatternHelper {
|
|
9
|
+
name: HelperName,
|
|
10
|
+
parameter: HelperParameter,
|
|
11
|
+
fields: BTreeSet<String>,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
15
|
+
struct HelperName {
|
|
16
|
+
name: String,
|
|
17
|
+
qualified_names: BTreeSet<String>,
|
|
18
|
+
method_owner: Option<String>,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
22
|
+
struct HelperParameter {
|
|
23
|
+
name: String,
|
|
24
|
+
positional_index: Option<usize>,
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
pub(super) fn object_pattern_field_reads_for_widget(
|
|
28
|
+
root: Node<'_>,
|
|
29
|
+
widget_class: &str,
|
|
30
|
+
widget_body: Node<'_>,
|
|
31
|
+
state_bodies: Option<&Vec<Node<'_>>>,
|
|
32
|
+
source: &str,
|
|
33
|
+
) -> BTreeSet<String> {
|
|
34
|
+
let mut reads =
|
|
35
|
+
object_pattern_field_reads_in_body(widget_body, widget_class, &["this"], source);
|
|
36
|
+
if let Some(state_bodies) = state_bodies {
|
|
37
|
+
for state_body in state_bodies {
|
|
38
|
+
reads.extend(object_pattern_field_reads_in_body(
|
|
39
|
+
*state_body,
|
|
40
|
+
widget_class,
|
|
41
|
+
&["widget", "oldWidget"],
|
|
42
|
+
source,
|
|
43
|
+
));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let helpers = object_pattern_helpers(root, widget_class, source);
|
|
48
|
+
for helper in helpers {
|
|
49
|
+
let widget_call = body_calls_helper_with_roots(
|
|
50
|
+
widget_body,
|
|
51
|
+
&helper.name,
|
|
52
|
+
&helper.parameter,
|
|
53
|
+
&["this"],
|
|
54
|
+
source,
|
|
55
|
+
);
|
|
56
|
+
if widget_call
|
|
57
|
+
|| state_bodies.is_some_and(|state_bodies| {
|
|
58
|
+
state_bodies.iter().any(|state_body| {
|
|
59
|
+
body_calls_helper_with_roots(
|
|
60
|
+
*state_body,
|
|
61
|
+
&helper.name,
|
|
62
|
+
&helper.parameter,
|
|
63
|
+
&["widget", "oldWidget"],
|
|
64
|
+
source,
|
|
65
|
+
)
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
{
|
|
69
|
+
reads.extend(helper.fields);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
reads
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
fn object_pattern_helpers(
|
|
76
|
+
root: Node<'_>,
|
|
77
|
+
widget_class: &str,
|
|
78
|
+
source: &str,
|
|
79
|
+
) -> Vec<ObjectPatternHelper> {
|
|
80
|
+
let mut declarations = Vec::new();
|
|
81
|
+
collect_nodes_in(
|
|
82
|
+
root,
|
|
83
|
+
&["function_declaration", "method_declaration"],
|
|
84
|
+
&mut declarations,
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
let mut helpers = Vec::new();
|
|
88
|
+
for declaration in declarations {
|
|
89
|
+
let Some(signature) = helper_signature(declaration) else {
|
|
90
|
+
continue;
|
|
91
|
+
};
|
|
92
|
+
let Some(name) = helper_name(declaration, signature, source) else {
|
|
93
|
+
continue;
|
|
94
|
+
};
|
|
95
|
+
let body = helper_body(declaration).unwrap_or(declaration);
|
|
96
|
+
for parameter in helper_widget_parameters(signature, widget_class, source) {
|
|
97
|
+
let fields =
|
|
98
|
+
object_pattern_field_reads_in_body(body, widget_class, &[¶meter.name], source);
|
|
99
|
+
if fields.is_empty() {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
helpers.push(ObjectPatternHelper {
|
|
103
|
+
name: name.clone(),
|
|
104
|
+
parameter,
|
|
105
|
+
fields,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
helpers
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
fn helper_signature(node: Node<'_>) -> Option<Node<'_>> {
|
|
113
|
+
node.child_by_field_name("signature")
|
|
114
|
+
.or_else(|| direct_named_child(node, "function_signature"))
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
fn helper_body(node: Node<'_>) -> Option<Node<'_>> {
|
|
118
|
+
node.child_by_field_name("body")
|
|
119
|
+
.or_else(|| direct_named_child(node, "function_body"))
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
fn helper_name(declaration: Node<'_>, signature: Node<'_>, source: &str) -> Option<HelperName> {
|
|
123
|
+
let name = field_text(signature, "name", source)
|
|
124
|
+
.or_else(|| identifier_before_parameters(signature, source))?;
|
|
125
|
+
let method_owner = if declaration.kind() == "method_declaration" {
|
|
126
|
+
Some(owner_class_like_name(declaration, source)?)
|
|
127
|
+
} else {
|
|
128
|
+
None
|
|
129
|
+
};
|
|
130
|
+
let mut qualified_names = BTreeSet::new();
|
|
131
|
+
if let Some(owner) = &method_owner {
|
|
132
|
+
qualified_names.insert(format!("{owner}.{name}"));
|
|
133
|
+
}
|
|
134
|
+
Some(HelperName {
|
|
135
|
+
name,
|
|
136
|
+
qualified_names,
|
|
137
|
+
method_owner,
|
|
138
|
+
})
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
fn owner_class_like_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
142
|
+
let mut parent = node.parent();
|
|
143
|
+
while let Some(ancestor) = parent {
|
|
144
|
+
if matches!(ancestor.kind(), "class_declaration" | "mixin_declaration") {
|
|
145
|
+
return field_text(ancestor, "name", source);
|
|
146
|
+
}
|
|
147
|
+
parent = ancestor.parent();
|
|
148
|
+
}
|
|
149
|
+
None
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
fn helper_widget_parameters(
|
|
153
|
+
signature: Node<'_>,
|
|
154
|
+
widget_class: &str,
|
|
155
|
+
source: &str,
|
|
156
|
+
) -> Vec<HelperParameter> {
|
|
157
|
+
let parameters = parameter_list(signature).unwrap_or(signature);
|
|
158
|
+
let formal_parameters = direct_formal_parameters(parameters);
|
|
159
|
+
let mut positional_index = 0usize;
|
|
160
|
+
let mut widget_parameters = Vec::new();
|
|
161
|
+
for param in formal_parameters {
|
|
162
|
+
let is_named = is_named_parameter(param, source);
|
|
163
|
+
let current_positional_index = (!is_named).then_some(positional_index);
|
|
164
|
+
if !is_named {
|
|
165
|
+
positional_index += 1;
|
|
166
|
+
}
|
|
167
|
+
let Some(name) = formal_parameter_name(param, source) else {
|
|
168
|
+
continue;
|
|
169
|
+
};
|
|
170
|
+
if formal_parameter_type(param, &name, source).as_deref() != Some(widget_class) {
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
widget_parameters.push(HelperParameter {
|
|
174
|
+
name,
|
|
175
|
+
positional_index: current_positional_index,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
widget_parameters
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
fn direct_formal_parameters(parameters: Node<'_>) -> Vec<Node<'_>> {
|
|
182
|
+
let mut formal_parameters = Vec::new();
|
|
183
|
+
let mut cursor = parameters.walk();
|
|
184
|
+
for candidate in parameters.named_children(&mut cursor) {
|
|
185
|
+
if candidate.kind() == "optional_formal_parameters" {
|
|
186
|
+
let mut optional_cursor = candidate.walk();
|
|
187
|
+
formal_parameters.extend(candidate.named_children(&mut optional_cursor));
|
|
188
|
+
} else {
|
|
189
|
+
formal_parameters.push(candidate);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
formal_parameters
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
fn parameter_list(node: Node<'_>) -> Option<Node<'_>> {
|
|
196
|
+
node.child_by_field_name("parameters")
|
|
197
|
+
.and_then(formal_parameter_list)
|
|
198
|
+
.or_else(|| own_parameter_list(node))
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
fn is_named_parameter(param: Node<'_>, source: &str) -> bool {
|
|
202
|
+
let Some(parent) = param.parent() else {
|
|
203
|
+
return false;
|
|
204
|
+
};
|
|
205
|
+
parent.kind() == "optional_formal_parameters"
|
|
206
|
+
&& parent
|
|
207
|
+
.utf8_text(source.as_bytes())
|
|
208
|
+
.ok()
|
|
209
|
+
.is_some_and(|text| text.trim_start().starts_with('{'))
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
fn formal_parameter_name(param: Node<'_>, source: &str) -> Option<String> {
|
|
213
|
+
param
|
|
214
|
+
.child_by_field_name("name")
|
|
215
|
+
.or_else(|| last_identifier_child(param, source))
|
|
216
|
+
.and_then(|name| name.utf8_text(source.as_bytes()).ok())
|
|
217
|
+
.map(str::to_owned)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
fn formal_parameter_type(param: Node<'_>, name: &str, source: &str) -> Option<String> {
|
|
221
|
+
let text = param.utf8_text(source.as_bytes()).ok()?;
|
|
222
|
+
let name_index = text.rfind(name)?;
|
|
223
|
+
let before_name = text[..name_index].trim();
|
|
224
|
+
let type_name = before_name
|
|
225
|
+
.split_whitespace()
|
|
226
|
+
.rfind(|part| !matches!(*part, "required" | "covariant" | "final" | "var"))?;
|
|
227
|
+
Some(simple_type_name(type_name.trim_end_matches('?')))
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
pub(super) fn object_pattern_field_reads_in_body(
|
|
231
|
+
body: Node<'_>,
|
|
232
|
+
widget_class: &str,
|
|
233
|
+
root_names: &[&str],
|
|
234
|
+
source: &str,
|
|
235
|
+
) -> BTreeSet<String> {
|
|
236
|
+
let mut fields = BTreeSet::new();
|
|
237
|
+
visit_named(body, &mut |node| {
|
|
238
|
+
if node.kind() != "object_pattern" {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if object_pattern_type_name(node, source).as_deref() != Some(widget_class) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
let mut roots = root_aliases_at(body, node, root_names, source);
|
|
245
|
+
remove_roots_shadowed_by_enclosing_callable(body, node, &mut roots, source);
|
|
246
|
+
if roots.is_empty() {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
if !object_pattern_matches_roots(node, &roots, source) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
fields.extend(object_pattern_field_names(node, source));
|
|
253
|
+
});
|
|
254
|
+
fields
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
pub(super) fn remove_roots_shadowed_by_enclosing_callable(
|
|
258
|
+
body: Node<'_>,
|
|
259
|
+
site: Node<'_>,
|
|
260
|
+
roots: &mut BTreeSet<String>,
|
|
261
|
+
source: &str,
|
|
262
|
+
) {
|
|
263
|
+
let owner = (body.kind() == "class_body")
|
|
264
|
+
.then(|| outermost_callable_before_body(body, site))
|
|
265
|
+
.flatten();
|
|
266
|
+
let mut parent = site.parent();
|
|
267
|
+
while let Some(ancestor) = parent {
|
|
268
|
+
if same_node(ancestor, body) {
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
for name in callable_direct_parameter_bound_names(ancestor, roots, source) {
|
|
272
|
+
if owner.is_some_and(|owner| same_node(owner, ancestor))
|
|
273
|
+
&& owner_parameter_preserves_root(ancestor, &name, source)
|
|
274
|
+
{
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
roots.remove(&name);
|
|
278
|
+
}
|
|
279
|
+
parent = ancestor.parent();
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
pub(super) fn top_level_name_shadowed_at(
|
|
284
|
+
body: Node<'_>,
|
|
285
|
+
site: Node<'_>,
|
|
286
|
+
name: &str,
|
|
287
|
+
source: &str,
|
|
288
|
+
) -> bool {
|
|
289
|
+
lexical_local_binding_before(body, site, name, source)
|
|
290
|
+
|| lexical_header_binding_before(body, site, name, source)
|
|
291
|
+
|| enclosing_callable_parameter_binds_name(site, body, name, source)
|
|
292
|
+
|| current_class_member_shadows_top_level(site, name, source)
|
|
293
|
+
|| import_alias_exists(root_node(site), name, source)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
fn owner_parameter_preserves_root(owner: Node<'_>, name: &str, source: &str) -> bool {
|
|
297
|
+
name == "oldWidget"
|
|
298
|
+
&& owner.kind() == "method_declaration"
|
|
299
|
+
&& callable_name(owner, source).as_deref() == Some("didUpdateWidget")
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
fn callable_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
303
|
+
let signature = helper_signature(node).unwrap_or(node);
|
|
304
|
+
field_text(signature, "name", source)
|
|
305
|
+
.or_else(|| identifier_before_parameters(signature, source))
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
fn outermost_callable_before_body<'tree>(
|
|
309
|
+
body: Node<'tree>,
|
|
310
|
+
site: Node<'tree>,
|
|
311
|
+
) -> Option<Node<'tree>> {
|
|
312
|
+
let mut owner = None;
|
|
313
|
+
let mut parent = site.parent();
|
|
314
|
+
while let Some(ancestor) = parent {
|
|
315
|
+
if same_node(ancestor, body) {
|
|
316
|
+
return owner;
|
|
317
|
+
}
|
|
318
|
+
if is_callable_node(ancestor.kind()) {
|
|
319
|
+
owner = Some(ancestor);
|
|
320
|
+
}
|
|
321
|
+
parent = ancestor.parent();
|
|
322
|
+
}
|
|
323
|
+
owner
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
pub(super) fn root_aliases_at(
|
|
327
|
+
body: Node<'_>,
|
|
328
|
+
site: Node<'_>,
|
|
329
|
+
root_names: &[&str],
|
|
330
|
+
source: &str,
|
|
331
|
+
) -> BTreeSet<String> {
|
|
332
|
+
let mut roots = root_names
|
|
333
|
+
.iter()
|
|
334
|
+
.map(|name| (*name).to_owned())
|
|
335
|
+
.collect::<BTreeSet<_>>();
|
|
336
|
+
let mut steps = Vec::new();
|
|
337
|
+
let mut path_child = site;
|
|
338
|
+
let mut parent = site.parent();
|
|
339
|
+
while let Some(scope) = parent {
|
|
340
|
+
steps.push((scope, path_child));
|
|
341
|
+
if same_node(scope, body) {
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
path_child = scope;
|
|
345
|
+
parent = scope.parent();
|
|
346
|
+
}
|
|
347
|
+
let owner = (body.kind() == "class_body")
|
|
348
|
+
.then(|| outermost_callable_before_body(body, site))
|
|
349
|
+
.flatten();
|
|
350
|
+
for (scope, child) in steps.into_iter().rev() {
|
|
351
|
+
remove_roots_shadowed_by_callable_scope(scope, owner, &mut roots, source);
|
|
352
|
+
remove_roots_shadowed_by_header_scope(scope, child, site.start_byte(), &mut roots, source);
|
|
353
|
+
collect_prior_root_aliases(scope, child, &mut roots, source);
|
|
354
|
+
}
|
|
355
|
+
roots
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
fn remove_roots_shadowed_by_callable_scope(
|
|
359
|
+
scope: Node<'_>,
|
|
360
|
+
owner: Option<Node<'_>>,
|
|
361
|
+
roots: &mut BTreeSet<String>,
|
|
362
|
+
source: &str,
|
|
363
|
+
) {
|
|
364
|
+
for name in callable_direct_parameter_bound_names(scope, roots, source) {
|
|
365
|
+
if owner.is_some_and(|owner| same_node(owner, scope))
|
|
366
|
+
&& owner_parameter_preserves_root(scope, &name, source)
|
|
367
|
+
{
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
roots.remove(&name);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
fn remove_roots_shadowed_by_header_scope(
|
|
375
|
+
scope: Node<'_>,
|
|
376
|
+
path_child: Node<'_>,
|
|
377
|
+
usage_start: usize,
|
|
378
|
+
roots: &mut BTreeSet<String>,
|
|
379
|
+
source: &str,
|
|
380
|
+
) {
|
|
381
|
+
let shadowed = roots
|
|
382
|
+
.iter()
|
|
383
|
+
.filter(|root| scoped_header_binding_exists(scope, path_child, usage_start, root, source))
|
|
384
|
+
.cloned()
|
|
385
|
+
.collect::<Vec<_>>();
|
|
386
|
+
for name in shadowed {
|
|
387
|
+
roots.remove(&name);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
fn collect_prior_root_aliases(
|
|
392
|
+
scope: Node<'_>,
|
|
393
|
+
path_child: Node<'_>,
|
|
394
|
+
roots: &mut BTreeSet<String>,
|
|
395
|
+
source: &str,
|
|
396
|
+
) {
|
|
397
|
+
let mut cursor = scope.walk();
|
|
398
|
+
for sibling in scope.named_children(&mut cursor) {
|
|
399
|
+
if same_node(sibling, path_child) || sibling.start_byte() >= path_child.start_byte() {
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
collect_root_aliases_from_lexical_declaration(sibling, roots, source);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
fn collect_root_aliases_from_lexical_declaration(
|
|
407
|
+
node: Node<'_>,
|
|
408
|
+
roots: &mut BTreeSet<String>,
|
|
409
|
+
source: &str,
|
|
410
|
+
) {
|
|
411
|
+
remove_roots_shadowed_by_lexical_sibling(node, roots, source);
|
|
412
|
+
remove_roots_changed_by_lexical_sibling(node, roots, source);
|
|
413
|
+
match node.kind() {
|
|
414
|
+
"local_variable_declaration" | "pattern_variable_declaration" => {
|
|
415
|
+
collect_root_aliases_from_declaration_shape(node, roots, source);
|
|
416
|
+
}
|
|
417
|
+
"expression_statement" | "statement" => {
|
|
418
|
+
let mut cursor = node.walk();
|
|
419
|
+
for child in node.named_children(&mut cursor) {
|
|
420
|
+
if matches!(
|
|
421
|
+
child.kind(),
|
|
422
|
+
"local_variable_declaration" | "pattern_variable_declaration"
|
|
423
|
+
) {
|
|
424
|
+
collect_root_aliases_from_declaration_shape(child, roots, source);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
_ => {}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
fn remove_roots_shadowed_by_lexical_sibling(
|
|
433
|
+
node: Node<'_>,
|
|
434
|
+
roots: &mut BTreeSet<String>,
|
|
435
|
+
source: &str,
|
|
436
|
+
) {
|
|
437
|
+
if node.kind() == "local_function_declaration"
|
|
438
|
+
&& let Some(name) = local_function_name(node, source)
|
|
439
|
+
{
|
|
440
|
+
roots.remove(&name);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
fn remove_roots_changed_by_lexical_sibling(
|
|
445
|
+
node: Node<'_>,
|
|
446
|
+
roots: &mut BTreeSet<String>,
|
|
447
|
+
source: &str,
|
|
448
|
+
) {
|
|
449
|
+
let changed = roots
|
|
450
|
+
.iter()
|
|
451
|
+
.filter(|root| lexical_sibling_changes_name(node, root, source))
|
|
452
|
+
.cloned()
|
|
453
|
+
.collect::<Vec<_>>();
|
|
454
|
+
for name in changed {
|
|
455
|
+
roots.remove(&name);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
fn lexical_sibling_changes_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
460
|
+
if is_callable_node(node.kind()) {
|
|
461
|
+
return false;
|
|
462
|
+
}
|
|
463
|
+
lexical_node_changes_name(node, name, false, source)
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
fn lexical_node_changes_name(node: Node<'_>, name: &str, shadowed: bool, source: &str) -> bool {
|
|
467
|
+
if is_callable_node(node.kind()) {
|
|
468
|
+
return false;
|
|
469
|
+
}
|
|
470
|
+
let mut shadowed = shadowed || lexical_node_binds_name(node, name, source);
|
|
471
|
+
if !shadowed && assignment_or_update_changes_name(node, name, source) {
|
|
472
|
+
return true;
|
|
473
|
+
}
|
|
474
|
+
let mut cursor = node.walk();
|
|
475
|
+
for child in node.named_children(&mut cursor) {
|
|
476
|
+
let child_shadowed = shadowed
|
|
477
|
+
|| lexical_child_binds_name_for_following_siblings(node.kind(), child, name, source);
|
|
478
|
+
if lexical_node_changes_name(child, name, child_shadowed, source) {
|
|
479
|
+
return true;
|
|
480
|
+
}
|
|
481
|
+
if lexical_child_binds_name_for_following_siblings(node.kind(), child, name, source) {
|
|
482
|
+
shadowed = true;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
false
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
fn lexical_node_binds_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
489
|
+
catch_clause_binds_name(node, name, source)
|
|
490
|
+
|| (matches!(
|
|
491
|
+
node.kind(),
|
|
492
|
+
"local_variable_declaration"
|
|
493
|
+
| "pattern_variable_declaration"
|
|
494
|
+
| "local_function_declaration"
|
|
495
|
+
) && declaration_binds_name(node, name, source))
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
fn lexical_child_binds_name_for_following_siblings(
|
|
499
|
+
scope_kind: &str,
|
|
500
|
+
child: Node<'_>,
|
|
501
|
+
name: &str,
|
|
502
|
+
source: &str,
|
|
503
|
+
) -> bool {
|
|
504
|
+
if child.kind() == "local_function_declaration"
|
|
505
|
+
&& local_function_name(child, source).as_deref() == Some(name)
|
|
506
|
+
{
|
|
507
|
+
return true;
|
|
508
|
+
}
|
|
509
|
+
if matches!(
|
|
510
|
+
child.kind(),
|
|
511
|
+
"local_variable_declaration" | "pattern_variable_declaration"
|
|
512
|
+
) && declaration_binds_name(child, name, source)
|
|
513
|
+
{
|
|
514
|
+
return true;
|
|
515
|
+
}
|
|
516
|
+
header_binding_child(scope_kind, child) && declaration_binds_name(child, name, source)
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
fn catch_clause_binds_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
520
|
+
node.kind() == "catch_clause"
|
|
521
|
+
&& (field_text(node, "exception", source).as_deref() == Some(name)
|
|
522
|
+
|| field_text(node, "stack_trace", source).as_deref() == Some(name))
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
fn header_binding_child(scope_kind: &str, child: Node<'_>) -> bool {
|
|
526
|
+
if matches!(scope_kind, "for_element" | "for_statement") {
|
|
527
|
+
return !is_body_statement_child(child.kind())
|
|
528
|
+
|| child.kind() == "local_variable_declaration";
|
|
529
|
+
}
|
|
530
|
+
!is_body_statement_child(child.kind()) && node_can_contain_pattern_binding(child)
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
fn node_can_contain_pattern_binding(node: Node<'_>) -> bool {
|
|
534
|
+
let mut found = false;
|
|
535
|
+
visit_named(node, &mut |candidate| {
|
|
536
|
+
if !found
|
|
537
|
+
&& matches!(
|
|
538
|
+
candidate.kind(),
|
|
539
|
+
"list_pattern"
|
|
540
|
+
| "local_variable_declaration"
|
|
541
|
+
| "map_pattern"
|
|
542
|
+
| "object_pattern"
|
|
543
|
+
| "pattern_variable_declaration"
|
|
544
|
+
| "record_pattern"
|
|
545
|
+
| "variable_pattern"
|
|
546
|
+
)
|
|
547
|
+
{
|
|
548
|
+
found = true;
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
found
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
fn is_body_statement_child(kind: &str) -> bool {
|
|
555
|
+
matches!(
|
|
556
|
+
kind,
|
|
557
|
+
"assert_statement"
|
|
558
|
+
| "block"
|
|
559
|
+
| "break_statement"
|
|
560
|
+
| "continue_statement"
|
|
561
|
+
| "declaration"
|
|
562
|
+
| "do_statement"
|
|
563
|
+
| "empty_statement"
|
|
564
|
+
| "expression_statement"
|
|
565
|
+
| "for_statement"
|
|
566
|
+
| "if_statement"
|
|
567
|
+
| "local_function_declaration"
|
|
568
|
+
| "local_variable_declaration"
|
|
569
|
+
| "return_statement"
|
|
570
|
+
| "switch_statement"
|
|
571
|
+
| "try_statement"
|
|
572
|
+
| "while_statement"
|
|
573
|
+
| "yield_statement"
|
|
574
|
+
)
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
fn assignment_or_update_changes_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
578
|
+
let assigned = match node.kind() {
|
|
579
|
+
"assignment_expression" => node
|
|
580
|
+
.child_by_field_name("left")
|
|
581
|
+
.and_then(|left| assigned_target_from_assignable(left, source)),
|
|
582
|
+
"postfix_expression" => node
|
|
583
|
+
.child_by_field_name("argument")
|
|
584
|
+
.and_then(|argument| assigned_target_from_assignable(argument, source)),
|
|
585
|
+
"unary_expression" if unary_update_expression(node, source) => {
|
|
586
|
+
direct_named_child(node, "assignable_expression")
|
|
587
|
+
.and_then(|argument| assigned_target_from_assignable(argument, source))
|
|
588
|
+
}
|
|
589
|
+
_ => None,
|
|
590
|
+
};
|
|
591
|
+
assigned.is_some_and(
|
|
592
|
+
|assigned| matches!(assigned, AssignedTarget::Name(assigned) if assigned == name),
|
|
593
|
+
)
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
fn unary_update_expression(node: Node<'_>, source: &str) -> bool {
|
|
597
|
+
node.utf8_text(source.as_bytes())
|
|
598
|
+
.ok()
|
|
599
|
+
.map(str::trim_start)
|
|
600
|
+
.is_some_and(|text| text.starts_with("++") || text.starts_with("--"))
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
enum AssignedTarget {
|
|
604
|
+
Name(String),
|
|
605
|
+
ThisMember,
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
fn assigned_target_from_assignable(node: Node<'_>, source: &str) -> Option<AssignedTarget> {
|
|
609
|
+
let text = node
|
|
610
|
+
.utf8_text(source.as_bytes())
|
|
611
|
+
.ok()
|
|
612
|
+
.map(strip_whitespace)?;
|
|
613
|
+
if is_identifier_text(&text) {
|
|
614
|
+
return Some(AssignedTarget::Name(text));
|
|
615
|
+
}
|
|
616
|
+
let object = node
|
|
617
|
+
.child_by_field_name("object")?
|
|
618
|
+
.utf8_text(source.as_bytes())
|
|
619
|
+
.ok()
|
|
620
|
+
.map(strip_whitespace)?;
|
|
621
|
+
(object == "this").then_some(AssignedTarget::ThisMember)
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
fn collect_root_aliases_from_declaration_shape(
|
|
625
|
+
node: Node<'_>,
|
|
626
|
+
roots: &mut BTreeSet<String>,
|
|
627
|
+
source: &str,
|
|
628
|
+
) {
|
|
629
|
+
let shadowed_roots = roots
|
|
630
|
+
.iter()
|
|
631
|
+
.filter(|root| {
|
|
632
|
+
binding_name(node, source).as_deref() == Some(root.as_str())
|
|
633
|
+
|| declaration_binds_name(node, root, source)
|
|
634
|
+
})
|
|
635
|
+
.cloned()
|
|
636
|
+
.collect::<Vec<_>>();
|
|
637
|
+
for name in shadowed_roots {
|
|
638
|
+
roots.remove(&name);
|
|
639
|
+
}
|
|
640
|
+
if let Some(alias) = local_alias_for_root(node, roots, source) {
|
|
641
|
+
roots.insert(alias);
|
|
642
|
+
}
|
|
643
|
+
let mut cursor = node.walk();
|
|
644
|
+
for child in node.named_children(&mut cursor) {
|
|
645
|
+
if matches!(
|
|
646
|
+
child.kind(),
|
|
647
|
+
"initialized_identifier"
|
|
648
|
+
| "initialized_identifier_list"
|
|
649
|
+
| "initialized_variable_definition"
|
|
650
|
+
| "variable_declaration_list"
|
|
651
|
+
) {
|
|
652
|
+
collect_root_aliases_from_declaration_shape(child, roots, source);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
pub(super) fn declaration_binds_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
658
|
+
if binding_name(node, source).as_deref() == Some(name) {
|
|
659
|
+
return true;
|
|
660
|
+
}
|
|
661
|
+
if node.kind() == "pattern_variable_declaration" {
|
|
662
|
+
return pattern_variable_declaration_binds_name(node, name, source);
|
|
663
|
+
}
|
|
664
|
+
if !matches!(
|
|
665
|
+
node.kind(),
|
|
666
|
+
"local_variable_declaration"
|
|
667
|
+
| "initialized_identifier_list"
|
|
668
|
+
| "initialized_variable_definition"
|
|
669
|
+
| "initialized_identifier"
|
|
670
|
+
| "variable_declaration_list"
|
|
671
|
+
) {
|
|
672
|
+
return false;
|
|
673
|
+
}
|
|
674
|
+
let mut cursor = node.walk();
|
|
675
|
+
node.named_children(&mut cursor)
|
|
676
|
+
.any(|child| declaration_binds_name(child, name, source))
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
fn pattern_variable_declaration_binds_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
680
|
+
let equals_byte = source
|
|
681
|
+
.get(node.start_byte()..node.end_byte())
|
|
682
|
+
.and_then(|text| text.find('='))
|
|
683
|
+
.map_or(node.end_byte(), |offset| node.start_byte() + offset);
|
|
684
|
+
let mut cursor = node.walk();
|
|
685
|
+
node.named_children(&mut cursor).any(|child| {
|
|
686
|
+
child.end_byte() <= equals_byte
|
|
687
|
+
&& is_pattern_node(child)
|
|
688
|
+
&& pattern_binds_name(child, name, source)
|
|
689
|
+
})
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
fn local_alias_for_root(node: Node<'_>, roots: &BTreeSet<String>, source: &str) -> Option<String> {
|
|
693
|
+
if !matches!(
|
|
694
|
+
node.kind(),
|
|
695
|
+
"initialized_identifier" | "initialized_variable_definition"
|
|
696
|
+
) {
|
|
697
|
+
return None;
|
|
698
|
+
}
|
|
699
|
+
let text = node.utf8_text(source.as_bytes()).ok()?;
|
|
700
|
+
let (_, right) = text.split_once('=')?;
|
|
701
|
+
if !expression_matches_roots(right, roots) {
|
|
702
|
+
return None;
|
|
703
|
+
}
|
|
704
|
+
field_text(node, "name", source).or_else(|| identifier_before_equals(text))
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
fn object_pattern_matches_roots(pattern: Node<'_>, roots: &BTreeSet<String>, source: &str) -> bool {
|
|
708
|
+
let mut parent = pattern.parent();
|
|
709
|
+
while let Some(ancestor) = parent {
|
|
710
|
+
match ancestor.kind() {
|
|
711
|
+
"pattern_assignment" | "pattern_variable_declaration" => {
|
|
712
|
+
return pattern_variable_expression_matches(pattern, ancestor, roots, source);
|
|
713
|
+
}
|
|
714
|
+
"if_element" | "if_statement" => {
|
|
715
|
+
return if_case_expression_matches(pattern, ancestor, roots, source);
|
|
716
|
+
}
|
|
717
|
+
"switch_expression" | "switch_statement" => {
|
|
718
|
+
return switch_expression_matches(pattern, ancestor, roots, source);
|
|
719
|
+
}
|
|
720
|
+
"class_body" | "class_declaration" => return false,
|
|
721
|
+
_ => {}
|
|
722
|
+
}
|
|
723
|
+
parent = ancestor.parent();
|
|
724
|
+
}
|
|
725
|
+
false
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
fn pattern_variable_expression_matches(
|
|
729
|
+
pattern: Node<'_>,
|
|
730
|
+
owner: Node<'_>,
|
|
731
|
+
roots: &BTreeSet<String>,
|
|
732
|
+
source: &str,
|
|
733
|
+
) -> bool {
|
|
734
|
+
pattern_variable_expression_for_pattern(pattern, owner, source)
|
|
735
|
+
.is_some_and(|expression| expression_matches_roots(&expression, roots))
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
fn pattern_variable_expression_for_pattern(
|
|
739
|
+
pattern: Node<'_>,
|
|
740
|
+
owner: Node<'_>,
|
|
741
|
+
source: &str,
|
|
742
|
+
) -> Option<String> {
|
|
743
|
+
let owner_text = source.get(owner.start_byte()..owner.end_byte())?;
|
|
744
|
+
let assignment_byte = owner.start_byte() + find_top_level_assignment(owner_text)?;
|
|
745
|
+
if pattern.end_byte() > assignment_byte {
|
|
746
|
+
return None;
|
|
747
|
+
}
|
|
748
|
+
let right = source.get(assignment_byte + 1..owner.end_byte())?.trim();
|
|
749
|
+
nested_expression_for_pattern(pattern, top_pattern_in_owner(pattern, owner), right, source)
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
fn top_pattern_in_owner<'tree>(pattern: Node<'tree>, owner: Node<'tree>) -> Node<'tree> {
|
|
753
|
+
let mut top = pattern;
|
|
754
|
+
let mut parent = pattern.parent();
|
|
755
|
+
while let Some(ancestor) = parent {
|
|
756
|
+
if same_node(ancestor, owner) {
|
|
757
|
+
break;
|
|
758
|
+
}
|
|
759
|
+
if is_pattern_node(ancestor) {
|
|
760
|
+
top = ancestor;
|
|
761
|
+
}
|
|
762
|
+
parent = ancestor.parent();
|
|
763
|
+
}
|
|
764
|
+
top
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
fn find_top_level_assignment(text: &str) -> Option<usize> {
|
|
768
|
+
let mut angle_depth = 0usize;
|
|
769
|
+
let mut grouping_depth = 0usize;
|
|
770
|
+
let mut string = DartStringState::default();
|
|
771
|
+
for (index, character) in text.char_indices() {
|
|
772
|
+
if string.consumes(text, index, character) {
|
|
773
|
+
continue;
|
|
774
|
+
}
|
|
775
|
+
match character {
|
|
776
|
+
'<' if angle_depth > 0 || opens_type_argument_list(text, index) => angle_depth += 1,
|
|
777
|
+
'>' if angle_depth > 0 => angle_depth = angle_depth.saturating_sub(1),
|
|
778
|
+
'(' | '[' | '{' => grouping_depth += 1,
|
|
779
|
+
')' | ']' | '}' => grouping_depth = grouping_depth.saturating_sub(1),
|
|
780
|
+
'=' if angle_depth == 0
|
|
781
|
+
&& grouping_depth == 0
|
|
782
|
+
&& !text[..index]
|
|
783
|
+
.chars()
|
|
784
|
+
.next_back()
|
|
785
|
+
.is_some_and(|previous| matches!(previous, '!' | '<' | '>' | '='))
|
|
786
|
+
&& !text[index + character.len_utf8()..]
|
|
787
|
+
.chars()
|
|
788
|
+
.next()
|
|
789
|
+
.is_some_and(|next| matches!(next, '=' | '>')) =>
|
|
790
|
+
{
|
|
791
|
+
return Some(index);
|
|
792
|
+
}
|
|
793
|
+
_ => {}
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
None
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
fn nested_expression_for_pattern(
|
|
800
|
+
pattern: Node<'_>,
|
|
801
|
+
top_pattern: Node<'_>,
|
|
802
|
+
expression: &str,
|
|
803
|
+
source: &str,
|
|
804
|
+
) -> Option<String> {
|
|
805
|
+
if same_node(pattern, top_pattern) {
|
|
806
|
+
return Some(expression.to_owned());
|
|
807
|
+
}
|
|
808
|
+
let mut path = Vec::new();
|
|
809
|
+
let mut child = pattern;
|
|
810
|
+
let mut parent = pattern.parent();
|
|
811
|
+
while let Some(container) = parent {
|
|
812
|
+
if is_pattern_node(container) {
|
|
813
|
+
path.push((container, child));
|
|
814
|
+
child = container;
|
|
815
|
+
if same_node(container, top_pattern) {
|
|
816
|
+
break;
|
|
817
|
+
}
|
|
818
|
+
} else {
|
|
819
|
+
child = container;
|
|
820
|
+
}
|
|
821
|
+
parent = container.parent();
|
|
822
|
+
}
|
|
823
|
+
if path
|
|
824
|
+
.last()
|
|
825
|
+
.is_none_or(|(container, _)| !same_node(*container, top_pattern))
|
|
826
|
+
{
|
|
827
|
+
return None;
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
let mut current = expression.to_owned();
|
|
831
|
+
for (container, child) in path.into_iter().rev() {
|
|
832
|
+
if same_node(container, child) || shorthand_pattern_wrapper(container.kind()) {
|
|
833
|
+
continue;
|
|
834
|
+
}
|
|
835
|
+
current = child_expression_for_nested_pattern(container, child, ¤t, source)?;
|
|
836
|
+
}
|
|
837
|
+
Some(current)
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
fn child_expression_for_nested_pattern(
|
|
841
|
+
container: Node<'_>,
|
|
842
|
+
child: Node<'_>,
|
|
843
|
+
expression: &str,
|
|
844
|
+
source: &str,
|
|
845
|
+
) -> Option<String> {
|
|
846
|
+
match container.kind() {
|
|
847
|
+
"record_pattern" => {
|
|
848
|
+
delimited_child_expression(container, child, expression, source, '(', ')')
|
|
849
|
+
}
|
|
850
|
+
"list_pattern" => {
|
|
851
|
+
delimited_child_expression(container, child, expression, source, '[', ']')
|
|
852
|
+
}
|
|
853
|
+
_ => None,
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
fn delimited_child_expression(
|
|
858
|
+
container: Node<'_>,
|
|
859
|
+
child: Node<'_>,
|
|
860
|
+
expression: &str,
|
|
861
|
+
source: &str,
|
|
862
|
+
open: char,
|
|
863
|
+
close: char,
|
|
864
|
+
) -> Option<String> {
|
|
865
|
+
let container_text = source.get(container.start_byte()..container.end_byte())?;
|
|
866
|
+
let (pattern_inner_offset, pattern_inner) =
|
|
867
|
+
enclosed_inner_with_offset(container_text, open, close)?;
|
|
868
|
+
let pattern_parts = split_top_level_ranges(pattern_inner);
|
|
869
|
+
let child_start = child
|
|
870
|
+
.start_byte()
|
|
871
|
+
.checked_sub(container.start_byte() + pattern_inner_offset)?;
|
|
872
|
+
let child_end = child
|
|
873
|
+
.end_byte()
|
|
874
|
+
.checked_sub(container.start_byte() + pattern_inner_offset)?;
|
|
875
|
+
let pattern_index = pattern_parts
|
|
876
|
+
.iter()
|
|
877
|
+
.position(|(start, end)| child_start >= *start && child_end <= *end)?;
|
|
878
|
+
let pattern_part = pattern_inner
|
|
879
|
+
.get(pattern_parts[pattern_index].0..pattern_parts[pattern_index].1)?
|
|
880
|
+
.trim();
|
|
881
|
+
|
|
882
|
+
let (_, expression_inner) = enclosed_inner_with_offset(expression, open, close)?;
|
|
883
|
+
let expression_parts = split_top_level_ranges(expression_inner);
|
|
884
|
+
let expression_part = expression_part_matching_pattern(
|
|
885
|
+
pattern_part,
|
|
886
|
+
expression_inner,
|
|
887
|
+
&expression_parts,
|
|
888
|
+
pattern_index,
|
|
889
|
+
)?;
|
|
890
|
+
Some(
|
|
891
|
+
expression_part
|
|
892
|
+
.trim()
|
|
893
|
+
.trim_end_matches(';')
|
|
894
|
+
.trim()
|
|
895
|
+
.to_owned(),
|
|
896
|
+
)
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
fn expression_part_matching_pattern(
|
|
900
|
+
pattern_part: &str,
|
|
901
|
+
expression_inner: &str,
|
|
902
|
+
expression_parts: &[(usize, usize)],
|
|
903
|
+
pattern_index: usize,
|
|
904
|
+
) -> Option<String> {
|
|
905
|
+
if let Some((pattern_label, _)) = top_level_label_value(pattern_part) {
|
|
906
|
+
return expression_parts.iter().find_map(|(start, end)| {
|
|
907
|
+
let part = expression_inner.get(*start..*end)?.trim();
|
|
908
|
+
let (label, value) = top_level_label_value(part)?;
|
|
909
|
+
(label == pattern_label).then(|| value.to_owned())
|
|
910
|
+
});
|
|
911
|
+
}
|
|
912
|
+
let (start, end) = *expression_parts.get(pattern_index)?;
|
|
913
|
+
let part = expression_inner.get(start..end)?.trim();
|
|
914
|
+
top_level_label_value(part)
|
|
915
|
+
.is_none()
|
|
916
|
+
.then(|| part.to_owned())
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
fn enclosed_inner_with_offset(text: &str, open: char, close: char) -> Option<(usize, &str)> {
|
|
920
|
+
let leading = text.len() - text.trim_start().len();
|
|
921
|
+
let trimmed = text.trim();
|
|
922
|
+
let prefix = collection_literal_prefix_len(trimmed, open)?;
|
|
923
|
+
let literal = trimmed.get(prefix..)?;
|
|
924
|
+
if !literal.starts_with(open) {
|
|
925
|
+
return None;
|
|
926
|
+
}
|
|
927
|
+
let end = matching_enclosed_end(literal, open, close)?;
|
|
928
|
+
if prefix + end + close.len_utf8() != trimmed.len() {
|
|
929
|
+
return None;
|
|
930
|
+
}
|
|
931
|
+
Some((
|
|
932
|
+
leading + prefix + open.len_utf8(),
|
|
933
|
+
literal.get(open.len_utf8()..end)?,
|
|
934
|
+
))
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
fn collection_literal_prefix_len(text: &str, open: char) -> Option<usize> {
|
|
938
|
+
if open != '[' {
|
|
939
|
+
return Some(0);
|
|
940
|
+
}
|
|
941
|
+
let mut offset = 0usize;
|
|
942
|
+
let mut rest = text;
|
|
943
|
+
if let Some((keyword_offset, after_keyword)) = strip_keyword_prefix(rest, "const") {
|
|
944
|
+
offset += keyword_offset;
|
|
945
|
+
rest = after_keyword;
|
|
946
|
+
}
|
|
947
|
+
if rest.starts_with('<') {
|
|
948
|
+
let end = matching_enclosed_end(rest, '<', '>')?;
|
|
949
|
+
let after_type = rest.get(end + '>'.len_utf8()..)?;
|
|
950
|
+
let whitespace = after_type.len() - after_type.trim_start().len();
|
|
951
|
+
offset += end + '>'.len_utf8() + whitespace;
|
|
952
|
+
}
|
|
953
|
+
Some(offset)
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
fn strip_keyword_prefix<'source>(
|
|
957
|
+
text: &'source str,
|
|
958
|
+
keyword: &str,
|
|
959
|
+
) -> Option<(usize, &'source str)> {
|
|
960
|
+
let rest = text.strip_prefix(keyword)?;
|
|
961
|
+
let whitespace = rest.len() - rest.trim_start().len();
|
|
962
|
+
if whitespace == 0 {
|
|
963
|
+
return None;
|
|
964
|
+
}
|
|
965
|
+
Some((keyword.len() + whitespace, rest.trim_start()))
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
fn split_top_level_ranges(text: &str) -> Vec<(usize, usize)> {
|
|
969
|
+
let mut ranges = Vec::new();
|
|
970
|
+
let mut start = 0usize;
|
|
971
|
+
let mut angle_depth = 0usize;
|
|
972
|
+
let mut grouping_depth = 0usize;
|
|
973
|
+
let mut string = DartStringState::default();
|
|
974
|
+
for (index, character) in text.char_indices() {
|
|
975
|
+
if string.consumes(text, index, character) {
|
|
976
|
+
continue;
|
|
977
|
+
}
|
|
978
|
+
match character {
|
|
979
|
+
'<' if angle_depth > 0 || opens_type_argument_list(text, index) => angle_depth += 1,
|
|
980
|
+
'>' if angle_depth > 0 => angle_depth = angle_depth.saturating_sub(1),
|
|
981
|
+
'(' | '[' | '{' => grouping_depth += 1,
|
|
982
|
+
')' | ']' | '}' => grouping_depth = grouping_depth.saturating_sub(1),
|
|
983
|
+
',' if angle_depth == 0 && grouping_depth == 0 => {
|
|
984
|
+
ranges.push((start, index));
|
|
985
|
+
start = index + character.len_utf8();
|
|
986
|
+
}
|
|
987
|
+
_ => {}
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
if start < text.len() {
|
|
991
|
+
ranges.push((start, text.len()));
|
|
992
|
+
}
|
|
993
|
+
ranges
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
fn top_level_label_value(text: &str) -> Option<(&str, &str)> {
|
|
997
|
+
let mut angle_depth = 0usize;
|
|
998
|
+
let mut grouping_depth = 0usize;
|
|
999
|
+
let mut string = DartStringState::default();
|
|
1000
|
+
for (index, character) in text.char_indices() {
|
|
1001
|
+
if string.consumes(text, index, character) {
|
|
1002
|
+
continue;
|
|
1003
|
+
}
|
|
1004
|
+
match character {
|
|
1005
|
+
'<' if angle_depth > 0 || opens_type_argument_list(text, index) => angle_depth += 1,
|
|
1006
|
+
'>' if angle_depth > 0 => angle_depth = angle_depth.saturating_sub(1),
|
|
1007
|
+
'(' | '[' | '{' => grouping_depth += 1,
|
|
1008
|
+
')' | ']' | '}' => grouping_depth = grouping_depth.saturating_sub(1),
|
|
1009
|
+
':' if angle_depth == 0 && grouping_depth == 0 => {
|
|
1010
|
+
let label = text[..index].trim();
|
|
1011
|
+
if is_identifier_text(label) {
|
|
1012
|
+
return Some((label, text[index + character.len_utf8()..].trim()));
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
_ => {}
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
None
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
fn matching_enclosed_end(text: &str, open: char, close: char) -> Option<usize> {
|
|
1022
|
+
let mut chars = text.char_indices();
|
|
1023
|
+
if chars.next().is_none_or(|(_, character)| character != open) {
|
|
1024
|
+
return None;
|
|
1025
|
+
}
|
|
1026
|
+
let mut depth = 0usize;
|
|
1027
|
+
let mut string = DartStringState::default();
|
|
1028
|
+
for (index, character) in text.char_indices() {
|
|
1029
|
+
if string.consumes(text, index, character) {
|
|
1030
|
+
continue;
|
|
1031
|
+
}
|
|
1032
|
+
if character == open {
|
|
1033
|
+
depth += 1;
|
|
1034
|
+
} else if character == close {
|
|
1035
|
+
depth = depth.checked_sub(1)?;
|
|
1036
|
+
if depth == 0 {
|
|
1037
|
+
return Some(index);
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
None
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
fn opens_type_argument_list(text: &str, index: usize) -> bool {
|
|
1045
|
+
let Some(relative_end) = matching_enclosed_end(text.get(index..).unwrap_or(""), '<', '>')
|
|
1046
|
+
else {
|
|
1047
|
+
return false;
|
|
1048
|
+
};
|
|
1049
|
+
let close = index + relative_end;
|
|
1050
|
+
let Some(inner) = text.get(index + 1..close) else {
|
|
1051
|
+
return false;
|
|
1052
|
+
};
|
|
1053
|
+
let Some(previous) = previous_identifier_segment(text, index) else {
|
|
1054
|
+
return false;
|
|
1055
|
+
};
|
|
1056
|
+
if !type_argument_parts_are_plausible(inner) {
|
|
1057
|
+
return false;
|
|
1058
|
+
}
|
|
1059
|
+
let after = text
|
|
1060
|
+
.get(close + '>'.len_utf8()..)
|
|
1061
|
+
.unwrap_or("")
|
|
1062
|
+
.trim_start()
|
|
1063
|
+
.chars()
|
|
1064
|
+
.next();
|
|
1065
|
+
after == Some('(') || type_identifier_is_plausible(previous)
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
fn previous_identifier_segment(text: &str, index: usize) -> Option<&str> {
|
|
1069
|
+
let before = text.get(..index)?.trim_end();
|
|
1070
|
+
let end = before.len();
|
|
1071
|
+
let start = before
|
|
1072
|
+
.char_indices()
|
|
1073
|
+
.rev()
|
|
1074
|
+
.find(|(_, character)| !is_identifier_character(*character) && *character != '.')
|
|
1075
|
+
.map_or(0, |(index, character)| index + character.len_utf8());
|
|
1076
|
+
let identifier = before.get(start..end)?.trim();
|
|
1077
|
+
if identifier.is_empty() {
|
|
1078
|
+
None
|
|
1079
|
+
} else {
|
|
1080
|
+
Some(identifier.rsplit('.').next().unwrap_or(identifier))
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
fn type_argument_parts_are_plausible(text: &str) -> bool {
|
|
1085
|
+
let parts = split_type_argument_parts(text);
|
|
1086
|
+
!parts.is_empty() && parts.into_iter().all(type_argument_part_is_plausible)
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
fn split_type_argument_parts(text: &str) -> Vec<&str> {
|
|
1090
|
+
let mut parts = Vec::new();
|
|
1091
|
+
let mut start = 0usize;
|
|
1092
|
+
let mut angle_depth = 0usize;
|
|
1093
|
+
let mut grouping_depth = 0usize;
|
|
1094
|
+
let mut string = DartStringState::default();
|
|
1095
|
+
for (index, character) in text.char_indices() {
|
|
1096
|
+
if string.consumes(text, index, character) {
|
|
1097
|
+
continue;
|
|
1098
|
+
}
|
|
1099
|
+
match character {
|
|
1100
|
+
'<' => angle_depth += 1,
|
|
1101
|
+
'>' if angle_depth > 0 => angle_depth = angle_depth.saturating_sub(1),
|
|
1102
|
+
'(' | '[' | '{' => grouping_depth += 1,
|
|
1103
|
+
')' | ']' | '}' => grouping_depth = grouping_depth.saturating_sub(1),
|
|
1104
|
+
',' if angle_depth == 0 && grouping_depth == 0 => {
|
|
1105
|
+
parts.push(text[start..index].trim());
|
|
1106
|
+
start = index + character.len_utf8();
|
|
1107
|
+
}
|
|
1108
|
+
_ => {}
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
let last = text[start..].trim();
|
|
1112
|
+
if !last.is_empty() {
|
|
1113
|
+
parts.push(last);
|
|
1114
|
+
}
|
|
1115
|
+
parts
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
fn type_argument_part_is_plausible(text: &str) -> bool {
|
|
1119
|
+
let trimmed = text.trim();
|
|
1120
|
+
let candidate = trimmed
|
|
1121
|
+
.split_whitespace()
|
|
1122
|
+
.next()
|
|
1123
|
+
.unwrap_or(trimmed)
|
|
1124
|
+
.trim_end_matches('?');
|
|
1125
|
+
let base = candidate.split('<').next().unwrap_or(candidate);
|
|
1126
|
+
let segment = base.rsplit('.').next().unwrap_or(base);
|
|
1127
|
+
type_identifier_is_plausible(segment)
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
fn type_identifier_is_plausible(text: &str) -> bool {
|
|
1131
|
+
let Some(first) = text.chars().next() else {
|
|
1132
|
+
return false;
|
|
1133
|
+
};
|
|
1134
|
+
first.is_ascii_uppercase()
|
|
1135
|
+
|| matches!(
|
|
1136
|
+
text,
|
|
1137
|
+
"bool"
|
|
1138
|
+
| "double"
|
|
1139
|
+
| "dynamic"
|
|
1140
|
+
| "int"
|
|
1141
|
+
| "num"
|
|
1142
|
+
| "void"
|
|
1143
|
+
| "Object"
|
|
1144
|
+
| "Never"
|
|
1145
|
+
| "Null"
|
|
1146
|
+
| "Function"
|
|
1147
|
+
| "FutureOr"
|
|
1148
|
+
)
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
#[derive(Default)]
|
|
1152
|
+
struct DartStringState {
|
|
1153
|
+
quote: Option<char>,
|
|
1154
|
+
triple: bool,
|
|
1155
|
+
raw: bool,
|
|
1156
|
+
escaped: bool,
|
|
1157
|
+
skip_until: usize,
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
impl DartStringState {
|
|
1161
|
+
fn consumes(&mut self, text: &str, index: usize, character: char) -> bool {
|
|
1162
|
+
if index < self.skip_until {
|
|
1163
|
+
return true;
|
|
1164
|
+
}
|
|
1165
|
+
if let Some(quote) = self.quote {
|
|
1166
|
+
if !self.raw && self.escaped {
|
|
1167
|
+
self.escaped = false;
|
|
1168
|
+
return true;
|
|
1169
|
+
}
|
|
1170
|
+
if !self.raw && character == '\\' {
|
|
1171
|
+
self.escaped = true;
|
|
1172
|
+
return true;
|
|
1173
|
+
}
|
|
1174
|
+
if character == quote {
|
|
1175
|
+
if self.triple {
|
|
1176
|
+
if starts_with_triple_quote(text, index, quote) {
|
|
1177
|
+
self.clear();
|
|
1178
|
+
self.skip_until = index + quote.len_utf8() * 3;
|
|
1179
|
+
}
|
|
1180
|
+
} else {
|
|
1181
|
+
self.clear();
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
return true;
|
|
1185
|
+
}
|
|
1186
|
+
if matches!(character, '\'' | '"') {
|
|
1187
|
+
self.quote = Some(character);
|
|
1188
|
+
self.triple = starts_with_triple_quote(text, index, character);
|
|
1189
|
+
self.raw = dart_string_has_raw_prefix(text, index);
|
|
1190
|
+
self.skip_until = if self.triple {
|
|
1191
|
+
index + character.len_utf8() * 3
|
|
1192
|
+
} else {
|
|
1193
|
+
index + character.len_utf8()
|
|
1194
|
+
};
|
|
1195
|
+
return true;
|
|
1196
|
+
}
|
|
1197
|
+
false
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
fn clear(&mut self) {
|
|
1201
|
+
self.quote = None;
|
|
1202
|
+
self.triple = false;
|
|
1203
|
+
self.raw = false;
|
|
1204
|
+
self.escaped = false;
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
fn starts_with_triple_quote(text: &str, index: usize, quote: char) -> bool {
|
|
1209
|
+
let mut chars = text[index..].chars();
|
|
1210
|
+
chars.next() == Some(quote) && chars.next() == Some(quote) && chars.next() == Some(quote)
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
fn dart_string_has_raw_prefix(text: &str, quote_index: usize) -> bool {
|
|
1214
|
+
let Some((raw_index, raw)) = text[..quote_index].char_indices().next_back() else {
|
|
1215
|
+
return false;
|
|
1216
|
+
};
|
|
1217
|
+
matches!(raw, 'r' | 'R') && !identifier_continues_before(text, raw_index)
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
fn if_case_expression_matches(
|
|
1221
|
+
pattern: Node<'_>,
|
|
1222
|
+
owner: Node<'_>,
|
|
1223
|
+
roots: &BTreeSet<String>,
|
|
1224
|
+
source: &str,
|
|
1225
|
+
) -> bool {
|
|
1226
|
+
let Some(before_pattern) = source.get(owner.start_byte()..pattern.start_byte()) else {
|
|
1227
|
+
return false;
|
|
1228
|
+
};
|
|
1229
|
+
let Some(case_index) = find_keyword(before_pattern, "case") else {
|
|
1230
|
+
return false;
|
|
1231
|
+
};
|
|
1232
|
+
let before_case = &before_pattern[..case_index];
|
|
1233
|
+
let Some(expression) = expression_after_if_keyword(before_case) else {
|
|
1234
|
+
return false;
|
|
1235
|
+
};
|
|
1236
|
+
case_pattern_expression_matches(pattern, owner, expression, roots, source)
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
fn expression_after_if_keyword(text: &str) -> Option<&str> {
|
|
1240
|
+
let keyword = find_keyword(text, "if")?;
|
|
1241
|
+
let after_keyword = text.get(keyword + "if".len()..)?.trim_start();
|
|
1242
|
+
Some(
|
|
1243
|
+
after_keyword
|
|
1244
|
+
.strip_prefix('(')
|
|
1245
|
+
.unwrap_or(after_keyword)
|
|
1246
|
+
.trim(),
|
|
1247
|
+
)
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
fn switch_expression_matches(
|
|
1251
|
+
pattern: Node<'_>,
|
|
1252
|
+
node: Node<'_>,
|
|
1253
|
+
roots: &BTreeSet<String>,
|
|
1254
|
+
source: &str,
|
|
1255
|
+
) -> bool {
|
|
1256
|
+
let Some(text) = node.utf8_text(source.as_bytes()).ok() else {
|
|
1257
|
+
return false;
|
|
1258
|
+
};
|
|
1259
|
+
let Some(expression) = parenthesized_after_keyword(text, "switch") else {
|
|
1260
|
+
return false;
|
|
1261
|
+
};
|
|
1262
|
+
case_pattern_expression_matches(pattern, node, expression, roots, source)
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
fn case_pattern_expression_matches(
|
|
1266
|
+
pattern: Node<'_>,
|
|
1267
|
+
owner: Node<'_>,
|
|
1268
|
+
expression: &str,
|
|
1269
|
+
roots: &BTreeSet<String>,
|
|
1270
|
+
source: &str,
|
|
1271
|
+
) -> bool {
|
|
1272
|
+
nested_expression_for_pattern(
|
|
1273
|
+
pattern,
|
|
1274
|
+
top_pattern_in_owner(pattern, owner),
|
|
1275
|
+
expression,
|
|
1276
|
+
source,
|
|
1277
|
+
)
|
|
1278
|
+
.is_some_and(|expression| expression_matches_roots(&expression, roots))
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
fn expression_matches_roots(expression: &str, roots: &BTreeSet<String>) -> bool {
|
|
1282
|
+
let expression = normalized_expression(expression);
|
|
1283
|
+
roots.contains(&expression)
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
fn normalized_expression(expression: &str) -> String {
|
|
1287
|
+
let mut expression = expression
|
|
1288
|
+
.split([';', ',', '}'])
|
|
1289
|
+
.next()
|
|
1290
|
+
.unwrap_or(expression)
|
|
1291
|
+
.trim()
|
|
1292
|
+
.trim_end_matches(';')
|
|
1293
|
+
.trim()
|
|
1294
|
+
.to_owned();
|
|
1295
|
+
while expression.starts_with('(') && expression.ends_with(')') {
|
|
1296
|
+
expression = expression[1..expression.len() - 1].trim().to_owned();
|
|
1297
|
+
}
|
|
1298
|
+
strip_whitespace(&expression)
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
fn parenthesized_after_keyword<'source>(text: &'source str, keyword: &str) -> Option<&'source str> {
|
|
1302
|
+
let keyword_index = text.find(keyword)?;
|
|
1303
|
+
let open = text[keyword_index + keyword.len()..].find('(')? + keyword_index + keyword.len();
|
|
1304
|
+
let mut depth = 0usize;
|
|
1305
|
+
for (offset, character) in text[open..].char_indices() {
|
|
1306
|
+
match character {
|
|
1307
|
+
'(' => depth += 1,
|
|
1308
|
+
')' => {
|
|
1309
|
+
depth = depth.saturating_sub(1);
|
|
1310
|
+
if depth == 0 {
|
|
1311
|
+
return text.get(open + 1..open + offset);
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
_ => {}
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
None
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
fn body_calls_helper_with_roots(
|
|
1321
|
+
body: Node<'_>,
|
|
1322
|
+
helper_name: &HelperName,
|
|
1323
|
+
parameter: &HelperParameter,
|
|
1324
|
+
root_names: &[&str],
|
|
1325
|
+
source: &str,
|
|
1326
|
+
) -> bool {
|
|
1327
|
+
let mut found = false;
|
|
1328
|
+
visit_named(body, &mut |node| {
|
|
1329
|
+
if found {
|
|
1330
|
+
return;
|
|
1331
|
+
}
|
|
1332
|
+
let Some(invocation_name) = invocation_name(node, source) else {
|
|
1333
|
+
return;
|
|
1334
|
+
};
|
|
1335
|
+
if !helper_name_matches(helper_name, &invocation_name, node, body, source) {
|
|
1336
|
+
return;
|
|
1337
|
+
}
|
|
1338
|
+
let mut roots = root_aliases_at(body, node, root_names, source);
|
|
1339
|
+
remove_roots_shadowed_by_enclosing_callable(body, node, &mut roots, source);
|
|
1340
|
+
if !roots.is_empty() && invocation_arguments_pass_roots(node, parameter, &roots, source) {
|
|
1341
|
+
found = true;
|
|
1342
|
+
}
|
|
1343
|
+
});
|
|
1344
|
+
found
|
|
1345
|
+
}
|
|
1346
|
+
|
|
1347
|
+
fn helper_name_matches(
|
|
1348
|
+
helper_name: &HelperName,
|
|
1349
|
+
invocation_name: &str,
|
|
1350
|
+
invocation: Node<'_>,
|
|
1351
|
+
body: Node<'_>,
|
|
1352
|
+
source: &str,
|
|
1353
|
+
) -> bool {
|
|
1354
|
+
if let Some((receiver, member)) = this_or_super_member_name(invocation_name) {
|
|
1355
|
+
return member == helper_name.name
|
|
1356
|
+
&& helper_name.method_owner.as_ref().is_some_and(|owner| {
|
|
1357
|
+
current_lexical_class(invocation)
|
|
1358
|
+
.and_then(|current| dispatched_member_owner(current, member, receiver, source))
|
|
1359
|
+
.as_deref()
|
|
1360
|
+
== Some(owner.as_str())
|
|
1361
|
+
});
|
|
1362
|
+
}
|
|
1363
|
+
if invocation_name.contains('.') {
|
|
1364
|
+
return qualified_helper_name_matches(
|
|
1365
|
+
helper_name,
|
|
1366
|
+
invocation_name,
|
|
1367
|
+
invocation,
|
|
1368
|
+
body,
|
|
1369
|
+
source,
|
|
1370
|
+
);
|
|
1371
|
+
}
|
|
1372
|
+
if invocation_name != helper_name.name {
|
|
1373
|
+
return false;
|
|
1374
|
+
}
|
|
1375
|
+
if lexical_local_binding_before(body, invocation, &helper_name.name, source) {
|
|
1376
|
+
return false;
|
|
1377
|
+
}
|
|
1378
|
+
if lexical_header_binding_before(body, invocation, &helper_name.name, source) {
|
|
1379
|
+
return false;
|
|
1380
|
+
}
|
|
1381
|
+
if enclosing_callable_parameter_binds_name(invocation, body, &helper_name.name, source) {
|
|
1382
|
+
return false;
|
|
1383
|
+
}
|
|
1384
|
+
if let Some(owner) = &helper_name.method_owner {
|
|
1385
|
+
return current_lexical_class(invocation)
|
|
1386
|
+
.and_then(|current| {
|
|
1387
|
+
dispatched_member_owner(current, &helper_name.name, MemberReceiver::This, source)
|
|
1388
|
+
})
|
|
1389
|
+
.as_deref()
|
|
1390
|
+
== Some(owner.as_str());
|
|
1391
|
+
}
|
|
1392
|
+
!current_class_member_shadows_top_level(invocation, &helper_name.name, source)
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
fn qualified_helper_name_matches(
|
|
1396
|
+
helper_name: &HelperName,
|
|
1397
|
+
invocation_name: &str,
|
|
1398
|
+
invocation: Node<'_>,
|
|
1399
|
+
body: Node<'_>,
|
|
1400
|
+
source: &str,
|
|
1401
|
+
) -> bool {
|
|
1402
|
+
let Some((qualifier, member)) = invocation_name.rsplit_once('.') else {
|
|
1403
|
+
return false;
|
|
1404
|
+
};
|
|
1405
|
+
if member != helper_name.name || !helper_name.qualified_names.contains(invocation_name) {
|
|
1406
|
+
return false;
|
|
1407
|
+
}
|
|
1408
|
+
if helper_name.method_owner.as_deref() != Some(qualifier) {
|
|
1409
|
+
return false;
|
|
1410
|
+
}
|
|
1411
|
+
if qualified_helper_qualifier_shadowed(body, invocation, qualifier, source) {
|
|
1412
|
+
return false;
|
|
1413
|
+
}
|
|
1414
|
+
find_class_like_declaration(root_node(invocation), qualifier, source).is_some()
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
fn qualified_helper_qualifier_shadowed(
|
|
1418
|
+
body: Node<'_>,
|
|
1419
|
+
invocation: Node<'_>,
|
|
1420
|
+
qualifier: &str,
|
|
1421
|
+
source: &str,
|
|
1422
|
+
) -> bool {
|
|
1423
|
+
top_level_name_shadowed_at(body, invocation, qualifier, source)
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
fn import_alias_exists(root: Node<'_>, alias: &str, source: &str) -> bool {
|
|
1427
|
+
let mut found = false;
|
|
1428
|
+
visit_named(root, &mut |node| {
|
|
1429
|
+
if found || node.kind() != "library_import" {
|
|
1430
|
+
return;
|
|
1431
|
+
}
|
|
1432
|
+
if import_alias(node, source).as_deref() == Some(alias) {
|
|
1433
|
+
found = true;
|
|
1434
|
+
}
|
|
1435
|
+
});
|
|
1436
|
+
found
|
|
1437
|
+
}
|
|
1438
|
+
|
|
1439
|
+
fn lexical_local_binding_before(body: Node<'_>, site: Node<'_>, name: &str, source: &str) -> bool {
|
|
1440
|
+
let mut steps = Vec::new();
|
|
1441
|
+
let mut path_child = site;
|
|
1442
|
+
let mut parent = site.parent();
|
|
1443
|
+
while let Some(scope) = parent {
|
|
1444
|
+
steps.push((scope, path_child));
|
|
1445
|
+
if same_node(scope, body) {
|
|
1446
|
+
break;
|
|
1447
|
+
}
|
|
1448
|
+
path_child = scope;
|
|
1449
|
+
parent = scope.parent();
|
|
1450
|
+
}
|
|
1451
|
+
steps
|
|
1452
|
+
.into_iter()
|
|
1453
|
+
.rev()
|
|
1454
|
+
.any(|(scope, child)| prior_lexical_sibling_binds_name(scope, child, name, source))
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
fn lexical_header_binding_before(body: Node<'_>, site: Node<'_>, name: &str, source: &str) -> bool {
|
|
1458
|
+
let mut steps = Vec::new();
|
|
1459
|
+
let mut path_child = site;
|
|
1460
|
+
let mut parent = site.parent();
|
|
1461
|
+
while let Some(scope) = parent {
|
|
1462
|
+
steps.push((scope, path_child));
|
|
1463
|
+
if same_node(scope, body) {
|
|
1464
|
+
break;
|
|
1465
|
+
}
|
|
1466
|
+
path_child = scope;
|
|
1467
|
+
parent = scope.parent();
|
|
1468
|
+
}
|
|
1469
|
+
steps.into_iter().rev().any(|(scope, child)| {
|
|
1470
|
+
scoped_header_binding_exists(scope, child, site.start_byte(), name, source)
|
|
1471
|
+
})
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
fn enclosing_callable_parameter_binds_name(
|
|
1475
|
+
site: Node<'_>,
|
|
1476
|
+
body: Node<'_>,
|
|
1477
|
+
name: &str,
|
|
1478
|
+
source: &str,
|
|
1479
|
+
) -> bool {
|
|
1480
|
+
let mut parent = site.parent();
|
|
1481
|
+
while let Some(ancestor) = parent {
|
|
1482
|
+
if same_node(ancestor, body) {
|
|
1483
|
+
return false;
|
|
1484
|
+
}
|
|
1485
|
+
if callable_direct_parameters_bind_name(ancestor, name, source) {
|
|
1486
|
+
return true;
|
|
1487
|
+
}
|
|
1488
|
+
parent = ancestor.parent();
|
|
1489
|
+
}
|
|
1490
|
+
false
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
fn prior_lexical_sibling_binds_name(
|
|
1494
|
+
scope: Node<'_>,
|
|
1495
|
+
path_child: Node<'_>,
|
|
1496
|
+
name: &str,
|
|
1497
|
+
source: &str,
|
|
1498
|
+
) -> bool {
|
|
1499
|
+
let mut cursor = scope.walk();
|
|
1500
|
+
for sibling in scope.named_children(&mut cursor) {
|
|
1501
|
+
if same_node(sibling, path_child) || sibling.start_byte() >= path_child.start_byte() {
|
|
1502
|
+
break;
|
|
1503
|
+
}
|
|
1504
|
+
if lexical_sibling_binds_name(sibling, name, source) {
|
|
1505
|
+
return true;
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
false
|
|
1509
|
+
}
|
|
1510
|
+
|
|
1511
|
+
fn lexical_sibling_binds_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
1512
|
+
if node.kind() == "local_function_declaration" {
|
|
1513
|
+
return local_function_name(node, source).as_deref() == Some(name);
|
|
1514
|
+
}
|
|
1515
|
+
matches!(
|
|
1516
|
+
node.kind(),
|
|
1517
|
+
"local_variable_declaration" | "pattern_variable_declaration"
|
|
1518
|
+
) && declaration_binds_name(node, name, source)
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
fn local_function_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
1522
|
+
direct_named_child(node, "function_signature")
|
|
1523
|
+
.and_then(|signature| signature.child_by_field_name("name"))
|
|
1524
|
+
.or_else(|| node.child_by_field_name("name"))
|
|
1525
|
+
.and_then(|name| name.utf8_text(source.as_bytes()).ok())
|
|
1526
|
+
.map(str::to_owned)
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1529
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
1530
|
+
enum MemberReceiver {
|
|
1531
|
+
This,
|
|
1532
|
+
Super,
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
fn this_or_super_member_name(invocation_name: &str) -> Option<(MemberReceiver, &str)> {
|
|
1536
|
+
let (receiver, member) = invocation_name
|
|
1537
|
+
.strip_prefix("this.")
|
|
1538
|
+
.map(|member| (MemberReceiver::This, member))
|
|
1539
|
+
.or_else(|| {
|
|
1540
|
+
invocation_name
|
|
1541
|
+
.strip_prefix("super.")
|
|
1542
|
+
.map(|member| (MemberReceiver::Super, member))
|
|
1543
|
+
})?;
|
|
1544
|
+
(!member.contains('.'))
|
|
1545
|
+
.then_some(member)
|
|
1546
|
+
.map(|member| (receiver, member))
|
|
1547
|
+
}
|
|
1548
|
+
|
|
1549
|
+
fn callable_direct_parameter_bound_names(
|
|
1550
|
+
node: Node<'_>,
|
|
1551
|
+
names: &BTreeSet<String>,
|
|
1552
|
+
source: &str,
|
|
1553
|
+
) -> BTreeSet<String> {
|
|
1554
|
+
if !is_callable_node(node.kind()) {
|
|
1555
|
+
return BTreeSet::new();
|
|
1556
|
+
}
|
|
1557
|
+
direct_parameter_lists(node)
|
|
1558
|
+
.into_iter()
|
|
1559
|
+
.flat_map(|parameters| parameter_list_directly_bound_names(parameters, names, source))
|
|
1560
|
+
.collect()
|
|
1561
|
+
}
|
|
1562
|
+
|
|
1563
|
+
fn callable_direct_parameters_bind_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
1564
|
+
if !is_callable_node(node.kind()) {
|
|
1565
|
+
return false;
|
|
1566
|
+
}
|
|
1567
|
+
direct_parameter_lists(node)
|
|
1568
|
+
.into_iter()
|
|
1569
|
+
.any(|parameters| parameter_list_directly_binds_name(parameters, name, source))
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
fn is_callable_node(kind: &str) -> bool {
|
|
1573
|
+
matches!(
|
|
1574
|
+
kind,
|
|
1575
|
+
"function_declaration"
|
|
1576
|
+
| "function_expression"
|
|
1577
|
+
| "local_function_declaration"
|
|
1578
|
+
| "method_declaration"
|
|
1579
|
+
)
|
|
1580
|
+
}
|
|
1581
|
+
|
|
1582
|
+
fn direct_parameter_lists(node: Node<'_>) -> Vec<Node<'_>> {
|
|
1583
|
+
match node.kind() {
|
|
1584
|
+
"function_expression" => parameter_field_lists(node),
|
|
1585
|
+
"function_declaration" | "local_function_declaration" | "method_declaration" => {
|
|
1586
|
+
helper_signature(node)
|
|
1587
|
+
.and_then(parameter_list)
|
|
1588
|
+
.into_iter()
|
|
1589
|
+
.collect()
|
|
1590
|
+
}
|
|
1591
|
+
_ => Vec::new(),
|
|
1592
|
+
}
|
|
1593
|
+
}
|
|
1594
|
+
|
|
1595
|
+
fn parameter_field_lists(node: Node<'_>) -> Vec<Node<'_>> {
|
|
1596
|
+
let mut cursor = node.walk();
|
|
1597
|
+
node.children_by_field_name("parameters", &mut cursor)
|
|
1598
|
+
.filter_map(formal_parameter_list)
|
|
1599
|
+
.collect()
|
|
1600
|
+
}
|
|
1601
|
+
|
|
1602
|
+
fn formal_parameter_list(node: Node<'_>) -> Option<Node<'_>> {
|
|
1603
|
+
if node.kind() == "formal_parameter_list" {
|
|
1604
|
+
return Some(node);
|
|
1605
|
+
}
|
|
1606
|
+
let mut cursor = node.walk();
|
|
1607
|
+
node.named_children(&mut cursor)
|
|
1608
|
+
.find(|child| child.kind() == "formal_parameter_list")
|
|
1609
|
+
}
|
|
1610
|
+
|
|
1611
|
+
fn own_parameter_list(node: Node<'_>) -> Option<Node<'_>> {
|
|
1612
|
+
if node.kind() == "formal_parameter_list" {
|
|
1613
|
+
return Some(node);
|
|
1614
|
+
}
|
|
1615
|
+
let mut cursor = node.walk();
|
|
1616
|
+
node.named_children(&mut cursor)
|
|
1617
|
+
.find_map(own_parameter_list)
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
fn parameter_list_directly_bound_names(
|
|
1621
|
+
parameters: Node<'_>,
|
|
1622
|
+
names: &BTreeSet<String>,
|
|
1623
|
+
source: &str,
|
|
1624
|
+
) -> BTreeSet<String> {
|
|
1625
|
+
let mut bound = BTreeSet::new();
|
|
1626
|
+
let mut cursor = parameters.walk();
|
|
1627
|
+
for candidate in parameters.named_children(&mut cursor) {
|
|
1628
|
+
if let Some(name) = formal_parameter_name(candidate, source)
|
|
1629
|
+
&& names.contains(&name)
|
|
1630
|
+
{
|
|
1631
|
+
bound.insert(name);
|
|
1632
|
+
}
|
|
1633
|
+
if candidate.kind() == "optional_formal_parameters" {
|
|
1634
|
+
bound.extend(optional_parameters_directly_bound_names(
|
|
1635
|
+
candidate, names, source,
|
|
1636
|
+
));
|
|
1637
|
+
}
|
|
1638
|
+
}
|
|
1639
|
+
bound
|
|
1640
|
+
}
|
|
1641
|
+
|
|
1642
|
+
fn optional_parameters_directly_bound_names(
|
|
1643
|
+
parameters: Node<'_>,
|
|
1644
|
+
names: &BTreeSet<String>,
|
|
1645
|
+
source: &str,
|
|
1646
|
+
) -> BTreeSet<String> {
|
|
1647
|
+
let mut bound = BTreeSet::new();
|
|
1648
|
+
let mut cursor = parameters.walk();
|
|
1649
|
+
for candidate in parameters
|
|
1650
|
+
.named_children(&mut cursor)
|
|
1651
|
+
.filter(|candidate| candidate.kind() == "formal_parameter")
|
|
1652
|
+
{
|
|
1653
|
+
if let Some(name) = formal_parameter_name(candidate, source)
|
|
1654
|
+
&& names.contains(&name)
|
|
1655
|
+
{
|
|
1656
|
+
bound.insert(name);
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
bound
|
|
1660
|
+
}
|
|
1661
|
+
|
|
1662
|
+
fn parameter_list_directly_binds_name(parameters: Node<'_>, name: &str, source: &str) -> bool {
|
|
1663
|
+
let mut cursor = parameters.walk();
|
|
1664
|
+
parameters.named_children(&mut cursor).any(|candidate| {
|
|
1665
|
+
if formal_parameter_name(candidate, source).as_deref() == Some(name) {
|
|
1666
|
+
return true;
|
|
1667
|
+
}
|
|
1668
|
+
candidate.kind() == "optional_formal_parameters"
|
|
1669
|
+
&& optional_parameters_directly_bind_name(candidate, name, source)
|
|
1670
|
+
})
|
|
1671
|
+
}
|
|
1672
|
+
|
|
1673
|
+
fn optional_parameters_directly_bind_name(parameters: Node<'_>, name: &str, source: &str) -> bool {
|
|
1674
|
+
let mut cursor = parameters.walk();
|
|
1675
|
+
parameters
|
|
1676
|
+
.named_children(&mut cursor)
|
|
1677
|
+
.filter(|candidate| candidate.kind() == "formal_parameter")
|
|
1678
|
+
.any(|candidate| formal_parameter_name(candidate, source).as_deref() == Some(name))
|
|
1679
|
+
}
|
|
1680
|
+
|
|
1681
|
+
fn current_lexical_class(node: Node<'_>) -> Option<Node<'_>> {
|
|
1682
|
+
let mut parent = node.parent();
|
|
1683
|
+
while let Some(ancestor) = parent {
|
|
1684
|
+
if ancestor.kind() == "class_declaration" {
|
|
1685
|
+
return Some(ancestor);
|
|
1686
|
+
}
|
|
1687
|
+
parent = ancestor.parent();
|
|
1688
|
+
}
|
|
1689
|
+
None
|
|
1690
|
+
}
|
|
1691
|
+
|
|
1692
|
+
fn dispatched_member_owner(
|
|
1693
|
+
current_class: Node<'_>,
|
|
1694
|
+
name: &str,
|
|
1695
|
+
receiver: MemberReceiver,
|
|
1696
|
+
source: &str,
|
|
1697
|
+
) -> Option<String> {
|
|
1698
|
+
let root = root_node(current_class);
|
|
1699
|
+
let mut visited = BTreeSet::new();
|
|
1700
|
+
match receiver {
|
|
1701
|
+
MemberReceiver::This => class_or_inherited_member_dispatch_owner(
|
|
1702
|
+
root,
|
|
1703
|
+
current_class,
|
|
1704
|
+
name,
|
|
1705
|
+
source,
|
|
1706
|
+
&mut visited,
|
|
1707
|
+
),
|
|
1708
|
+
MemberReceiver::Super => {
|
|
1709
|
+
inherited_member_dispatch_owner(root, current_class, name, source, &mut visited)
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
}
|
|
1713
|
+
|
|
1714
|
+
fn class_or_inherited_member_dispatch_owner(
|
|
1715
|
+
root: Node<'_>,
|
|
1716
|
+
class: Node<'_>,
|
|
1717
|
+
name: &str,
|
|
1718
|
+
source: &str,
|
|
1719
|
+
visited: &mut BTreeSet<String>,
|
|
1720
|
+
) -> Option<String> {
|
|
1721
|
+
if class_declares_member(class, name, source) {
|
|
1722
|
+
return field_text(class, "name", source);
|
|
1723
|
+
}
|
|
1724
|
+
inherited_member_dispatch_owner(root, class, name, source, visited)
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1727
|
+
fn inherited_member_dispatch_owner(
|
|
1728
|
+
root: Node<'_>,
|
|
1729
|
+
class: Node<'_>,
|
|
1730
|
+
name: &str,
|
|
1731
|
+
source: &str,
|
|
1732
|
+
visited: &mut BTreeSet<String>,
|
|
1733
|
+
) -> Option<String> {
|
|
1734
|
+
for inherited in dispatch_inherited_types(root, class, source) {
|
|
1735
|
+
if inherited.name.contains('.') {
|
|
1736
|
+
return None;
|
|
1737
|
+
}
|
|
1738
|
+
if !visited.insert(inherited.name.clone()) {
|
|
1739
|
+
continue;
|
|
1740
|
+
}
|
|
1741
|
+
let inherited_class = find_class_like_declaration(root, &inherited.name, source)?;
|
|
1742
|
+
if let Some(owner) =
|
|
1743
|
+
class_or_inherited_member_dispatch_owner(root, inherited_class, name, source, visited)
|
|
1744
|
+
{
|
|
1745
|
+
return Some(owner);
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
None
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
fn dispatch_inherited_types(root: Node<'_>, class: Node<'_>, source: &str) -> Vec<InheritedType> {
|
|
1752
|
+
let inherited = class_inherited_types(root, class, source);
|
|
1753
|
+
let mut ordered = Vec::new();
|
|
1754
|
+
ordered.extend(
|
|
1755
|
+
inherited
|
|
1756
|
+
.iter()
|
|
1757
|
+
.filter(|inherited| inherited.kind == InheritedTypeKind::Mixin)
|
|
1758
|
+
.rev()
|
|
1759
|
+
.cloned(),
|
|
1760
|
+
);
|
|
1761
|
+
ordered.extend(
|
|
1762
|
+
inherited
|
|
1763
|
+
.iter()
|
|
1764
|
+
.filter(|inherited| inherited.kind == InheritedTypeKind::Superclass)
|
|
1765
|
+
.cloned(),
|
|
1766
|
+
);
|
|
1767
|
+
ordered
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
fn current_class_member_shadows_top_level(invocation: Node<'_>, name: &str, source: &str) -> bool {
|
|
1771
|
+
current_lexical_class(invocation)
|
|
1772
|
+
.is_some_and(|class| class_or_inherited_member_shadows_top_level(class, name, source))
|
|
1773
|
+
}
|
|
1774
|
+
|
|
1775
|
+
fn class_or_inherited_member_shadows_top_level(class: Node<'_>, name: &str, source: &str) -> bool {
|
|
1776
|
+
let root = root_node(class);
|
|
1777
|
+
let mut visited = BTreeSet::new();
|
|
1778
|
+
class_or_inherited_member_shadows_top_level_in(root, class, name, source, &mut visited)
|
|
1779
|
+
}
|
|
1780
|
+
|
|
1781
|
+
fn class_or_inherited_member_shadows_top_level_in(
|
|
1782
|
+
root: Node<'_>,
|
|
1783
|
+
class: Node<'_>,
|
|
1784
|
+
name: &str,
|
|
1785
|
+
source: &str,
|
|
1786
|
+
visited: &mut BTreeSet<String>,
|
|
1787
|
+
) -> bool {
|
|
1788
|
+
if class_declares_member(class, name, source) {
|
|
1789
|
+
return true;
|
|
1790
|
+
}
|
|
1791
|
+
for inherited in class_inherited_types(root, class, source) {
|
|
1792
|
+
if !visited.insert(inherited.name.clone()) {
|
|
1793
|
+
continue;
|
|
1794
|
+
}
|
|
1795
|
+
let Some(inherited_class) = find_class_like_declaration(root, &inherited.name, source)
|
|
1796
|
+
else {
|
|
1797
|
+
if unresolved_inherited_type_may_shadow(root, &inherited, source) {
|
|
1798
|
+
return true;
|
|
1799
|
+
}
|
|
1800
|
+
continue;
|
|
1801
|
+
};
|
|
1802
|
+
if class_or_inherited_member_shadows_top_level_in(
|
|
1803
|
+
root,
|
|
1804
|
+
inherited_class,
|
|
1805
|
+
name,
|
|
1806
|
+
source,
|
|
1807
|
+
visited,
|
|
1808
|
+
) {
|
|
1809
|
+
return true;
|
|
1810
|
+
}
|
|
1811
|
+
}
|
|
1812
|
+
false
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
1816
|
+
struct InheritedType {
|
|
1817
|
+
name: String,
|
|
1818
|
+
kind: InheritedTypeKind,
|
|
1819
|
+
}
|
|
1820
|
+
|
|
1821
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
1822
|
+
enum InheritedTypeKind {
|
|
1823
|
+
Superclass,
|
|
1824
|
+
Mixin,
|
|
1825
|
+
Interface,
|
|
1826
|
+
}
|
|
1827
|
+
|
|
1828
|
+
fn unresolved_inherited_type_may_shadow(
|
|
1829
|
+
root: Node<'_>,
|
|
1830
|
+
inherited: &InheritedType,
|
|
1831
|
+
source: &str,
|
|
1832
|
+
) -> bool {
|
|
1833
|
+
let inherited_name = simple_type_name(&inherited.name);
|
|
1834
|
+
inherited.kind != InheritedTypeKind::Superclass
|
|
1835
|
+
|| !framework_or_object_base_is_known(
|
|
1836
|
+
root,
|
|
1837
|
+
&inherited.name,
|
|
1838
|
+
inherited_name.as_str(),
|
|
1839
|
+
source,
|
|
1840
|
+
)
|
|
1841
|
+
}
|
|
1842
|
+
|
|
1843
|
+
fn framework_or_object_base_is_known(
|
|
1844
|
+
root: Node<'_>,
|
|
1845
|
+
inherited_name: &str,
|
|
1846
|
+
simple_name: &str,
|
|
1847
|
+
source: &str,
|
|
1848
|
+
) -> bool {
|
|
1849
|
+
if simple_name == "Object" {
|
|
1850
|
+
return !inherited_name.contains('.');
|
|
1851
|
+
}
|
|
1852
|
+
if !matches!(
|
|
1853
|
+
simple_name,
|
|
1854
|
+
"StatelessWidget"
|
|
1855
|
+
| "StatefulWidget"
|
|
1856
|
+
| "ConsumerWidget"
|
|
1857
|
+
| "ConsumerStatefulWidget"
|
|
1858
|
+
| "HookWidget"
|
|
1859
|
+
| "HookConsumerWidget"
|
|
1860
|
+
| "State"
|
|
1861
|
+
| "ConsumerState"
|
|
1862
|
+
) {
|
|
1863
|
+
return false;
|
|
1864
|
+
}
|
|
1865
|
+
if let Some((prefix, _)) = inherited_name.split_once('.') {
|
|
1866
|
+
return import_prefix_resolves_to_framework(root, prefix, source);
|
|
1867
|
+
}
|
|
1868
|
+
true
|
|
1869
|
+
}
|
|
1870
|
+
|
|
1871
|
+
fn import_prefix_resolves_to_framework(root: Node<'_>, prefix: &str, source: &str) -> bool {
|
|
1872
|
+
let mut found = false;
|
|
1873
|
+
visit_named(root, &mut |node| {
|
|
1874
|
+
if found || node.kind() != "library_import" {
|
|
1875
|
+
return;
|
|
1876
|
+
}
|
|
1877
|
+
if import_alias(node, source).as_deref() != Some(prefix) {
|
|
1878
|
+
return;
|
|
1879
|
+
}
|
|
1880
|
+
if import_uri(node, source).is_some_and(|uri| framework_import_uri(&uri)) {
|
|
1881
|
+
found = true;
|
|
1882
|
+
}
|
|
1883
|
+
});
|
|
1884
|
+
found
|
|
1885
|
+
}
|
|
1886
|
+
|
|
1887
|
+
fn import_alias(node: Node<'_>, source: &str) -> Option<String> {
|
|
1888
|
+
find_first_named_descendant(node, "import_specification")
|
|
1889
|
+
.and_then(|specification| field_text(specification, "alias", source))
|
|
1890
|
+
}
|
|
1891
|
+
|
|
1892
|
+
fn import_uri(node: Node<'_>, source: &str) -> Option<String> {
|
|
1893
|
+
find_first_named_descendant(node, "uri")
|
|
1894
|
+
.and_then(|uri| uri.utf8_text(source.as_bytes()).ok())
|
|
1895
|
+
.and_then(unquote_dart_string)
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1898
|
+
fn unquote_dart_string(text: &str) -> Option<String> {
|
|
1899
|
+
let trimmed = text.trim().trim_start_matches('r');
|
|
1900
|
+
let quote = trimmed.chars().next()?;
|
|
1901
|
+
if quote != '\'' && quote != '"' {
|
|
1902
|
+
return None;
|
|
1903
|
+
}
|
|
1904
|
+
trimmed
|
|
1905
|
+
.strip_prefix(quote)
|
|
1906
|
+
.and_then(|rest| rest.strip_suffix(quote))
|
|
1907
|
+
.map(str::to_owned)
|
|
1908
|
+
}
|
|
1909
|
+
|
|
1910
|
+
fn framework_import_uri(uri: &str) -> bool {
|
|
1911
|
+
uri.starts_with("package:flutter/")
|
|
1912
|
+
|| uri.starts_with("package:flutter_riverpod/")
|
|
1913
|
+
|| uri.starts_with("package:hooks_riverpod/")
|
|
1914
|
+
}
|
|
1915
|
+
|
|
1916
|
+
fn class_inherited_types(_root: Node<'_>, class: Node<'_>, source: &str) -> Vec<InheritedType> {
|
|
1917
|
+
let Some(body) = class.child_by_field_name("body") else {
|
|
1918
|
+
return Vec::new();
|
|
1919
|
+
};
|
|
1920
|
+
let Some(header) = source.get(class.start_byte()..body.start_byte()) else {
|
|
1921
|
+
return Vec::new();
|
|
1922
|
+
};
|
|
1923
|
+
let mut inherited = Vec::new();
|
|
1924
|
+
if let Some(superclass) = clause_after_keyword(header, "extends", &["with", "implements"])
|
|
1925
|
+
&& let Some(name) = inherited_type_name(superclass)
|
|
1926
|
+
{
|
|
1927
|
+
inherited.push(InheritedType {
|
|
1928
|
+
name,
|
|
1929
|
+
kind: InheritedTypeKind::Superclass,
|
|
1930
|
+
});
|
|
1931
|
+
}
|
|
1932
|
+
if let Some(mixins) = clause_after_keyword(header, "with", &["implements"]) {
|
|
1933
|
+
inherited.extend(
|
|
1934
|
+
split_top_level_commas(mixins)
|
|
1935
|
+
.into_iter()
|
|
1936
|
+
.filter_map(inherited_type_name)
|
|
1937
|
+
.map(|name| InheritedType {
|
|
1938
|
+
name,
|
|
1939
|
+
kind: InheritedTypeKind::Mixin,
|
|
1940
|
+
}),
|
|
1941
|
+
);
|
|
1942
|
+
}
|
|
1943
|
+
if let Some(interfaces) = clause_after_keyword(header, "implements", &[]) {
|
|
1944
|
+
inherited.extend(
|
|
1945
|
+
split_top_level_commas(interfaces)
|
|
1946
|
+
.into_iter()
|
|
1947
|
+
.filter_map(inherited_type_name)
|
|
1948
|
+
.map(|name| InheritedType {
|
|
1949
|
+
name,
|
|
1950
|
+
kind: InheritedTypeKind::Interface,
|
|
1951
|
+
}),
|
|
1952
|
+
);
|
|
1953
|
+
}
|
|
1954
|
+
inherited
|
|
1955
|
+
}
|
|
1956
|
+
|
|
1957
|
+
fn clause_after_keyword<'source>(
|
|
1958
|
+
text: &'source str,
|
|
1959
|
+
keyword: &str,
|
|
1960
|
+
stop_keywords: &[&str],
|
|
1961
|
+
) -> Option<&'source str> {
|
|
1962
|
+
let start = find_keyword(text, keyword)? + keyword.len();
|
|
1963
|
+
let rest = text.get(start..)?;
|
|
1964
|
+
let end = stop_keywords
|
|
1965
|
+
.iter()
|
|
1966
|
+
.filter_map(|stop| find_keyword(rest, stop))
|
|
1967
|
+
.min()
|
|
1968
|
+
.unwrap_or(rest.len());
|
|
1969
|
+
Some(rest[..end].trim())
|
|
1970
|
+
}
|
|
1971
|
+
|
|
1972
|
+
fn find_keyword(text: &str, keyword: &str) -> Option<usize> {
|
|
1973
|
+
let mut cursor = 0usize;
|
|
1974
|
+
while let Some(relative) = text[cursor..].find(keyword) {
|
|
1975
|
+
let start = cursor + relative;
|
|
1976
|
+
let end = start + keyword.len();
|
|
1977
|
+
let continues_before = text[..start]
|
|
1978
|
+
.chars()
|
|
1979
|
+
.next_back()
|
|
1980
|
+
.is_some_and(is_identifier_character);
|
|
1981
|
+
let continues_after = text[end..]
|
|
1982
|
+
.chars()
|
|
1983
|
+
.next()
|
|
1984
|
+
.is_some_and(is_identifier_character);
|
|
1985
|
+
if !continues_before && !continues_after {
|
|
1986
|
+
return Some(start);
|
|
1987
|
+
}
|
|
1988
|
+
cursor = end;
|
|
1989
|
+
}
|
|
1990
|
+
None
|
|
1991
|
+
}
|
|
1992
|
+
|
|
1993
|
+
fn split_top_level_commas(text: &str) -> Vec<&str> {
|
|
1994
|
+
let mut parts = Vec::new();
|
|
1995
|
+
let mut start = 0usize;
|
|
1996
|
+
let mut angle_depth = 0usize;
|
|
1997
|
+
let mut grouping_depth = 0usize;
|
|
1998
|
+
for (index, character) in text.char_indices() {
|
|
1999
|
+
match character {
|
|
2000
|
+
'<' => angle_depth += 1,
|
|
2001
|
+
'>' => angle_depth = angle_depth.saturating_sub(1),
|
|
2002
|
+
'(' | '[' | '{' => grouping_depth += 1,
|
|
2003
|
+
')' | ']' | '}' => grouping_depth = grouping_depth.saturating_sub(1),
|
|
2004
|
+
',' if angle_depth == 0 && grouping_depth == 0 => {
|
|
2005
|
+
parts.push(text[start..index].trim());
|
|
2006
|
+
start = index + 1;
|
|
2007
|
+
}
|
|
2008
|
+
_ => {}
|
|
2009
|
+
}
|
|
2010
|
+
}
|
|
2011
|
+
let last = text[start..].trim();
|
|
2012
|
+
if !last.is_empty() {
|
|
2013
|
+
parts.push(last);
|
|
2014
|
+
}
|
|
2015
|
+
parts
|
|
2016
|
+
}
|
|
2017
|
+
|
|
2018
|
+
fn inherited_type_name(text: &str) -> Option<String> {
|
|
2019
|
+
let mut name = String::new();
|
|
2020
|
+
for character in text.trim().chars() {
|
|
2021
|
+
if character == '<' {
|
|
2022
|
+
break;
|
|
2023
|
+
}
|
|
2024
|
+
if is_identifier_character(character) || character == '.' {
|
|
2025
|
+
name.push(character);
|
|
2026
|
+
continue;
|
|
2027
|
+
}
|
|
2028
|
+
if !name.is_empty() {
|
|
2029
|
+
break;
|
|
2030
|
+
}
|
|
2031
|
+
}
|
|
2032
|
+
is_qualified_identifier_text(&name).then_some(name)
|
|
2033
|
+
}
|
|
2034
|
+
|
|
2035
|
+
fn is_qualified_identifier_text(text: &str) -> bool {
|
|
2036
|
+
!text.is_empty() && text.split('.').all(is_identifier_text)
|
|
2037
|
+
}
|
|
2038
|
+
|
|
2039
|
+
fn find_class_like_declaration<'tree>(
|
|
2040
|
+
node: Node<'tree>,
|
|
2041
|
+
name: &str,
|
|
2042
|
+
source: &str,
|
|
2043
|
+
) -> Option<Node<'tree>> {
|
|
2044
|
+
if matches!(node.kind(), "class_declaration" | "mixin_declaration")
|
|
2045
|
+
&& field_text(node, "name", source).as_deref() == Some(name)
|
|
2046
|
+
{
|
|
2047
|
+
return Some(node);
|
|
2048
|
+
}
|
|
2049
|
+
let mut cursor = node.walk();
|
|
2050
|
+
node.named_children(&mut cursor)
|
|
2051
|
+
.find_map(|child| find_class_like_declaration(child, name, source))
|
|
2052
|
+
}
|
|
2053
|
+
|
|
2054
|
+
fn root_node(mut node: Node<'_>) -> Node<'_> {
|
|
2055
|
+
while let Some(parent) = node.parent() {
|
|
2056
|
+
node = parent;
|
|
2057
|
+
}
|
|
2058
|
+
node
|
|
2059
|
+
}
|
|
2060
|
+
|
|
2061
|
+
fn class_declares_member(class: Node<'_>, name: &str, source: &str) -> bool {
|
|
2062
|
+
let Some(body) = class.child_by_field_name("body") else {
|
|
2063
|
+
return false;
|
|
2064
|
+
};
|
|
2065
|
+
let mut cursor = body.walk();
|
|
2066
|
+
body.named_children(&mut cursor)
|
|
2067
|
+
.any(|child| match child.kind() {
|
|
2068
|
+
"class_member" => class_member_declares_name(child, name, source),
|
|
2069
|
+
"method_declaration" => member_signature_name(child, source).as_deref() == Some(name),
|
|
2070
|
+
"declaration" => declaration_declares_member(child, name, source),
|
|
2071
|
+
_ => false,
|
|
2072
|
+
})
|
|
2073
|
+
}
|
|
2074
|
+
|
|
2075
|
+
fn class_member_declares_name(member: Node<'_>, name: &str, source: &str) -> bool {
|
|
2076
|
+
let mut cursor = member.walk();
|
|
2077
|
+
member
|
|
2078
|
+
.named_children(&mut cursor)
|
|
2079
|
+
.any(|child| match child.kind() {
|
|
2080
|
+
"method_declaration" => member_signature_name(child, source).as_deref() == Some(name),
|
|
2081
|
+
"declaration" => declaration_declares_member(child, name, source),
|
|
2082
|
+
_ => false,
|
|
2083
|
+
})
|
|
2084
|
+
}
|
|
2085
|
+
|
|
2086
|
+
fn declaration_declares_member(declaration: Node<'_>, name: &str, source: &str) -> bool {
|
|
2087
|
+
if member_signature_name(declaration, source).as_deref() == Some(name) {
|
|
2088
|
+
return true;
|
|
2089
|
+
}
|
|
2090
|
+
let mut names = Vec::new();
|
|
2091
|
+
collect_member_field_names(declaration, source, &mut names);
|
|
2092
|
+
names.iter().any(|candidate| candidate == name)
|
|
2093
|
+
}
|
|
2094
|
+
|
|
2095
|
+
fn member_signature_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
2096
|
+
if matches!(
|
|
2097
|
+
node.kind(),
|
|
2098
|
+
"function_signature" | "getter_signature" | "setter_signature"
|
|
2099
|
+
) {
|
|
2100
|
+
return field_text(node, "name", source);
|
|
2101
|
+
}
|
|
2102
|
+
let mut cursor = node.walk();
|
|
2103
|
+
node.named_children(&mut cursor)
|
|
2104
|
+
.find_map(|child| member_signature_name(child, source))
|
|
2105
|
+
}
|
|
2106
|
+
|
|
2107
|
+
fn collect_member_field_names(node: Node<'_>, source: &str, names: &mut Vec<String>) {
|
|
2108
|
+
collect_named_fields(
|
|
2109
|
+
node,
|
|
2110
|
+
source,
|
|
2111
|
+
&["static_final_declaration", "initialized_identifier"],
|
|
2112
|
+
names,
|
|
2113
|
+
);
|
|
2114
|
+
if names.is_empty()
|
|
2115
|
+
&& let Some(identifier_list) = find_first_named_descendant(node, "identifier_list")
|
|
2116
|
+
{
|
|
2117
|
+
collect_direct_identifier_children(identifier_list, source, names);
|
|
2118
|
+
}
|
|
2119
|
+
}
|
|
2120
|
+
|
|
2121
|
+
fn collect_named_fields(
|
|
2122
|
+
node: Node<'_>,
|
|
2123
|
+
source: &str,
|
|
2124
|
+
owner_kinds: &[&str],
|
|
2125
|
+
names: &mut Vec<String>,
|
|
2126
|
+
) {
|
|
2127
|
+
if owner_kinds.contains(&node.kind())
|
|
2128
|
+
&& let Some(name) = field_text(node, "name", source)
|
|
2129
|
+
{
|
|
2130
|
+
names.push(name);
|
|
2131
|
+
}
|
|
2132
|
+
|
|
2133
|
+
let mut cursor = node.walk();
|
|
2134
|
+
for child in node.named_children(&mut cursor) {
|
|
2135
|
+
collect_named_fields(child, source, owner_kinds, names);
|
|
2136
|
+
}
|
|
2137
|
+
}
|
|
2138
|
+
|
|
2139
|
+
fn find_first_named_descendant<'tree>(node: Node<'tree>, kind: &str) -> Option<Node<'tree>> {
|
|
2140
|
+
if node.kind() == kind {
|
|
2141
|
+
return Some(node);
|
|
2142
|
+
}
|
|
2143
|
+
let mut cursor = node.walk();
|
|
2144
|
+
node.named_children(&mut cursor)
|
|
2145
|
+
.find_map(|child| find_first_named_descendant(child, kind))
|
|
2146
|
+
}
|
|
2147
|
+
|
|
2148
|
+
fn collect_direct_identifier_children(node: Node<'_>, source: &str, names: &mut Vec<String>) {
|
|
2149
|
+
let mut cursor = node.walk();
|
|
2150
|
+
names.extend(
|
|
2151
|
+
node.named_children(&mut cursor)
|
|
2152
|
+
.filter(|child| child.kind() == "identifier")
|
|
2153
|
+
.filter_map(|child| child.utf8_text(source.as_bytes()).ok())
|
|
2154
|
+
.map(str::to_owned),
|
|
2155
|
+
);
|
|
2156
|
+
}
|
|
2157
|
+
|
|
2158
|
+
fn invocation_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
2159
|
+
if !matches!(
|
|
2160
|
+
node.kind(),
|
|
2161
|
+
"call_expression"
|
|
2162
|
+
| "constructor_invocation"
|
|
2163
|
+
| "const_object_expression"
|
|
2164
|
+
| "new_expression"
|
|
2165
|
+
| "function_expression_invocation"
|
|
2166
|
+
) {
|
|
2167
|
+
return None;
|
|
2168
|
+
}
|
|
2169
|
+
if let Some(name) = constructor_invocation_name(node, source) {
|
|
2170
|
+
return Some(name);
|
|
2171
|
+
}
|
|
2172
|
+
let arguments = argument_list(node)?;
|
|
2173
|
+
source
|
|
2174
|
+
.get(node.start_byte()..arguments.start_byte())
|
|
2175
|
+
.map(normalized_invocation_prefix)
|
|
2176
|
+
}
|
|
2177
|
+
|
|
2178
|
+
fn argument_list(node: Node<'_>) -> Option<Node<'_>> {
|
|
2179
|
+
node.child_by_field_name("arguments")
|
|
2180
|
+
.or_else(|| direct_named_child(node, "arguments"))
|
|
2181
|
+
.or_else(|| direct_named_child(node, "argument_part"))
|
|
2182
|
+
}
|
|
2183
|
+
|
|
2184
|
+
fn constructor_invocation_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
2185
|
+
if !matches!(
|
|
2186
|
+
node.kind(),
|
|
2187
|
+
"constructor_invocation" | "const_object_expression" | "new_expression"
|
|
2188
|
+
) {
|
|
2189
|
+
return None;
|
|
2190
|
+
}
|
|
2191
|
+
let type_name = node
|
|
2192
|
+
.child_by_field_name("type")?
|
|
2193
|
+
.utf8_text(source.as_bytes())
|
|
2194
|
+
.ok()
|
|
2195
|
+
.map(strip_whitespace)
|
|
2196
|
+
.map(|name| strip_type_arguments(&name))?;
|
|
2197
|
+
node.child_by_field_name("constructor")
|
|
2198
|
+
.and_then(|constructor| constructor.utf8_text(source.as_bytes()).ok())
|
|
2199
|
+
.map_or(Some(type_name.clone()), |constructor| {
|
|
2200
|
+
Some(format!("{type_name}.{constructor}"))
|
|
2201
|
+
})
|
|
2202
|
+
}
|
|
2203
|
+
|
|
2204
|
+
fn normalized_invocation_prefix(prefix: &str) -> String {
|
|
2205
|
+
let prefix = strip_whitespace(
|
|
2206
|
+
prefix
|
|
2207
|
+
.trim()
|
|
2208
|
+
.strip_prefix("const ")
|
|
2209
|
+
.or_else(|| prefix.trim().strip_prefix("new "))
|
|
2210
|
+
.unwrap_or(prefix.trim()),
|
|
2211
|
+
);
|
|
2212
|
+
strip_type_arguments(&prefix)
|
|
2213
|
+
}
|
|
2214
|
+
|
|
2215
|
+
fn invocation_arguments_pass_roots(
|
|
2216
|
+
node: Node<'_>,
|
|
2217
|
+
parameter: &HelperParameter,
|
|
2218
|
+
roots: &BTreeSet<String>,
|
|
2219
|
+
source: &str,
|
|
2220
|
+
) -> bool {
|
|
2221
|
+
let Some(arguments) = argument_list(node) else {
|
|
2222
|
+
return false;
|
|
2223
|
+
};
|
|
2224
|
+
let mut positional_index = 0usize;
|
|
2225
|
+
let mut cursor = arguments.walk();
|
|
2226
|
+
for argument in arguments.named_children(&mut cursor) {
|
|
2227
|
+
if argument.kind() == "named_argument" {
|
|
2228
|
+
if named_argument_label(argument, source).as_deref() == Some(parameter.name.as_str())
|
|
2229
|
+
&& named_argument_value(argument)
|
|
2230
|
+
.is_some_and(|value| argument_matches_roots(value, roots, source))
|
|
2231
|
+
{
|
|
2232
|
+
return true;
|
|
2233
|
+
}
|
|
2234
|
+
continue;
|
|
2235
|
+
}
|
|
2236
|
+
if parameter.positional_index == Some(positional_index)
|
|
2237
|
+
&& argument_matches_roots(argument, roots, source)
|
|
2238
|
+
{
|
|
2239
|
+
return true;
|
|
2240
|
+
}
|
|
2241
|
+
positional_index += 1;
|
|
2242
|
+
}
|
|
2243
|
+
argument_texts_pass_roots(arguments, parameter, roots, source)
|
|
2244
|
+
}
|
|
2245
|
+
|
|
2246
|
+
fn argument_texts_pass_roots(
|
|
2247
|
+
arguments: Node<'_>,
|
|
2248
|
+
parameter: &HelperParameter,
|
|
2249
|
+
roots: &BTreeSet<String>,
|
|
2250
|
+
source: &str,
|
|
2251
|
+
) -> bool {
|
|
2252
|
+
let Some(text) = arguments.utf8_text(source.as_bytes()).ok() else {
|
|
2253
|
+
return false;
|
|
2254
|
+
};
|
|
2255
|
+
let mut positional_index = 0usize;
|
|
2256
|
+
for argument in split_argument_texts(text) {
|
|
2257
|
+
if let Some((label, value)) = argument.split_once(':') {
|
|
2258
|
+
if label.trim() == parameter.name && expression_matches_roots(value, roots) {
|
|
2259
|
+
return true;
|
|
2260
|
+
}
|
|
2261
|
+
continue;
|
|
2262
|
+
}
|
|
2263
|
+
if parameter.positional_index == Some(positional_index)
|
|
2264
|
+
&& expression_matches_roots(argument, roots)
|
|
2265
|
+
{
|
|
2266
|
+
return true;
|
|
2267
|
+
}
|
|
2268
|
+
positional_index += 1;
|
|
2269
|
+
}
|
|
2270
|
+
false
|
|
2271
|
+
}
|
|
2272
|
+
|
|
2273
|
+
fn split_argument_texts(text: &str) -> Vec<&str> {
|
|
2274
|
+
let inner = text
|
|
2275
|
+
.trim()
|
|
2276
|
+
.strip_prefix('(')
|
|
2277
|
+
.and_then(|text| text.strip_suffix(')'))
|
|
2278
|
+
.unwrap_or(text)
|
|
2279
|
+
.trim();
|
|
2280
|
+
if inner.is_empty() {
|
|
2281
|
+
return Vec::new();
|
|
2282
|
+
}
|
|
2283
|
+
let mut parts = Vec::new();
|
|
2284
|
+
let mut start = 0usize;
|
|
2285
|
+
let mut depth = 0usize;
|
|
2286
|
+
for (index, character) in inner.char_indices() {
|
|
2287
|
+
match character {
|
|
2288
|
+
'(' | '[' | '{' => depth += 1,
|
|
2289
|
+
')' | ']' | '}' => depth = depth.saturating_sub(1),
|
|
2290
|
+
',' if depth == 0 => {
|
|
2291
|
+
parts.push(inner[start..index].trim());
|
|
2292
|
+
start = index + 1;
|
|
2293
|
+
}
|
|
2294
|
+
_ => {}
|
|
2295
|
+
}
|
|
2296
|
+
}
|
|
2297
|
+
parts.push(inner[start..].trim());
|
|
2298
|
+
parts
|
|
2299
|
+
}
|
|
2300
|
+
|
|
2301
|
+
fn named_argument_label(node: Node<'_>, source: &str) -> Option<String> {
|
|
2302
|
+
let mut cursor = node.walk();
|
|
2303
|
+
node.named_children(&mut cursor)
|
|
2304
|
+
.find(|child| child.kind() == "label")
|
|
2305
|
+
.and_then(|label| label.utf8_text(source.as_bytes()).ok())
|
|
2306
|
+
.map(str::trim)
|
|
2307
|
+
.and_then(|label| label.strip_suffix(':'))
|
|
2308
|
+
.map(str::to_owned)
|
|
2309
|
+
}
|
|
2310
|
+
|
|
2311
|
+
fn named_argument_value(node: Node<'_>) -> Option<Node<'_>> {
|
|
2312
|
+
let mut cursor = node.walk();
|
|
2313
|
+
node.named_children(&mut cursor)
|
|
2314
|
+
.find(|child| child.kind() != "label")
|
|
2315
|
+
}
|
|
2316
|
+
|
|
2317
|
+
fn argument_matches_roots(argument: Node<'_>, roots: &BTreeSet<String>, source: &str) -> bool {
|
|
2318
|
+
argument
|
|
2319
|
+
.utf8_text(source.as_bytes())
|
|
2320
|
+
.ok()
|
|
2321
|
+
.is_some_and(|text| expression_matches_roots(text, roots))
|
|
2322
|
+
}
|
|
2323
|
+
|
|
2324
|
+
pub(super) fn pattern_binds_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
2325
|
+
if node.kind() == "variable_pattern"
|
|
2326
|
+
&& field_text(node, "name", source).as_deref() == Some(name)
|
|
2327
|
+
{
|
|
2328
|
+
return true;
|
|
2329
|
+
}
|
|
2330
|
+
if shorthand_pattern_name(node, source).as_deref() == Some(name) {
|
|
2331
|
+
return true;
|
|
2332
|
+
}
|
|
2333
|
+
if node.kind() == "object_pattern" {
|
|
2334
|
+
return object_pattern_binds_name(node, name, source);
|
|
2335
|
+
}
|
|
2336
|
+
let mut cursor = node.walk();
|
|
2337
|
+
node.named_children(&mut cursor)
|
|
2338
|
+
.any(|child| pattern_binds_name(child, name, source))
|
|
2339
|
+
}
|
|
2340
|
+
|
|
2341
|
+
fn binding_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
2342
|
+
if !matches!(
|
|
2343
|
+
node.kind(),
|
|
2344
|
+
"formal_parameter"
|
|
2345
|
+
| "initialized_identifier"
|
|
2346
|
+
| "initialized_variable_definition"
|
|
2347
|
+
| "typed_identifier"
|
|
2348
|
+
| "variable_pattern"
|
|
2349
|
+
) {
|
|
2350
|
+
return None;
|
|
2351
|
+
}
|
|
2352
|
+
node.child_by_field_name("name")
|
|
2353
|
+
.and_then(|name| name.utf8_text(source.as_bytes()).ok())
|
|
2354
|
+
.map(str::to_owned)
|
|
2355
|
+
}
|
|
2356
|
+
|
|
2357
|
+
fn object_pattern_binds_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
2358
|
+
let mut saw_type = false;
|
|
2359
|
+
let mut cursor = node.walk();
|
|
2360
|
+
for child in node.named_children(&mut cursor) {
|
|
2361
|
+
if !saw_type && is_type_child(child) {
|
|
2362
|
+
saw_type = true;
|
|
2363
|
+
continue;
|
|
2364
|
+
}
|
|
2365
|
+
if child.kind() == "type_arguments" {
|
|
2366
|
+
continue;
|
|
2367
|
+
}
|
|
2368
|
+
if child.kind() == "label" {
|
|
2369
|
+
continue;
|
|
2370
|
+
}
|
|
2371
|
+
if !is_pattern_node(child) {
|
|
2372
|
+
continue;
|
|
2373
|
+
}
|
|
2374
|
+
if shorthand_pattern_name(child, source).as_deref() == Some(name)
|
|
2375
|
+
|| pattern_binds_name(child, name, source)
|
|
2376
|
+
{
|
|
2377
|
+
return true;
|
|
2378
|
+
}
|
|
2379
|
+
}
|
|
2380
|
+
false
|
|
2381
|
+
}
|
|
2382
|
+
|
|
2383
|
+
fn object_pattern_type_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
2384
|
+
let text = node.utf8_text(source.as_bytes()).ok()?.trim();
|
|
2385
|
+
let type_text = text.get(..object_pattern_fields_start(text)?)?.trim();
|
|
2386
|
+
let base = type_text.split('<').next().unwrap_or(type_text).trim();
|
|
2387
|
+
(!base.contains('.')).then(|| simple_type_name(base))
|
|
2388
|
+
}
|
|
2389
|
+
|
|
2390
|
+
fn object_pattern_fields_start(text: &str) -> Option<usize> {
|
|
2391
|
+
let mut angle_depth = 0usize;
|
|
2392
|
+
for (index, character) in text.char_indices() {
|
|
2393
|
+
match character {
|
|
2394
|
+
'<' => angle_depth += 1,
|
|
2395
|
+
'>' => angle_depth = angle_depth.saturating_sub(1),
|
|
2396
|
+
'(' if angle_depth == 0 => return Some(index),
|
|
2397
|
+
_ => {}
|
|
2398
|
+
}
|
|
2399
|
+
}
|
|
2400
|
+
None
|
|
2401
|
+
}
|
|
2402
|
+
|
|
2403
|
+
fn object_pattern_field_names(node: Node<'_>, source: &str) -> BTreeSet<String> {
|
|
2404
|
+
let mut fields = BTreeSet::new();
|
|
2405
|
+
let mut saw_type = false;
|
|
2406
|
+
let mut pending_label = None;
|
|
2407
|
+
let mut cursor = node.walk();
|
|
2408
|
+
for child in node.named_children(&mut cursor) {
|
|
2409
|
+
if !saw_type && is_type_child(child) {
|
|
2410
|
+
saw_type = true;
|
|
2411
|
+
continue;
|
|
2412
|
+
}
|
|
2413
|
+
if child.kind() == "type_arguments" {
|
|
2414
|
+
continue;
|
|
2415
|
+
}
|
|
2416
|
+
if child.kind() == "label" {
|
|
2417
|
+
pending_label = label_name(child, source);
|
|
2418
|
+
continue;
|
|
2419
|
+
}
|
|
2420
|
+
if !is_pattern_node(child) {
|
|
2421
|
+
continue;
|
|
2422
|
+
}
|
|
2423
|
+
if let Some(field) = pending_label.take() {
|
|
2424
|
+
fields.insert(field);
|
|
2425
|
+
} else if let Some(field) = shorthand_pattern_name(child, source) {
|
|
2426
|
+
fields.insert(field);
|
|
2427
|
+
}
|
|
2428
|
+
}
|
|
2429
|
+
fields
|
|
2430
|
+
}
|
|
2431
|
+
|
|
2432
|
+
fn label_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
2433
|
+
let mut cursor = node.walk();
|
|
2434
|
+
node.named_children(&mut cursor)
|
|
2435
|
+
.find(|child| is_identifier(*child))
|
|
2436
|
+
.and_then(|child| child.utf8_text(source.as_bytes()).ok())
|
|
2437
|
+
.map(str::to_owned)
|
|
2438
|
+
}
|
|
2439
|
+
|
|
2440
|
+
fn shorthand_pattern_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
2441
|
+
if node.kind() == "variable_pattern" {
|
|
2442
|
+
return field_text(node, "name", source).filter(|name| name != "_");
|
|
2443
|
+
}
|
|
2444
|
+
let text = node.utf8_text(source.as_bytes()).ok()?.trim();
|
|
2445
|
+
if is_identifier_text(text) && text != "_" {
|
|
2446
|
+
return Some(text.to_owned());
|
|
2447
|
+
}
|
|
2448
|
+
if shorthand_pattern_wrapper(node.kind()) {
|
|
2449
|
+
let mut cursor = node.walk();
|
|
2450
|
+
return node
|
|
2451
|
+
.named_children(&mut cursor)
|
|
2452
|
+
.find_map(|child| shorthand_pattern_name(child, source));
|
|
2453
|
+
}
|
|
2454
|
+
None
|
|
2455
|
+
}
|
|
2456
|
+
|
|
2457
|
+
fn shorthand_pattern_wrapper(kind: &str) -> bool {
|
|
2458
|
+
matches!(
|
|
2459
|
+
kind,
|
|
2460
|
+
"cast_pattern"
|
|
2461
|
+
| "null_assert_pattern"
|
|
2462
|
+
| "null_check_pattern"
|
|
2463
|
+
| "parenthesized_pattern"
|
|
2464
|
+
| "pattern"
|
|
2465
|
+
)
|
|
2466
|
+
}
|
|
2467
|
+
|
|
2468
|
+
fn field_text(node: Node<'_>, field_name: &str, source: &str) -> Option<String> {
|
|
2469
|
+
node.child_by_field_name(field_name)
|
|
2470
|
+
.and_then(|field| field.utf8_text(source.as_bytes()).ok())
|
|
2471
|
+
.map(str::to_owned)
|
|
2472
|
+
}
|
|
2473
|
+
|
|
2474
|
+
fn is_type_child(node: Node<'_>) -> bool {
|
|
2475
|
+
matches!(
|
|
2476
|
+
node.kind(),
|
|
2477
|
+
"type_identifier" | "identifier" | "qualified" | "type"
|
|
2478
|
+
)
|
|
2479
|
+
}
|
|
2480
|
+
|
|
2481
|
+
fn is_pattern_node(node: Node<'_>) -> bool {
|
|
2482
|
+
node.kind().ends_with("_pattern") || node.kind() == "pattern"
|
|
2483
|
+
}
|
|
2484
|
+
|
|
2485
|
+
fn is_identifier(node: Node<'_>) -> bool {
|
|
2486
|
+
matches!(
|
|
2487
|
+
node.kind(),
|
|
2488
|
+
"identifier" | "identifier_dollar_escaped" | "type_identifier"
|
|
2489
|
+
)
|
|
2490
|
+
}
|
|
2491
|
+
|
|
2492
|
+
fn is_identifier_text(text: &str) -> bool {
|
|
2493
|
+
let mut chars = text.chars();
|
|
2494
|
+
let Some(first) = chars.next() else {
|
|
2495
|
+
return false;
|
|
2496
|
+
};
|
|
2497
|
+
(first == '_' || first == '$' || first.is_ascii_alphabetic())
|
|
2498
|
+
&& chars.all(|character| {
|
|
2499
|
+
character == '_' || character == '$' || character.is_ascii_alphanumeric()
|
|
2500
|
+
})
|
|
2501
|
+
}
|
|
2502
|
+
|
|
2503
|
+
fn identifier_continues_before(text: &str, index: usize) -> bool {
|
|
2504
|
+
text[..index]
|
|
2505
|
+
.chars()
|
|
2506
|
+
.next_back()
|
|
2507
|
+
.is_some_and(is_identifier_character)
|
|
2508
|
+
}
|
|
2509
|
+
|
|
2510
|
+
fn is_identifier_character(character: char) -> bool {
|
|
2511
|
+
character == '_' || character == '$' || character.is_ascii_alphanumeric()
|
|
2512
|
+
}
|
|
2513
|
+
|
|
2514
|
+
fn identifier_before_equals(text: &str) -> Option<String> {
|
|
2515
|
+
let before_equals = text.split_once('=')?.0;
|
|
2516
|
+
before_equals
|
|
2517
|
+
.split(|character: char| {
|
|
2518
|
+
!(character == '_' || character == '$' || character.is_ascii_alphanumeric())
|
|
2519
|
+
})
|
|
2520
|
+
.rfind(|part| !part.is_empty())
|
|
2521
|
+
.map(str::to_owned)
|
|
2522
|
+
}
|
|
2523
|
+
|
|
2524
|
+
fn identifier_before_parameters(node: Node<'_>, source: &str) -> Option<String> {
|
|
2525
|
+
let text = node.utf8_text(source.as_bytes()).ok()?;
|
|
2526
|
+
let before_parameters = text.split_once('(')?.0;
|
|
2527
|
+
before_parameters
|
|
2528
|
+
.split(|character: char| {
|
|
2529
|
+
!(character == '_' || character == '$' || character.is_ascii_alphanumeric())
|
|
2530
|
+
})
|
|
2531
|
+
.rfind(|part| !part.is_empty())
|
|
2532
|
+
.map(str::to_owned)
|
|
2533
|
+
}
|
|
2534
|
+
|
|
2535
|
+
fn last_identifier_child<'tree>(node: Node<'tree>, source: &str) -> Option<Node<'tree>> {
|
|
2536
|
+
let mut cursor = node.walk();
|
|
2537
|
+
node.named_children(&mut cursor)
|
|
2538
|
+
.filter(|child| {
|
|
2539
|
+
matches!(
|
|
2540
|
+
child.kind(),
|
|
2541
|
+
"identifier" | "identifier_dollar_escaped" | "type_identifier"
|
|
2542
|
+
) && child.utf8_text(source.as_bytes()).ok() != Some("key")
|
|
2543
|
+
})
|
|
2544
|
+
.last()
|
|
2545
|
+
}
|
|
2546
|
+
|
|
2547
|
+
fn collect_nodes_in<'tree>(node: Node<'tree>, kinds: &[&str], nodes: &mut Vec<Node<'tree>>) {
|
|
2548
|
+
if kinds.contains(&node.kind()) {
|
|
2549
|
+
nodes.push(node);
|
|
2550
|
+
}
|
|
2551
|
+
let mut cursor = node.walk();
|
|
2552
|
+
for child in node.named_children(&mut cursor) {
|
|
2553
|
+
collect_nodes_in(child, kinds, nodes);
|
|
2554
|
+
}
|
|
2555
|
+
}
|
|
2556
|
+
|
|
2557
|
+
fn direct_named_child<'tree>(node: Node<'tree>, kind: &str) -> Option<Node<'tree>> {
|
|
2558
|
+
let mut cursor = node.walk();
|
|
2559
|
+
node.named_children(&mut cursor)
|
|
2560
|
+
.find(|child| child.kind() == kind)
|
|
2561
|
+
}
|
|
2562
|
+
|
|
2563
|
+
fn strip_whitespace(text: &str) -> String {
|
|
2564
|
+
text.chars()
|
|
2565
|
+
.filter(|character| !character.is_whitespace())
|
|
2566
|
+
.collect()
|
|
2567
|
+
}
|
|
2568
|
+
|
|
2569
|
+
fn strip_type_arguments(text: &str) -> String {
|
|
2570
|
+
let mut stripped = String::new();
|
|
2571
|
+
let mut depth = 0usize;
|
|
2572
|
+
for character in text.chars() {
|
|
2573
|
+
match character {
|
|
2574
|
+
'<' => depth += 1,
|
|
2575
|
+
'>' => depth = depth.saturating_sub(1),
|
|
2576
|
+
_ if depth == 0 => stripped.push(character),
|
|
2577
|
+
_ => {}
|
|
2578
|
+
}
|
|
2579
|
+
}
|
|
2580
|
+
stripped
|
|
2581
|
+
}
|
|
2582
|
+
|
|
2583
|
+
fn visit_named(node: Node<'_>, visitor: &mut impl FnMut(Node<'_>)) {
|
|
2584
|
+
visitor(node);
|
|
2585
|
+
let mut cursor = node.walk();
|
|
2586
|
+
for child in node.named_children(&mut cursor) {
|
|
2587
|
+
visit_named(child, visitor);
|
|
2588
|
+
}
|
|
2589
|
+
}
|
|
2590
|
+
|
|
2591
|
+
fn same_node(left: Node<'_>, right: Node<'_>) -> bool {
|
|
2592
|
+
left.kind() == right.kind()
|
|
2593
|
+
&& left.start_byte() == right.start_byte()
|
|
2594
|
+
&& left.end_byte() == right.end_byte()
|
|
2595
|
+
}
|