-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathrust_lang.rs
More file actions
720 lines (672 loc) · 30.1 KB
/
Copy pathrust_lang.rs
File metadata and controls
720 lines (672 loc) · 30.1 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
use super::helpers::*;
use super::SymbolExtractor;
use crate::ast_analysis::cfg::build_function_cfg;
use crate::ast_analysis::complexity::compute_all_metrics;
use crate::types::*;
use tree_sitter::{Node, Tree};
pub struct RustExtractor;
impl SymbolExtractor for RustExtractor {
fn extract(&self, tree: &Tree, source: &[u8], file_path: &str) -> FileSymbols {
let mut symbols = FileSymbols::new(file_path.to_string());
walk_tree(&tree.root_node(), source, &mut symbols, match_rust_node);
walk_ast_nodes_with_config(&tree.root_node(), source, &mut symbols.ast_nodes, &RUST_AST_CONFIG);
walk_tree(&tree.root_node(), source, &mut symbols, match_rust_type_map);
walk_tree(&tree.root_node(), source, &mut symbols, match_rust_return_type_map);
// Must run after type_map is populated — resolves `receiver.method()` call
// assignments against locally-typed receivers (mirrors javascript.rs's ordering).
walk_tree(&tree.root_node(), source, &mut symbols, match_rust_call_assignments);
dedup_type_map(&mut symbols.type_map);
dedup_type_map(&mut symbols.return_type_map);
symbols
}
}
fn find_current_impl<'a>(node: &Node<'a>, source: &[u8]) -> Option<String> {
let mut current = node.parent();
while let Some(parent) = current {
if parent.kind() == "impl_item" {
return named_child_text(&parent, "type", source)
.map(|s| s.to_string());
}
current = parent.parent();
}
None
}
fn match_rust_node(node: &Node, source: &[u8], symbols: &mut FileSymbols, _depth: usize) {
match node.kind() {
"function_item" => handle_function_item(node, source, symbols),
"struct_item" => handle_struct_item(node, source, symbols),
"enum_item" => handle_enum_item(node, source, symbols),
"const_item" => handle_const_item(node, source, symbols),
"trait_item" => handle_trait_item(node, source, symbols),
"impl_item" => handle_impl_item(node, source, symbols),
"use_declaration" => handle_use_decl(node, source, symbols),
"call_expression" => handle_call_expr(node, source, symbols),
"macro_invocation" => handle_macro_invocation(node, source, symbols),
_ => {}
}
}
// ── Per-node-kind handlers for walk_node_depth ───────────────────────────────
fn handle_function_item(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
// Skip default-impl functions inside traits — already emitted by trait_item handler
if node.parent()
.and_then(|p| p.parent())
.map_or(false, |gp| gp.kind() == "trait_item")
{
return;
}
let Some(name_node) = node.child_by_field_name("name") else { return };
let name = node_text(&name_node, source);
let impl_type = find_current_impl(node, source);
let (full_name, kind) = match &impl_type {
Some(t) => (format!("{}.{}", t, name), "method".to_string()),
None => (name.to_string(), "function".to_string()),
};
let children = extract_rust_parameters(node, source);
symbols.definitions.push(Definition {
name: full_name,
kind,
line: start_line(node),
end_line: Some(end_line(node)),
decorators: None,
complexity: compute_all_metrics(node, source, "rust"),
cfg: build_function_cfg(node, "rust", source),
children: opt_children(children),
});
}
fn handle_struct_item(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
if let Some(name_node) = node.child_by_field_name("name") {
let struct_name = node_text(&name_node, source).to_string();
let children = extract_rust_struct_fields(node, source);
symbols.definitions.push(Definition {
name: struct_name.clone(),
kind: "struct".to_string(),
line: start_line(node),
end_line: Some(end_line(node)),
decorators: None,
complexity: None,
cfg: None,
children: opt_children(children),
});
seed_rust_struct_field_types(node, &struct_name, source, symbols);
}
}
/// Seed `${StructName}.${fieldName}` → field-type entries in `symbols.type_map`
/// so `self.field.method()` inside the struct's own impl methods resolves via
/// the class-scoped receiver lookup — mirrors JS's `this.field` class-scoped
/// typing (issues #1323, #1458) and fixes #1876's `self.field` false negatives.
fn seed_rust_struct_field_types(node: &Node, struct_name: &str, source: &[u8], symbols: &mut FileSymbols) {
let Some(body) = node.child_by_field_name("body") else { return };
for i in 0..body.child_count() {
let Some(field) = body.child(i) else { continue };
if field.kind() != "field_declaration" { continue }
let Some(field_name) = field.child_by_field_name("name") else { continue };
let Some(type_node) = field.child_by_field_name("type") else { continue };
let Some(type_name) = extract_rust_type_name(&type_node, source) else { continue };
push_type_map_entry(
symbols,
format!("{}.{}", struct_name, node_text(&field_name, source)),
type_name,
);
}
}
fn handle_enum_item(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
if let Some(name_node) = node.child_by_field_name("name") {
let children = extract_rust_enum_variants(node, source);
symbols.definitions.push(Definition {
name: node_text(&name_node, source).to_string(),
kind: "enum".to_string(),
line: start_line(node),
end_line: Some(end_line(node)),
decorators: None,
complexity: None,
cfg: None,
children: opt_children(children),
});
}
}
fn handle_const_item(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
if let Some(name_node) = node.child_by_field_name("name") {
symbols.definitions.push(Definition {
name: node_text(&name_node, source).to_string(),
kind: "constant".to_string(),
line: start_line(node),
end_line: Some(end_line(node)),
decorators: None,
complexity: None,
cfg: None,
children: None,
});
}
}
fn handle_trait_item(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
let Some(name_node) = node.child_by_field_name("name") else { return };
let trait_name = node_text(&name_node, source).to_string();
symbols.definitions.push(Definition {
name: trait_name.clone(),
kind: "trait".to_string(),
line: start_line(node),
end_line: Some(end_line(node)),
decorators: None,
complexity: None,
cfg: None,
children: None,
});
if let Some(body) = node.child_by_field_name("body") {
for i in 0..body.child_count() {
let Some(child) = body.child(i) else { continue };
if child.kind() != "function_signature_item" && child.kind() != "function_item" {
continue;
}
if let Some(meth_name) = child.child_by_field_name("name") {
symbols.definitions.push(Definition {
name: format!("{}.{}", trait_name, node_text(&meth_name, source)),
kind: "method".to_string(),
line: start_line(&child),
end_line: Some(end_line(&child)),
decorators: None,
complexity: compute_all_metrics(&child, source, "rust"),
cfg: build_function_cfg(&child, "rust", source),
children: None,
});
}
}
}
}
fn handle_impl_item(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
let type_node = node.child_by_field_name("type");
let trait_node = node.child_by_field_name("trait");
if let (Some(type_node), Some(trait_node)) = (type_node, trait_node) {
symbols.classes.push(ClassRelation {
name: node_text(&type_node, source).to_string(),
extends: None,
implements: Some(node_text(&trait_node, source).to_string()),
line: start_line(node),
});
}
}
fn handle_use_decl(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
if let Some(arg_node) = node.child(1) {
let use_paths = extract_rust_use_path(&arg_node, source);
for (src, names) in use_paths {
let mut imp = Import::new(src, names, start_line(node));
imp.rust_use = Some(true);
symbols.imports.push(imp);
}
}
}
fn handle_call_expr(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
let Some(fn_node) = node.child_by_field_name("function") else { return };
match fn_node.kind() {
"identifier" => {
symbols.calls.push(Call {
name: node_text(&fn_node, source).to_string(),
line: start_line(node),
dynamic: None,
receiver: None,
..Default::default()
});
}
"field_expression" => {
if let Some(field) = fn_node.child_by_field_name("field") {
let receiver = named_child_text(&fn_node, "value", source)
.map(|s| s.to_string());
symbols.calls.push(Call {
name: node_text(&field, source).to_string(),
line: start_line(node),
dynamic: None,
receiver,
..Default::default()
});
}
}
"scoped_identifier" => {
if let Some(name) = fn_node.child_by_field_name("name") {
let receiver = named_child_text(&fn_node, "path", source)
.map(|s| s.to_string());
symbols.calls.push(Call {
name: node_text(&name, source).to_string(),
line: start_line(node),
dynamic: None,
receiver,
..Default::default()
});
}
}
_ => {}
}
}
fn handle_macro_invocation(node: &Node, source: &[u8], symbols: &mut FileSymbols) {
if let Some(macro_node) = node.child(0) {
symbols.calls.push(Call {
name: format!("{}!", node_text(¯o_node, source)),
line: start_line(node),
dynamic: None,
receiver: None,
..Default::default()
});
}
}
// ── Extended kinds helpers ──────────────────────────────────────────────────
fn extract_rust_parameters(node: &Node, source: &[u8]) -> Vec<Definition> {
let mut params = Vec::new();
let params_node = node.child_by_field_name("parameters");
if let Some(params_node) = params_node {
for i in 0..params_node.child_count() {
if let Some(child) = params_node.child(i) {
if child.kind() == "parameter" {
if let Some(pattern) = child.child_by_field_name("pattern") {
let name = node_text(&pattern, source);
// Skip self parameters
if name == "self" || name == "&self" || name == "&mut self" || name == "mut self" {
continue;
}
params.push(child_def(name.to_string(), "parameter", start_line(&child)));
}
} else if child.kind() == "self_parameter" {
// Skip self
continue;
}
}
}
}
params
}
fn extract_rust_struct_fields(node: &Node, source: &[u8]) -> Vec<Definition> {
let mut fields = Vec::new();
let body = node.child_by_field_name("body")
.or_else(|| find_child(node, "field_declaration_list"));
if let Some(body) = body {
for i in 0..body.child_count() {
if let Some(child) = body.child(i) {
if child.kind() == "field_declaration" {
if let Some(name_node) = child.child_by_field_name("name") {
fields.push(child_def(
node_text(&name_node, source).to_string(),
"property",
start_line(&child),
));
}
}
}
}
}
fields
}
fn extract_rust_enum_variants(node: &Node, source: &[u8]) -> Vec<Definition> {
let mut variants = Vec::new();
let body = node.child_by_field_name("body")
.or_else(|| find_child(node, "enum_variant_list"));
if let Some(body) = body {
for i in 0..body.child_count() {
if let Some(child) = body.child(i) {
if child.kind() == "enum_variant" {
if let Some(name_node) = child.child_by_field_name("name") {
variants.push(child_def(
node_text(&name_node, source).to_string(),
"constant",
start_line(&child),
));
}
}
}
}
}
variants
}
// ── Existing helpers ────────────────────────────────────────────────────────
fn extract_rust_use_path(node: &Node, source: &[u8]) -> Vec<(String, Vec<String>)> {
match node.kind() {
"use_list" => {
let mut results = Vec::new();
for i in 0..node.child_count() {
let Some(child) = node.child(i) else { continue };
results.extend(extract_rust_use_path(&child, source));
}
results
}
"scoped_use_list" => extract_scoped_use_list(node, source),
"use_as_clause" => {
let name = node
.child_by_field_name("alias")
.or_else(|| node.child_by_field_name("name"))
.map(|n| node_text(&n, source).to_string());
vec![(node_text(node, source).to_string(), name.into_iter().collect())]
}
"use_wildcard" => {
let src = named_child_text(&node, "path", source)
.map(|s| s.to_string())
.unwrap_or_else(|| "*".to_string());
vec![(src, vec!["*".to_string()])]
}
"scoped_identifier" | "identifier" => {
let text = node_text(node, source).to_string();
let last_name = text.split("::").last().unwrap_or("").to_string();
vec![(text, vec![last_name])]
}
_ => vec![],
}
}
fn extract_scoped_use_list(node: &Node, source: &[u8]) -> Vec<(String, Vec<String>)> {
let prefix = named_child_text(&node, "path", source)
.map(|s| s.to_string())
.unwrap_or_default();
let Some(list_node) = node.child_by_field_name("list") else {
return vec![(prefix, vec![])];
};
let mut names = Vec::new();
for i in 0..list_node.child_count() {
let Some(child) = list_node.child(i) else { continue };
match child.kind() {
"identifier" | "self" => {
names.push(node_text(&child, source).to_string());
}
"use_as_clause" => {
let name = child
.child_by_field_name("alias")
.or_else(|| child.child_by_field_name("name"))
.map(|n| node_text(&n, source).to_string());
if let Some(name) = name {
names.push(name);
}
}
_ => {}
}
}
vec![(prefix, names)]
}
/// True if `name` matches a struct defined in this file (match_rust_node runs before this).
fn is_known_unit_struct(name: &str, symbols: &FileSymbols) -> bool {
symbols.definitions.iter().any(|d| d.kind == "struct" && d.name == name)
}
fn extract_rust_type_name<'a>(type_node: &Node<'a>, source: &'a [u8]) -> Option<&'a str> {
match type_node.kind() {
"type_identifier" | "identifier" | "scoped_type_identifier" => Some(node_text(type_node, source)),
"reference_type" => {
for i in 0..type_node.child_count() {
if let Some(child) = type_node.child(i) {
if child.kind() == "type_identifier" || child.kind() == "scoped_type_identifier" {
return Some(node_text(&child, source));
}
}
}
None
}
"generic_type" => type_node.child(0).map(|n| node_text(&n, source)),
_ => None,
}
}
fn match_rust_type_map(node: &Node, source: &[u8], symbols: &mut FileSymbols, _depth: usize) {
match node.kind() {
"let_declaration" => {
if let Some(pattern) = node.child_by_field_name("pattern") {
if pattern.kind() == "identifier" {
if let Some(type_node) = node.child_by_field_name("type") {
if let Some(type_name) = extract_rust_type_name(&type_node, source) {
symbols.type_map.push(TypeMapEntry {
name: node_text(&pattern, source).to_string(),
type_name: type_name.to_string(),
confidence: 0.9,
});
}
} else if let Some(value_node) = node.child_by_field_name("value") {
// let x = TypeName; — a bare capitalized identifier value binds
// a unit-struct instance (e.g. `let v = NameValidator;` for
// `struct NameValidator;`), not a reference to another variable (#1876).
// Requiring a same-file `struct` definition excludes unit enum variants
// like `None`/`Ok` (Option/Result, always in scope) and any custom
// fieldless variant brought into scope via `use Enum::Variant` — those
// also parse as a bare capitalized identifier but are values, not types
// (Greptile review). A struct defined elsewhere in the crate is missed,
// same as every other same-file-only heuristic in this extractor.
if value_node.kind() == "identifier" {
let type_name = node_text(&value_node, source);
if type_name.starts_with(|c: char| c.is_uppercase())
&& is_known_unit_struct(type_name, symbols)
{
symbols.type_map.push(TypeMapEntry {
name: node_text(&pattern, source).to_string(),
type_name: type_name.to_string(),
confidence: 0.7,
});
}
}
}
}
}
}
"parameter" => {
if let Some(pattern) = node.child_by_field_name("pattern") {
if pattern.kind() == "identifier" {
let name = node_text(&pattern, source);
if name != "self" && name != "&self" && name != "&mut self" && name != "mut self" {
if let Some(type_node) = node.child_by_field_name("type") {
if let Some(type_name) = extract_rust_type_name(&type_node, source) {
symbols.type_map.push(TypeMapEntry {
name: name.to_string(),
type_name: type_name.to_string(),
confidence: 0.9,
});
}
}
}
}
}
}
_ => {}
}
}
// ── Return-type map extraction (Phase 8.2 parity, #1876) ────────────────────
/// Populate `symbols.return_type_map` with declared `-> ReturnType` return
/// types for free functions and impl methods, resolving `Self` to the
/// enclosing impl's type name. Mirrors `extractRustReturnTypeMap` in
/// `src/extractors/rust.ts`. Consumed by `propagate_return_types_across_files`
/// (Phase 8.2) — the same generic cross-file mechanism the JS/TS extractor
/// feeds — so a local var typed from a cross-file call's return value
/// (`let service = build_service();`) resolves without any Rust-specific
/// propagation logic.
fn match_rust_return_type_map(node: &Node, source: &[u8], symbols: &mut FileSymbols, _depth: usize) {
if node.kind() != "function_item" { return }
// Skip default-impl functions inside traits, matching handle_function_item —
// their return type is not tied to a concrete implementing type.
if node.parent().and_then(|p| p.parent()).map_or(false, |gp| gp.kind() == "trait_item") {
return;
}
let Some(name_node) = node.child_by_field_name("name") else { return };
let Some(return_type_node) = node.child_by_field_name("return_type") else { return };
let Some(raw_type) = extract_rust_type_name(&return_type_node, source) else { return };
let impl_type = find_current_impl(node, source);
// `-> Self` inside an impl block returns the concrete implementing type.
let type_name = if raw_type == "Self" {
impl_type.as_deref().unwrap_or(raw_type)
} else {
raw_type
};
let full_name = match &impl_type {
Some(t) => format!("{}.{}", t, node_text(&name_node, source)),
None => node_text(&name_node, source).to_string(),
};
let existing_confidence = symbols.return_type_map.iter()
.find(|e| e.name == full_name)
.map(|e| e.confidence);
if existing_confidence.map_or(true, |c| c < 1.0) {
symbols.return_type_map.push(TypeMapEntry {
name: full_name,
type_name: type_name.to_string(),
confidence: 1.0,
});
}
}
// ── Call-assignment extraction (Phase 8.2 parity, #1876) ─────────────────────
/// Record `let x = callee(...);` bindings into `symbols.call_assignments` so
/// `propagate_return_types_across_files` can type `x` from `callee`'s
/// declared return type. Mirrors `extractRustCallAssignments` in
/// `src/extractors/rust.ts` — see that function's doc comment for the three
/// call shapes handled (bare function call, `Type::assoc_fn()`, and method
/// call on a locally-typed receiver).
fn match_rust_call_assignments(node: &Node, source: &[u8], symbols: &mut FileSymbols, _depth: usize) {
if node.kind() != "let_declaration" { return }
let Some(pattern) = node.child_by_field_name("pattern") else { return };
if pattern.kind() != "identifier" { return }
let Some(value) = node.child_by_field_name("value") else { return };
if value.kind() != "call_expression" { return }
let Some(fn_node) = value.child_by_field_name("function") else { return };
let var_name = node_text(&pattern, source).to_string();
match fn_node.kind() {
"identifier" => {
symbols.call_assignments.push(NativeCallAssignment {
var_name,
callee_name: node_text(&fn_node, source).to_string(),
receiver_type_name: None,
});
}
"scoped_identifier" => {
let name = fn_node.child_by_field_name("name");
let path = fn_node.child_by_field_name("path");
if let (Some(name), Some(path)) = (name, path) {
symbols.call_assignments.push(NativeCallAssignment {
var_name,
callee_name: node_text(&name, source).to_string(),
receiver_type_name: Some(node_text(&path, source).to_string()),
});
}
}
"field_expression" => {
let field = fn_node.child_by_field_name("field");
let receiver = fn_node.child_by_field_name("value");
if let (Some(field), Some(receiver)) = (field, receiver) {
if receiver.kind() == "identifier" {
let receiver_type = symbols.type_map.iter()
.find(|e| e.name == node_text(&receiver, source))
.map(|e| e.type_name.clone());
symbols.call_assignments.push(NativeCallAssignment {
var_name,
callee_name: node_text(&field, source).to_string(),
receiver_type_name: receiver_type,
});
}
}
}
_ => {}
}
}
#[cfg(test)]
mod tests {
use super::*;
use tree_sitter::Parser;
fn parse_rust(code: &str) -> FileSymbols {
let mut parser = Parser::new();
parser
.set_language(&tree_sitter_rust::LANGUAGE.into())
.unwrap();
let tree = parser.parse(code.as_bytes(), None).unwrap();
RustExtractor.extract(&tree, code.as_bytes(), "test.rs")
}
// ── Extended kinds tests ────────────────────────────────────────────────
#[test]
fn extracts_function_parameters() {
let s = parse_rust("fn add(a: i32, b: i32) -> i32 { a + b }");
let add = s.definitions.iter().find(|d| d.name == "add").unwrap();
let children = add.children.as_ref().unwrap();
assert_eq!(children.len(), 2);
assert_eq!(children[0].name, "a");
assert_eq!(children[0].kind, "parameter");
assert_eq!(children[1].name, "b");
}
#[test]
fn extracts_struct_fields() {
let s = parse_rust("struct User { name: String, age: u32 }");
let user = s.definitions.iter().find(|d| d.name == "User").unwrap();
let children = user.children.as_ref().unwrap();
assert_eq!(children.len(), 2);
assert_eq!(children[0].name, "name");
assert_eq!(children[0].kind, "property");
assert_eq!(children[1].name, "age");
}
#[test]
fn extracts_const_item() {
let s = parse_rust("const MAX: i32 = 100;");
let c = s.definitions.iter().find(|d| d.name == "MAX").unwrap();
assert_eq!(c.kind, "constant");
}
#[test]
fn extracts_enum_variants() {
let s = parse_rust("enum Color { Red, Green, Blue }");
let color = s.definitions.iter().find(|d| d.name == "Color").unwrap();
let children = color.children.as_ref().unwrap();
assert_eq!(children.len(), 3);
assert_eq!(children[0].name, "Red");
assert_eq!(children[0].kind, "constant");
assert_eq!(children[1].name, "Green");
assert_eq!(children[2].name, "Blue");
}
#[test]
fn skips_self_parameter() {
let s = parse_rust("struct Foo {}\nimpl Foo {\n fn bar(&self, x: i32) {}\n}");
let bar = s.definitions.iter().find(|d| d.name == "Foo.bar").unwrap();
let children = bar.children.as_ref().unwrap();
assert_eq!(children.len(), 1);
assert_eq!(children[0].name, "x");
}
// ── #1876: receiver-typed locals + self.field type map ──────────────────
#[test]
fn seeds_struct_field_type_map() {
let s = parse_rust("struct UserService { repo: UserRepository }");
let entry = s.type_map.iter().find(|e| e.name == "UserService.repo").unwrap();
assert_eq!(entry.type_name, "UserRepository");
}
#[test]
fn seeds_unit_struct_value_type_map() {
let s = parse_rust("struct NameValidator;\nfn f() { let v = NameValidator; }");
let entry = s.type_map.iter().find(|e| e.name == "v").unwrap();
assert_eq!(entry.type_name, "NameValidator");
}
#[test]
fn does_not_type_unit_enum_variant_as_unit_struct() {
// `None` (Option::None) parses identically to a unit-struct reference — a bare
// capitalized identifier — but is an enum variant, not a struct. Without a same-file
// `struct` definition for the name, it must not be typed (#1876 review).
let s = parse_rust("fn f() { let x = None; }");
assert!(s.type_map.iter().all(|e| e.name != "x"));
}
#[test]
fn does_not_type_lowercase_bare_identifier_binding() {
let s = parse_rust("fn f() { let a = 1; let b = a; }");
assert!(s.type_map.iter().all(|e| e.name != "b"));
}
#[test]
fn stores_return_type_for_free_function() {
let s = parse_rust("fn build_service() -> UserService { todo!() }");
let entry = s.return_type_map.iter().find(|e| e.name == "build_service").unwrap();
assert_eq!(entry.type_name, "UserService");
assert_eq!(entry.confidence, 1.0);
}
#[test]
fn resolves_self_return_type_to_impl_type() {
let s = parse_rust("struct UserRepository;\nimpl UserRepository {\n fn new() -> Self { UserRepository }\n}");
let entry = s.return_type_map.iter().find(|e| e.name == "UserRepository.new").unwrap();
assert_eq!(entry.type_name, "UserRepository");
}
#[test]
fn records_call_assignment_for_bare_function_call() {
let s = parse_rust("fn f() { let service = build_service(); }");
let ca = s.call_assignments.iter().find(|c| c.var_name == "service").unwrap();
assert_eq!(ca.callee_name, "build_service");
assert_eq!(ca.receiver_type_name, None);
}
#[test]
fn records_call_assignment_for_associated_function_call() {
let s = parse_rust("fn f() { let repo = UserRepository::new(); }");
let ca = s.call_assignments.iter().find(|c| c.var_name == "repo").unwrap();
assert_eq!(ca.callee_name, "new");
assert_eq!(ca.receiver_type_name.as_deref(), Some("UserRepository"));
}
#[test]
fn records_call_assignment_for_method_call_on_typed_receiver() {
let s = parse_rust(
"fn f() {\n let repo: UserRepository = make();\n let user = repo.find_by_id(1);\n}",
);
let ca = s.call_assignments.iter().find(|c| c.var_name == "user").unwrap();
assert_eq!(ca.callee_name, "find_by_id");
assert_eq!(ca.receiver_type_name.as_deref(), Some("UserRepository"));
}
}