forked from colbymchenry/codegraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdart.rs
More file actions
1547 lines (1456 loc) · 59.2 KB
/
Copy pathdart.rs
File metadata and controls
1547 lines (1456 loc) · 59.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Dart extraction — a faithful Rust port of the dart paths of
//! `TreeSitterExtractor` (src/extraction/tree-sitter.ts) plus
//! languages/dart.ts.
//!
//! Same porting contract as the other walkers: behavior parity, bug-for-bug.
//! The authoritative quirk list is docs/design/dart-kernel-port-checklist.md.
//! The center of gravity is THE SIBLING-BODY DOUBLE-WALK: dart attaches every
//! function/method body as a NEXT SIBLING of its signature node, and the TS
//! walkers consume the body TWICE — once via resolveBody (attributed to the
//! function/method) and once via the enclosing generic walk (attributed to
//! the file/class). The deterministic result — duplicate local-function
//! nodes with the SAME id under different parents, duplicated
//! calls/instantiates refs, file/class-attributed fn-ref twins — must be
//! reproduced byte-for-byte in the observed interleave; a "helpful" dedupe
//! breaks parity. Other load-bearing oddities preserved on purpose:
//! callTypes is EMPTY (all call refs ride extractBareCall's selector
//! walking in the body walker — cascades are invisible, `?.` encodes like
//! `.`); `ConfigT.load()` double-emits (calls + a static-member references
//! ref — no callee-of-call skip in the dart branch); operator methods mint
//! `method "<anonymous>"`; the unnamed constructor is skipped
//! (isMisparsedFunction) while named ctors/factories are named by the CTOR
//! name with the class as returnType; instance fields mint NO nodes (only
//! static_final_declaration → constant, via the hook); prefixed return
//! types keep the PREFIX (`other.OtherClass f()` → returnType `other` —
//! bug, preserved); enum `with` mixins emit nothing while enum `implements`
//! works; deferred imports are invisible; named-argument callbacks are NOT
//! fn-ref-captured; `async*`/`sync*` are NOT async. Positions in UTF-16
//! code units. Files with parse errors defer to wasm (3.4–20.7% both-arm
//! incidence — empty object patterns and unnamed `library;` dominate).
use crate::buffers::{
build_meta, edge_kind_index, node_kind_index, Arena, BoolFlags, EdgeRow, EmitOut, NodeRow,
RefRow, StrRef, Tables, FLAG_IS_ASYNC, FLAG_IS_EXPORTED, FLAG_IS_STATIC, FUNCTION_REF_CODE,
NONE, NONE_STR,
};
use crate::docstring::preceding_docstring;
use crate::ids;
use crate::textutil as util;
use regex::Regex;
use std::collections::{HashMap, HashSet};
use std::sync::OnceLock;
use tree_sitter::{Node, Parser};
const MAX_VALUE_REF_NODES: usize = 20_000;
/// NAME_STOPLIST (function-ref.ts).
fn is_stoplisted(name: &str) -> bool {
matches!(
name,
"this" | "self" | "super" | "null" | "nil" | "true" | "false" | "undefined" | "new"
| "NULL" | "nullptr" | "None"
)
}
/// BUILTIN_TYPES (tree-sitter.ts:5768-5782).
fn is_builtin_type(name: &str) -> bool {
matches!(
name,
"string" | "number" | "boolean" | "void" | "null" | "undefined" | "never" | "any"
| "unknown" | "object" | "symbol" | "bigint" | "true" | "false"
| "str" | "bool" | "i8" | "i16" | "i32" | "i64" | "i128" | "isize"
| "u8" | "u16" | "u32" | "u64" | "u128" | "usize" | "f32" | "f64" | "char"
| "int" | "long" | "short" | "byte" | "float" | "double"
| "int8" | "int16" | "int32" | "int64" | "uint8" | "uint16" | "uint32" | "uint64"
| "float32" | "float64" | "complex64" | "complex128" | "rune" | "error"
| "Int" | "Long" | "Short" | "Byte" | "Float" | "Double" | "Boolean" | "Char"
| "Unit" | "String" | "Any" | "AnyRef" | "AnyVal" | "Nothing" | "Null"
)
}
/// extractDartReturnType's simple-name gate + the static-member receiver gate.
fn simple_type_name_re() -> &'static Regex {
static RE: OnceLock<Regex> = OnceLock::new();
RE.get_or_init(|| Regex::new(r"^[A-Za-z_]\w*$").unwrap())
}
fn cap_ident_re() -> &'static Regex {
static RE: OnceLock<Regex> = OnceLock::new();
RE.get_or_init(|| Regex::new(r"^[A-Z][A-Za-z0-9_]*$").unwrap())
}
/// The chained-call re-encode gate (`/^[A-Z]/`).
fn starts_upper_re() -> &'static Regex {
static RE: OnceLock<Regex> = OnceLock::new();
RE.get_or_init(|| Regex::new(r"^[A-Z]").unwrap())
}
/// extractDartReturnType's `<...>` strip (`/<[^>]*>/g`).
fn angle_args_re() -> &'static Regex {
static RE: OnceLock<Regex> = OnceLock::new();
RE.get_or_init(|| Regex::new(r"<[^>]*>").unwrap())
}
struct Scope {
row: u32,
kind: &'static str,
name: String,
}
struct Cand {
from: u32,
name: String,
line: u32,
column_byte: usize,
row: usize,
}
struct ValueScope<'t> {
row: u32,
node: Node<'t>,
name: String,
}
#[derive(Default)]
struct Extra {
docstring: Option<String>,
signature: Option<String>,
/// 0 = absent; 1 public, 2 private.
visibility: u8,
is_async: Option<bool>,
is_static: Option<bool>,
return_type: Option<String>,
/// resolveBody-driven endLine extension (LIVE for dart sibling bodies).
end_line_override: Option<u32>,
}
pub struct Walker<'t> {
src: &'t str,
file_path: &'t str,
line_starts: Vec<usize>,
arena: Arena,
tables: Tables,
stack: Vec<Scope>,
node_ids: Vec<String>,
defined_fn_names: HashSet<String>,
imported_names: HashSet<String>,
fn_ref_cands: Vec<Cand>,
fs_values: HashMap<String, u32>,
fs_value_counts: HashMap<String, u32>,
value_scopes: Vec<ValueScope<'t>>,
}
pub fn extract(file_path: &str, source: &str) -> Result<EmitOut, String> {
let grammar = crate::langs::grammar_for("dart").ok_or("no dart grammar")?;
let t0 = std::time::Instant::now();
let mut parser = Parser::new();
parser
.set_language(&grammar)
.map_err(|e| format!("set_language(dart) failed: {e}"))?;
let tree = parser
.parse(source, None)
.ok_or_else(|| "parser returned null tree".to_string())?;
if tree.root_node().has_error() {
return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
}
let mut w = Walker {
src: source,
file_path,
line_starts: util::line_starts(source),
arena: Arena::default(),
tables: Tables::default(),
stack: Vec::new(),
node_ids: Vec::new(),
defined_fn_names: HashSet::new(),
imported_names: HashSet::new(),
fn_ref_cands: Vec::new(),
fs_values: HashMap::new(),
fs_value_counts: HashMap::new(),
value_scopes: Vec::new(),
};
// File node (tree-sitter.ts:508-521).
let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1;
let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path);
let mut flags = BoolFlags::default();
flags.set(FLAG_IS_EXPORTED, false);
let file_id = w.arena.put(&ids::file_node_id(file_path));
let name_ref = w.arena.put(base_name);
let qn_ref = w.arena.put(file_path);
w.tables.push_node(&NodeRow {
kind: node_kind_index("file").unwrap(),
visibility: 0,
flags,
start_line: 1,
end_line: line_count,
start_column: 0,
end_column: 0,
name: name_ref,
qualified_name: qn_ref,
id: file_id,
docstring: NONE_STR,
signature: NONE_STR,
decorators: NONE_STR,
type_parameters: NONE_STR,
return_type: NONE_STR,
extra_json: NONE_STR,
});
w.node_ids.push(ids::file_node_id(file_path));
w.stack.push(Scope { row: 0, kind: "file", name: base_name.to_string() });
w.visit(tree.root_node());
w.flush_fn_ref_candidates();
w.flush_value_refs(tree.root_node());
w.stack.pop();
let duration_ms = t0.elapsed().as_secs_f64() * 1000.0;
let meta = build_meta(&w.tables, w.arena.len(), NONE_STR, duration_ms);
Ok(EmitOut {
meta,
nodes: w.tables.nodes,
edges: w.tables.edges,
refs: w.tables.refs,
arena: w.arena.into_vec(),
})
}
impl<'t> Walker<'t> {
fn text(&self, node: Node) -> &'t str {
&self.src[node.byte_range()]
}
fn line_of(&self, node: Node) -> u32 {
node.start_position().row as u32 + 1
}
fn col_of(&self, node: Node) -> u32 {
util::col16(self.src, &self.line_starts, node.start_position().row, node.start_byte())
}
fn end_col_of(&self, node: Node) -> u32 {
util::col16(self.src, &self.line_starts, node.end_position().row, node.end_byte())
}
fn top_row(&self) -> u32 {
self.stack.last().map(|s| s.row).unwrap_or(0)
}
fn inside_class_like(&self) -> bool {
self.stack
.last()
.map(|s| matches!(s.kind, "class" | "struct" | "interface" | "trait" | "enum" | "module"))
.unwrap_or(false)
}
fn push_ref_at(&mut self, from_row: u32, name: &str, kind: &str, node: Node) {
let name_ref = self.arena.put(name);
self.tables.push_ref(&RefRow {
from_idx: from_row,
kind: edge_kind_index(kind).unwrap(),
line: self.line_of(node),
column: self.col_of(node),
reference_name: name_ref,
candidates: NONE_STR,
from_id_str: NONE_STR,
});
// Dart import names are URIs (`package:x/y.dart`) — they match neither
// SIMPLE_NAME nor QUALIFIED_IMPORT, so importedNames stays empty in
// practice; ported for fidelity.
if kind == "imports" {
if util::simple_name().is_match(name) {
self.imported_names.insert(name.to_string());
} else if let Some(c) = util::qualified_import().captures(name) {
self.imported_names.insert(c[1].to_string());
}
}
}
// --- createNode (tree-sitter.ts:1308) ---------------------------------
fn create_node(&mut self, kind: &'static str, name: &str, node: Node<'t>, extra: Extra) -> Option<u32> {
if name.is_empty() {
return None;
}
let start_line = self.line_of(node);
let id = ids::node_id(self.file_path, kind, name, start_line);
let qualified = {
let mut parts: Vec<&str> = Vec::new();
for s in &self.stack {
if s.kind != "file" {
parts.push(&s.name);
}
}
let mut qn = parts.join("::");
if !qn.is_empty() {
qn.push_str("::");
}
qn.push_str(name);
qn
};
// endLine extension (:1322-1334) — LIVE for dart: a function/method
// node's endLine extends to its sibling function_body's end.
let mut end_line = node.end_position().row as u32 + 1;
if let Some(ext) = extra.end_line_override {
if ext > end_line {
end_line = ext;
}
}
let name_ref = self.arena.put(name);
let qn_ref = self.arena.put(&qualified);
let id_ref = self.arena.put(&id);
let doc_ref = opt_str(&mut self.arena, extra.docstring.as_deref());
let sig_ref = opt_str(&mut self.arena, extra.signature.as_deref());
let ret_ref = opt_str(&mut self.arena, extra.return_type.as_deref());
let mut flags = BoolFlags::default();
if let Some(v) = extra.is_async {
flags.set(FLAG_IS_ASYNC, v);
}
if let Some(v) = extra.is_static {
flags.set(FLAG_IS_STATIC, v);
}
let row = self.tables.push_node(&NodeRow {
kind: node_kind_index(kind).unwrap(),
visibility: extra.visibility,
flags,
start_line,
end_line,
start_column: self.col_of(node),
end_column: self.end_col_of(node),
name: name_ref,
qualified_name: qn_ref,
id: id_ref,
docstring: doc_ref,
signature: sig_ref,
decorators: NONE_STR,
type_parameters: NONE_STR,
return_type: ret_ref,
extra_json: NONE_STR,
});
self.node_ids.push(id.clone());
if kind == "function" || kind == "method" {
self.defined_fn_names.insert(name.to_string());
}
let parent_row = self.top_row();
self.tables.push_edge(&EdgeRow {
source_idx: parent_row,
target_idx: row,
kind: edge_kind_index("contains").unwrap(),
provenance: 0,
line: NONE,
column: NONE,
metadata_json: NONE_STR,
source_id_str: NONE_STR,
target_id_str: NONE_STR,
});
// captureValueRefScope (:735-767). Dart mints only `constant` targets.
if (kind == "constant" || kind == "variable")
&& util::utf16_len(name) >= 3
&& util::has_upper_or_underscore().is_match(name)
{
let parent_ok = self
.stack
.last()
.map(|s| matches!(s.kind, "file" | "class" | "module" | "struct" | "enum"))
.unwrap_or(false);
if parent_ok {
self.fs_values.insert(name.to_string(), row);
*self.fs_value_counts.entry(name.to_string()).or_insert(0) += 1;
}
}
if matches!(kind, "function" | "method" | "constant" | "variable") {
self.value_scopes.push(ValueScope { row, node, name: name.to_string() });
}
Some(row)
}
// --- languages/dart.ts helper transcriptions --------------------------
/// dartInnerSignature (dart.ts:9-17).
fn inner_signature(&self, node: Node<'t>) -> Node<'t> {
if node.kind() == "method_signature" {
let mut cursor = node.walk();
let inner = node.named_children(&mut cursor).find(|c| {
matches!(c.kind(), "function_signature" | "getter_signature" | "setter_signature")
});
if let Some(inner) = inner {
return inner;
}
}
node
}
/// dartConstructorSignature (dart.ts:25-35).
fn constructor_signature(&self, node: Node<'t>) -> Option<Node<'t>> {
if matches!(node.kind(), "factory_constructor_signature" | "constructor_signature") {
return Some(node);
}
if node.kind() == "method_signature" {
let mut cursor = node.walk();
return node.named_children(&mut cursor).find(|c| {
matches!(c.kind(), "factory_constructor_signature" | "constructor_signature")
});
}
None
}
/// dartEnclosingTypeName (dart.ts:38-50).
fn enclosing_type_name(&self, node: Node<'t>) -> Option<&'t str> {
let mut p = node.parent();
while let Some(parent) = p {
if matches!(
parent.kind(),
"class_definition" | "mixin_declaration" | "extension_declaration" | "enum_declaration"
) {
return parent.child_by_field_name("name").map(|n| self.text(n));
}
p = parent.parent();
}
None
}
/// dartCtorInfo (dart.ts:61-70).
fn ctor_info(&self, node: Node<'t>) -> Option<(String, String)> {
let ctor = self.constructor_signature(node)?;
let mut cursor = ctor.walk();
let ids: Vec<Node<'t>> = ctor
.named_children(&mut cursor)
.filter(|c| c.kind() == "identifier")
.collect();
let class_name = self.enclosing_type_name(node)?;
let first = ids.first()?;
if self.text(*first) != class_name {
return None; // misparsed method, not a ctor
}
let ctor_name = ids.get(1).map(|n| self.text(*n)).unwrap_or(class_name);
Some((class_name.to_string(), ctor_name.to_string()))
}
/// extractDartReturnType (dart.ts:80-92).
fn return_type_of(&self, node: Node<'t>) -> Option<String> {
if let Some((class_name, _)) = self.ctor_info(node) {
return Some(class_name);
}
let sig = self.inner_signature(node);
let mut cursor = sig.walk();
let ret = sig
.named_children(&mut cursor)
.find(|c| c.kind() == "type_identifier")?;
let text = angle_args_re().replace_all(self.text(ret), "");
let text = text.trim();
let last = text.split('.').next_back()?;
if last.is_empty() || !simple_type_name_re().is_match(last) {
return None;
}
Some(last.to_string())
}
/// isMisparsedFunction (dart.ts:177-188) — skip the UNNAMED constructor.
fn is_unnamed_ctor(&self, node: Node<'t>) -> bool {
match self.ctor_info(node) {
Some((class_name, ctor_name)) => ctor_name == class_name,
None => false,
}
}
/// getSignature (dart.ts:189-208).
fn signature_of(&self, node: Node<'t>) -> Option<String> {
let sig = self.inner_signature(node);
let mut c1 = sig.walk();
let params = sig
.named_children(&mut c1)
.find(|c| c.kind() == "formal_parameter_list");
let mut c2 = sig.walk();
let ret = sig
.named_children(&mut c2)
.find(|c| matches!(c.kind(), "type_identifier" | "void_type"));
if params.is_none() && ret.is_none() {
return None;
}
let mut result = String::new();
if let Some(r) = ret {
result.push_str(self.text(r));
result.push(' ');
}
if let Some(p) = params {
result.push_str(self.text(p));
}
let trimmed = result.trim();
if trimmed.is_empty() {
None
} else {
Some(trimmed.to_string())
}
}
/// getVisibility (dart.ts:209-222) — `_` prefix = private; every
/// constructor is public (the unwrap misses ctor signatures / the name
/// FIELD is the class identifier).
fn visibility_of(&self, node: Node<'t>) -> u8 {
let name_node = if node.kind() == "method_signature" {
let mut cursor = node.walk();
let inner = node.named_children(&mut cursor).find(|c| {
matches!(c.kind(), "function_signature" | "getter_signature" | "setter_signature")
});
inner.and_then(|i| {
let mut ic = i.walk();
let found = i.named_children(&mut ic).find(|c| c.kind() == "identifier");
found
})
} else {
node.child_by_field_name("name")
};
match name_node {
Some(n) if self.text(n).starts_with('_') => 2,
_ => 1,
}
}
/// isAsync (dart.ts:223-233) — the `async` anon child of the SIBLING
/// function_body; `async*`/`sync*` are different token types → false.
fn is_async_of(&self, node: Node<'t>) -> bool {
if let Some(next) = node.next_named_sibling() {
if next.kind() == "function_body" {
for i in 0..next.child_count() {
if let Some(c) = next.child(i) {
if c.kind() == "async" {
return true;
}
}
}
}
}
false
}
/// isStatic (dart.ts:234-243).
fn is_static_of(&self, node: Node<'t>) -> bool {
if node.kind() == "method_signature" {
for i in 0..node.child_count() {
if let Some(c) = node.child(i) {
if c.kind() == "static" {
return true;
}
}
}
}
false
}
/// resolveBody (dart.ts:158-171).
fn resolve_body(&self, node: Node<'t>) -> Option<Node<'t>> {
if matches!(node.kind(), "function_signature" | "method_signature") {
let next = node.next_named_sibling()?;
if next.kind() == "function_body" {
return Some(next);
}
return None;
}
if let Some(standard) = node.child_by_field_name("body") {
return Some(standard);
}
let mut cursor = node.walk();
let found = node
.named_children(&mut cursor)
.find(|c| matches!(c.kind(), "class_body" | "extension_body"));
found
}
/// extractName (tree-sitter.ts:90-192) — resolveName (ctor names) →
/// name field → the method_signature inner unwrap → identifier-ish
/// child → `<anonymous>` (operators land here).
fn extract_name(&self, node: Node<'t>) -> String {
// resolveName hook (dart.ts:244-260): named ctor/factory → ctor name.
if let Some((class_name, ctor_name)) = self.ctor_info(node) {
if ctor_name != class_name {
return ctor_name;
}
}
if let Some(name_node) = node.child_by_field_name("name") {
return self.text(name_node).to_string();
}
if node.kind() == "method_signature" {
let mut cursor = node.walk();
let inner = node.named_children(&mut cursor).find(|c| {
matches!(
c.kind(),
"function_signature" | "getter_signature" | "setter_signature"
| "constructor_signature" | "factory_constructor_signature"
)
});
if let Some(inner) = inner {
let mut ic = inner.walk();
let id = inner.named_children(&mut ic).find(|c| c.kind() == "identifier");
if let Some(id) = id {
return self.text(id).to_string();
}
}
}
let mut cursor = node.walk();
for c in node.named_children(&mut cursor) {
if matches!(c.kind(), "identifier" | "type_identifier" | "simple_identifier" | "constant") {
return self.text(c).to_string();
}
}
"<anonymous>".to_string()
}
// --- the main walk (visitNode, tree-sitter.ts:936-1303) ---------------
fn visit(&mut self, node: Node<'t>) {
// The visitNode hook (dart.ts:144-157) — the constants branch.
if node.kind() == "static_final_declaration" {
let mut cursor = node.walk();
let name_node = node.named_children(&mut cursor).find(|c| c.kind() == "identifier");
if let Some(name_node) = name_node {
// signature = first value sibling's text, sliced to 100
// UTF-16 units (a flattened chain captures just its head).
let signature = name_node.next_named_sibling().map(|v| {
let (sliced, _) = util::slice_utf16(self.text(v), 100);
if util::utf16_len(&sliced) >= 100 {
format!("= {sliced}...")
} else {
format!("= {sliced}")
}
});
let name = self.text(name_node).to_string();
self.create_node("constant", &name, node, Extra { signature, ..Default::default() });
}
self.scan_fn_ref_subtree(node, 0);
return;
}
// maybeCaptureFnRefs (:990) — the double-walk fn-ref twin source.
self.maybe_capture_fn_refs(node);
match node.kind() {
"function_signature" => {
// functionTypes row — method_signature does NOT include it →
// always extractFunction, even inside a class (abstract
// members become kind `function` contained by the class).
self.extract_function(node);
return;
}
"class_definition" | "mixin_declaration" | "extension_declaration" => {
self.extract_class(node);
return;
}
"method_signature" | "constructor_signature" => {
self.extract_method(node);
return;
}
"enum_declaration" => {
self.extract_enum(node);
return;
}
"type_alias" => {
let skip = self.extract_type_alias(node);
if skip {
return;
}
}
"import_or_export" => {
self.extract_import(node);
return;
}
"new_expression" => {
// INSTANTIATION_KINDS row — from the FILE/CLASS on the
// sibling revisit (the double-walk's pass 2a).
self.extract_instantiation(node);
}
_ => {}
}
let mut cursor = node.walk();
let children: Vec<Node<'t>> = node.named_children(&mut cursor).collect();
for child in children {
self.visit(child);
}
}
// --- extractFunction / extractMethod (:1517 / :1737) ------------------
fn extract_function(&mut self, node: Node<'t>) {
// No receiver hook. Name first (resolveName inside extract_name).
let name = self.extract_name(node);
if name == "<anonymous>" {
// :1549 — body-only walk (nothing pushed). Dart signatures always
// name; preserved for fidelity.
if let Some(body) = self.resolve_body(node) {
self.visit_body(body);
}
return;
}
// isMisparsedFunction: the unnamed constructor is skipped — node
// suppressed, body still walked (attributed to the current stack top).
if self.is_unnamed_ctor(node) {
if let Some(body) = self.resolve_body(node) {
self.visit_body(body);
}
return;
}
let docstring = preceding_docstring(node, self.src);
let signature = self.signature_of(node);
let visibility = self.visibility_of(node);
let is_async = self.is_async_of(node);
let is_static = self.is_static_of(node);
let return_type = self.return_type_of(node);
let body = self.resolve_body(node);
let end_line_override = body.map(|b| b.end_position().row as u32 + 1);
let row = self.create_node(
"function",
&name,
node,
Extra {
docstring,
signature,
visibility,
is_async: Some(is_async),
is_static: Some(is_static),
return_type,
end_line_override,
},
);
let Some(row) = row else { return };
self.extract_type_annotations(node, row);
self.extract_decorators_for(node, row);
self.stack.push(Scope { row, kind: "function", name });
if let Some(body) = body {
self.visit_body(body);
}
self.stack.pop();
}
fn extract_method(&mut self, node: Node<'t>) {
// Gate (:1747): not inside class-like (no methodsAreTopLevel, no
// receiver, parent never object/object_expression) → extractFunction.
if !self.inside_class_like() {
self.extract_function(node);
return;
}
let name = self.extract_name(node);
// isMisparsedFunction — the unnamed ctor: body-only walk.
if self.is_unnamed_ctor(node) {
if let Some(body) = self.resolve_body(node) {
self.visit_body(body);
}
return;
}
let docstring = preceding_docstring(node, self.src);
let signature = self.signature_of(node);
let visibility = self.visibility_of(node);
let is_async = self.is_async_of(node);
let is_static = self.is_static_of(node);
let return_type = self.return_type_of(node);
let body = self.resolve_body(node);
let end_line_override = body.map(|b| b.end_position().row as u32 + 1);
// Operators mint method "<anonymous>" — extractMethod has NO skip.
let row = self.create_node(
"method",
&name,
node,
Extra {
docstring,
signature,
visibility,
is_async: Some(is_async),
is_static: Some(is_static),
return_type,
end_line_override,
},
);
let Some(row) = row else { return };
self.extract_type_annotations(node, row);
self.extract_decorators_for(node, row);
self.stack.push(Scope { row, kind: "method", name });
if let Some(body) = body {
self.visit_body(body);
}
self.stack.pop();
}
// --- extractClass (:1679) — classes, mixins, extensions ---------------
fn extract_class(&mut self, node: Node<'t>) {
let resolved_body = self.resolve_body(node);
// No skipBodilessClass. Anonymous `extension on String` → the name
// fallback finds the ON type's type_identifier — a class named after
// the extended type (preserved).
let name = self.extract_name(node);
let docstring = preceding_docstring(node, self.src);
let visibility = self.visibility_of(node);
let row = self.create_node(
"class",
&name,
node,
Extra { docstring, visibility, ..Default::default() },
);
let Some(row) = row else { return };
self.extract_inheritance(node, row);
// extractCsharpPrimaryCtorParamRefs — csharp-gated no-op.
self.extract_decorators_for(node, row);
self.stack.push(Scope { row, kind: "class", name });
let body = resolved_body.unwrap_or(node);
let mut cursor = body.walk();
let children: Vec<Node<'t>> = body.named_children(&mut cursor).collect();
for child in children {
self.visit(child);
}
self.stack.pop();
}
// --- extractEnum (:1914) ----------------------------------------------
fn extract_enum(&mut self, node: Node<'t>) {
let body = match self.resolve_body(node) {
Some(b) => b,
None => return,
};
let name = self.extract_name(node);
let docstring = preceding_docstring(node, self.src);
let visibility = self.visibility_of(node);
let row = self.create_node(
"enum",
&name,
node,
Extra { docstring, visibility, ..Default::default() },
);
let Some(row) = row else { return };
// Enum `with` mixins are a DIRECT child (no superclass wrapper) →
// no clause matches; `interfaces` DOES → implements only.
self.extract_inheritance(node, row);
// No extractDecoratorsFor on the enum path.
self.stack.push(Scope { row, kind: "enum", name });
let mut cursor = body.walk();
let children: Vec<Node<'t>> = body.named_children(&mut cursor).collect();
for child in children {
if child.kind() == "enum_constant" {
self.extract_enum_members(child);
} else {
self.visit(child);
}
}
self.stack.pop();
}
/// extractEnumMembers (:1958) — one enum_member per constant, positioned
/// at the enum_constant node; ctor arguments never walked.
fn extract_enum_members(&mut self, node: Node<'t>) {
if let Some(name_node) = node.child_by_field_name("name") {
let name = self.text(name_node).to_string();
self.create_node("enum_member", &name, node, Extra::default());
}
}
// --- extractTypeAlias (:2890, plain path) -----------------------------
fn extract_type_alias(&mut self, node: Node<'t>) -> bool {
let name = self.extract_name(node);
if name == "<anonymous>" {
return false;
}
let docstring = preceding_docstring(node, self.src);
// `value` field is null (type_alias has no fields) → no refs from
// the aliased type; returns false → children re-visited.
self.create_node("type_alias", &name, node, Extra { docstring, ..Default::default() });
false
}
// --- extractImport (:3170; hook dart.ts:261-304) ----------------------
fn extract_import(&mut self, node: Node<'t>) {
let find_child = |parent: Node<'t>, kind: &str| -> Option<Node<'t>> {
let mut cursor = parent.walk();
let found = parent.named_children(&mut cursor).find(|c| c.kind() == kind);
found
};
let uri_of = |spec: Node<'t>| -> Option<Node<'t>> {
let configurable = find_child(spec, "configurable_uri")?;
let uri = find_child(configurable, "uri")?;
find_child(uri, "string_literal")
};
let mut module: Option<String> = None;
if let Some(li) = find_child(node, "library_import") {
if let Some(spec) = find_child(li, "import_specification") {
if let Some(sl) = uri_of(spec) {
module = Some(self.text(sl).replace(['\'', '"'], ""));
}
}
}
if module.is_none() {
if let Some(le) = find_child(node, "library_export") {
if let Some(sl) = uri_of(le) {
module = Some(self.text(sl).replace(['\'', '"'], ""));
}
}
}
// Deferred imports (bare `uri`, no configurable_uri) → hook null →
// nothing at all (invisible).
let Some(module) = module.filter(|m| !m.is_empty()) else { return };
let signature = self.text(node).trim().to_string();
let created = self.create_node(
"import",
&module,
node,
Extra { signature: Some(signature), ..Default::default() },
);
if created.is_some() && !self.stack.is_empty() {
let parent_row = self.top_row();
self.push_ref_at(parent_row, &module, "imports", node);
}
}
// --- extractInstantiation (:4610, generic tail) -----------------------
fn extract_instantiation(&mut self, node: Node<'t>) {
if self.stack.is_empty() {
return;
}
let from_row = self.top_row();
let ctor = node
.child_by_field_name("constructor")
.or_else(|| node.child_by_field_name("type"))
.or_else(|| node.child_by_field_name("name"))
.or_else(|| node.named_child(0));
let Some(ctor) = ctor else { return };
let mut class_name = self.text(ctor).to_string();
if let Some(lt) = class_name.find('<') {
if lt > 0 {
class_name.truncate(lt);
}
}
let last_dot = class_name.rfind('.').map(|i| i as i64).unwrap_or(-1);
let last_colons = class_name.rfind("::").map(|i| (i + 1) as i64).unwrap_or(-1);
let last = last_dot.max(last_colons);
if last >= 0 {
class_name = class_name[(last as usize + 1)..].to_string();
class_name = class_name.trim_start_matches([':', '.']).to_string();
}
let class_name = class_name.trim().to_string();
if class_name.is_empty() {
return;
}
self.push_ref_at(from_row, &class_name, "instantiates", node);
}
// --- extractBareCall (dart.ts:305-379) --------------------------------
fn bare_call_name(&self, node: Node<'t>) -> Option<String> {
if node.kind() == "selector" {
let mut cursor = node.walk();
let has_arg_part = node.named_children(&mut cursor).any(|c| c.kind() == "argument_part");
if !has_arg_part {
return None;
}
let prev = node.prev_named_sibling()?;
if prev.kind() == "identifier" {
return Some(self.text(prev).to_string());
}
if prev.kind() == "selector" {
let mut pc = prev.walk();
let accessor = prev.named_children(&mut pc).find(|c| {
matches!(
c.kind(),
"unconditional_assignable_selector" | "conditional_assignable_selector"
)
});
if let Some(accessor) = accessor {
let mut ac = accessor.walk();
let method_id = accessor.named_children(&mut ac).find(|c| c.kind() == "identifier");
if let Some(method_id) = method_id {
let accessor_prev = prev.prev_named_sibling();
if let Some(ap) = accessor_prev {
if ap.kind() == "identifier" {
return Some(format!("{}.{}", self.text(ap), self.text(method_id)));
}
// Chained static-factory: the receiver is itself
// a call — re-encode `<inner>().<method>` when
// the chain starts capitalized (#750).
if ap.kind() == "selector" {
let mut apc = ap.walk();
if ap.named_children(&mut apc).any(|c| c.kind() == "argument_part") {
if let Some(inner) = self.callee_of_arg_part(ap) {
if starts_upper_re().is_match(&inner) {
return Some(format!("{}().{}", inner, self.text(method_id)));
}
}
}
}
}
return Some(self.text(method_id).to_string());
}
}
}
// super.method() / this.method(): prev is a bare accessor.
if matches!(
prev.kind(),
"unconditional_assignable_selector" | "conditional_assignable_selector"
) {
let mut pc = prev.walk();
let id = prev.named_children(&mut pc).find(|c| c.kind() == "identifier");
if let Some(id) = id {
return Some(self.text(id).to_string());
}
}
return None;
}
// new_expression arm — DEAD in practice (the INSTANTIATION branch
// fires first in the body walker); ported for fidelity.
if node.kind() == "new_expression" {
let mut cursor = node.walk();
let found = node