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,421 @@
1
+ use super::{DeclarationKind, IdentifierReference, Location, SignatureReference};
2
+
3
+ struct PrimaryConstructorHeader {
4
+ declaration: String,
5
+ declaration_kind: DeclarationKind,
6
+ params_start: usize,
7
+ params_end: usize,
8
+ }
9
+
10
+ struct Token {
11
+ text: String,
12
+ start: usize,
13
+ preceded_by_dot: bool,
14
+ }
15
+
16
+ pub(super) fn extract_primary_constructor_identifier_references(
17
+ source: &str,
18
+ ) -> Vec<IdentifierReference> {
19
+ primary_constructor_type_tokens(source)
20
+ .into_iter()
21
+ .map(|token| IdentifierReference {
22
+ name: token.text,
23
+ location: byte_location(source, token.start),
24
+ })
25
+ .collect()
26
+ }
27
+
28
+ pub(super) fn extract_primary_constructor_signature_references(
29
+ source: &str,
30
+ ) -> Vec<SignatureReference> {
31
+ let mut references = Vec::new();
32
+ for header in primary_constructor_headers(source) {
33
+ for token in type_tokens_in_params(source, header.params_start, header.params_end) {
34
+ if token.text == header.declaration {
35
+ continue;
36
+ }
37
+ references.push(SignatureReference {
38
+ declaration: header.declaration.clone(),
39
+ declaration_kind: header.declaration_kind,
40
+ name: token.text,
41
+ location: byte_location(source, token.start),
42
+ });
43
+ }
44
+ }
45
+ references
46
+ }
47
+
48
+ fn primary_constructor_type_tokens(source: &str) -> Vec<Token> {
49
+ primary_constructor_headers(source)
50
+ .into_iter()
51
+ .flat_map(|header| type_tokens_in_params(source, header.params_start, header.params_end))
52
+ .collect()
53
+ }
54
+
55
+ fn primary_constructor_headers(source: &str) -> Vec<PrimaryConstructorHeader> {
56
+ let mut headers = Vec::new();
57
+ let mut cursor = 0;
58
+ while cursor < source.len() {
59
+ let Some((keyword_start, keyword, kind)) = find_next_header_keyword(source, cursor) else {
60
+ break;
61
+ };
62
+ cursor = keyword_start + keyword.len();
63
+ let Some(mut name_start) = skip_whitespace(source, cursor) else {
64
+ continue;
65
+ };
66
+ if matches!(keyword, "class" | "enum") && starts_keyword(source, name_start, "const") {
67
+ let const_end = name_start + "const".len();
68
+ let Some(after_const) = skip_whitespace(source, const_end) else {
69
+ continue;
70
+ };
71
+ name_start = after_const;
72
+ }
73
+ let Some(name_end) = identifier_end(source, name_start) else {
74
+ continue;
75
+ };
76
+ let declaration = source[name_start..name_end].to_owned();
77
+ let mut header_cursor = skip_whitespace(source, name_end).unwrap_or(name_end);
78
+ if source.as_bytes().get(header_cursor).copied() == Some(b'<')
79
+ && let Some(type_params_end) = matching_delimiter(source, header_cursor, b'<', b'>')
80
+ {
81
+ header_cursor =
82
+ skip_whitespace(source, type_params_end + 1).unwrap_or(type_params_end + 1);
83
+ }
84
+ if source.as_bytes().get(header_cursor).copied() == Some(b'.') {
85
+ let suffix_start =
86
+ skip_whitespace(source, header_cursor + 1).unwrap_or(header_cursor + 1);
87
+ let Some(suffix_end) = identifier_end(source, suffix_start) else {
88
+ continue;
89
+ };
90
+ header_cursor = skip_whitespace(source, suffix_end).unwrap_or(suffix_end);
91
+ }
92
+ if source.as_bytes().get(header_cursor).copied() != Some(b'(') {
93
+ continue;
94
+ }
95
+ let Some(params_end) = matching_delimiter(source, header_cursor, b'(', b')') else {
96
+ continue;
97
+ };
98
+ headers.push(PrimaryConstructorHeader {
99
+ declaration,
100
+ declaration_kind: kind,
101
+ params_start: header_cursor + 1,
102
+ params_end,
103
+ });
104
+ cursor = params_end + 1;
105
+ }
106
+ headers
107
+ }
108
+
109
+ fn type_tokens_in_params(source: &str, start: usize, end: usize) -> Vec<Token> {
110
+ split_top_level_commas(source, start, end)
111
+ .into_iter()
112
+ .flat_map(|(param_start, param_end)| type_tokens_in_param(source, param_start, param_end))
113
+ .collect()
114
+ }
115
+
116
+ fn type_tokens_in_param(source: &str, start: usize, end: usize) -> Vec<Token> {
117
+ let tokens = identifier_tokens(source, start, end)
118
+ .into_iter()
119
+ .filter(|token| !token.preceded_by_dot)
120
+ .filter(|token| !is_parameter_modifier(&token.text))
121
+ .filter(|token| !is_builtin_type(&token.text))
122
+ .collect::<Vec<_>>();
123
+ if tokens.len() <= 1 {
124
+ return tokens
125
+ .into_iter()
126
+ .filter(|token| is_type_like(&token.text))
127
+ .collect();
128
+ }
129
+ let last_index = tokens.len() - 1;
130
+ tokens
131
+ .into_iter()
132
+ .enumerate()
133
+ .filter_map(|(index, token)| {
134
+ if index == last_index && !is_type_like(&token.text) {
135
+ None
136
+ } else if is_type_like(&token.text) {
137
+ Some(token)
138
+ } else {
139
+ None
140
+ }
141
+ })
142
+ .collect()
143
+ }
144
+
145
+ fn identifier_tokens(source: &str, start: usize, end: usize) -> Vec<Token> {
146
+ let mut tokens = Vec::new();
147
+ let mut cursor = start;
148
+ while cursor < end {
149
+ let Some(ch) = source
150
+ .get(cursor..)
151
+ .and_then(|suffix| suffix.chars().next())
152
+ else {
153
+ break;
154
+ };
155
+ if is_identifier_start(ch) {
156
+ let token_start = cursor;
157
+ let Some(token_end) = identifier_end(source, token_start) else {
158
+ break;
159
+ };
160
+ tokens.push(Token {
161
+ text: source[token_start..token_end].to_owned(),
162
+ start: token_start,
163
+ preceded_by_dot: previous_non_whitespace_byte(source.as_bytes(), token_start)
164
+ == Some(b'.'),
165
+ });
166
+ cursor = token_end;
167
+ continue;
168
+ }
169
+ if matches!(source.as_bytes().get(cursor), Some(b'\'' | b'"')) {
170
+ let Some(quote_end) = skip_quoted(source, cursor) else {
171
+ break;
172
+ };
173
+ cursor = quote_end + 1;
174
+ continue;
175
+ }
176
+ cursor += ch.len_utf8();
177
+ }
178
+ tokens
179
+ }
180
+
181
+ fn split_top_level_commas(source: &str, start: usize, end: usize) -> Vec<(usize, usize)> {
182
+ let mut spans = Vec::new();
183
+ let mut segment_start = start;
184
+ let mut cursor = start;
185
+ let mut depth = 0usize;
186
+ while cursor < end {
187
+ match source.as_bytes()[cursor] {
188
+ b'(' | b'[' | b'{' | b'<' => depth += 1,
189
+ b')' | b']' | b'}' | b'>' => depth = depth.saturating_sub(1),
190
+ b',' if depth == 0 => {
191
+ spans.push((segment_start, cursor));
192
+ segment_start = cursor + 1;
193
+ }
194
+ b'\'' | b'"' => {
195
+ if let Some(quote_end) = skip_quoted(source, cursor) {
196
+ cursor = quote_end;
197
+ }
198
+ }
199
+ _ => {}
200
+ }
201
+ cursor += 1;
202
+ }
203
+ if segment_start < end {
204
+ spans.push((segment_start, end));
205
+ }
206
+ spans
207
+ }
208
+
209
+ fn find_next_header_keyword(
210
+ source: &str,
211
+ start: usize,
212
+ ) -> Option<(usize, &'static str, DeclarationKind)> {
213
+ let mut cursor = start;
214
+ while cursor < source.len() {
215
+ if starts_keyword(source, cursor, "class") {
216
+ return Some((cursor, "class", DeclarationKind::Class));
217
+ }
218
+ if starts_keyword(source, cursor, "enum") {
219
+ return Some((cursor, "enum", DeclarationKind::Enum));
220
+ }
221
+ cursor = next_char_boundary(source, cursor)?;
222
+ }
223
+ None
224
+ }
225
+
226
+ fn is_parameter_modifier(name: &str) -> bool {
227
+ matches!(
228
+ name,
229
+ "covariant" | "final" | "required" | "super" | "this" | "var"
230
+ )
231
+ }
232
+
233
+ fn is_type_like(name: &str) -> bool {
234
+ name.starts_with('_') || name.chars().next().is_some_and(char::is_uppercase)
235
+ }
236
+
237
+ fn is_builtin_type(name: &str) -> bool {
238
+ matches!(
239
+ name,
240
+ "bool"
241
+ | "double"
242
+ | "dynamic"
243
+ | "Future"
244
+ | "int"
245
+ | "Iterable"
246
+ | "List"
247
+ | "Map"
248
+ | "Never"
249
+ | "Null"
250
+ | "num"
251
+ | "Object"
252
+ | "Set"
253
+ | "Stream"
254
+ | "String"
255
+ | "void"
256
+ )
257
+ }
258
+
259
+ fn byte_location(source: &str, byte: usize) -> Location {
260
+ let mut line = 1;
261
+ let mut column = 0;
262
+ for ch in source[..byte].chars() {
263
+ if ch == '\n' {
264
+ line += 1;
265
+ column = 0;
266
+ } else {
267
+ column += ch.len_utf8();
268
+ }
269
+ }
270
+ Location { line, column }
271
+ }
272
+
273
+ fn starts_keyword(source: &str, start: usize, keyword: &str) -> bool {
274
+ source
275
+ .get(start..)
276
+ .is_some_and(|suffix| suffix.starts_with(keyword))
277
+ && !source
278
+ .get(..start)
279
+ .and_then(|prefix| prefix.chars().next_back())
280
+ .is_some_and(is_identifier_char)
281
+ && !source
282
+ .get(start + keyword.len()..)
283
+ .and_then(|suffix| suffix.chars().next())
284
+ .is_some_and(is_identifier_char)
285
+ }
286
+
287
+ fn skip_whitespace(source: &str, start: usize) -> Option<usize> {
288
+ let mut cursor = start;
289
+ while cursor < source.len() {
290
+ let ch = source.get(cursor..)?.chars().next()?;
291
+ if !ch.is_whitespace() {
292
+ return Some(cursor);
293
+ }
294
+ cursor += ch.len_utf8();
295
+ }
296
+ Some(cursor)
297
+ }
298
+
299
+ fn identifier_end(source: &str, start: usize) -> Option<usize> {
300
+ let mut chars = source.get(start..)?.char_indices();
301
+ let (_, first) = chars.next()?;
302
+ if !is_identifier_start(first) {
303
+ return None;
304
+ }
305
+ for (offset, ch) in chars {
306
+ if !is_identifier_char(ch) {
307
+ return Some(start + offset);
308
+ }
309
+ }
310
+ Some(source.len())
311
+ }
312
+
313
+ fn matching_delimiter(source: &str, start: usize, open: u8, close: u8) -> Option<usize> {
314
+ let bytes = source.as_bytes();
315
+ if bytes.get(start).copied() != Some(open) {
316
+ return None;
317
+ }
318
+ let mut depth = 0usize;
319
+ let mut cursor = start;
320
+ while cursor < bytes.len() {
321
+ match bytes[cursor] {
322
+ byte if byte == open => depth += 1,
323
+ byte if byte == close => {
324
+ depth = depth.checked_sub(1)?;
325
+ if depth == 0 {
326
+ return Some(cursor);
327
+ }
328
+ }
329
+ b'\'' | b'"' => cursor = skip_quoted(source, cursor)?,
330
+ _ => {}
331
+ }
332
+ cursor += 1;
333
+ }
334
+ None
335
+ }
336
+
337
+ fn skip_quoted(source: &str, start: usize) -> Option<usize> {
338
+ let quote = source.as_bytes().get(start).copied()?;
339
+ let mut cursor = start + 1;
340
+ while cursor < source.len() {
341
+ match source.as_bytes()[cursor] {
342
+ b'\\' => cursor += 2,
343
+ byte if byte == quote => return Some(cursor),
344
+ _ => cursor += 1,
345
+ }
346
+ }
347
+ None
348
+ }
349
+
350
+ fn previous_non_whitespace_byte(bytes: &[u8], cursor: usize) -> Option<u8> {
351
+ bytes
352
+ .get(..cursor)?
353
+ .iter()
354
+ .rev()
355
+ .copied()
356
+ .find(|byte| !byte.is_ascii_whitespace())
357
+ }
358
+
359
+ fn next_char_boundary(source: &str, start: usize) -> Option<usize> {
360
+ source
361
+ .get(start..)?
362
+ .chars()
363
+ .next()
364
+ .map(|ch| start + ch.len_utf8())
365
+ }
366
+
367
+ fn is_identifier_start(ch: char) -> bool {
368
+ ch == '_' || ch.is_ascii_alphabetic()
369
+ }
370
+
371
+ fn is_identifier_char(ch: char) -> bool {
372
+ is_identifier_start(ch) || ch.is_ascii_digit()
373
+ }
374
+
375
+ #[cfg(test)]
376
+ mod tests {
377
+ use super::super::extract_dart_source;
378
+
379
+ #[test]
380
+ fn primary_constructor_references_survive_compatibility_parse()
381
+ -> Result<(), Box<dyn std::error::Error>> {
382
+ let source = "\
383
+ class Screen(
384
+ _Controller controller,
385
+ {required _Service service, this.child, super.key}
386
+ ) extends Widget {}
387
+
388
+ enum Tone(_Palette palette) {
389
+ dark(_Palette());
390
+ }
391
+ ";
392
+ let extracted = extract_dart_source("lib/screen.dart", source)?;
393
+ let references = extracted
394
+ .references
395
+ .iter()
396
+ .map(|reference| reference.name.as_str())
397
+ .collect::<Vec<_>>();
398
+ let signatures = extracted
399
+ .signature_references
400
+ .iter()
401
+ .map(|reference| {
402
+ (
403
+ reference.declaration.as_str(),
404
+ reference.name.as_str(),
405
+ reference.location.line,
406
+ )
407
+ })
408
+ .collect::<Vec<_>>();
409
+
410
+ assert!(references.contains(&"_Controller"));
411
+ assert!(references.contains(&"_Service"));
412
+ assert!(references.contains(&"_Palette"));
413
+ assert!(!references.contains(&"controller"));
414
+ assert!(!references.contains(&"child"));
415
+ assert!(signatures.contains(&("Screen", "_Controller", 2)));
416
+ assert!(signatures.contains(&("Screen", "_Service", 3)));
417
+ assert!(signatures.contains(&("Tone", "_Palette", 6)));
418
+
419
+ Ok(())
420
+ }
421
+ }
@@ -0,0 +1,151 @@
1
+ use tree_sitter::Node;
2
+
3
+ use super::IdentifierReference;
4
+
5
+ pub(super) fn extract_identifier_references(
6
+ root: Node<'_>,
7
+ source: &str,
8
+ ) -> Vec<IdentifierReference> {
9
+ let mut references = Vec::new();
10
+ collect_identifier_references(root, source, &mut references);
11
+ references
12
+ }
13
+
14
+ fn collect_identifier_references(
15
+ node: Node<'_>,
16
+ source: &str,
17
+ references: &mut Vec<IdentifierReference>,
18
+ ) {
19
+ if is_reference_identifier(node, source)
20
+ && let Ok(name) = node.utf8_text(source.as_bytes())
21
+ {
22
+ references.push(IdentifierReference {
23
+ name: name.to_owned(),
24
+ location: node.start_position().into(),
25
+ });
26
+ }
27
+
28
+ let mut cursor = node.walk();
29
+ for child in node.named_children(&mut cursor) {
30
+ collect_identifier_references(child, source, references);
31
+ }
32
+ }
33
+
34
+ fn is_reference_identifier(node: Node<'_>, source: &str) -> bool {
35
+ if !matches!(node.kind(), "identifier" | "type_identifier") {
36
+ return false;
37
+ }
38
+ if node.utf8_text(source.as_bytes()).ok() == Some("_") {
39
+ return false;
40
+ }
41
+ if has_ancestor_kind(node, &["import_or_export", "part_directive"]) {
42
+ return false;
43
+ }
44
+ let Some(parent) = node.parent() else {
45
+ return true;
46
+ };
47
+ if parent.kind() == "variable_pattern" || is_pattern_binding(node, source) {
48
+ return false;
49
+ }
50
+ if is_pattern_field_label(node, source) {
51
+ return false;
52
+ }
53
+ if parent.kind() == "type_alias" && is_type_alias_name(parent, node) {
54
+ return false;
55
+ }
56
+ if DECLARATION_NAME_OWNER_KINDS.contains(&parent.kind()) && is_child_field(parent, node, "name")
57
+ {
58
+ return false;
59
+ }
60
+ true
61
+ }
62
+
63
+ const DECLARATION_NAME_OWNER_KINDS: &[&str] = &[
64
+ "class_declaration",
65
+ "constant_constructor_signature",
66
+ "constructor_signature",
67
+ "default_formal_parameter",
68
+ "enum_declaration",
69
+ "enum_constant",
70
+ "extension_declaration",
71
+ "extension_type_declaration",
72
+ "factory_constructor_signature",
73
+ "field_formal_parameter",
74
+ "formal_parameter",
75
+ "function_signature",
76
+ "getter_signature",
77
+ "initialized_identifier",
78
+ "mixin_declaration",
79
+ "normal_formal_parameter",
80
+ "operator_signature",
81
+ "redirecting_factory_constructor_signature",
82
+ "setter_signature",
83
+ "static_final_declaration",
84
+ "type_alias",
85
+ ];
86
+
87
+ fn has_ancestor_kind(node: Node<'_>, kinds: &[&str]) -> bool {
88
+ let mut parent = node.parent();
89
+ while let Some(ancestor) = parent {
90
+ if kinds.contains(&ancestor.kind()) {
91
+ return true;
92
+ }
93
+ parent = ancestor.parent();
94
+ }
95
+ false
96
+ }
97
+
98
+ fn is_child_field(parent: Node<'_>, child: Node<'_>, field_name: &str) -> bool {
99
+ let mut cursor = parent.walk();
100
+ parent
101
+ .children_by_field_name(field_name, &mut cursor)
102
+ .any(|field| same_node(field, child))
103
+ }
104
+
105
+ fn is_type_alias_name(parent: Node<'_>, child: Node<'_>) -> bool {
106
+ let mut cursor = parent.walk();
107
+ parent
108
+ .named_children(&mut cursor)
109
+ .find(|node| matches!(node.kind(), "identifier" | "type_identifier"))
110
+ .is_some_and(|name| same_node(name, child))
111
+ }
112
+
113
+ fn is_pattern_field_label(node: Node<'_>, source: &str) -> bool {
114
+ if !has_ancestor_kind(node, &["object_pattern", "record_pattern"]) {
115
+ return false;
116
+ }
117
+ source
118
+ .get(node.end_byte()..)
119
+ .is_some_and(|suffix| suffix.trim_start().starts_with(':'))
120
+ }
121
+
122
+ fn is_pattern_binding(node: Node<'_>, source: &str) -> bool {
123
+ if !has_ancestor_kind(
124
+ node,
125
+ &[
126
+ "constant_pattern",
127
+ "list_pattern",
128
+ "map_pattern",
129
+ "object_pattern",
130
+ "record_pattern",
131
+ ],
132
+ ) {
133
+ return false;
134
+ }
135
+ let Some(prefix) = source.get(..node.start_byte()) else {
136
+ return false;
137
+ };
138
+ let token_prefix = prefix
139
+ .rsplit(|character: char| {
140
+ character.is_whitespace() || matches!(character, '(' | '[' | '{' | ':' | ',')
141
+ })
142
+ .find(|segment| !segment.is_empty())
143
+ .unwrap_or_default();
144
+ matches!(token_prefix, "final" | "var")
145
+ }
146
+
147
+ fn same_node(left: Node<'_>, right: Node<'_>) -> bool {
148
+ left.kind() == right.kind()
149
+ && left.start_byte() == right.start_byte()
150
+ && left.end_byte() == right.end_byte()
151
+ }