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
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
//! JavaScript parser implementation
//!
//! **Tree-sitter ABI Version**: ABI-14 (tree-sitter-javascript 0.23.0)
//!
//! Note: This parser uses ABI-14 with JSX support via the tree-sitter-javascript grammar.
//! JavaScript and TypeScript share very similar syntax, but JavaScript doesn't have
//! TypeScript-specific features like interfaces, type aliases, type annotations, abstract classes, etc.
use crate::parsing::Import;
use crate::parsing::parser::check_recursion_depth;
use crate::parsing::{
LanguageParser, MethodCall, NodeTracker, NodeTrackingState, ParserContext, ScopeType,
};
use crate::types::SymbolCounter;
use crate::{FileId, Range, Symbol, SymbolKind, Visibility};
use std::any::Any;
use tree_sitter::{Language, Node, Parser};
/// JavaScript language parser
pub struct JavaScriptParser {
parser: Parser,
context: ParserContext,
node_tracker: NodeTrackingState,
/// Track symbols that are default exported (e.g., export default Container)
default_exported_symbols: std::collections::HashSet<String>,
/// Track symbols that are named exported (e.g., export { Card, CardHeader })
named_exported_symbols: std::collections::HashSet<String>,
/// Track JSX component usages (caller -> component name)
component_usages: Vec<(String, String)>,
}
impl JavaScriptParser {
/// Helper to create a symbol with all optional fields
fn create_symbol(
&self,
id: crate::types::SymbolId,
name: String,
kind: SymbolKind,
file_id: FileId,
range: Range,
signature: Option<String>,
doc_comment: Option<String>,
module_path: &str,
visibility: Visibility,
) -> Symbol {
let mut symbol = Symbol::new(id, name, kind, file_id, range);
if let Some(sig) = signature {
symbol = symbol.with_signature(sig);
}
if let Some(doc) = doc_comment {
symbol = symbol.with_doc(doc);
}
if !module_path.is_empty() {
symbol = symbol.with_module_path(module_path);
}
symbol = symbol.with_visibility(visibility);
// Set scope context based on parser's current scope
symbol.scope_context = Some(self.context.current_scope_context());
symbol
}
/// Parse JavaScript source code and extract all symbols
pub fn parse(
&mut self,
code: &str,
file_id: FileId,
symbol_counter: &mut SymbolCounter,
) -> Vec<Symbol> {
// Reset context and exports for each file
self.context = ParserContext::new();
self.default_exported_symbols.clear();
self.named_exported_symbols.clear();
self.component_usages.clear();
let mut symbols = Vec::new();
match self.parser.parse(code, None) {
Some(tree) => {
let root_node = tree.root_node();
self.extract_symbols_from_node(
root_node,
code,
file_id,
symbol_counter,
&mut symbols,
"", // Module path will be determined by behavior
0,
);
}
None => {
eprintln!("Failed to parse JavaScript file");
}
}
// Update visibility for default exported symbols
for symbol in &mut symbols {
if self.default_exported_symbols.contains(symbol.name.as_ref()) {
tracing::debug!(
"[javascript] marking '{}' as Public (default export)",
symbol.name
);
symbol.visibility = Visibility::Public;
}
}
// Update visibility for named exported symbols
for symbol in &mut symbols {
if self.named_exported_symbols.contains(symbol.name.as_ref()) {
symbol.visibility = Visibility::Public;
}
}
symbols
}
/// Create a new JavaScript parser
pub fn new() -> Result<Self, String> {
let mut parser = Parser::new();
// Use the JavaScript grammar which includes JSX support
let language: Language = tree_sitter_javascript::LANGUAGE.into();
parser
.set_language(&language)
.map_err(|e| format!("Failed to set JavaScript language: {e}"))?;
Ok(Self {
parser,
context: ParserContext::new(),
node_tracker: NodeTrackingState::new(),
default_exported_symbols: std::collections::HashSet::new(),
named_exported_symbols: std::collections::HashSet::new(),
component_usages: Vec::new(),
})
}
/// Extract symbols from a JavaScript node
fn extract_symbols_from_node(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
symbols: &mut Vec<Symbol>,
module_path: &str,
depth: usize,
) {
// Guard against stack overflow
if !check_recursion_depth(depth, node) {
return;
}
match node.kind() {
"function_declaration" | "generator_function_declaration" => {
// Register ALL child nodes for audit (including parameters, etc.)
self.register_node_recursively(node);
// Extract function name for parent tracking
let func_name = node
.child_by_field_name("name")
.map(|n| code[n.byte_range()].to_string());
if let Some(symbol) =
self.process_function(node, code, file_id, counter, module_path)
{
symbols.push(symbol);
}
// Note: In JavaScript, function declarations are hoisted
// But we process nested symbols in the function's scope
self.context.enter_scope(ScopeType::hoisting_function());
// Save the current parent context before setting new one
let saved_function = self.context.current_function().map(|s| s.to_string());
let saved_class = self.context.current_class().map(|s| s.to_string());
// Set current function for parent tracking BEFORE processing children
self.context.set_current_function(func_name.clone());
// Process function body for nested symbols
if let Some(body) = node.child_by_field_name("body") {
// Register the body node for audit tracking
self.register_handled_node(body.kind(), body.kind_id());
// Process the body using the standard extraction
// This ensures all nodes are properly registered
self.extract_symbols_from_node(
body,
code,
file_id,
counter,
symbols,
module_path,
depth + 1,
);
}
// Exit scope first (this clears the current context)
self.context.exit_scope();
// Then restore the previous parent context
self.context.set_current_function(saved_function);
self.context.set_current_class(saved_class);
}
"class_declaration" => {
// Register ALL child nodes for audit
self.register_node_recursively(node);
// Extract class name for parent tracking
let class_name = node
.child_by_field_name("name")
.map(|n| code[n.byte_range()].to_string());
if let Some(symbol) = self.process_class(node, code, file_id, counter, module_path)
{
symbols.push(symbol);
// Enter class scope for processing members
self.context.enter_scope(ScopeType::Class);
// Save the current parent context before setting new one
let saved_function = self.context.current_function().map(|s| s.to_string());
let saved_class = self.context.current_class().map(|s| s.to_string());
// Set current class for parent tracking
self.context.set_current_class(class_name.clone());
// Extract class members
self.extract_class_members(
node,
code,
file_id,
counter,
symbols,
module_path,
depth + 1,
);
// Exit scope first (this clears the current context)
self.context.exit_scope();
// Then restore the previous parent context
self.context.set_current_function(saved_function);
self.context.set_current_class(saved_class);
}
}
"lexical_declaration" | "variable_declaration" => {
self.register_handled_node(node.kind(), node.kind_id());
self.process_variable_declaration(
node,
code,
file_id,
counter,
symbols,
module_path,
depth + 1,
);
}
"arrow_function" => {
self.register_handled_node(node.kind(), node.kind_id());
// Handle arrow functions assigned to variables
if let Some(symbol) =
self.process_arrow_function(node, code, file_id, counter, module_path)
{
symbols.push(symbol);
}
}
"ERROR" => {
// ERROR nodes occur when tree-sitter can't parse something
// (e.g., "use client" directive in React Server Components)
// We still want to extract symbols from the children
self.register_handled_node(node.kind(), node.kind_id());
// Check if this looks like a fragmented function declaration
// Pattern: identifier followed by formal_parameters
let mut cursor = node.walk();
let children: Vec<Node> = node.children(&mut cursor).collect();
let mut i = 0;
while i < children.len() {
let child = children[i];
// Check if this is an identifier followed by formal_parameters
if child.kind() == "identifier" && i + 1 < children.len() {
let next = children[i + 1];
if next.kind() == "formal_parameters" {
// This looks like a function declaration that got fragmented
// Extract it as a function
let func_name = &code[child.byte_range()];
// Create a synthetic function symbol
let symbol_id = counter.next_id();
let range = Range::new(
child.start_position().row as u32,
child.start_position().column as u16,
next.end_position().row as u32,
next.end_position().column as u16,
);
let mut symbol = Symbol::new(
symbol_id,
func_name.to_string(),
SymbolKind::Function,
file_id,
range,
);
symbol = symbol
.with_visibility(Visibility::Public)
.with_signature(format!("function {func_name}()"));
if !module_path.is_empty() {
symbol = symbol.with_module_path(module_path.to_string());
}
// Set scope context
symbol.scope_context = Some(self.context.current_scope_context());
symbols.push(symbol);
// Skip the formal_parameters node since we processed it
i += 2;
continue;
}
}
// Process child normally
self.extract_symbols_from_node(
child,
code,
file_id,
counter,
symbols,
module_path,
depth + 1,
);
i += 1;
}
}
"export_statement" => {
// Check if this is a default export (export default SomeName)
self.register_handled_node(node.kind(), node.kind_id());
// Look for 'default' keyword followed by an identifier
let mut cursor = node.walk();
let children: Vec<Node> = node.children(&mut cursor).collect();
// Check if this is "export default <identifier>"
let mut found_default = false;
for (i, child) in children.iter().enumerate() {
if child.kind() == "default" {
found_default = true;
// The next node should be the identifier being exported
if i + 1 < children.len() {
let next = &children[i + 1];
if next.kind() == "identifier" {
let symbol_name = &code[next.byte_range()];
self.default_exported_symbols
.insert(symbol_name.to_string());
tracing::debug!(
"[javascript] found default export of '{symbol_name}'"
);
}
}
}
}
// Check for named export lists (export { Card, CardHeader })
for child in &children {
if child.kind() == "export_clause" {
// Process export specifiers within the export clause
let mut export_cursor = child.walk();
for export_child in child.children(&mut export_cursor) {
if export_child.kind() == "export_specifier" {
// Get the name being exported
if let Some(name_node) = export_child.child_by_field_name("name") {
let symbol_name = &code[name_node.byte_range()];
self.named_exported_symbols.insert(symbol_name.to_string());
}
}
}
}
}
// Still process children for nested declarations (e.g., export function foo())
if !found_default {
for child in children {
self.extract_symbols_from_node(
child,
code,
file_id,
counter,
symbols,
module_path,
depth + 1,
);
}
}
}
"jsx_element" | "jsx_self_closing_element" => {
// Track JSX component usage as Uses relationship
self.register_handled_node(node.kind(), node.kind_id());
tracing::debug!(
"[javascript] found JSX node: {} at {}:{}, current function: {:?}",
node.kind(),
node.start_position().row,
node.start_position().column,
self.context.current_function()
);
self.track_jsx_component_usage(node, code);
// Process children to find nested JSX elements
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
self.extract_symbols_from_node(
child,
code,
file_id,
counter,
symbols,
module_path,
depth + 1,
);
}
}
_ => {
// Track all nodes we encounter, even if not extracting symbols
self.register_handled_node(node.kind(), node.kind_id());
// For unhandled node types, recursively process children
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
self.extract_symbols_from_node(
child,
code,
file_id,
counter,
symbols,
module_path,
depth + 1,
);
}
}
}
}
/// Process a function declaration
fn process_function(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
module_path: &str,
) -> Option<Symbol> {
let name_node = node.child_by_field_name("name")?;
let name = &code[name_node.byte_range()];
let signature = self.extract_signature(node, code);
let doc_comment = self.extract_doc_comment(&node, code);
let visibility = self.determine_visibility(node, code);
Some(self.create_symbol(
counter.next_id(),
name.to_string(),
SymbolKind::Function,
file_id,
Range::new(
node.start_position().row as u32,
node.start_position().column as u16,
node.end_position().row as u32,
node.end_position().column as u16,
),
Some(signature),
doc_comment,
module_path,
visibility,
))
}
/// Process a class declaration
fn process_class(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
module_path: &str,
) -> Option<Symbol> {
// Get the class name from the name field
let name_node = node.child_by_field_name("name")?;
let name = &code[name_node.byte_range()];
let signature = self.extract_class_signature(node, code);
let doc_comment = self.extract_doc_comment(&node, code);
let visibility = self.determine_visibility(node, code);
Some(self.create_symbol(
counter.next_id(),
name.to_string(),
SymbolKind::Class,
file_id,
Range::new(
node.start_position().row as u32,
node.start_position().column as u16,
node.end_position().row as u32,
node.end_position().column as u16,
),
Some(signature),
doc_comment,
module_path,
visibility,
))
}
/// Extract class members (methods, properties)
fn extract_class_members(
&mut self,
class_node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
symbols: &mut Vec<Symbol>,
module_path: &str,
depth: usize,
) {
if let Some(body) = class_node.child_by_field_name("body") {
let mut cursor = body.walk();
for child in body.children(&mut cursor) {
match child.kind() {
"method_definition" => {
self.register_handled_node(child.kind(), child.kind_id());
// Extract method name for parent tracking
let method_name = child
.child_by_field_name("name")
.map(|n| code[n.byte_range()].to_string());
if let Some(symbol) =
self.process_method(child, code, file_id, counter, module_path)
{
symbols.push(symbol);
}
// Also process the method body for nested classes/functions
if let Some(body) = child.child_by_field_name("body") {
// Enter function scope for method body
self.context
.enter_scope(ScopeType::Function { hoisting: false });
// Save the current parent context before setting new one
let saved_function =
self.context.current_function().map(|s| s.to_string());
// Set current function to the method name
self.context.set_current_function(method_name.clone());
// Register the body node for audit tracking
self.register_handled_node(body.kind(), body.kind_id());
// Process the body using standard extraction
self.extract_symbols_from_node(
body,
code,
file_id,
counter,
symbols,
module_path,
depth + 1,
);
// Exit scope first (this clears the current context)
self.context.exit_scope();
// Then restore the previous parent context when exiting method
self.context.set_current_function(saved_function);
}
}
"field_definition" | "public_field_definition" => {
self.register_handled_node(child.kind(), child.kind_id());
if let Some(symbol) =
self.process_property(child, code, file_id, counter, module_path)
{
symbols.push(symbol);
}
}
_ => {
self.register_handled_node(child.kind(), child.kind_id());
}
}
}
}
}
/// Process variable declarations
fn process_variable_declaration(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
symbols: &mut Vec<Symbol>,
module_path: &str,
depth: usize,
) {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "variable_declarator" {
if let Some(name_node) = child.child_by_field_name("name") {
if name_node.kind() == "identifier" {
let name = &code[name_node.byte_range()];
// Check if this is an arrow function assignment
let is_arrow_function =
if let Some(value_node) = child.child_by_field_name("value") {
value_node.kind() == "arrow_function"
} else {
false
};
// Determine the kind based on whether it's a function or regular variable
let kind = if is_arrow_function {
SymbolKind::Function
} else if code[node.byte_range()].starts_with("const") {
SymbolKind::Constant
} else {
SymbolKind::Variable
};
let visibility = self.determine_visibility(node, code);
// Extract JSDoc comment for const declarations
let doc_comment = self.extract_doc_comment(&node, code);
let mut symbol = self.create_symbol(
counter.next_id(),
name.to_string(),
kind,
file_id,
Range::new(
child.start_position().row as u32,
child.start_position().column as u16,
child.end_position().row as u32,
child.end_position().column as u16,
),
None,
doc_comment,
module_path,
visibility,
);
// Override scope context for arrow functions - they are never hoisted
if is_arrow_function {
// Arrow functions are not hoisted, but keep the parent context that was already set
match symbol.scope_context {
Some(crate::symbol::ScopeContext::Local {
parent_name,
parent_kind,
..
}) => {
symbol.scope_context =
Some(crate::symbol::ScopeContext::Local {
hoisted: false, // Arrow functions are never hoisted
parent_name, // Keep the parent context
parent_kind, // Keep the parent kind
});
}
_ => {
// If not already Local, make it Local with parent context
let (parent_name, parent_kind) = if let Some(func_name) =
self.context.current_function()
{
(Some(func_name.into()), Some(crate::SymbolKind::Function))
} else if let Some(class_name) = self.context.current_class() {
(Some(class_name.into()), Some(crate::SymbolKind::Class))
} else {
(None, None)
};
symbol.scope_context =
Some(crate::symbol::ScopeContext::Local {
hoisted: false,
parent_name,
parent_kind,
});
}
}
}
symbols.push(symbol);
// CRITICAL FIX: Process arrow function body for nested symbols
if is_arrow_function {
if let Some(value_node) = child.child_by_field_name("value") {
if value_node.kind() == "arrow_function" {
if let Some(body) = value_node.child_by_field_name("body") {
// Save current context
let saved_function =
self.context.current_function().map(|s| s.to_string());
let saved_class =
self.context.current_class().map(|s| s.to_string());
// Enter function scope for the arrow function
self.context.enter_scope(ScopeType::function());
self.context.set_current_function(Some(name.to_string()));
// Register the body node for audit tracking
self.register_handled_node(body.kind(), body.kind_id());
// Process the body using standard extraction
self.extract_symbols_from_node(
body,
code,
file_id,
counter,
symbols,
module_path,
depth + 1,
);
// Exit scope and restore context
self.context.exit_scope();
self.context.set_current_function(saved_function);
self.context.set_current_class(saved_class);
}
}
}
}
}
}
}
}
}
/// Process arrow functions
fn process_arrow_function(
&mut self,
_node: Node,
_code: &str,
_file_id: FileId,
_counter: &mut SymbolCounter,
_module_path: &str,
) -> Option<Symbol> {
// Arrow functions are typically anonymous
// We'll handle named arrow functions when assigned to variables
None
}
/// Process a method definition
fn process_method(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
module_path: &str,
) -> Option<Symbol> {
let name_node = node.child_by_field_name("name")?;
let name = &code[name_node.byte_range()];
let signature = self.extract_signature(node, code);
let doc_comment = self.extract_doc_comment(&node, code);
let visibility = self.determine_method_visibility(node, code);
Some(self.create_symbol(
counter.next_id(),
name.to_string(),
SymbolKind::Method,
file_id,
Range::new(
node.start_position().row as u32,
node.start_position().column as u16,
node.end_position().row as u32,
node.end_position().column as u16,
),
Some(signature),
doc_comment,
module_path,
visibility,
))
}
/// Process a property/field definition
fn process_property(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
module_path: &str,
) -> Option<Symbol> {
let name_node = node.child_by_field_name("property")?;
let name = &code[name_node.byte_range()];
let visibility = self.determine_method_visibility(node, code);
let doc_comment = self.extract_doc_comment(&node, code);
Some(self.create_symbol(
counter.next_id(),
name.to_string(),
SymbolKind::Field,
file_id,
Range::new(
node.start_position().row as u32,
node.start_position().column as u16,
node.end_position().row as u32,
node.end_position().column as u16,
),
None,
doc_comment,
module_path,
visibility,
))
}
/// Extract function/method signature
fn extract_signature(&self, node: Node, code: &str) -> String {
// Extract the signature without the body
let start = node.start_byte();
let mut end = node.end_byte();
// Try to find the body and exclude it
if let Some(body) = node.child_by_field_name("body") {
end = body.start_byte();
}
code[start..end].trim().to_string()
}
/// Extract class signature (with extends)
fn extract_class_signature(&self, node: Node, code: &str) -> String {
let start = node.start_byte();
let mut end = node.end_byte();
// Find the class body and exclude it
if let Some(body) = node.child_by_field_name("body") {
end = body.start_byte();
}
code[start..end].trim().to_string()
}
/// Determine visibility based on export keywords
fn determine_visibility(&self, node: Node, code: &str) -> Visibility {
// 1) Ancestor check: many JS grammars wrap declarations in export_statement
let mut anc = node.parent();
for _ in 0..3 {
// walk a few levels conservatively
if let Some(a) = anc {
if a.kind() == "export_statement" {
return Visibility::Public;
}
anc = a.parent();
} else {
break;
}
}
// 2) Sibling check (rare, but safe)
if let Some(prev) = node.prev_sibling() {
if prev.kind() == "export_statement" {
return Visibility::Public;
}
}
// 3) Token check: if the source preceding the node contains 'export '
// This catches inline modifiers when export is not represented as a wrapper.
let start = node.start_byte();
let prefix = crate::parsing::safe_substring_window(code, start, 10);
if prefix.contains("export ") || prefix.contains("export\n") {
return Visibility::Public;
}
// Default: not exported
Visibility::Private
}
/// Determine method/property visibility
fn determine_method_visibility(&self, node: Node, code: &str) -> Visibility {
let signature = &code[node.byte_range()];
// In JavaScript, private fields start with #
if signature.starts_with("#") || signature.contains(" #") {
Visibility::Private
} else {
Visibility::Public // Default for class members
}
}
/// Find class extends relationships in JavaScript
fn find_implementations_in_node<'a>(
node: Node,
code: &'a str,
implementations: &mut Vec<(&'a str, &'a str, Range)>,
) {
if node.kind() == "class_declaration" {
// Get class name first
let class_name = node
.child_by_field_name("name")
.map(|n| &code[n.byte_range()]);
if let Some(class_name) = class_name {
// Look for class_heritage child node (it's a child, not a field!)
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "class_heritage" {
// The heritage node directly contains the parent class identifier
// Structure: class_heritage -> extends keyword + identifier
let mut heritage_cursor = child.walk();
for heritage_child in child.children(&mut heritage_cursor) {
if heritage_child.kind() == "identifier"
|| heritage_child.kind() == "member_expression"
{
let base_name = &code[heritage_child.byte_range()];
let range = Range::new(
heritage_child.start_position().row as u32,
heritage_child.start_position().column as u16,
heritage_child.end_position().row as u32,
heritage_child.end_position().column as u16,
);
implementations.push((class_name, base_name, range));
}
}
}
}
}
}
// Recurse into children
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
Self::find_implementations_in_node(child, code, implementations);
}
}
/// Extract imports from AST node recursively
fn extract_imports_from_node(
&self,
node: Node,
code: &str,
file_id: FileId,
imports: &mut Vec<Import>,
) {
match node.kind() {
"import_statement" => {
self.process_import_statement(node, code, file_id, imports);
}
"export_statement" => {
// Check if it's a re-export (has source)
if node.child_by_field_name("source").is_some() {
self.process_export_statement(node, code, file_id, imports);
}
}
_ => {
// Recurse into children
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
self.extract_imports_from_node(child, code, file_id, imports);
}
}
}
}
/// Process an import statement node
fn process_import_statement(
&self,
node: Node,
code: &str,
file_id: FileId,
imports: &mut Vec<Import>,
) {
tracing::debug!(
"[javascript] process_import_statement, code: {}",
&code[node.byte_range()]
);
// Get the source (the module being imported from)
let source_node = match node.child_by_field_name("source") {
Some(n) => n,
None => return,
};
let source_path = &code[source_node.byte_range()];
let source_path = source_path.trim_matches(|c| c == '"' || c == '\'' || c == '`');
// Process import clause (what's being imported)
// Note: import_clause is not a named field, we need to find it by kind
let import_clause = {
let mut cursor = node.walk();
node.children(&mut cursor)
.find(|c| c.kind() == "import_clause")
};
if let Some(import_clause) = import_clause {
tracing::debug!(
"[javascript] found import_clause: {}",
&code[import_clause.byte_range()]
);
// Check for different import types
let mut has_default = false;
let mut has_named = false;
let mut has_namespace = false;
let mut default_name = None;
let mut namespace_name = None;
let mut cursor = import_clause.walk();
for child in import_clause.children(&mut cursor) {
tracing::debug!(
"[javascript] child kind: {}, text: {}",
child.kind(),
&code[child.byte_range()]
);
match child.kind() {
"identifier" => {
// Default import
has_default = true;
let name = code[child.byte_range()].to_string();
tracing::debug!("[javascript] setting default_name = {name}");
default_name = Some(name);
}
"named_imports" => {
// Named imports exist
has_named = true;
// Extract named import specifiers: { Foo as Bar, Baz }
let mut nc = child.walk();
for ni in child.children(&mut nc) {
if ni.kind() == "import_specifier" {
let mut sp = ni.walk();
let mut local: Option<String> = None;
// Prefer the aliased local name if present
for part in ni.children(&mut sp) {
if part.kind() == "identifier" {
local = Some(code[part.byte_range()].to_string());
}
}
imports.push(Import {
path: source_path.to_string(),
alias: local,
file_id,
is_glob: false,
is_type_only: false, // JavaScript doesn't have type-only imports
});
}
}
}
"namespace_import" => {
// * as name
has_namespace = true;
let mut ns_cursor = child.walk();
let children: Vec<_> = child.children(&mut ns_cursor).collect();
if let Some(identifier) =
children.iter().rev().find(|n| n.kind() == "identifier")
{
namespace_name = Some(code[identifier.byte_range()].to_string());
}
}
_ => {}
}
}
// Add imports based on what we found
// Following Rust pattern: one Import per module, with alias for default/namespace
tracing::debug!(
"[javascript] summary: has_default={has_default}, has_named={has_named}, has_namespace={has_namespace}, default_name={default_name:?}, namespace_name={namespace_name:?}"
);
if has_namespace {
// Namespace import: import * as utils from './utils'
imports.push(Import {
path: source_path.to_string(),
alias: namespace_name,
file_id,
is_glob: true,
is_type_only: false,
});
} else if has_default && has_named {
// Mixed import: import React, { Component } from 'react'
// We create one import with the default as alias
imports.push(Import {
path: source_path.to_string(),
alias: default_name,
file_id,
is_glob: false,
is_type_only: false,
});
} else if has_default {
// Default only: import React from 'react'
tracing::debug!(
"[javascript] adding default import: path='{source_path}', alias={default_name:?}"
);
imports.push(Import {
path: source_path.to_string(),
alias: default_name,
file_id,
is_glob: false,
is_type_only: false,
});
} else if has_named {
// Named-only already pushed per specifier above
}
} else {
// Side-effect import (no import clause)
imports.push(Import {
path: source_path.to_string(),
alias: None,
file_id,
is_glob: false,
is_type_only: false,
});
}
}
/// Process export statements (for re-exports)
fn process_export_statement(
&self,
node: Node,
code: &str,
file_id: FileId,
imports: &mut Vec<Import>,
) {
// Get the source module
let source_node = match node.child_by_field_name("source") {
Some(n) => n,
None => return,
};
let source_path = &code[source_node.byte_range()];
let source_path = source_path.trim_matches(|c| c == '"' || c == '\'' || c == '`');
// Check what's being exported
let node_text = &code[node.byte_range()];
if node_text.contains("* from") {
// export * from './module'
imports.push(Import {
path: source_path.to_string(),
alias: None,
file_id,
is_glob: true,
is_type_only: false,
});
} else {
// Named re-exports - just track the module being imported from
imports.push(Import {
path: source_path.to_string(),
alias: None,
file_id,
is_glob: false,
is_type_only: false,
});
}
}
// Helper methods for find_calls()
#[allow(clippy::only_used_in_recursion)]
fn extract_calls_recursive<'a>(
&self,
node: &tree_sitter::Node,
code: &'a str,
current_function: Option<&'a str>,
calls: &mut Vec<(&'a str, &'a str, Range)>,
) {
// Handle export wrappers that contain a function declaration
if node.kind() == "export_statement" {
let mut w = node.walk();
for child in node.children(&mut w) {
if child.kind() == "function_declaration"
|| child.kind() == "generator_function_declaration"
{
// Try to get function name
let func_name = child
.child_by_field_name("name")
.or_else(|| {
let mut cw = child.walk();
child.children(&mut cw).find(|n| n.kind() == "identifier")
})
.map(|n| &code[n.byte_range()]);
// Recurse into the function with proper context
self.extract_calls_recursive(&child, code, func_name, calls);
// Continue scanning other children as well
}
}
}
// Handle function context - track which function we're inside
// CRITICAL: Only set NEW context when entering a function, otherwise INHERIT current context
let function_context = if node.kind() == "function_declaration"
|| node.kind() == "generator_function_declaration"
|| node.kind() == "method_definition"
|| node.kind() == "arrow_function"
|| node.kind() == "function_expression"
{
// We're entering a NEW function scope - extract its name
if let Some(name_node) = node.child_by_field_name("name").or_else(|| {
// Fallback: some fragmented/ERROR-wrapped trees may not label fields
let mut w = node.walk();
node.children(&mut w).find(|n| n.kind() == "identifier")
}) {
let name = &code[name_node.byte_range()];
tracing::debug!(
"[javascript] entering {} '{}' at line {}",
node.kind(),
name,
node.start_position().row + 1
);
Some(name)
} else {
// Arrow functions might not have a name, check parent for variable declaration
// Handle case: const ComponentName = () => { ... }
if node.kind() == "arrow_function" {
if let Some(parent) = node.parent() {
if parent.kind() == "variable_declarator" {
// Get the name from the variable declarator
if let Some(name_node) = parent.child_by_field_name("name") {
Some(&code[name_node.byte_range()])
} else {
current_function
}
} else {
current_function
}
} else {
current_function
}
} else {
current_function
}
}
} else if node.kind() == "identifier" && current_function.is_none() {
// ONLY check for fragmented functions if we're NOT already in a function
// Fragmented function detection only at top level error/program contexts.
if let Some(parent) = node.parent() {
if parent.kind() == "ERROR" || parent.kind() == "program" {
if let Some(next_sibling) = node.next_sibling() {
if next_sibling.kind() == "formal_parameters" {
// This is a fragmented function (e.g., due to "use client" causing ERROR root)
Some(&code[node.byte_range()])
} else {
current_function
}
} else {
current_function
}
} else {
current_function
}
} else {
current_function
}
} else if node.kind() == "variable_declarator" && current_function.is_none() {
// ONLY check variable declarators at top level, not inside functions
// Check if this variable contains an arrow function or function expression
if let Some(init) = node.child_by_field_name("value") {
if init.kind() == "arrow_function" || init.kind() == "function_expression" {
// Get the variable name to use as function context
if let Some(name_node) = node.child_by_field_name("name") {
Some(&code[name_node.byte_range()])
} else {
current_function
}
} else {
current_function
}
} else {
current_function
}
} else {
// Not a function declaration - INHERIT the current context
current_function
};
// Check if this is a call expression
if node.kind() == "call_expression" {
// Try to obtain the callee node robustly: prefer 'function' field,
// but fall back to the first child if fields are missing under ERROR nodes.
let function_node = node.child_by_field_name("function").or_else(|| {
let mut w = node.walk();
node.children(&mut w).next()
});
if let Some(function_node) = function_node {
// Extract function name for all types of calls (including member expressions like console.log)
if let Some(fn_name) = Self::extract_function_name(&function_node, code) {
tracing::debug!(
"[javascript] found call to {} at line {}, context = {:?}",
fn_name,
node.start_position().row + 1,
function_context
);
// If we don't have a function context yet, try to infer it from ancestors
let inferred_context = if function_context.is_none() {
let mut anc = node.parent();
let mut ctx: Option<&'a str> = None;
while let Some(a) = anc {
match a.kind() {
"function_declaration" | "generator_function_declaration" => {
if let Some(name_node) =
a.child_by_field_name("name").or_else(|| {
let mut w = a.walk();
a.children(&mut w).find(|n| n.kind() == "identifier")
})
{
ctx = Some(&code[name_node.byte_range()]);
break;
}
}
"arrow_function" | "function_expression" => {
if let Some(p) = a.parent() {
if p.kind() == "variable_declarator" {
if let Some(name_node) = p.child_by_field_name("name") {
ctx = Some(&code[name_node.byte_range()]);
break;
}
} else if p.kind() == "pair" {
// Handle object property: { propertyName: () => { ... } }
// Look for the parent object's variable name
let mut obj_anc = p.parent();
while let Some(oa) = obj_anc {
if oa.kind() == "object" {
// Found the object, now find its variable declarator
if let Some(obj_parent) = oa.parent() {
if obj_parent.kind()
== "variable_declarator"
{
if let Some(name_node) = obj_parent
.child_by_field_name("name")
{
ctx = Some(
&code[name_node.byte_range()],
);
break;
}
}
}
}
obj_anc = oa.parent();
}
if ctx.is_some() {
break;
}
}
}
}
_ => {}
}
anc = a.parent();
}
ctx
} else {
None
};
if let Some(context) = function_context.or(inferred_context) {
let range = Range {
start_line: (node.start_position().row + 1) as u32,
start_column: node.start_position().column as u16,
end_line: (node.end_position().row + 1) as u32,
end_column: node.end_position().column as u16,
};
calls.push((context, fn_name, range));
}
}
}
}
// Special handling for fragmented functions
// If this is an identifier followed by formal_parameters, we need to process
// the following siblings with this function's context
if node.kind() == "identifier" {
if let Some(parent) = node.parent() {
if parent.kind() == "ERROR" || parent.kind() == "program" {
if let Some(next_sibling) = node.next_sibling() {
if next_sibling.kind() == "formal_parameters" {
// Process subsequent siblings with this function's context
let mut current = next_sibling.next_sibling();
while let Some(sibling) = current {
// Heuristic boundary: stop if we hit another top-level declaration
let k = sibling.kind();
if k == "function_declaration"
|| k == "generator_function_declaration"
|| k == "class_declaration"
|| k == "export_statement"
{
break;
}
self.extract_calls_recursive(
&sibling,
code,
function_context,
calls,
);
current = sibling.next_sibling();
}
// Don't process children since we handled siblings
return;
}
}
}
}
}
// Recurse to children
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
self.extract_calls_recursive(&child, code, function_context, calls);
}
}
#[allow(clippy::only_used_in_recursion)]
fn extract_method_calls_recursive(
&self,
node: &tree_sitter::Node,
code: &str,
current_function: Option<&str>,
calls: &mut Vec<MethodCall>,
) {
// Track function context - SAME FIX as extract_calls_recursive
// Only set NEW context when entering a function, otherwise INHERIT
let function_context = if node.kind() == "function_declaration"
|| node.kind() == "generator_function_declaration"
|| node.kind() == "method_definition"
|| node.kind() == "arrow_function"
|| node.kind() == "function_expression"
{
// We're entering a NEW function - extract its name
if let Some(name_node) = node.child_by_field_name("name") {
Some(&code[name_node.byte_range()])
} else if node.kind() == "arrow_function" {
// Check parent for variable declarator name
if let Some(parent) = node.parent() {
if parent.kind() == "variable_declarator" {
if let Some(name_node) = parent.child_by_field_name("name") {
Some(&code[name_node.byte_range()])
} else {
current_function // Anonymous, inherit context
}
} else {
current_function // Anonymous, inherit context
}
} else {
current_function // Anonymous, inherit context
}
} else {
current_function // Anonymous function, inherit context
}
} else if node.kind() == "identifier" && current_function.is_none() {
// Check for fragmented functions only at top level
if let Some(parent) = node.parent() {
if parent.kind() == "ERROR" || parent.kind() == "program" {
if let Some(next_sibling) = node.next_sibling() {
if next_sibling.kind() == "formal_parameters" {
Some(&code[node.byte_range()])
} else {
current_function
}
} else {
current_function
}
} else {
current_function
}
} else {
current_function
}
} else {
// Not a function declaration - INHERIT the current context
current_function
};
// Check for method calls
if node.kind() == "call_expression" {
if let Some(function_node) = node.child_by_field_name("function") {
if function_node.kind() == "member_expression" {
// It's a method call!
if let Some((receiver, method_name, is_static)) =
self.extract_method_signature(&function_node, code)
{
if let Some(context) = function_context {
let range = Range {
start_line: (node.start_position().row + 1) as u32,
start_column: node.start_position().column as u16,
end_line: (node.end_position().row + 1) as u32,
end_column: node.end_position().column as u16,
};
let method_call = MethodCall {
caller: context.to_string(),
method_name: method_name.to_string(),
receiver: receiver.map(|r| r.to_string()),
is_static,
range,
caller_range: None, // TODO: track caller definition range
};
calls.push(method_call);
}
}
}
}
}
// Recurse
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
self.extract_method_calls_recursive(&child, code, function_context, calls);
}
}
fn extract_method_signature<'a>(
&self,
member_expr: &tree_sitter::Node,
code: &'a str,
) -> Option<(Option<&'a str>, &'a str, bool)> {
// member_expression has 'object' and 'property' fields
let object = member_expr.child_by_field_name("object");
let property = member_expr.child_by_field_name("property");
match (object, property) {
(Some(obj), Some(prop)) => {
let receiver = &code[obj.byte_range()];
let method_name = &code[prop.byte_range()];
// JavaScript doesn't have static method calls like :: but uses .
// We can't easily distinguish static from instance without type information
let is_static = false;
Some((Some(receiver), method_name, is_static))
}
_ => None,
}
}
/// Track JSX component usage relationships
fn track_jsx_component_usage(&mut self, node: Node, code: &str) {
let component_name = match node.kind() {
"jsx_element" => {
// For <Component>...</Component>, get name from opening element
node.child_by_field_name("open_tag")
.and_then(|tag| tag.child_by_field_name("name"))
.map(|name| &code[name.byte_range()])
}
"jsx_self_closing_element" => {
// For <Component />, get name directly
node.child_by_field_name("name")
.map(|name| &code[name.byte_range()])
}
_ => None,
};
tracing::debug!("[javascript] JSX component_name extracted: {component_name:?}");
if let Some(component_name) = component_name {
// Filter out HTML elements (lowercase) - only track React components (uppercase)
if component_name
.chars()
.next()
.is_some_and(|c| c.is_uppercase())
{
// Track this as a component usage from current context
if let Some(current_fn) = self.context.current_function() {
tracing::debug!(
"[javascript] tracking JSX usage: {current_fn} uses {component_name}"
);
self.component_usages
.push((current_fn.to_string(), component_name.to_string()));
}
} else {
tracing::debug!("[javascript] skipping lowercase JSX element: {component_name}");
}
}
}
fn extract_function_name<'a>(node: &tree_sitter::Node, code: &'a str) -> Option<&'a str> {
match node.kind() {
"identifier" => Some(&code[node.byte_range()]),
"member_expression" => {
// For member expressions like console.log, return the full dotted name
Some(&code[node.byte_range()])
}
"await_expression" => {
// Handle await foo()
if let Some(expr) = node.child_by_field_name("expression") {
Self::extract_function_name(&expr, code)
} else {
// Sometimes await_expression has the identifier as a direct child
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if let Some(name) = Self::extract_function_name(&child, code) {
return Some(name);
}
}
None
}
}
_ => None,
}
}
/// Recursively register all nodes for audit tracking
/// This is separate from symbol extraction - it just ensures all nodes are counted
fn register_node_recursively(&mut self, node: Node) {
self.register_handled_node(node.kind(), node.kind_id());
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
self.register_node_recursively(child);
}
}
/// Extract JSX component usages recursively
/// Tracks function context and collects JSX component uses
fn extract_jsx_uses_recursive<'a>(
node: &Node,
code: &'a str,
current_fn: Option<&'a str>,
uses: &mut Vec<(&'a str, &'a str, Range)>,
) -> Option<&'a str> {
// Track current function context
let func_context = if node.kind() == "function_declaration"
|| node.kind() == "generator_function_declaration"
|| node.kind() == "arrow_function"
{
if let Some(name_node) = node.child_by_field_name("name") {
Some(&code[name_node.byte_range()])
} else {
current_fn
}
} else {
current_fn
};
// Extract JSX component usage
if node.kind() == "jsx_element" || node.kind() == "jsx_self_closing_element" {
let component_name = match node.kind() {
"jsx_element" => node
.child_by_field_name("open_tag")
.and_then(|tag| tag.child_by_field_name("name"))
.map(|name| &code[name.byte_range()]),
"jsx_self_closing_element" => node
.child_by_field_name("name")
.map(|name| &code[name.byte_range()]),
_ => None,
};
if let Some(component_name) = component_name {
// Only track uppercase components (React convention)
if component_name
.chars()
.next()
.is_some_and(|c| c.is_uppercase())
{
if let Some(fn_name) = func_context {
let range = Range {
start_line: node.start_position().row as u32,
start_column: node.start_position().column as u16,
end_line: node.end_position().row as u32,
end_column: node.end_position().column as u16,
};
uses.push((fn_name, component_name, range));
}
}
}
}
// Recurse to children with current context
for child in node.children(&mut node.walk()) {
Self::extract_jsx_uses_recursive(&child, code, func_context, uses);
}
func_context
}
}
impl NodeTracker for JavaScriptParser {
fn get_handled_nodes(&self) -> &std::collections::HashSet<crate::parsing::HandledNode> {
self.node_tracker.get_handled_nodes()
}
fn register_handled_node(&mut self, node_kind: &str, node_id: u16) {
self.node_tracker.register_handled_node(node_kind, node_id);
}
}
impl LanguageParser for JavaScriptParser {
fn parse(
&mut self,
code: &str,
file_id: FileId,
symbol_counter: &mut SymbolCounter,
) -> Vec<Symbol> {
self.parse(code, file_id, symbol_counter)
}
fn as_any(&self) -> &dyn Any {
self
}
fn extract_doc_comment(&self, node: &Node, code: &str) -> Option<String> {
// Look for JSDoc comments (/** ... */)
// First, check if this node is inside an export_statement
// If so, we need to check the export_statement's previous sibling for the comment
let comment_node = if let Some(parent) = node.parent() {
if parent.kind() == "export_statement" {
// For exported functions, check the export statement's previous sibling
parent.prev_sibling()
} else {
// For non-exported functions, check the node's previous sibling
node.prev_sibling()
}
} else {
// No parent, check the node's previous sibling
node.prev_sibling()
};
if let Some(prev) = comment_node {
if prev.kind() == "comment" {
let comment = &code[prev.byte_range()];
if comment.starts_with("/**") {
// Clean up the comment
let cleaned = comment
.trim_start_matches("/**")
.trim_end_matches("*/")
.lines()
.map(|line| line.trim_start_matches(" * ").trim_start_matches(" *"))
.collect::<Vec<_>>()
.join("\n")
.trim()
.to_string();
return Some(cleaned);
}
}
}
None
}
fn find_calls<'a>(&mut self, code: &'a str) -> Vec<(&'a str, &'a str, Range)> {
let tree = match self.parser.parse(code, None) {
Some(tree) => tree,
None => return Vec::new(),
};
let root = tree.root_node();
let mut calls = Vec::new();
// Track current function context
self.extract_calls_recursive(&root, code, None, &mut calls);
calls
}
fn find_method_calls(&mut self, code: &str) -> Vec<MethodCall> {
let tree = match self.parser.parse(code, None) {
Some(tree) => tree,
None => return Vec::new(),
};
let root = tree.root_node();
let mut method_calls = Vec::new();
self.extract_method_calls_recursive(&root, code, None, &mut method_calls);
method_calls
}
fn find_implementations<'a>(&mut self, code: &'a str) -> Vec<(&'a str, &'a str, Range)> {
let mut implementations = Vec::new();
if let Some(tree) = self.parser.parse(code, None) {
Self::find_implementations_in_node(tree.root_node(), code, &mut implementations);
}
implementations
}
fn find_extends<'a>(&mut self, code: &'a str) -> Vec<(&'a str, &'a str, Range)> {
// In JavaScript, extends is the only inheritance mechanism
// So find_extends and find_implementations return the same thing
self.find_implementations(code)
}
fn find_imports(&mut self, code: &str, file_id: FileId) -> Vec<Import> {
let mut imports = Vec::new();
if let Some(tree) = self.parser.parse(code, None) {
let root = tree.root_node();
self.extract_imports_from_node(root, code, file_id, &mut imports);
}
imports
}
fn find_uses<'a>(&mut self, code: &'a str) -> Vec<(&'a str, &'a str, Range)> {
let tree = match self.parser.parse(code, None) {
Some(tree) => tree,
None => return Vec::new(),
};
let root = tree.root_node();
let mut uses = Vec::new();
// Extract JSX component usages during find_uses traversal
Self::extract_jsx_uses_recursive(&root, code, None, &mut uses);
uses
}
fn find_defines<'a>(&mut self, code: &'a str) -> Vec<(&'a str, &'a str, Range)> {
// JavaScript doesn't have interfaces or abstract methods
// Class methods are already extracted as symbols, not as defines
let _tree = match self.parser.parse(code, None) {
Some(tree) => tree,
None => return Vec::new(),
};
Vec::new()
}
fn language(&self) -> crate::parsing::Language {
crate::parsing::Language::JavaScript
}
fn find_variable_types<'a>(&mut self, code: &'a str) -> Vec<(&'a str, &'a str, Range)> {
// Basic JS variable type inference for `const/let/var x = new Type()` patterns
let mut bindings = Vec::new();
if let Some(tree) = self.parser.parse(code, None) {
let root = tree.root_node();
fn walk<'a>(
node: &tree_sitter::Node,
code: &'a str,
out: &mut Vec<(&'a str, &'a str, Range)>,
) {
// Look for lexical_declaration -> variable_declarator with new_expression initializer
if node.kind() == "lexical_declaration" || node.kind() == "variable_declaration" {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "variable_declarator" {
let name = child.child_by_field_name("name").and_then(|n| {
if n.kind() == "identifier" {
Some(&code[n.byte_range()])
} else {
None
}
});
let init = child.child_by_field_name("value");
if let (Some(var), Some(init_node)) = (name, init) {
if init_node.kind() == "new_expression" {
// Extract constructor type: new TypeName(...)
if let Some(constructor) =
init_node.child_by_field_name("constructor")
{
// constructor might be an identifier or qualified name
// We take the last identifier as the type name
let type_name = if constructor.kind() == "identifier" {
Some(&code[constructor.byte_range()])
} else {
// Fallback: try to find a trailing identifier
let mut last_ident: Option<&str> = None;
let mut c2 = constructor.walk();
for part in constructor.children(&mut c2) {
if part.kind() == "identifier" {
last_ident = Some(&code[part.byte_range()]);
}
}
last_ident
};
if let Some(typ) = type_name {
let range = Range::new(
child.start_position().row as u32,
child.start_position().column as u16,
child.end_position().row as u32,
child.end_position().column as u16,
);
out.push((var, typ, range));
}
}
}
}
}
}
}
// Recurse
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
walk(&child, code, out);
}
}
walk(&root, code, &mut bindings);
}
bindings
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::FileId;
#[test]
fn test_javascript_import_extraction() {
println!("\n=== JavaScript Import Extraction Test ===\n");
let mut parser = JavaScriptParser::new().unwrap();
let file_id = FileId::new(1).unwrap();
let code = r#"
import { Component, useState } from 'react';
import React from 'react';
import * as utils from './utils';
import './styles.css';
export { Button } from './Button';
export * from './common';
"#;
println!("Test code:\n{code}");
let imports = parser.find_imports(code, file_id);
println!("\nExtracted {} imports:", imports.len());
for (i, import) in imports.iter().enumerate() {
println!(
" {}. {} -> {:?} (glob: {})",
i + 1,
import.path,
import.alias,
import.is_glob
);
}
// Verify counts (per-specifier imports now included)
assert_eq!(imports.len(), 7, "Should extract 7 imports");
// Verify specific imports
// Named imports create one Import per specifier with local alias
assert!(
imports
.iter()
.any(|i| i.path == "react" && i.alias == Some("Component".to_string()))
);
assert!(
imports
.iter()
.any(|i| i.path == "react" && i.alias == Some("useState".to_string()))
);
// Default import has alias
assert!(
imports
.iter()
.any(|i| i.path == "react" && i.alias == Some("React".to_string()))
);
// Namespace import has alias and is_glob
assert!(
imports
.iter()
.any(|i| i.path == "./utils" && i.alias == Some("utils".to_string()) && i.is_glob)
);
// Side-effect import
assert!(
imports
.iter()
.any(|i| i.path == "./styles.css" && i.alias.is_none())
);
// Re-export
assert!(imports.iter().any(|i| i.path == "./Button"));
// Re-export all
assert!(imports.iter().any(|i| i.path == "./common" && i.is_glob));
println!("\n✅ Import extraction test passed");
}
#[test]
fn test_javascript_export_visibility_is_public() {
let mut parser = JavaScriptParser::new().unwrap();
let file_id = FileId::new(1).unwrap();
let code = r#"export function createChat() { return 'ok'; }"#;
let mut counter = SymbolCounter::new();
let symbols = parser.parse(code, file_id, &mut counter);
// Should produce exactly one function symbol named createChat with Public visibility
assert!(
symbols
.iter()
.any(|s| s.name.as_ref() == "createChat"
&& matches!(s.visibility, Visibility::Public))
);
}
#[test]
fn test_javascript_find_variable_types_new_expression() {
let mut parser = JavaScriptParser::new().unwrap();
let code = r#"
class ChatSDK { createChat() { return 'x'; } }
function start() {
const sdk = new ChatSDK();
sdk.createChat();
}
"#;
let bindings = parser.find_variable_types(code);
// Expect a binding for sdk -> ChatSDK
assert!(
bindings
.iter()
.any(|(var, typ, _)| *var == "sdk" && *typ == "ChatSDK")
);
}
#[test]
fn test_javascript_find_method_calls_extraction() {
let mut parser = JavaScriptParser::new().unwrap();
let code = r#"
class ChatSDK { createChat() { return 'x'; } }
function startVoiceConversation() {
const sdk = new ChatSDK();
sdk.createChat();
}
"#;
let calls = parser.find_method_calls(code);
// Check that we have at least one call to createChat with receiver sdk
assert!(calls.iter().any(|c| c.caller == "startVoiceConversation"
&& c.method_name == "createChat"
&& c.receiver.as_deref() == Some("sdk")));
}
#[test]
fn test_jsx_component_usage_tracking() {
let mut parser = JavaScriptParser::new().unwrap();
let code = r#"
import React from 'react';
import { Button } from './components/ui/button';
export function MyPage() {
return (
<div>
<Button>Click me</Button>
</div>
);
}
export function AnotherComponent() {
return <Button>Another</Button>;
}
"#;
let uses = parser.find_uses(code);
println!("\nJSX Uses found:");
for (caller, component, _range) in &uses {
println!(" {caller} uses {component}");
}
// Check that MyPage uses Button
assert!(
uses.iter()
.any(|(caller, component, _)| *caller == "MyPage" && *component == "Button"),
"MyPage should use Button component"
);
// Check that AnotherComponent uses Button
assert!(
uses.iter()
.any(|(caller, component, _)| *caller == "AnotherComponent"
&& *component == "Button"),
"AnotherComponent should use Button component"
);
println!("✅ JSX component usage tracking working");
}
#[test]
fn test_class_extends_extraction() {
let mut parser = JavaScriptParser::new().unwrap();
let code = r#"
class Animal {
constructor(name) {
this.name = name;
}
}
class Dog extends Animal {
constructor(name, breed) {
super(name);
this.breed = breed;
}
}
"#;
let extends = parser.find_extends(code);
println!("\nExtends relationships found:");
for (child, parent, _) in &extends {
println!(" {child} extends {parent}");
}
assert!(
extends
.iter()
.any(|(child, parent, _)| *child == "Dog" && *parent == "Animal"),
"Dog should extend Animal"
);
println!("✅ Class extends extraction working");
}
#[test]
fn test_arrow_function_extraction() {
let mut parser = JavaScriptParser::new().unwrap();
let file_id = FileId::new(1).unwrap();
let mut counter = SymbolCounter::new();
let code = r#"
const myFunction = () => {
console.log('Hello');
};
const add = (a, b) => a + b;
"#;
let symbols = parser.parse(code, file_id, &mut counter);
println!("\nSymbols found:");
for symbol in &symbols {
println!(" {} ({:?})", symbol.name, symbol.kind);
}
// Should extract arrow functions as Function symbols
assert!(
symbols
.iter()
.any(|s| s.name.as_ref() == "myFunction" && s.kind == SymbolKind::Function),
"Should extract myFunction as Function"
);
assert!(
symbols
.iter()
.any(|s| s.name.as_ref() == "add" && s.kind == SymbolKind::Function),
"Should extract add as Function"
);
println!("✅ Arrow function extraction working");
}
#[test]
fn test_const_vs_let_vs_var() {
let mut parser = JavaScriptParser::new().unwrap();
let file_id = FileId::new(1).unwrap();
let mut counter = SymbolCounter::new();
let code = r#"
const myConst = 42;
let myLet = 'hello';
var myVar = true;
"#;
let symbols = parser.parse(code, file_id, &mut counter);
println!("\nSymbols found:");
for symbol in &symbols {
println!(" {} ({:?})", symbol.name, symbol.kind);
}
// const should be Constant
assert!(
symbols
.iter()
.any(|s| s.name.as_ref() == "myConst" && s.kind == SymbolKind::Constant),
"const should be Constant"
);
// let should be Variable
assert!(
symbols
.iter()
.any(|s| s.name.as_ref() == "myLet" && s.kind == SymbolKind::Variable),
"let should be Variable"
);
// var should be Variable
assert!(
symbols
.iter()
.any(|s| s.name.as_ref() == "myVar" && s.kind == SymbolKind::Variable),
"var should be Variable"
);
println!("✅ const/let/var extraction working");
}
}