dart-decimate 0.0.1

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.
Files changed (188) hide show
  1. package/Cargo.lock +572 -0
  2. package/Cargo.toml +41 -0
  3. package/LICENSE +21 -0
  4. package/README.md +617 -0
  5. package/assets/dart-decimate-banner.png +0 -0
  6. package/npm/bin/dart-decimate-mcp.js +48 -0
  7. package/npm/bin/dart-decimate.js +48 -0
  8. package/npm/scripts/postinstall.js +46 -0
  9. package/npm/scripts/test-npx-local.js +54 -0
  10. package/npm/scripts/test-npx-mcp-local.js +56 -0
  11. package/package.json +45 -0
  12. package/src/baseline.rs +643 -0
  13. package/src/bin/dart-decimate-mcp.rs +11 -0
  14. package/src/boundaries/tests.rs +60 -0
  15. package/src/boundaries.rs +317 -0
  16. package/src/changed_scope.rs +117 -0
  17. package/src/ci_template.rs +399 -0
  18. package/src/cli/analyze.rs +462 -0
  19. package/src/cli/analyzer_options.rs +82 -0
  20. package/src/cli/audit_run.rs +327 -0
  21. package/src/cli/boundary_args.rs +44 -0
  22. package/src/cli/ci_template_run.rs +196 -0
  23. package/src/cli/command_name.rs +20 -0
  24. package/src/cli/common_args.rs +167 -0
  25. package/src/cli/config_run.rs +78 -0
  26. package/src/cli/coverage_run.rs +393 -0
  27. package/src/cli/decision_surface_run.rs +99 -0
  28. package/src/cli/default_command.rs +69 -0
  29. package/src/cli/dupes_args.rs +150 -0
  30. package/src/cli/entry_points.rs +121 -0
  31. package/src/cli/error.rs +149 -0
  32. package/src/cli/error_output.rs +68 -0
  33. package/src/cli/explain_run.rs +72 -0
  34. package/src/cli/fix_run.rs +162 -0
  35. package/src/cli/flags_args.rs +23 -0
  36. package/src/cli/health_args.rs +200 -0
  37. package/src/cli/hooks_run.rs +191 -0
  38. package/src/cli/impact_run.rs +95 -0
  39. package/src/cli/init_run.rs +49 -0
  40. package/src/cli/inspect_args.rs +20 -0
  41. package/src/cli/inspect_run.rs +66 -0
  42. package/src/cli/issue_filter_args.rs +240 -0
  43. package/src/cli/list_run.rs +311 -0
  44. package/src/cli/mode_args.rs +72 -0
  45. package/src/cli/output_format.rs +62 -0
  46. package/src/cli/regression_args.rs +156 -0
  47. package/src/cli/request_args.rs +175 -0
  48. package/src/cli/schema_commands.rs +72 -0
  49. package/src/cli/scope_args.rs +174 -0
  50. package/src/cli/security_args.rs +175 -0
  51. package/src/cli/security_gate_run.rs +66 -0
  52. package/src/cli/security_summary_run.rs +43 -0
  53. package/src/cli/summary_args.rs +12 -0
  54. package/src/cli/tests.rs +516 -0
  55. package/src/cli/trace_args.rs +67 -0
  56. package/src/cli/trace_run.rs +131 -0
  57. package/src/cli/trace_tests.rs +235 -0
  58. package/src/cli/unsupported_run.rs +116 -0
  59. package/src/cli/watch_run.rs +153 -0
  60. package/src/cli.rs +603 -0
  61. package/src/config/boundary_config.rs +235 -0
  62. package/src/config/cache.rs +25 -0
  63. package/src/config/dependencies.rs +114 -0
  64. package/src/config/health.rs +237 -0
  65. package/src/config/jsonc.rs +66 -0
  66. package/src/config/policy.rs +91 -0
  67. package/src/config/rule_aliases.rs +487 -0
  68. package/src/config/rules.rs +339 -0
  69. package/src/config.rs +635 -0
  70. package/src/coverage.rs +469 -0
  71. package/src/dart_parser.rs +673 -0
  72. package/src/decision_surface.rs +405 -0
  73. package/src/dependencies/analyze.rs +218 -0
  74. package/src/dependencies/codegen.rs +118 -0
  75. package/src/dependencies/discovery.rs +60 -0
  76. package/src/dependencies/overrides.rs +76 -0
  77. package/src/dependencies/private_src_imports.rs +32 -0
  78. package/src/dependencies/pubspec_document.rs +86 -0
  79. package/src/dependencies/pubspec_entry.rs +96 -0
  80. package/src/dependencies/tests.rs +544 -0
  81. package/src/dependencies/usage.rs +33 -0
  82. package/src/dependencies.rs +553 -0
  83. package/src/dependency_scripts.rs +91 -0
  84. package/src/dupes/lex.rs +236 -0
  85. package/src/dupes/tests.rs +119 -0
  86. package/src/dupes.rs +625 -0
  87. package/src/explain.rs +692 -0
  88. package/src/extract/directives.rs +252 -0
  89. package/src/extract/error_tests.rs +25 -0
  90. package/src/extract/members.rs +210 -0
  91. package/src/extract/primary_constructors.rs +421 -0
  92. package/src/extract/references.rs +151 -0
  93. package/src/extract/routes.rs +691 -0
  94. package/src/extract/signatures.rs +266 -0
  95. package/src/extract/string_tests.rs +18 -0
  96. package/src/extract/strings.rs +107 -0
  97. package/src/extract/tests.rs +697 -0
  98. package/src/extract.rs +680 -0
  99. package/src/feature_flags/tests.rs +76 -0
  100. package/src/feature_flags.rs +453 -0
  101. package/src/finding_identity.rs +60 -0
  102. package/src/fix.rs +551 -0
  103. package/src/generated.rs +53 -0
  104. package/src/graph/augment_tests.rs +212 -0
  105. package/src/graph/conditionals.rs +74 -0
  106. package/src/graph/package_config_tests.rs +100 -0
  107. package/src/graph/parts.rs +248 -0
  108. package/src/graph/pubspec_tests.rs +119 -0
  109. package/src/graph/tests.rs +837 -0
  110. package/src/graph/uri_tests.rs +102 -0
  111. package/src/graph.rs +644 -0
  112. package/src/health/coverage.rs +309 -0
  113. package/src/health/ownership.rs +175 -0
  114. package/src/health/runtime_coverage.rs +616 -0
  115. package/src/health/runtime_intelligence.rs +511 -0
  116. package/src/health/scores.rs +296 -0
  117. package/src/health/tests.rs +240 -0
  118. package/src/health/threshold_types.rs +163 -0
  119. package/src/health/thresholds.rs +229 -0
  120. package/src/health/types.rs +636 -0
  121. package/src/health.rs +597 -0
  122. package/src/hooks/agent_settings.rs +175 -0
  123. package/src/hooks/templates.rs +51 -0
  124. package/src/hooks.rs +569 -0
  125. package/src/impact.rs +287 -0
  126. package/src/init.rs +237 -0
  127. package/src/inspect.rs +142 -0
  128. package/src/intelligence/tests.rs +232 -0
  129. package/src/intelligence.rs +305 -0
  130. package/src/lib.rs +198 -0
  131. package/src/main.rs +5 -0
  132. package/src/manifest/mcp.rs +565 -0
  133. package/src/manifest.rs +552 -0
  134. package/src/mcp/cli_args/value_args.rs +175 -0
  135. package/src/mcp/cli_args.rs +658 -0
  136. package/src/mcp/cli_args_tests.rs +522 -0
  137. package/src/mcp/code_execute.rs +671 -0
  138. package/src/mcp/tools.rs +660 -0
  139. package/src/mcp.rs +325 -0
  140. package/src/output/audit_risk.rs +135 -0
  141. package/src/output/dependency_findings.rs +327 -0
  142. package/src/output/duplication_findings.rs +114 -0
  143. package/src/output/feature_flag_findings.rs +122 -0
  144. package/src/output/format.rs +31 -0
  145. package/src/output/graph_findings.rs +461 -0
  146. package/src/output/health_findings.rs +612 -0
  147. package/src/output/human.rs +27 -0
  148. package/src/output/next_steps.rs +92 -0
  149. package/src/output/route_findings.rs +76 -0
  150. package/src/output/runtime_coverage.rs +515 -0
  151. package/src/output/scope.rs +183 -0
  152. package/src/output/security_findings.rs +390 -0
  153. package/src/output/security_sarif.rs +150 -0
  154. package/src/output/suppressions.rs +323 -0
  155. package/src/output/symbol_findings.rs +257 -0
  156. package/src/output/tests.rs +114 -0
  157. package/src/output/types.rs +604 -0
  158. package/src/output/widget_findings.rs +213 -0
  159. package/src/output.rs +682 -0
  160. package/src/package_map.rs +495 -0
  161. package/src/policy/packs.rs +232 -0
  162. package/src/policy/tests.rs +134 -0
  163. package/src/policy.rs +640 -0
  164. package/src/project_list.rs +376 -0
  165. package/src/report_schema/inventory.rs +418 -0
  166. package/src/report_schema.rs +488 -0
  167. package/src/routes.rs +354 -0
  168. package/src/scan/tests.rs +250 -0
  169. package/src/scan.rs +275 -0
  170. package/src/security/detect.rs +639 -0
  171. package/src/security/tests.rs +178 -0
  172. package/src/security.rs +373 -0
  173. package/src/security_gate.rs +555 -0
  174. package/src/symbols/path_filters.rs +27 -0
  175. package/src/symbols/private_type_leaks.rs +125 -0
  176. package/src/symbols/riverpod.rs +113 -0
  177. package/src/symbols/tests.rs +692 -0
  178. package/src/symbols.rs +693 -0
  179. package/src/trace/tests.rs +225 -0
  180. package/src/trace.rs +690 -0
  181. package/src/unsupported.rs +47 -0
  182. package/src/widgets/lifecycle.rs +484 -0
  183. package/src/widgets/params.rs +236 -0
  184. package/src/widgets/tests.rs +430 -0
  185. package/src/widgets/top_level.rs +110 -0
  186. package/src/widgets/unrendered.rs +374 -0
  187. package/src/widgets.rs +590 -0
  188. package/src/workspace_scope.rs +358 -0
@@ -0,0 +1,47 @@
1
+ use serde::{Deserialize, Serialize};
2
+
3
+ pub const UNSUPPORTED_SCHEMA_VERSION: &str = "dart-decimate.unsupported.v1";
4
+
5
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
6
+ pub struct UnsupportedReport {
7
+ pub schema_version: String,
8
+ pub kind: String,
9
+ pub tool: String,
10
+ pub command: String,
11
+ pub supported: bool,
12
+ pub status: String,
13
+ pub reason: String,
14
+ pub alternatives: Vec<String>,
15
+ }
16
+
17
+ #[must_use]
18
+ pub fn unsupported_report(
19
+ command: impl Into<String>,
20
+ status: impl Into<String>,
21
+ reason: impl Into<String>,
22
+ alternatives: Vec<String>,
23
+ ) -> UnsupportedReport {
24
+ UnsupportedReport {
25
+ schema_version: UNSUPPORTED_SCHEMA_VERSION.to_owned(),
26
+ kind: "unsupported".to_owned(),
27
+ tool: "dart-decimate".to_owned(),
28
+ command: command.into(),
29
+ supported: false,
30
+ status: status.into(),
31
+ reason: reason.into(),
32
+ alternatives,
33
+ }
34
+ }
35
+
36
+ #[must_use]
37
+ pub fn render_unsupported_report(report: &UnsupportedReport) -> String {
38
+ let alternatives = if report.alternatives.is_empty() {
39
+ String::new()
40
+ } else {
41
+ format!("\nAlternatives:\n- {}\n", report.alternatives.join("\n- "))
42
+ };
43
+ format!(
44
+ "{}: {} ({}){}\n",
45
+ report.command, report.reason, report.status, alternatives
46
+ )
47
+ }
@@ -0,0 +1,484 @@
1
+ use std::path::{Path, PathBuf};
2
+
3
+ use serde::{Deserialize, Serialize};
4
+ use tree_sitter::Node;
5
+
6
+ use crate::Location;
7
+
8
+ use super::{state_widget_class, widget_kind};
9
+
10
+ /// A widget or `State` await without the required post-await mounted guard.
11
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
12
+ pub struct MissingContextMountedAfterAwait {
13
+ /// Dart file containing the await.
14
+ pub path: PathBuf,
15
+ /// Widget or `State` method containing the await.
16
+ pub owner: String,
17
+ /// Location of the await expression.
18
+ pub location: Location,
19
+ }
20
+
21
+ #[derive(Debug, Default, Clone, PartialEq, Eq)]
22
+ pub(super) struct LifecycleFindings {
23
+ pub(super) missing_context_mounted_after_await: Vec<MissingContextMountedAfterAwait>,
24
+ }
25
+
26
+ pub(super) fn lifecycle_findings(
27
+ path: &Path,
28
+ classes: &[Node<'_>],
29
+ source: &str,
30
+ ) -> LifecycleFindings {
31
+ let mut findings = LifecycleFindings::default();
32
+ for class in classes {
33
+ let class_name = class_name(*class, source).unwrap_or_default();
34
+ if widget_kind(*class, source).is_some() || state_widget_class(*class, source).is_some() {
35
+ collect_context_guards(path, *class, &class_name, source, &mut findings);
36
+ }
37
+ }
38
+ findings
39
+ }
40
+
41
+ fn collect_context_guards(
42
+ path: &Path,
43
+ class: Node<'_>,
44
+ class_name: &str,
45
+ source: &str,
46
+ findings: &mut LifecycleFindings,
47
+ ) {
48
+ for method in class_methods(class) {
49
+ let method_name = method_name(method, source).unwrap_or_else(|| "<method>".to_owned());
50
+ let Some(body) = method.child_by_field_name("body") else {
51
+ continue;
52
+ };
53
+ for await_node in unguarded_awaits(body, source, "context") {
54
+ findings
55
+ .missing_context_mounted_after_await
56
+ .push(MissingContextMountedAfterAwait {
57
+ path: path.to_path_buf(),
58
+ owner: format!("{class_name}.{method_name}"),
59
+ location: await_node.start_position().into(),
60
+ });
61
+ }
62
+ }
63
+ }
64
+
65
+ fn class_methods(class: Node<'_>) -> Vec<Node<'_>> {
66
+ let mut methods = Vec::new();
67
+ collect_nodes(class, "method_declaration", &mut methods);
68
+ methods
69
+ }
70
+
71
+ #[derive(Clone, Copy)]
72
+ enum GuardMode {
73
+ Normal,
74
+ Finally,
75
+ }
76
+
77
+ #[derive(Clone, Copy)]
78
+ enum GuardRequirement {
79
+ LifecycleUse,
80
+ }
81
+
82
+ fn unguarded_awaits<'tree>(body: Node<'tree>, source: &str, receiver: &str) -> Vec<Node<'tree>> {
83
+ let mut findings = Vec::new();
84
+ collect_unguarded_awaits_in_function_body(
85
+ body,
86
+ source,
87
+ receiver,
88
+ GuardRequirement::LifecycleUse,
89
+ &mut findings,
90
+ );
91
+ findings
92
+ }
93
+
94
+ fn collect_unguarded_awaits_in_function_body<'tree>(
95
+ body: Node<'tree>,
96
+ source: &str,
97
+ receiver: &str,
98
+ requirement: GuardRequirement,
99
+ findings: &mut Vec<Node<'tree>>,
100
+ ) {
101
+ if body.kind() == "block" {
102
+ collect_unguarded_awaits_in_block(
103
+ body,
104
+ source,
105
+ receiver,
106
+ GuardMode::Normal,
107
+ requirement,
108
+ false,
109
+ findings,
110
+ );
111
+ return;
112
+ }
113
+ if let Some(block) = direct_named_child(body, "block") {
114
+ collect_unguarded_awaits_in_block(
115
+ block,
116
+ source,
117
+ receiver,
118
+ GuardMode::Normal,
119
+ requirement,
120
+ false,
121
+ findings,
122
+ );
123
+ return;
124
+ }
125
+ collect_expression_body_awaits(body, source, receiver, findings);
126
+ collect_child_block_awaits(
127
+ body,
128
+ source,
129
+ receiver,
130
+ GuardMode::Normal,
131
+ requirement,
132
+ findings,
133
+ );
134
+ }
135
+
136
+ fn collect_expression_body_awaits<'tree>(
137
+ body: Node<'tree>,
138
+ source: &str,
139
+ receiver: &str,
140
+ findings: &mut Vec<Node<'tree>>,
141
+ ) {
142
+ if !contains_await(body) || !contains_lifecycle_use(body, source, receiver) {
143
+ return;
144
+ }
145
+ let mut awaits = Vec::new();
146
+ collect_awaits(body, true, &mut awaits);
147
+ findings.extend(awaits);
148
+ }
149
+
150
+ fn collect_unguarded_awaits_in_block<'tree>(
151
+ block: Node<'tree>,
152
+ source: &str,
153
+ receiver: &str,
154
+ mode: GuardMode,
155
+ requirement: GuardRequirement,
156
+ trailing_guard: bool,
157
+ findings: &mut Vec<Node<'tree>>,
158
+ ) {
159
+ let statements = block_statements(block);
160
+ for (index, statement) in statements.iter().copied().enumerate() {
161
+ let awaits = direct_awaits(statement);
162
+ if awaits.is_empty() {
163
+ collect_child_block_awaits(statement, source, receiver, mode, requirement, findings);
164
+ continue;
165
+ }
166
+ if is_terminal_return_await(statement) {
167
+ collect_child_block_awaits(statement, source, receiver, mode, requirement, findings);
168
+ continue;
169
+ }
170
+ let guarded = statements
171
+ .get(index + 1)
172
+ .copied()
173
+ .is_some_and(|next| is_valid_post_await_guard(next, source, receiver, mode))
174
+ || (index + 1 == statements.len() && trailing_guard);
175
+ let required = match requirement {
176
+ GuardRequirement::LifecycleUse => {
177
+ requires_post_await_guard(statement, &statements[index + 1..], source, receiver)
178
+ }
179
+ };
180
+ if !guarded && required {
181
+ findings.extend(awaits);
182
+ }
183
+ collect_child_block_awaits(statement, source, receiver, mode, requirement, findings);
184
+ }
185
+ }
186
+
187
+ fn requires_post_await_guard(
188
+ statement: Node<'_>,
189
+ following: &[Node<'_>],
190
+ source: &str,
191
+ receiver: &str,
192
+ ) -> bool {
193
+ contains_lifecycle_use(statement, source, receiver)
194
+ || following
195
+ .iter()
196
+ .copied()
197
+ .any(|statement| contains_lifecycle_use(statement, source, receiver))
198
+ }
199
+
200
+ fn collect_child_block_awaits<'tree>(
201
+ node: Node<'tree>,
202
+ source: &str,
203
+ receiver: &str,
204
+ mode: GuardMode,
205
+ requirement: GuardRequirement,
206
+ findings: &mut Vec<Node<'tree>>,
207
+ ) {
208
+ if node.kind() == "function_expression" {
209
+ if let Some(body) = node.child_by_field_name("body") {
210
+ collect_unguarded_awaits_in_function_body(
211
+ body,
212
+ source,
213
+ receiver,
214
+ GuardRequirement::LifecycleUse,
215
+ findings,
216
+ );
217
+ }
218
+ return;
219
+ }
220
+ if is_nested_non_closure_scope(node.kind()) {
221
+ return;
222
+ }
223
+ if node.kind() == "try_statement" {
224
+ collect_try_awaits(node, source, receiver, mode, requirement, findings);
225
+ return;
226
+ }
227
+ let mut cursor = node.walk();
228
+ for child in node.named_children(&mut cursor) {
229
+ if child.kind() == "block" {
230
+ collect_unguarded_awaits_in_block(
231
+ child,
232
+ source,
233
+ receiver,
234
+ mode,
235
+ requirement,
236
+ false,
237
+ findings,
238
+ );
239
+ } else {
240
+ collect_child_block_awaits(child, source, receiver, mode, requirement, findings);
241
+ }
242
+ }
243
+ }
244
+
245
+ fn collect_try_awaits<'tree>(
246
+ try_statement: Node<'tree>,
247
+ source: &str,
248
+ receiver: &str,
249
+ mode: GuardMode,
250
+ requirement: GuardRequirement,
251
+ findings: &mut Vec<Node<'tree>>,
252
+ ) {
253
+ let body = try_statement.child_by_field_name("body");
254
+ let has_finally_guard = receiver == "ref" && finally_has_positive_guard(try_statement, source);
255
+ if let Some(block) = body {
256
+ collect_unguarded_awaits_in_block(
257
+ block,
258
+ source,
259
+ receiver,
260
+ mode,
261
+ requirement,
262
+ has_finally_guard,
263
+ findings,
264
+ );
265
+ }
266
+
267
+ let mut cursor = try_statement.walk();
268
+ for child in try_statement.named_children(&mut cursor) {
269
+ if body.is_some_and(|body| same_node(body, child)) {
270
+ continue;
271
+ }
272
+ if child.kind() == "finally_clause" {
273
+ if let Some(block) = direct_named_child(child, "block") {
274
+ collect_unguarded_awaits_in_block(
275
+ block,
276
+ source,
277
+ receiver,
278
+ GuardMode::Finally,
279
+ requirement,
280
+ false,
281
+ findings,
282
+ );
283
+ }
284
+ } else {
285
+ collect_child_block_awaits(child, source, receiver, mode, requirement, findings);
286
+ }
287
+ }
288
+ }
289
+
290
+ fn block_statements(block: Node<'_>) -> Vec<Node<'_>> {
291
+ let mut cursor = block.walk();
292
+ block
293
+ .named_children(&mut cursor)
294
+ .filter(|child| is_statement_kind(child.kind()))
295
+ .collect()
296
+ }
297
+
298
+ fn direct_awaits(statement: Node<'_>) -> Vec<Node<'_>> {
299
+ let mut awaits = Vec::new();
300
+ collect_awaits(statement, true, &mut awaits);
301
+ awaits
302
+ }
303
+
304
+ fn collect_awaits<'tree>(node: Node<'tree>, root: bool, awaits: &mut Vec<Node<'tree>>) {
305
+ if !root && (node.kind() == "block" || is_nested_function_scope(node.kind())) {
306
+ return;
307
+ }
308
+ if node.kind() == "await_expression" {
309
+ awaits.push(node);
310
+ }
311
+ let mut cursor = node.walk();
312
+ for child in node.named_children(&mut cursor) {
313
+ collect_awaits(child, false, awaits);
314
+ }
315
+ }
316
+
317
+ fn is_valid_post_await_guard(
318
+ node: Node<'_>,
319
+ source: &str,
320
+ receiver: &str,
321
+ mode: GuardMode,
322
+ ) -> bool {
323
+ match mode {
324
+ GuardMode::Normal => is_mounted_return_guard(node, source, receiver),
325
+ GuardMode::Finally => receiver == "ref" && is_mounted_positive_block_guard(node, source),
326
+ }
327
+ }
328
+
329
+ fn is_mounted_return_guard(node: Node<'_>, source: &str, receiver: &str) -> bool {
330
+ if node.kind() != "if_statement" {
331
+ return false;
332
+ }
333
+ let Ok(text) = node.utf8_text(source.as_bytes()) else {
334
+ return false;
335
+ };
336
+ let compact = compact(text);
337
+ let unbraced = format!("if(!{receiver}.mounted)return");
338
+ let braced = format!("if(!{receiver}.mounted){{return");
339
+ (compact.starts_with(&unbraced) && compact.ends_with(';'))
340
+ || (compact.starts_with(&braced) && compact.ends_with(";}"))
341
+ }
342
+
343
+ fn is_mounted_positive_block_guard(node: Node<'_>, source: &str) -> bool {
344
+ if node.kind() != "if_statement" || node.child_by_field_name("alternative").is_some() {
345
+ return false;
346
+ }
347
+ let Ok(text) = node.utf8_text(source.as_bytes()) else {
348
+ return false;
349
+ };
350
+ let compact = compact(text);
351
+ compact.starts_with("if(ref.mounted){") && compact.ends_with('}')
352
+ }
353
+
354
+ fn finally_has_positive_guard(try_statement: Node<'_>, source: &str) -> bool {
355
+ let Some(finally) = direct_named_child(try_statement, "finally_clause") else {
356
+ return false;
357
+ };
358
+ let Some(block) = direct_named_child(finally, "block") else {
359
+ return false;
360
+ };
361
+ block_statements(block)
362
+ .first()
363
+ .copied()
364
+ .is_some_and(|statement| is_mounted_positive_block_guard(statement, source))
365
+ }
366
+
367
+ fn is_terminal_return_await(statement: Node<'_>) -> bool {
368
+ statement.kind() == "return_statement" && contains_await(statement)
369
+ }
370
+
371
+ fn contains_await(node: Node<'_>) -> bool {
372
+ if node.kind() == "await_expression" {
373
+ return true;
374
+ }
375
+ let mut cursor = node.walk();
376
+ node.named_children(&mut cursor).any(contains_await)
377
+ }
378
+
379
+ fn contains_lifecycle_use(node: Node<'_>, source: &str, receiver: &str) -> bool {
380
+ if receiver == "context" {
381
+ contains_identifier(node, source, "context", true)
382
+ } else {
383
+ contains_identifier(node, source, "ref", true)
384
+ || contains_identifier(node, source, "state", true)
385
+ }
386
+ }
387
+
388
+ fn contains_identifier(node: Node<'_>, source: &str, name: &str, root: bool) -> bool {
389
+ if !root && is_nested_function_scope(node.kind()) {
390
+ return false;
391
+ }
392
+ if node.kind() == "identifier" && node.utf8_text(source.as_bytes()).ok() == Some(name) {
393
+ return true;
394
+ }
395
+ let mut cursor = node.walk();
396
+ node.named_children(&mut cursor)
397
+ .any(|child| contains_identifier(child, source, name, false))
398
+ }
399
+
400
+ fn class_name(class: Node<'_>, source: &str) -> Option<String> {
401
+ class
402
+ .child_by_field_name("name")?
403
+ .utf8_text(source.as_bytes())
404
+ .ok()
405
+ .map(str::to_owned)
406
+ }
407
+
408
+ fn method_name(method: Node<'_>, source: &str) -> Option<String> {
409
+ let signature = method.child_by_field_name("signature")?;
410
+ let inner = first_named_child(signature)?;
411
+ field_text(inner, "name", source)
412
+ }
413
+
414
+ fn field_text(node: Node<'_>, field_name: &str, source: &str) -> Option<String> {
415
+ node.child_by_field_name(field_name)
416
+ .and_then(|child| child.utf8_text(source.as_bytes()).ok())
417
+ .map(str::to_owned)
418
+ }
419
+
420
+ fn collect_nodes<'tree>(node: Node<'tree>, kind: &str, nodes: &mut Vec<Node<'tree>>) {
421
+ if node.kind() == kind {
422
+ nodes.push(node);
423
+ }
424
+ let mut cursor = node.walk();
425
+ for child in node.named_children(&mut cursor) {
426
+ collect_nodes(child, kind, nodes);
427
+ }
428
+ }
429
+
430
+ fn direct_named_child<'tree>(node: Node<'tree>, kind: &str) -> Option<Node<'tree>> {
431
+ let mut cursor = node.walk();
432
+ node.named_children(&mut cursor)
433
+ .find(|child| child.kind() == kind)
434
+ }
435
+
436
+ fn first_named_child(node: Node<'_>) -> Option<Node<'_>> {
437
+ let mut cursor = node.walk();
438
+ node.named_children(&mut cursor).next()
439
+ }
440
+
441
+ fn same_node(left: Node<'_>, right: Node<'_>) -> bool {
442
+ left.start_byte() == right.start_byte() && left.end_byte() == right.end_byte()
443
+ }
444
+
445
+ fn is_statement_kind(kind: &str) -> bool {
446
+ matches!(
447
+ kind,
448
+ "assert_statement"
449
+ | "block"
450
+ | "break_statement"
451
+ | "continue_statement"
452
+ | "do_statement"
453
+ | "expression_statement"
454
+ | "for_statement"
455
+ | "if_statement"
456
+ | "local_variable_declaration"
457
+ | "return_statement"
458
+ | "switch_statement"
459
+ | "try_statement"
460
+ | "while_statement"
461
+ | "yield_statement"
462
+ )
463
+ }
464
+
465
+ fn is_nested_non_closure_scope(kind: &str) -> bool {
466
+ is_nested_function_scope(kind) && kind != "function_expression"
467
+ }
468
+
469
+ fn is_nested_function_scope(kind: &str) -> bool {
470
+ matches!(
471
+ kind,
472
+ "function_expression"
473
+ | "function_declaration"
474
+ | "method_declaration"
475
+ | "getter_declaration"
476
+ | "setter_declaration"
477
+ )
478
+ }
479
+
480
+ fn compact(text: &str) -> String {
481
+ text.chars()
482
+ .filter(|character| !character.is_whitespace())
483
+ .collect()
484
+ }