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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
//! Swift language handler for call graph analysis.
//!
//! This module provides Swift call graph support using tree-sitter-swift (v0.7.1, ABI v15).
//!
//! # Import Patterns Supported
//!
//! | Pattern | ImportDef |
//! |---------|-----------|
//! | `import Foundation` | `{module: "Foundation"}` |
//! | `import struct Foundation.Date` | `{module: "Foundation", names: ["Date"], kind: "struct"}` |
//! | `import class UIKit.UIView` | `{module: "UIKit", names: ["UIView"], kind: "class"}` |
//! | `import func Foundation.strcmp` | `{module: "Foundation", names: ["strcmp"], kind: "func"}` |
//! | `@testable import MyApp` | `{module: "MyApp", is_testable: true}` |
//!
//! # Call Extraction
//!
//! - Direct calls: `func()` -> CallType::Direct or CallType::Intra
//! - Method calls: `obj.method()` -> CallType::Attr
//! - Static calls: `Type.staticMethod()` -> CallType::Attr
//! - Property initializer calls: `let x = Foo()` at class/struct scope
//! - Default parameter calls: `func f(x: Int = defaultVal())`
//! - Global/file-level calls: Calls at file scope use `<module>` as caller
//! - Lazy property calls: `lazy var x = computeValue()`
//! - Closure calls: `{ compute() }` inside function bodies
//! - Guard let/if let calls: `guard let x = tryFoo() else { ... }`
//! - Switch case calls: Calls inside switch patterns
//! - Protocol extension calls: Calls in extension methods
//!
//! # Spec Reference
//!
//! See `migration/spec/callgraph-spec.md` Section 9.11 for Swift-specific details.
use std::collections::{HashMap, HashSet};
use std::path::Path;
use regex::Regex;
use tree_sitter::{Node, Parser, Tree};
use super::base::{get_node_text, walk_tree};
use super::{CallGraphLanguageSupport, ParseError};
use crate::callgraph::cross_file_types::{CallSite, CallType, ClassDef, FuncDef, ImportDef};
// =============================================================================
// Regex Patterns (kept for import parsing)
// =============================================================================
lazy_static::lazy_static! {
// Import patterns:
// - import Foundation
// - import struct Foundation.Date
// - @testable import MyApp
static ref IMPORT_RE: Regex = Regex::new(
r"(?m)^[ \t]*(@testable\s+)?import\s+(struct|class|enum|protocol|func|var|let|typealias)?\s*(\S+)"
).unwrap();
}
fn parse_swift_bases(line: &str) -> Vec<String> {
let mut bases = Vec::new();
let colon_pos = match line.find(':') {
Some(pos) => pos,
None => return bases,
};
let mut after = &line[colon_pos + 1..];
if let Some(before_brace) = after.split('{').next() {
after = before_brace;
}
if let Some(before_where) = after.split(" where ").next() {
after = before_where;
}
for part in after.split(',') {
let name = part.trim();
if name.is_empty() {
continue;
}
let name = name.split('<').next().unwrap_or(name).trim();
if !name.is_empty() {
bases.push(name.to_string());
}
}
bases
}
// =============================================================================
// Swift Handler
// =============================================================================
/// Swift language handler using tree-sitter-swift for AST-based call extraction.
///
/// Supports:
/// - Import declaration parsing (simple, selective, @testable) via regex
/// - Function/method declaration extraction via tree-sitter
/// - Type (class/struct/enum/protocol) declaration extraction via tree-sitter
/// - Call extraction (direct, method, static, property init, default params,
/// closures, guard/if-let, switch cases) via tree-sitter
#[derive(Debug, Default)]
pub struct SwiftHandler;
impl SwiftHandler {
/// Creates a new SwiftHandler.
pub fn new() -> Self {
Self
}
/// Parse the source code into a tree-sitter Tree using the Swift grammar.
fn parse_source(&self, source: &str) -> Result<Tree, ParseError> {
let mut parser = Parser::new();
parser
.set_language(&tree_sitter_swift::LANGUAGE.into())
.map_err(|e| ParseError::ParseFailed {
file: std::path::PathBuf::new(),
message: format!("Failed to set Swift language: {}", e),
})?;
parser
.parse(source, None)
.ok_or_else(|| ParseError::ParseFailed {
file: std::path::PathBuf::new(),
message: "Parser returned None".to_string(),
})
}
/// Parse imports from source code using regex.
fn parse_imports_regex(&self, source: &str) -> Vec<ImportDef> {
let mut imports = Vec::new();
for cap in IMPORT_RE.captures_iter(source) {
let is_testable = cap.get(1).is_some();
let kind = cap.get(2).map(|m| m.as_str().to_string());
let module_path = cap.get(3).map(|m| m.as_str()).unwrap_or("");
if module_path.is_empty() {
continue;
}
// Parse module path: Foundation.Date -> module=Foundation, name=Date
let (module, names) = if let Some(_kind_str) = &kind {
// Selective import: import struct Foundation.Date
if let Some(last_dot) = module_path.rfind('.') {
let module = module_path[..last_dot].to_string();
let name = module_path[last_dot + 1..].to_string();
(module, vec![name])
} else {
// No dot, entire thing is the name from implicit module
// This is unusual but handle it
(module_path.to_string(), vec![])
}
} else {
// Simple import: import Foundation
(module_path.to_string(), vec![])
};
let mut import_def = if names.is_empty() {
ImportDef::simple_import(module)
} else {
ImportDef::from_import(module, names)
};
// Store kind and testable flag in resolved_module for now
// since ImportDef doesn't have dedicated fields for these
if is_testable {
import_def.resolved_module = Some("@testable".to_string());
}
imports.push(import_def);
}
imports
}
/// Collect all function names defined in the file using tree-sitter.
fn collect_function_definitions_ts(&self, tree: &Tree, source: &[u8]) -> HashSet<String> {
let mut functions = HashSet::new();
for node in walk_tree(tree.root_node()) {
match node.kind() {
"function_declaration" => {
if let Some(name_node) = node.child_by_field_name("name") {
functions.insert(get_node_text(&name_node, source).to_string());
}
}
"protocol_function_declaration" => {
// Protocol requirement: func required()
if let Some(name_node) = node.child_by_field_name("name") {
functions.insert(get_node_text(&name_node, source).to_string());
}
}
"init_declaration" => {
functions.insert("init".to_string());
}
"class_declaration" => {
// Type names are callable (as initializers)
if let Some(name_node) = node.child_by_field_name("name") {
let text = get_node_text(&name_node, source);
// name_node might be a type_identifier or user_type
// For classes: type_identifier directly
// For extensions: user_type containing type_identifier
let type_name = if name_node.kind() == "user_type" {
// Extract the type_identifier from user_type
name_node
.child(0)
.map(|c| get_node_text(&c, source))
.unwrap_or(text)
} else {
text
};
functions.insert(type_name.to_string());
}
// Also check declaration_kind to see if it's a struct/enum/protocol
}
"protocol_declaration" => {
if let Some(name_node) = node.child_by_field_name("name") {
functions.insert(get_node_text(&name_node, source).to_string());
}
}
_ => {}
}
}
functions
}
/// Collect function definitions using regex (kept for backward compat with tests).
pub fn collect_function_definitions(&self, source: &str) -> HashSet<String> {
let tree = match self.parse_source(source) {
Ok(t) => t,
Err(_) => return HashSet::new(),
};
self.collect_function_definitions_ts(&tree, source.as_bytes())
}
/// Resolve the target and receiver from a call_expression node.
///
/// Returns (target_string, receiver_option, call_type_hint).
fn resolve_call_target(
&self,
node: &Node,
source: &[u8],
defined_funcs: &HashSet<String>,
) -> Option<(String, Option<String>, CallType)> {
// The first child of call_expression is the callable
let func_node = node.child(0)?;
match func_node.kind() {
"simple_identifier" => {
let target = get_node_text(&func_node, source).to_string();
if is_swift_keyword(&target) {
return None;
}
let call_type = if defined_funcs.contains(&target) {
CallType::Intra
} else {
CallType::Direct
};
Some((target, None, call_type))
}
"navigation_expression" => self.resolve_navigation_call(&func_node, source),
_ => None,
}
}
/// Resolve a navigation_expression (receiver.method) into call target info.
fn resolve_navigation_call(
&self,
nav_node: &Node,
source: &[u8],
) -> Option<(String, Option<String>, CallType)> {
let target_node = nav_node.child_by_field_name("target")?;
let suffix_node = nav_node.child_by_field_name("suffix")?;
// Extract the method name from the suffix
let method = self.extract_suffix_name(&suffix_node, source)?;
// Extract the receiver
let receiver = self.extract_target_name(&target_node, source)?;
let target = format!("{}.{}", receiver, method);
Some((target, Some(receiver), CallType::Attr))
}
/// Extract the method/property name from a navigation_suffix node.
fn extract_suffix_name(&self, node: &Node, source: &[u8]) -> Option<String> {
// navigation_suffix has children: "." and simple_identifier
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if child.kind() == "simple_identifier" {
return Some(get_node_text(&child, source).to_string());
}
}
}
None
}
/// Extract the receiver name from the target of a navigation_expression.
fn extract_target_name(&self, node: &Node, source: &[u8]) -> Option<String> {
match node.kind() {
"simple_identifier" => Some(get_node_text(node, source).to_string()),
"self_expression" => Some("self".to_string()),
"super_expression" => Some("super".to_string()),
"call_expression" => {
// Chained call: receiver is the full call expression text, but
// for our purposes we want the outermost simple_identifier
// e.g., data.filter { ... }.map { ... } -> "data.filter"
// Actually, let's use the full text for chained expressions
let text = get_node_text(node, source).to_string();
// Extract just the first identifier for a cleaner receiver
if let Some(first_child) = node.child(0) {
if first_child.kind() == "navigation_expression" {
if let Some(t) = first_child.child_by_field_name("target") {
return self.extract_target_name(&t, source);
}
} else if first_child.kind() == "simple_identifier" {
return Some(get_node_text(&first_child, source).to_string());
}
}
Some(text)
}
"navigation_expression" => {
// Nested navigation: a.b.c - use the full text
Some(get_node_text(node, source).to_string())
}
_ => Some(get_node_text(node, source).to_string()),
}
}
/// Extract all call_expression nodes from a subtree, returning CallSites.
fn extract_calls_from_subtree(
&self,
node: &Node,
source: &[u8],
defined_funcs: &HashSet<String>,
caller: &str,
) -> Vec<CallSite> {
let mut calls = Vec::new();
for child in walk_tree(*node) {
if child.kind() == "call_expression" {
let line = child.start_position().row as u32 + 1;
if let Some((target, receiver, call_type)) =
self.resolve_call_target(&child, source, defined_funcs)
{
calls.push(CallSite::new(
caller.to_string(),
target,
call_type,
Some(line),
None,
receiver,
None,
));
}
}
}
calls
}
/// Extract calls from the entire source using tree-sitter AST walking.
///
/// This walks the AST looking for:
/// 1. function_declaration / init_declaration nodes -> extract calls from their bodies
/// 2. Property initializer calls at class/struct scope
/// 3. Default parameter calls
/// 4. Global/file-level calls
fn extract_calls_from_source_ts(
&self,
tree: &Tree,
source: &[u8],
defined_funcs: &HashSet<String>,
) -> HashMap<String, Vec<CallSite>> {
let mut calls_by_func: HashMap<String, Vec<CallSite>> = HashMap::new();
// Walk the tree looking for class/extension and function declarations
self.walk_for_calls(
&tree.root_node(),
source,
defined_funcs,
&mut calls_by_func,
None, // no enclosing type at top level
);
// Remove empty entries
calls_by_func.retain(|_, v| !v.is_empty());
calls_by_func
}
/// Recursively walk AST nodes to find function declarations and extract calls.
fn walk_for_calls(
&self,
node: &Node,
source: &[u8],
defined_funcs: &HashSet<String>,
calls_by_func: &mut HashMap<String, Vec<CallSite>>,
current_type: Option<&str>,
) {
match node.kind() {
"class_declaration" => {
// Get the type name
let type_name = node.child_by_field_name("name").map(|n| {
let text = get_node_text(&n, source);
if n.kind() == "user_type" {
// For extensions, name is user_type > type_identifier
n.child(0)
.map(|c| get_node_text(&c, source))
.unwrap_or(text)
.to_string()
} else {
text.to_string()
}
});
// Walk the body with the type context
if let Some(body) = node.child_by_field_name("body") {
for i in 0..body.child_count() {
if let Some(child) = body.child(i) {
self.walk_for_calls(
&child,
source,
defined_funcs,
calls_by_func,
type_name.as_deref(),
);
}
}
}
}
"function_declaration" => {
if let Some(name_node) = node.child_by_field_name("name") {
let func_name = get_node_text(&name_node, source).to_string();
let qualified_name = if let Some(type_name) = current_type {
format!("{}.{}", type_name, func_name)
} else {
func_name.clone()
};
let mut all_calls = Vec::new();
// Extract calls from the function body
if let Some(body) = node.child_by_field_name("body") {
let body_calls = self.extract_calls_from_subtree(
&body,
source,
defined_funcs,
&qualified_name,
);
all_calls.extend(body_calls);
}
// Extract calls from default parameter values
let default_calls = self.extract_default_param_calls(
node,
source,
defined_funcs,
&qualified_name,
);
all_calls.extend(default_calls);
if !all_calls.is_empty() {
// Insert under qualified name
calls_by_func
.entry(qualified_name.clone())
.or_default()
.extend(all_calls.iter().cloned());
// Also insert under unqualified name if different
if qualified_name != func_name {
calls_by_func
.entry(func_name)
.or_default()
.extend(all_calls);
}
}
}
}
"init_declaration" => {
let func_name = "init".to_string();
let qualified_name = if let Some(type_name) = current_type {
format!("{}.{}", type_name, func_name)
} else {
func_name.clone()
};
if let Some(body) = node.child_by_field_name("body") {
let calls = self.extract_calls_from_subtree(
&body,
source,
defined_funcs,
&qualified_name,
);
if !calls.is_empty() {
calls_by_func
.entry(qualified_name.clone())
.or_default()
.extend(calls.iter().cloned());
if qualified_name != func_name {
calls_by_func.entry(func_name).or_default().extend(calls);
}
}
}
}
"property_declaration" => {
// Property initializer calls at class/struct scope
// e.g., `let x = Foo()` or `lazy var y = computeValue()`
// These are attributed to the enclosing type or <module>
if let Some(value_node) = node.child_by_field_name("value") {
if value_node.kind() == "call_expression" {
let caller_name = current_type
.map(|t| t.to_string())
.unwrap_or_else(|| "<module>".to_string());
if let Some((target, receiver, call_type)) =
self.resolve_call_target(&value_node, source, defined_funcs)
{
let line = value_node.start_position().row as u32 + 1;
let call = CallSite::new(
caller_name.clone(),
target,
call_type,
Some(line),
None,
receiver,
None,
);
calls_by_func.entry(caller_name).or_default().push(call);
}
}
}
}
_ => {
// For other nodes (e.g., source_file), recurse into children
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.walk_for_calls(
&child,
source,
defined_funcs,
calls_by_func,
current_type,
);
}
}
}
}
}
/// Extract calls from default parameter values in a function declaration.
fn extract_default_param_calls(
&self,
func_node: &Node,
source: &[u8],
defined_funcs: &HashSet<String>,
caller: &str,
) -> Vec<CallSite> {
let mut calls = Vec::new();
// Look for `default_value` field children which are call_expressions
for i in 0..func_node.child_count() {
if let Some(child) = func_node.child(i) {
if child.kind() == "parameter" {
// Parameters may have default values
// In the AST, defaults appear as siblings of the parameter
// Actually, from exploration: default_value is a field on function_declaration directly
// as `= call_expression` after the parameter
continue;
}
// default_value field: call_expression with field "default_value"
if let Some(field_name) = func_node.field_name_for_child(i as u32) {
if field_name == "default_value" && child.kind() == "call_expression" {
let line = child.start_position().row as u32 + 1;
if let Some((target, receiver, call_type)) =
self.resolve_call_target(&child, source, defined_funcs)
{
calls.push(CallSite::new(
caller.to_string(),
target,
call_type,
Some(line),
None,
receiver,
None,
));
}
}
}
}
}
calls
}
/// Backward-compatible method for tests that call extract_calls_from_source directly.
pub fn extract_calls_from_source(
&self,
source: &str,
_defined_funcs: &HashSet<String>,
) -> HashMap<String, Vec<CallSite>> {
let tree = match self.parse_source(source) {
Ok(t) => t,
Err(_) => return HashMap::new(),
};
let source_bytes = source.as_bytes();
let defined_funcs = self.collect_function_definitions_ts(&tree, source_bytes);
self.extract_calls_from_source_ts(&tree, source_bytes, &defined_funcs)
}
}
/// Check if a string is a Swift keyword (to avoid false call detections).
fn is_swift_keyword(s: &str) -> bool {
matches!(
s,
"if" | "else"
| "for"
| "while"
| "switch"
| "case"
| "default"
| "do"
| "try"
| "catch"
| "throw"
| "throws"
| "return"
| "break"
| "continue"
| "fallthrough"
| "in"
| "where"
| "guard"
| "defer"
| "self"
| "super"
| "nil"
| "true"
| "false"
| "let"
| "var"
| "func"
| "class"
| "struct"
| "enum"
| "protocol"
| "extension"
| "import"
| "typealias"
| "associatedtype"
| "static"
| "override"
| "final"
| "mutating"
| "nonmutating"
| "public"
| "private"
| "internal"
| "fileprivate"
| "open"
| "init"
| "deinit"
| "subscript"
| "convenience"
| "required"
| "get"
| "set"
| "willSet"
| "didSet"
| "is"
| "as"
| "Any"
| "Self"
| "Type"
| "async"
| "await"
| "actor"
)
}
impl CallGraphLanguageSupport for SwiftHandler {
fn name(&self) -> &str {
"swift"
}
fn extensions(&self) -> &[&str] {
&[".swift"]
}
fn parse_imports(&self, source: &str, _path: &Path) -> Result<Vec<ImportDef>, ParseError> {
Ok(self.parse_imports_regex(source))
}
fn extract_calls(
&self,
_path: &Path,
source: &str,
_tree: &Tree,
) -> Result<HashMap<String, Vec<CallSite>>, ParseError> {
// Parse with the real Swift grammar (ignore the passed tree which may be a dummy)
let tree = self.parse_source(source)?;
let source_bytes = source.as_bytes();
let defined_funcs = self.collect_function_definitions_ts(&tree, source_bytes);
Ok(self.extract_calls_from_source_ts(&tree, source_bytes, &defined_funcs))
}
fn extract_definitions(
&self,
source: &str,
_path: &Path,
_tree: &Tree,
) -> Result<(Vec<FuncDef>, Vec<ClassDef>), super::ParseError> {
// Parse with the real Swift grammar
let tree = self.parse_source(source)?;
let source_bytes = source.as_bytes();
let mut funcs = Vec::new();
let mut classes = Vec::new();
let mut class_lines: HashMap<String, (u32, u32)> = HashMap::new();
let mut class_bases: HashMap<String, Vec<String>> = HashMap::new();
let mut methods_by_type: HashMap<String, Vec<String>> = HashMap::new();
// Use tree-sitter for definitions with regex fallback for base parsing
let lines: Vec<&str> = source.lines().collect();
for node in walk_tree(tree.root_node()) {
match node.kind() {
"class_declaration" => {
let line_number = node.start_position().row as u32 + 1;
let end_line = node.end_position().row as u32 + 1;
// Determine if this is a class, struct, enum, or extension
let _decl_kind = node
.child_by_field_name("declaration_kind")
.map(|dk| get_node_text(&dk, source_bytes).to_string());
let type_name = node.child_by_field_name("name").map(|n| {
let text = get_node_text(&n, source_bytes);
if n.kind() == "user_type" {
n.child(0)
.map(|c| get_node_text(&c, source_bytes))
.unwrap_or(text)
.to_string()
} else {
text.to_string()
}
});
if let Some(type_name) = type_name {
class_lines
.entry(type_name.clone())
.and_modify(|(_, end)| *end = (*end).max(end_line))
.or_insert((line_number, end_line));
// Parse base types using regex on the declaration line
if let Some(decl_line) = lines.get(line_number as usize - 1) {
let bases = parse_swift_bases(decl_line);
if !bases.is_empty() {
let entry = class_bases.entry(type_name.clone()).or_default();
for base in bases {
if !entry.contains(&base) {
entry.push(base);
}
}
}
}
// Walk the class body for function declarations
if let Some(body) = node.child_by_field_name("body") {
for i in 0..body.child_count() {
if let Some(child) = body.child(i) {
match child.kind() {
"function_declaration" => {
if let Some(name_node) =
child.child_by_field_name("name")
{
let func_name =
get_node_text(&name_node, source_bytes)
.to_string();
let fn_line = child.start_position().row as u32 + 1;
let fn_end = child.end_position().row as u32 + 1;
funcs.push(FuncDef::method(
func_name.clone(),
type_name.clone(),
fn_line,
fn_end,
));
methods_by_type
.entry(type_name.clone())
.or_default()
.push(func_name);
}
}
"init_declaration" => {
let func_name = "init".to_string();
let fn_line = child.start_position().row as u32 + 1;
let fn_end = child.end_position().row as u32 + 1;
funcs.push(FuncDef::method(
func_name.clone(),
type_name.clone(),
fn_line,
fn_end,
));
methods_by_type
.entry(type_name.clone())
.or_default()
.push(func_name);
}
_ => {}
}
}
}
}
}
}
"protocol_declaration" => {
let line_number = node.start_position().row as u32 + 1;
let end_line = node.end_position().row as u32 + 1;
if let Some(name_node) = node.child_by_field_name("name") {
let type_name = get_node_text(&name_node, source_bytes).to_string();
class_lines
.entry(type_name.clone())
.and_modify(|(_, end)| *end = (*end).max(end_line))
.or_insert((line_number, end_line));
// Parse base protocols
if let Some(decl_line) = lines.get(line_number as usize - 1) {
let bases = parse_swift_bases(decl_line);
if !bases.is_empty() {
let entry = class_bases.entry(type_name.clone()).or_default();
for base in bases {
if !entry.contains(&base) {
entry.push(base);
}
}
}
}
// Walk for required function declarations
// Protocol body is "protocol_body", not "class_body"
if let Some(body) = node.child_by_field_name("body") {
for i in 0..body.child_count() {
if let Some(child) = body.child(i) {
// Protocol functions are protocol_function_declaration
if child.kind() == "function_declaration"
|| child.kind() == "protocol_function_declaration"
{
if let Some(fn_name_node) =
child.child_by_field_name("name")
{
let func_name =
get_node_text(&fn_name_node, source_bytes)
.to_string();
let fn_line = child.start_position().row as u32 + 1;
let fn_end = child.end_position().row as u32 + 1;
funcs.push(FuncDef::method(
func_name.clone(),
type_name.clone(),
fn_line,
fn_end,
));
methods_by_type
.entry(type_name.clone())
.or_default()
.push(func_name);
}
}
}
}
}
}
}
"function_declaration" => {
// Top-level functions (not inside a class/extension)
// Only process if parent is source_file
if let Some(parent) = node.parent() {
if parent.kind() == "source_file" {
if let Some(name_node) = node.child_by_field_name("name") {
let func_name = get_node_text(&name_node, source_bytes).to_string();
let fn_line = node.start_position().row as u32 + 1;
let fn_end = node.end_position().row as u32 + 1;
funcs.push(FuncDef::function(func_name, fn_line, fn_end));
}
}
}
}
"init_declaration" => {
// Top-level init (unusual but handle it)
if let Some(parent) = node.parent() {
if parent.kind() == "source_file" {
let func_name = "init".to_string();
let fn_line = node.start_position().row as u32 + 1;
let fn_end = node.end_position().row as u32 + 1;
funcs.push(FuncDef::function(func_name, fn_line, fn_end));
}
}
}
_ => {}
}
}
for (name, (line, end_line)) in class_lines {
let methods = methods_by_type.remove(&name).unwrap_or_default();
let bases = class_bases.remove(&name).unwrap_or_default();
classes.push(ClassDef::new(name, line, end_line, methods, bases));
}
Ok((funcs, classes))
}
}
// =============================================================================
// Tests
// =============================================================================
#[cfg(test)]
mod tests {
use super::*;
fn parse_imports(source: &str) -> Vec<ImportDef> {
let handler = SwiftHandler::new();
handler
.parse_imports(source, Path::new("test.swift"))
.unwrap()
}
fn extract_calls(source: &str) -> HashMap<String, Vec<CallSite>> {
let handler = SwiftHandler::new();
// Create a dummy tree (the handler parses with its own Swift parser)
let mut parser = tree_sitter::Parser::new();
parser
.set_language(&tree_sitter_python::LANGUAGE.into())
.unwrap();
let tree = parser.parse("", None).unwrap();
handler
.extract_calls(Path::new("test.swift"), source, &tree)
.unwrap()
}
// -------------------------------------------------------------------------
// Import Parsing Tests
// -------------------------------------------------------------------------
mod import_tests {
use super::*;
#[test]
fn test_parse_import_simple() {
let imports = parse_imports("import Foundation");
assert_eq!(imports.len(), 1);
assert_eq!(imports[0].module, "Foundation");
assert!(imports[0].names.is_empty());
}
#[test]
fn test_parse_import_uikit() {
let imports = parse_imports("import UIKit");
assert_eq!(imports.len(), 1);
assert_eq!(imports[0].module, "UIKit");
}
#[test]
fn test_parse_import_selective_struct() {
let imports = parse_imports("import struct Foundation.Date");
assert_eq!(imports.len(), 1);
assert_eq!(imports[0].module, "Foundation");
assert!(imports[0].names.contains(&"Date".to_string()));
}
#[test]
fn test_parse_import_selective_class() {
let imports = parse_imports("import class UIKit.UIView");
assert_eq!(imports.len(), 1);
assert_eq!(imports[0].module, "UIKit");
assert!(imports[0].names.contains(&"UIView".to_string()));
}
#[test]
fn test_parse_import_selective_func() {
let imports = parse_imports("import func Foundation.strcmp");
assert_eq!(imports.len(), 1);
assert_eq!(imports[0].module, "Foundation");
assert!(imports[0].names.contains(&"strcmp".to_string()));
}
#[test]
fn test_parse_import_testable() {
let imports = parse_imports("@testable import MyApp");
assert_eq!(imports.len(), 1);
assert_eq!(imports[0].module, "MyApp");
assert_eq!(imports[0].resolved_module, Some("@testable".to_string()));
}
#[test]
fn test_parse_import_testable_with_whitespace() {
let imports = parse_imports("@testable import MyModule");
assert_eq!(imports.len(), 1);
assert_eq!(imports[0].module, "MyModule");
assert_eq!(imports[0].resolved_module, Some("@testable".to_string()));
}
#[test]
fn test_parse_import_nested_module() {
let imports = parse_imports("import struct Foundation.URLSession.DataTask");
assert_eq!(imports.len(), 1);
// Foundation.URLSession is the module, DataTask is the imported name
assert_eq!(imports[0].module, "Foundation.URLSession");
assert!(imports[0].names.contains(&"DataTask".to_string()));
}
#[test]
fn test_parse_multiple_imports() {
let source = r#"
import Foundation
import UIKit
@testable import MyApp
import struct Foundation.Date
"#;
let imports = parse_imports(source);
assert_eq!(imports.len(), 4);
assert_eq!(imports[0].module, "Foundation");
assert_eq!(imports[1].module, "UIKit");
assert_eq!(imports[2].module, "MyApp");
assert_eq!(imports[3].module, "Foundation");
}
}
// -------------------------------------------------------------------------
// Function Definition Tests
// -------------------------------------------------------------------------
mod definition_tests {
use super::*;
#[test]
fn test_collect_function_definitions() {
let source = r#"
func helper() {
print("hello")
}
public func publicHelper() -> Int {
return 42
}
private func privateHelper() {
}
"#;
let handler = SwiftHandler::new();
let funcs = handler.collect_function_definitions(source);
assert!(funcs.contains("helper"));
assert!(funcs.contains("publicHelper"));
assert!(funcs.contains("privateHelper"));
}
#[test]
fn test_collect_type_definitions() {
let source = r#"
class MyClass {
func method() {}
}
struct MyStruct {
func method() {}
}
enum MyEnum {
case one
}
protocol MyProtocol {
func required()
}
"#;
let handler = SwiftHandler::new();
let funcs = handler.collect_function_definitions(source);
// Types are callable (initializers)
assert!(funcs.contains("MyClass"));
assert!(funcs.contains("MyStruct"));
assert!(funcs.contains("MyEnum"));
assert!(funcs.contains("MyProtocol"));
// Methods
assert!(funcs.contains("method"));
assert!(funcs.contains("required"));
}
#[test]
fn test_collect_static_functions() {
let source = r#"
class MyClass {
static func staticMethod() {}
class func classMethod() {}
}
"#;
let handler = SwiftHandler::new();
let funcs = handler.collect_function_definitions(source);
assert!(funcs.contains("staticMethod"));
assert!(funcs.contains("classMethod"));
}
}
// -------------------------------------------------------------------------
// Call Extraction Tests
// -------------------------------------------------------------------------
mod call_tests {
use super::*;
#[test]
fn test_extract_calls_direct() {
let source = r#"
func main() {
print("hello")
someFunction()
}
"#;
let calls = extract_calls(source);
let main_calls = calls.get("main").unwrap();
assert!(main_calls.iter().any(|c| c.target == "print"));
assert!(main_calls.iter().any(|c| c.target == "someFunction"));
}
#[test]
fn test_extract_calls_intra_file() {
let source = r#"
func helper() -> Int {
return 42
}
func main() {
let x = helper()
}
"#;
let calls = extract_calls(source);
let main_calls = calls.get("main").unwrap();
let helper_call = main_calls.iter().find(|c| c.target == "helper").unwrap();
assert_eq!(helper_call.call_type, CallType::Intra);
}
#[test]
fn test_extract_calls_method() {
let source = r#"
func main() {
let arr = [1, 2, 3]
arr.append(4)
arr.map({ $0 * 2 })
}
"#;
let calls = extract_calls(source);
let main_calls = calls.get("main").unwrap();
let append_call = main_calls
.iter()
.find(|c| c.target == "arr.append")
.unwrap();
assert_eq!(append_call.call_type, CallType::Attr);
assert_eq!(append_call.receiver, Some("arr".to_string()));
let map_call = main_calls.iter().find(|c| c.target == "arr.map").unwrap();
assert_eq!(map_call.call_type, CallType::Attr);
}
#[test]
fn test_trailing_closure_not_detected() {
// With tree-sitter, trailing closure syntax `arr.map { }` IS detected
// as a call_expression (unlike the old regex parser). Update test
// to verify this improvement.
let source = r#"
func main() {
let arr = [1, 2, 3]
arr.map { $0 * 2 }
}
"#;
let calls = extract_calls(source);
let main_calls = calls.get("main");
// With tree-sitter, trailing closures ARE detected as calls
// This is an improvement over the regex parser
if let Some(mc) = main_calls {
// Tree-sitter correctly identifies arr.map { } as a call
assert!(mc.iter().any(|c| c.target == "arr.map"));
}
}
#[test]
fn test_extract_calls_static() {
let source = r#"
func main() {
let date = Date()
let url = URL(string: "https://example.com")
let result = MyClass.staticMethod()
}
"#;
let calls = extract_calls(source);
let main_calls = calls.get("main").unwrap();
// Date() is a direct call (initializer)
assert!(main_calls.iter().any(|c| c.target == "Date"));
// URL(string:) is a direct call (initializer)
assert!(main_calls.iter().any(|c| c.target == "URL"));
// MyClass.staticMethod() is an attr call
let static_call = main_calls
.iter()
.find(|c| c.target == "MyClass.staticMethod")
.unwrap();
assert_eq!(static_call.call_type, CallType::Attr);
assert_eq!(static_call.receiver, Some("MyClass".to_string()));
}
#[test]
fn test_extract_calls_in_class_method() {
let source = r#"
class MyService {
func process() {
helper()
self.validate()
OtherService.fetch()
}
func helper() {}
func validate() {}
}
"#;
let calls = extract_calls(source);
// Should have calls from MyService.process (qualified name for class method)
let process_calls = calls.get("MyService.process");
assert!(
process_calls.is_some(),
"Expected calls from MyService.process, got keys: {:?}",
calls.keys().collect::<Vec<_>>()
);
let process_calls = process_calls.unwrap();
assert!(process_calls.iter().any(|c| c.target == "helper"));
assert!(process_calls.iter().any(|c| c.target == "self.validate"));
assert!(process_calls
.iter()
.any(|c| c.target == "OtherService.fetch"));
}
#[test]
fn test_extract_calls_method_to_toplevel() {
let source = r#"
class Service {
func process() {
helper()
}
}
func helper() {}
"#;
let calls = extract_calls(source);
// Should have calls from Service.process (qualified name)
let process_calls = calls.get("Service.process");
assert!(
process_calls.is_some(),
"Expected calls from Service.process, got keys: {:?}",
calls.keys().collect::<Vec<_>>()
);
let process_calls = process_calls.unwrap();
// Should call helper() as intra-file call
let helper_call = process_calls.iter().find(|c| c.target == "helper");
assert!(
helper_call.is_some(),
"Expected call to helper from Service.process, got: {:?}",
process_calls.iter().map(|c| &c.target).collect::<Vec<_>>()
);
assert_eq!(helper_call.unwrap().call_type, CallType::Intra);
assert_eq!(helper_call.unwrap().caller, "Service.process");
}
#[test]
fn test_extract_calls_in_extension() {
let source = r#"
extension String {
func customMethod() {
let len = self.count
helper()
}
}
func helper() {}
"#;
let calls = extract_calls(source);
// Should have calls from String.customMethod
let method_calls = calls.get("String.customMethod").unwrap();
// self.count is a property access, not detected as call
// helper() should be detected
assert!(method_calls.iter().any(|c| c.target == "helper"));
}
#[test]
fn test_extract_calls_chained() {
let source = r#"
func main() {
let result = data.filter { $0 > 0 }.map { $0 * 2 }.reduce(0, +)
}
"#;
let calls = extract_calls(source);
let main_calls = calls.get("main").unwrap();
// Chained calls - tree-sitter handles these
assert!(!main_calls.is_empty());
}
#[test]
fn test_skip_keywords() {
let source = r#"
func main() {
if condition {
for item in items {
switch value {
case .one:
break
}
}
}
}
"#;
let calls = extract_calls(source);
let main_calls = calls.get("main");
// Keywords like if, for, switch should not be detected as calls
if let Some(mc) = main_calls {
assert!(!mc.iter().any(|c| c.target == "if"));
assert!(!mc.iter().any(|c| c.target == "for"));
assert!(!mc.iter().any(|c| c.target == "switch"));
}
}
#[test]
fn test_line_numbers() {
let source = r#"func main() {
helper()
other()
}"#;
let calls = extract_calls(source);
let main_calls = calls.get("main").unwrap();
// helper() is on line 2
let helper_call = main_calls.iter().find(|c| c.target == "helper").unwrap();
assert_eq!(helper_call.line, Some(2));
// other() is on line 3
let other_call = main_calls.iter().find(|c| c.target == "other").unwrap();
assert_eq!(other_call.line, Some(3));
}
}
// -------------------------------------------------------------------------
// Handler Trait Tests
// -------------------------------------------------------------------------
mod trait_tests {
use super::*;
#[test]
fn test_handler_name() {
let handler = SwiftHandler::new();
assert_eq!(handler.name(), "swift");
}
#[test]
fn test_handler_extensions() {
let handler = SwiftHandler::new();
assert_eq!(handler.extensions(), &[".swift"]);
}
#[test]
fn test_handler_supports() {
let handler = SwiftHandler::new();
assert!(handler.supports("swift"));
assert!(handler.supports("Swift"));
assert!(handler.supports("SWIFT"));
assert!(!handler.supports("rust"));
}
#[test]
fn test_handler_supports_extension() {
let handler = SwiftHandler::new();
assert!(handler.supports_extension(".swift"));
assert!(handler.supports_extension(".SWIFT"));
assert!(!handler.supports_extension(".rs"));
}
}
// -------------------------------------------------------------------------
// Integration Tests
// -------------------------------------------------------------------------
mod integration_tests {
use super::*;
#[test]
fn test_complete_swift_file() {
let source = r#"
import Foundation
import UIKit
@testable import MyApp
class ViewController: UIViewController {
private let service = MyService()
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
service.fetchData()
}
private func setupUI() {
let label = UILabel()
label.text = "Hello"
view.addSubview(label)
}
}
extension ViewController {
func handleAction() {
print("Action handled")
dismiss(animated: true)
}
}
"#;
let handler = SwiftHandler::new();
// Test imports
let imports = handler
.parse_imports(source, Path::new("ViewController.swift"))
.unwrap();
assert_eq!(imports.len(), 3);
// Test calls
let defined_funcs = handler.collect_function_definitions(source);
assert!(defined_funcs.contains("viewDidLoad"));
assert!(defined_funcs.contains("setupUI"));
assert!(defined_funcs.contains("handleAction"));
assert!(defined_funcs.contains("ViewController"));
let calls = handler.extract_calls_from_source(source, &defined_funcs);
// viewDidLoad should call setupUI
let view_did_load_calls = calls.get("ViewController.viewDidLoad").unwrap();
assert!(view_did_load_calls.iter().any(|c| c.target == "setupUI"));
assert!(view_did_load_calls
.iter()
.any(|c| c.target == "service.fetchData"));
// handleAction should call print and dismiss
let handle_action_calls = calls.get("ViewController.handleAction").unwrap();
assert!(handle_action_calls.iter().any(|c| c.target == "print"));
assert!(handle_action_calls.iter().any(|c| c.target == "dismiss"));
}
}
// -------------------------------------------------------------------------
// Public API Tests
// -------------------------------------------------------------------------
mod public_api_tests {
use super::*;
use crate::callgraph::languages::{extract_calls_for_language, parse_imports_for_language};
#[test]
fn test_extract_calls_via_public_api() {
let source = r#"
func main() {
print("hello")
helper()
}
func helper() {}
"#;
let calls =
extract_calls_for_language("swift", Path::new("test.swift"), source).unwrap();
let main_calls = calls.get("main").unwrap();
assert!(main_calls.iter().any(|c| c.target == "print"));
assert!(main_calls.iter().any(|c| c.target == "helper"));
}
#[test]
fn test_parse_imports_via_public_api() {
let source = r#"
import Foundation
import UIKit
@testable import MyApp
"#;
let imports =
parse_imports_for_language("swift", source, Path::new("test.swift")).unwrap();
assert_eq!(imports.len(), 3);
assert!(imports.iter().any(|i| i.module == "Foundation"));
assert!(imports.iter().any(|i| i.module == "UIKit"));
assert!(imports.iter().any(|i| i.module == "MyApp"));
}
}
// -------------------------------------------------------------------------
// New Tree-Sitter Feature Tests
// -------------------------------------------------------------------------
mod treesitter_tests {
use super::*;
#[test]
fn test_property_initializer_calls() {
let source = r#"
class MyClass {
let x = Foo()
var y = Bar.create()
}
"#;
let calls = extract_calls(source);
// Property initializer calls should be attributed to the class
let class_calls = calls.get("MyClass").unwrap();
assert!(class_calls.iter().any(|c| c.target == "Foo"));
assert!(class_calls.iter().any(|c| c.target == "Bar.create"));
}
#[test]
fn test_default_parameter_calls() {
let source = r#"
func doStuff(x: Int = defaultVal()) {
print("hello")
}
"#;
let calls = extract_calls(source);
let do_stuff_calls = calls.get("doStuff").unwrap();
assert!(do_stuff_calls.iter().any(|c| c.target == "defaultVal"));
assert!(do_stuff_calls.iter().any(|c| c.target == "print"));
}
#[test]
fn test_global_file_level_calls() {
let source = r#"
let globalInit = makeGlobal()
"#;
let calls = extract_calls(source);
let module_calls = calls.get("<module>").unwrap();
assert!(module_calls.iter().any(|c| c.target == "makeGlobal"));
}
#[test]
fn test_lazy_property_calls() {
let source = r#"
class MyClass {
lazy var computed = computeValue()
}
"#;
let calls = extract_calls(source);
let class_calls = calls.get("MyClass").unwrap();
assert!(class_calls.iter().any(|c| c.target == "computeValue"));
}
#[test]
fn test_closure_calls() {
let source = r#"
func test() {
let closure = { compute() }
}
"#;
let calls = extract_calls(source);
let test_calls = calls.get("test").unwrap();
assert!(test_calls.iter().any(|c| c.target == "compute"));
}
#[test]
fn test_guard_let_calls() {
let source = r#"
func test() {
guard let val = tryFoo() else { return }
process(val)
}
"#;
let calls = extract_calls(source);
let test_calls = calls.get("test").unwrap();
assert!(test_calls.iter().any(|c| c.target == "tryFoo"));
assert!(test_calls.iter().any(|c| c.target == "process"));
}
#[test]
fn test_if_let_calls() {
let source = r#"
func test() {
if let opt = tryBar() {
handle()
}
}
"#;
let calls = extract_calls(source);
let test_calls = calls.get("test").unwrap();
assert!(test_calls.iter().any(|c| c.target == "tryBar"));
assert!(test_calls.iter().any(|c| c.target == "handle"));
}
#[test]
fn test_switch_case_calls() {
let source = r#"
func test() {
switch value {
case .one:
caseCall()
default:
defaultCall()
}
}
"#;
let calls = extract_calls(source);
let test_calls = calls.get("test").unwrap();
assert!(test_calls.iter().any(|c| c.target == "caseCall"));
assert!(test_calls.iter().any(|c| c.target == "defaultCall"));
}
#[test]
fn test_protocol_extension_calls() {
let source = r#"
extension Array {
func customFilter() {
extCall()
self.forEach { _ in }
}
}
"#;
let calls = extract_calls(source);
let ext_calls = calls.get("Array.customFilter").unwrap();
assert!(ext_calls.iter().any(|c| c.target == "extCall"));
}
#[test]
fn test_trailing_closure_detected() {
// Tree-sitter correctly detects trailing closure calls
let source = r#"
func test() {
arr.map { $0 * 2 }
arr.filter { $0 > 0 }
}
"#;
let calls = extract_calls(source);
let test_calls = calls.get("test").unwrap();
assert!(test_calls.iter().any(|c| c.target == "arr.map"));
assert!(test_calls.iter().any(|c| c.target == "arr.filter"));
}
#[test]
fn test_super_calls() {
let source = r#"
class Child: Parent {
override func viewDidLoad() {
super.viewDidLoad()
setup()
}
}
"#;
let calls = extract_calls(source);
let vdl_calls = calls.get("Child.viewDidLoad").unwrap();
assert!(vdl_calls.iter().any(|c| c.target == "super.viewDidLoad"));
assert!(vdl_calls.iter().any(|c| c.target == "setup"));
}
#[test]
fn test_self_method_calls() {
let source = r#"
class Foo {
func bar() {
self.baz()
}
func baz() {}
}
"#;
let calls = extract_calls(source);
let bar_calls = calls.get("Foo.bar").unwrap();
assert!(bar_calls.iter().any(|c| c.target == "self.baz"));
assert_eq!(
bar_calls
.iter()
.find(|c| c.target == "self.baz")
.unwrap()
.receiver,
Some("self".to_string())
);
}
#[test]
fn test_init_declaration_calls() {
let source = r#"
class Foo {
convenience init(x: Int) {
self.init()
helper()
}
func helper() {}
}
"#;
let calls = extract_calls(source);
let init_calls = calls.get("Foo.init").unwrap();
assert!(init_calls.iter().any(|c| c.target == "self.init"));
assert!(init_calls.iter().any(|c| c.target == "helper"));
}
}
}