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
package/src/extract.rs ADDED
@@ -0,0 +1,680 @@
1
+ use std::fs;
2
+ use std::path::{Path, PathBuf};
3
+
4
+ use serde::{Deserialize, Serialize};
5
+ use thiserror::Error;
6
+ use tree_sitter::{Node, TreeCursor};
7
+
8
+ mod directives;
9
+ mod members;
10
+ mod primary_constructors;
11
+ mod references;
12
+ mod routes;
13
+ mod signatures;
14
+ mod strings;
15
+ use directives::{
16
+ extract_directive, extract_library_name, extract_part_directive, extract_part_of_directive,
17
+ };
18
+ use members::push_class_like_members;
19
+ use primary_constructors::{
20
+ extract_primary_constructor_identifier_references,
21
+ extract_primary_constructor_signature_references,
22
+ };
23
+ use references::extract_identifier_references;
24
+ pub use routes::DartRouteDeclaration;
25
+ use routes::extract_route_declarations;
26
+ pub use signatures::SignatureReference;
27
+ use signatures::extract_signature_references;
28
+ use strings::unquote_dart_string;
29
+
30
+ /// A 1-based line and 0-based byte column in a Dart source file.
31
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
32
+ pub struct Location {
33
+ /// 1-based source line.
34
+ pub line: usize,
35
+ /// 0-based UTF-8 byte column.
36
+ pub column: usize,
37
+ }
38
+
39
+ impl From<tree_sitter::Point> for Location {
40
+ fn from(point: tree_sitter::Point) -> Self {
41
+ Self {
42
+ line: point.row + 1,
43
+ column: point.column,
44
+ }
45
+ }
46
+ }
47
+
48
+ /// A 1-based source line range.
49
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
50
+ pub struct SourceRange {
51
+ /// 1-based first line.
52
+ pub start_line: usize,
53
+ /// 1-based last line.
54
+ pub end_line: usize,
55
+ }
56
+
57
+ impl SourceRange {
58
+ fn from_node(node: Node<'_>) -> Self {
59
+ Self {
60
+ start_line: node.start_position().row + 1,
61
+ end_line: node.end_position().row + 1,
62
+ }
63
+ }
64
+ }
65
+
66
+ /// A parsed Dart file reduced to graph-relevant syntax facts.
67
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
68
+ pub struct DartFile {
69
+ /// File path supplied by the caller.
70
+ pub path: PathBuf,
71
+ /// Optional `library` directive metadata.
72
+ pub library: Option<DartLibrary>,
73
+ /// Optional `part of` directive metadata.
74
+ pub part_of: Option<DartPartOf>,
75
+ /// Static import directives in source order.
76
+ pub imports: Vec<DartImport>,
77
+ /// Static export directives in source order.
78
+ pub exports: Vec<DartExport>,
79
+ /// Part directives in source order.
80
+ pub parts: Vec<DartPart>,
81
+ /// Top-level declarations in source order.
82
+ pub declarations: Vec<TopLevelDeclaration>,
83
+ /// Class-like member declarations in source order.
84
+ pub members: Vec<MemberDeclaration>,
85
+ /// Identifier uses in source order, excluding import/export metadata and
86
+ /// obvious declaration-name positions.
87
+ pub references: Vec<IdentifierReference>,
88
+ /// Type references from public declaration signatures, excluding bodies.
89
+ pub signature_references: Vec<SignatureReference>,
90
+ /// Typed `GoRouter` route declarations found in metadata.
91
+ pub routes: Vec<DartRouteDeclaration>,
92
+ }
93
+
94
+ /// A Dart `library` directive.
95
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
96
+ pub struct DartLibrary {
97
+ /// Dotted library name, if the directive names the library.
98
+ pub name: Option<String>,
99
+ /// Augmentation URI from `library augment`, if present.
100
+ pub augment_uri: Option<String>,
101
+ /// Location of the `library_name` syntax node.
102
+ pub location: Location,
103
+ }
104
+
105
+ /// A Dart `import` directive.
106
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
107
+ pub struct DartImport {
108
+ /// The import URI without surrounding quotes.
109
+ pub uri: String,
110
+ /// Conditional import guard for a `if (dart.library.*)` branch.
111
+ #[serde(skip_serializing_if = "Option::is_none")]
112
+ pub condition: Option<DartUriCondition>,
113
+ /// Optional import prefix after `as`.
114
+ pub prefix: Option<String>,
115
+ /// Whether this import uses `deferred as`.
116
+ pub deferred: bool,
117
+ /// `show` and `hide` combinators applied to this import.
118
+ pub combinators: Vec<DartCombinator>,
119
+ /// Location of the `import_or_export` syntax node.
120
+ pub location: Location,
121
+ }
122
+
123
+ /// A Dart `export` directive.
124
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
125
+ pub struct DartExport {
126
+ /// The export URI without surrounding quotes.
127
+ pub uri: String,
128
+ /// Conditional export guard for a `if (dart.library.*)` branch.
129
+ #[serde(skip_serializing_if = "Option::is_none")]
130
+ pub condition: Option<DartUriCondition>,
131
+ /// `show` and `hide` combinators applied to this export.
132
+ pub combinators: Vec<DartCombinator>,
133
+ /// Location of the `import_or_export` syntax node.
134
+ pub location: Location,
135
+ }
136
+
137
+ /// A Dart import/export combinator.
138
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
139
+ pub struct DartCombinator {
140
+ /// `show` or `hide`.
141
+ pub kind: DartCombinatorKind,
142
+ /// Names listed by the combinator.
143
+ pub names: Vec<String>,
144
+ /// Location of the combinator syntax node.
145
+ pub location: Location,
146
+ }
147
+
148
+ /// A Dart configurable URI condition, such as `dart.library.html == 'true'`.
149
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
150
+ pub struct DartUriCondition {
151
+ /// Environment declaration tested by the configurable URI branch.
152
+ pub variable: String,
153
+ /// String value required by the branch. Bare tests require `"true"`.
154
+ pub expected_value: String,
155
+ /// Location of the `uri_test` syntax node.
156
+ pub location: Location,
157
+ }
158
+
159
+ /// Dart import/export combinator kind.
160
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
161
+ #[serde(rename_all = "kebab-case")]
162
+ pub enum DartCombinatorKind {
163
+ /// `show`.
164
+ Show,
165
+ /// `hide`.
166
+ Hide,
167
+ }
168
+
169
+ /// A Dart `part` directive.
170
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
171
+ pub struct DartPart {
172
+ /// The part URI without surrounding quotes.
173
+ pub uri: String,
174
+ /// Location of the `part_directive` syntax node.
175
+ pub location: Location,
176
+ }
177
+
178
+ /// A Dart `part of` directive.
179
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
180
+ pub struct DartPartOf {
181
+ /// Dotted library name form, if the directive uses `part of name;`.
182
+ pub name: Option<String>,
183
+ /// URI form, if the directive uses `part of 'library.dart';`.
184
+ pub uri: Option<String>,
185
+ /// Location of the `part_of_directive` syntax node.
186
+ pub location: Location,
187
+ }
188
+
189
+ /// A top-level declaration that can become a graph or symbol node.
190
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
191
+ pub struct TopLevelDeclaration {
192
+ /// Declaration kind.
193
+ pub kind: DeclarationKind,
194
+ /// Declared name.
195
+ pub name: String,
196
+ /// Location of the declaration node.
197
+ pub location: Location,
198
+ /// Source line range covered by the declaration node.
199
+ pub range: SourceRange,
200
+ }
201
+
202
+ /// A class-like member declaration that can become a future symbol node.
203
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
204
+ pub struct MemberDeclaration {
205
+ /// Owning top-level class-like declaration.
206
+ pub owner: String,
207
+ /// Member kind.
208
+ pub kind: MemberKind,
209
+ /// Declared member name.
210
+ pub name: String,
211
+ /// Location of the member declaration node.
212
+ pub location: Location,
213
+ }
214
+
215
+ /// A syntactic identifier or type identifier use.
216
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
217
+ pub struct IdentifierReference {
218
+ /// Referenced identifier text.
219
+ pub name: String,
220
+ /// Location of the identifier token.
221
+ pub location: Location,
222
+ }
223
+
224
+ /// Top-level declaration categories extracted in Phase 1.
225
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
226
+ #[serde(rename_all = "kebab-case")]
227
+ pub enum DeclarationKind {
228
+ /// A `class` declaration, including modifiers such as `sealed` or `base`.
229
+ Class,
230
+ /// A `mixin` declaration.
231
+ Mixin,
232
+ /// A named `extension` declaration.
233
+ Extension,
234
+ /// An `extension type` declaration.
235
+ ExtensionType,
236
+ /// An `enum` declaration.
237
+ Enum,
238
+ /// A `typedef` declaration.
239
+ TypeAlias,
240
+ /// A top-level variable declaration.
241
+ Variable,
242
+ /// A top-level function declaration, including `external` functions.
243
+ Function,
244
+ }
245
+
246
+ /// Class-like member declaration categories extracted in Phase 1.
247
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
248
+ #[serde(rename_all = "kebab-case")]
249
+ pub enum MemberKind {
250
+ /// An enum constant.
251
+ EnumConstant,
252
+ /// A field declaration.
253
+ Field,
254
+ /// A getter declaration.
255
+ Getter,
256
+ /// A setter declaration.
257
+ Setter,
258
+ /// A method declaration.
259
+ Method,
260
+ /// A constructor or factory constructor.
261
+ Constructor,
262
+ /// An operator overload.
263
+ Operator,
264
+ }
265
+
266
+ /// Errors returned by the Dart extraction phase.
267
+ #[derive(Debug, Error)]
268
+ pub enum ExtractError {
269
+ /// The file could not be read from disk.
270
+ #[error("failed to read Dart file {path}: {source}")]
271
+ Read {
272
+ /// Path that failed to read.
273
+ path: PathBuf,
274
+ /// Underlying IO error.
275
+ source: std::io::Error,
276
+ },
277
+ /// Tree-Sitter rejected the Dart grammar.
278
+ #[error("failed to load Dart grammar: {0}")]
279
+ Language(#[from] tree_sitter::LanguageError),
280
+ /// Tree-Sitter did not produce a parse tree.
281
+ #[error("tree-sitter did not return a parse tree for {path}")]
282
+ ParseCancelled {
283
+ /// Path being parsed.
284
+ path: PathBuf,
285
+ },
286
+ /// The source parsed with syntax errors.
287
+ #[error("Dart syntax errors found in {path}")]
288
+ Syntax {
289
+ /// Path being parsed.
290
+ path: PathBuf,
291
+ },
292
+ }
293
+
294
+ /// Extract graph-relevant syntax facts from a Dart file on disk.
295
+ ///
296
+ /// This phase intentionally ignores function bodies and does not perform type
297
+ /// evaluation, name resolution, or import resolution.
298
+ ///
299
+ /// # Errors
300
+ ///
301
+ /// Returns [`ExtractError::Read`] when the file cannot be read, or a parser
302
+ /// error when Tree-Sitter cannot load the Dart grammar, cannot produce a tree,
303
+ /// or finds Dart syntax errors.
304
+ pub fn extract_dart_file(path: impl AsRef<Path>) -> Result<DartFile, ExtractError> {
305
+ let path = path.as_ref();
306
+ let source = fs::read_to_string(path).map_err(|source| ExtractError::Read {
307
+ path: path.to_path_buf(),
308
+ source,
309
+ })?;
310
+
311
+ extract_dart_source(path, &source)
312
+ }
313
+
314
+ /// Extract graph-relevant syntax facts from Dart source text.
315
+ ///
316
+ /// `path` is carried into the returned facts and error messages. It does not
317
+ /// need to exist on disk.
318
+ ///
319
+ /// # Errors
320
+ ///
321
+ /// Returns a parser error when Tree-Sitter cannot load the Dart grammar, cannot
322
+ /// produce a tree, or finds Dart syntax errors.
323
+ pub fn extract_dart_source(path: impl AsRef<Path>, source: &str) -> Result<DartFile, ExtractError> {
324
+ let path = path.as_ref().to_path_buf();
325
+ let parsed =
326
+ crate::dart_parser::parse_dart_source_strict(&path, source).map_err(extract_parse_error)?;
327
+ let root = parsed.tree().root_node();
328
+ let parse_source = parsed.source();
329
+
330
+ let mut library = None;
331
+ let mut part_of = None;
332
+ let mut imports = Vec::new();
333
+ let mut exports = Vec::new();
334
+ let mut parts = Vec::new();
335
+ let mut declarations = Vec::new();
336
+ let mut members = Vec::new();
337
+ let mut references = extract_identifier_references(root, parse_source);
338
+ references.extend(extract_primary_constructor_identifier_references(source));
339
+ sort_identifier_references(&mut references);
340
+ let mut signature_references = extract_signature_references(root, parse_source);
341
+ signature_references.extend(extract_primary_constructor_signature_references(source));
342
+ sort_signature_references(&mut signature_references);
343
+ let routes = extract_route_declarations(root, parse_source);
344
+
345
+ let mut cursor = root.walk();
346
+ for child in root.named_children(&mut cursor) {
347
+ match child.kind() {
348
+ "library_name" => library = Some(extract_library_name(child, parse_source)),
349
+ "import_or_export" => {
350
+ extract_directive(child, parse_source, &mut imports, &mut exports);
351
+ }
352
+ "part_directive" => extract_part_directive(child, parse_source, &mut parts),
353
+ "part_of_directive" => {
354
+ part_of = Some(extract_part_of_directive(child, parse_source));
355
+ }
356
+ "class_declaration" => {
357
+ push_class_declaration(&mut declarations, child, parse_source);
358
+ push_class_like_members(&mut members, child, parse_source);
359
+ }
360
+ "mixin_declaration" => {
361
+ push_named_declaration(
362
+ &mut declarations,
363
+ child,
364
+ parse_source,
365
+ DeclarationKind::Mixin,
366
+ );
367
+ push_class_like_members(&mut members, child, parse_source);
368
+ }
369
+ "extension_declaration" => {
370
+ push_named_declaration(
371
+ &mut declarations,
372
+ child,
373
+ parse_source,
374
+ DeclarationKind::Extension,
375
+ );
376
+ push_class_like_members(&mut members, child, parse_source);
377
+ }
378
+ "extension_type_declaration" => {
379
+ push_named_declaration(
380
+ &mut declarations,
381
+ child,
382
+ parse_source,
383
+ DeclarationKind::ExtensionType,
384
+ );
385
+ push_class_like_members(&mut members, child, parse_source);
386
+ }
387
+ "enum_declaration" => {
388
+ push_named_declaration(
389
+ &mut declarations,
390
+ child,
391
+ parse_source,
392
+ DeclarationKind::Enum,
393
+ );
394
+ push_class_like_members(&mut members, child, parse_source);
395
+ }
396
+ "type_alias" => push_type_alias_declaration(&mut declarations, child, parse_source),
397
+ "top_level_variable_declaration" | "external_variable_declaration" => {
398
+ push_variable_declarations(&mut declarations, child, parse_source);
399
+ }
400
+ "function_declaration"
401
+ | "external_function_declaration"
402
+ | "getter_declaration"
403
+ | "external_getter_declaration"
404
+ | "setter_declaration"
405
+ | "external_setter_declaration" => {
406
+ push_function_declaration(&mut declarations, child, parse_source);
407
+ }
408
+ _ => {}
409
+ }
410
+ }
411
+
412
+ Ok(DartFile {
413
+ path,
414
+ library,
415
+ part_of,
416
+ imports,
417
+ exports,
418
+ parts,
419
+ declarations,
420
+ members,
421
+ references,
422
+ signature_references,
423
+ routes,
424
+ })
425
+ }
426
+
427
+ fn extract_parse_error(error: crate::dart_parser::DartParseError) -> ExtractError {
428
+ match error {
429
+ crate::dart_parser::DartParseError::Language(source) => ExtractError::Language(source),
430
+ crate::dart_parser::DartParseError::ParseCancelled { path } => {
431
+ ExtractError::ParseCancelled { path }
432
+ }
433
+ crate::dart_parser::DartParseError::Syntax { path } => ExtractError::Syntax { path },
434
+ }
435
+ }
436
+
437
+ fn sort_identifier_references(references: &mut Vec<IdentifierReference>) {
438
+ references.sort_by(|left, right| {
439
+ (left.location.line, left.location.column, left.name.as_str()).cmp(&(
440
+ right.location.line,
441
+ right.location.column,
442
+ right.name.as_str(),
443
+ ))
444
+ });
445
+ references.dedup();
446
+ }
447
+
448
+ fn sort_signature_references(references: &mut Vec<SignatureReference>) {
449
+ references.sort_by(|left, right| {
450
+ (
451
+ left.location.line,
452
+ left.location.column,
453
+ left.declaration.as_str(),
454
+ left.name.as_str(),
455
+ )
456
+ .cmp(&(
457
+ right.location.line,
458
+ right.location.column,
459
+ right.declaration.as_str(),
460
+ right.name.as_str(),
461
+ ))
462
+ });
463
+ references.dedup();
464
+ }
465
+
466
+ fn push_class_declaration(
467
+ declarations: &mut Vec<TopLevelDeclaration>,
468
+ node: Node<'_>,
469
+ source: &str,
470
+ ) {
471
+ if let Some(name) = field_text(node, "name", source).or_else(|| {
472
+ find_first_named_descendant(node, "mixin_application_class")
473
+ .and_then(|child| first_identifier_text(child, source))
474
+ }) {
475
+ declarations.push(TopLevelDeclaration {
476
+ kind: DeclarationKind::Class,
477
+ name,
478
+ location: node.start_position().into(),
479
+ range: SourceRange::from_node(node),
480
+ });
481
+ }
482
+ }
483
+
484
+ fn push_named_declaration(
485
+ declarations: &mut Vec<TopLevelDeclaration>,
486
+ node: Node<'_>,
487
+ source: &str,
488
+ kind: DeclarationKind,
489
+ ) {
490
+ if let Some(name) = node
491
+ .child_by_field_name("name")
492
+ .and_then(|child| first_identifier_text(child, source))
493
+ .or_else(|| field_text(node, "name", source))
494
+ {
495
+ declarations.push(TopLevelDeclaration {
496
+ kind,
497
+ name,
498
+ location: node.start_position().into(),
499
+ range: SourceRange::from_node(node),
500
+ });
501
+ }
502
+ }
503
+
504
+ fn push_type_alias_declaration(
505
+ declarations: &mut Vec<TopLevelDeclaration>,
506
+ node: Node<'_>,
507
+ source: &str,
508
+ ) {
509
+ let mut cursor = node.walk();
510
+ let name = node
511
+ .named_children(&mut cursor)
512
+ .find(|child| matches!(child.kind(), "identifier" | "type_identifier"))
513
+ .and_then(|child| child.utf8_text(source.as_bytes()).ok())
514
+ .map(str::to_owned);
515
+
516
+ if let Some(name) = name {
517
+ declarations.push(TopLevelDeclaration {
518
+ kind: DeclarationKind::TypeAlias,
519
+ name,
520
+ location: node.start_position().into(),
521
+ range: SourceRange::from_node(node),
522
+ });
523
+ }
524
+ }
525
+
526
+ fn push_variable_declarations(
527
+ declarations: &mut Vec<TopLevelDeclaration>,
528
+ node: Node<'_>,
529
+ source: &str,
530
+ ) {
531
+ let mut names = Vec::new();
532
+ collect_named_fields(
533
+ node,
534
+ source,
535
+ &["static_final_declaration", "initialized_identifier"],
536
+ &mut names,
537
+ );
538
+
539
+ if names.is_empty()
540
+ && let Some(identifier_list) = find_first_named_descendant(node, "identifier_list")
541
+ {
542
+ collect_direct_identifier_children(identifier_list, source, &mut names);
543
+ }
544
+
545
+ declarations.extend(names.into_iter().map(|name| TopLevelDeclaration {
546
+ kind: DeclarationKind::Variable,
547
+ name,
548
+ location: node.start_position().into(),
549
+ range: SourceRange::from_node(node),
550
+ }));
551
+ }
552
+
553
+ fn push_function_declaration(
554
+ declarations: &mut Vec<TopLevelDeclaration>,
555
+ node: Node<'_>,
556
+ source: &str,
557
+ ) {
558
+ let Some(signature) = node.child_by_field_name("signature") else {
559
+ return;
560
+ };
561
+ let Some(name) = field_text(signature, "name", source) else {
562
+ return;
563
+ };
564
+
565
+ declarations.push(TopLevelDeclaration {
566
+ kind: DeclarationKind::Function,
567
+ name,
568
+ location: node.start_position().into(),
569
+ range: SourceRange::from_node(node),
570
+ });
571
+ }
572
+
573
+ pub(super) fn extract_uri(uri_node: Node<'_>, source: &str) -> Option<String> {
574
+ let literal = find_first_named_descendant(uri_node, "string_literal")?;
575
+ unquote_dart_string(literal.utf8_text(source.as_bytes()).ok()?)
576
+ }
577
+
578
+ pub(super) fn field_text(node: Node<'_>, field_name: &str, source: &str) -> Option<String> {
579
+ node.child_by_field_name(field_name)
580
+ .and_then(|child| child.utf8_text(source.as_bytes()).ok())
581
+ .map(str::to_owned)
582
+ }
583
+
584
+ fn collect_named_fields(
585
+ node: Node<'_>,
586
+ source: &str,
587
+ owner_kinds: &[&str],
588
+ names: &mut Vec<String>,
589
+ ) {
590
+ if owner_kinds.contains(&node.kind())
591
+ && let Some(name) = field_text(node, "name", source)
592
+ {
593
+ names.push(name);
594
+ }
595
+
596
+ let mut cursor = node.walk();
597
+ for child in node.named_children(&mut cursor) {
598
+ collect_named_fields(child, source, owner_kinds, names);
599
+ }
600
+ }
601
+
602
+ pub(super) fn collect_direct_identifier_children(
603
+ node: Node<'_>,
604
+ source: &str,
605
+ names: &mut Vec<String>,
606
+ ) {
607
+ let mut cursor = node.walk();
608
+ names.extend(
609
+ node.named_children(&mut cursor)
610
+ .filter(|child| child.kind() == "identifier")
611
+ .filter_map(|child| child.utf8_text(source.as_bytes()).ok())
612
+ .map(str::to_owned),
613
+ );
614
+ }
615
+
616
+ fn first_identifier_text(node: Node<'_>, source: &str) -> Option<String> {
617
+ if matches!(node.kind(), "identifier" | "type_identifier") {
618
+ return node.utf8_text(source.as_bytes()).ok().map(str::to_owned);
619
+ }
620
+
621
+ let mut cursor = node.walk();
622
+ node.named_children(&mut cursor)
623
+ .find_map(|child| first_identifier_text(child, source))
624
+ }
625
+
626
+ pub(super) fn first_named_child<'tree>(node: &Node<'tree>) -> Option<Node<'tree>> {
627
+ let mut cursor = node.walk();
628
+ node.named_children(&mut cursor).next()
629
+ }
630
+
631
+ pub(super) fn find_first_named_descendant<'tree>(
632
+ node: Node<'tree>,
633
+ kind: &str,
634
+ ) -> Option<Node<'tree>> {
635
+ if node.kind() == kind {
636
+ return Some(node);
637
+ }
638
+
639
+ let mut cursor = node.walk();
640
+ find_first_named_descendant_with_cursor(node, kind, &mut cursor)
641
+ }
642
+
643
+ fn find_first_named_descendant_in<'tree>(node: Node<'tree>, kinds: &[&str]) -> Option<Node<'tree>> {
644
+ if kinds.contains(&node.kind()) {
645
+ return Some(node);
646
+ }
647
+
648
+ let mut cursor = node.walk();
649
+ for child in node.named_children(&mut cursor) {
650
+ if let Some(found) = find_first_named_descendant_in(child, kinds) {
651
+ return Some(found);
652
+ }
653
+ }
654
+
655
+ None
656
+ }
657
+
658
+ fn find_first_named_descendant_with_cursor<'tree>(
659
+ node: Node<'tree>,
660
+ kind: &str,
661
+ cursor: &mut TreeCursor<'tree>,
662
+ ) -> Option<Node<'tree>> {
663
+ for child in node.named_children(cursor) {
664
+ if child.kind() == kind {
665
+ return Some(child);
666
+ }
667
+ if let Some(found) = find_first_named_descendant(child, kind) {
668
+ return Some(found);
669
+ }
670
+ }
671
+
672
+ None
673
+ }
674
+
675
+ #[cfg(test)]
676
+ mod error_tests;
677
+ #[cfg(test)]
678
+ mod string_tests;
679
+ #[cfg(test)]
680
+ mod tests;