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
//! Symbol resolution helpers (type resolution, intrinsic detection, identifier lookup).
//! - Qualified name resolution
//! - Private identifier resolution
//! - Type parameter resolution
//! - Library type resolution
//! - Type query resolution
//! - Namespace member resolution
//! - Global value resolution
//! - Heritage symbol resolution
//! - Access class resolution
//!
//! This module extends `CheckerState` with additional methods for symbol-related
//! operations, providing cleaner APIs for common patterns.
use crate::state::CheckerState;
use std::sync::Arc;
use tracing::trace;
use tsz_binder::{SymbolId, symbol_flags};
use tsz_parser::parser::NodeIndex;
use tsz_parser::parser::syntax_kind_ext;
use tsz_scanner::SyntaxKind;
use tsz_solver::TypeId;
use tsz_solver::is_compiler_managed_type;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TypeSymbolResolution {
Type(SymbolId),
ValueOnly(SymbolId),
NotFound,
}
// =============================================================================
// Symbol Resolution Methods
// =============================================================================
impl<'a> CheckerState<'a> {
// =========================================================================
// Symbol Type Resolution
// =========================================================================
/// Get the type of a symbol with caching.
///
/// This is a convenience wrapper around `get_type_of_symbol` that
/// provides a clearer name for the operation.
pub fn get_symbol_type(&mut self, sym_id: SymbolId) -> TypeId {
self.get_type_of_symbol(sym_id)
}
// =========================================================================
// Global Symbol Detection
// =========================================================================
/// Check if a name refers to a global intrinsic value.
///
/// Returns true for names like `undefined`, `NaN`, `Infinity`, etc.
pub fn is_global_intrinsic(&self, name: &str) -> bool {
matches!(
name,
"undefined"
| "NaN"
| "Infinity"
| "Math"
| "JSON"
| "Object"
| "Array"
| "String"
| "Number"
| "Boolean"
| "Symbol"
| "Date"
| "RegExp"
| "Error"
| "Function"
| "Promise"
)
}
/// Check if a name refers to a global constructor.
///
/// Returns true for built-in constructor names like `Object`, `Array`, etc.
pub fn is_global_constructor(&self, name: &str) -> bool {
matches!(
name,
"Object"
| "Array"
| "String"
| "Number"
| "Boolean"
| "Symbol"
| "Date"
| "RegExp"
| "Error"
| "Function"
| "Promise"
| "Map"
| "Set"
| "WeakMap"
| "WeakSet"
| "Proxy"
| "Reflect"
)
}
// =========================================================================
// Symbol Information Queries
// =========================================================================
/// Get the name of a symbol.
///
/// Returns the symbol's name as a string, or None if the symbol doesn't exist.
pub fn get_symbol_name(&self, sym_id: SymbolId) -> Option<String> {
self.ctx
.binder
.symbols
.get(sym_id)
.map(|symbol| symbol.escaped_name.clone())
}
/// Check if a symbol is exported.
///
/// Returns true if the symbol has the exported flag set.
pub fn is_symbol_exported(&self, sym_id: SymbolId) -> bool {
self.ctx
.binder
.symbols
.get(sym_id)
.is_some_and(|symbol| symbol.is_exported)
}
/// Check if a symbol is type-only (e.g., from `import type`).
///
/// Returns true if the symbol has the type-only flag set.
pub fn is_symbol_type_only(&self, sym_id: SymbolId) -> bool {
self.ctx
.binder
.symbols
.get(sym_id)
.is_some_and(|symbol| symbol.is_type_only)
}
// =========================================================================
// Symbol Property Queries
// =========================================================================
/// Get the value declaration of a symbol.
///
/// Returns the primary value declaration node for the symbol, if any.
pub fn get_symbol_value_declaration(
&self,
sym_id: SymbolId,
) -> Option<tsz_parser::parser::NodeIndex> {
self.ctx.binder.symbols.get(sym_id).and_then(|symbol| {
let decl = symbol.value_declaration;
(decl.0 != u32::MAX).then_some(decl)
})
}
/// Get all declarations for a symbol.
///
/// Returns all declaration nodes associated with the symbol.
pub fn get_symbol_declarations(&self, sym_id: SymbolId) -> Vec<tsz_parser::parser::NodeIndex> {
self.ctx
.binder
.symbols
.get(sym_id)
.map(|symbol| symbol.declarations.clone())
.unwrap_or_default()
}
/// Check if a symbol has a specific flag.
///
/// Returns true if the symbol has the specified flag bit set.
/// Returns false if the symbol doesn't exist.
pub fn symbol_has_flag(&self, sym_id: SymbolId, flag: u32) -> bool {
self.ctx
.binder
.symbols
.get(sym_id)
.is_some_and(|symbol| (symbol.flags & flag) != 0)
}
/// Safely get symbol flags, returning 0 if symbol doesn't exist.
///
/// This defensive accessor prevents crashes when symbol IDs are invalid
/// or reference symbols that don't exist in any binder.
pub fn symbol_flags_safe(&self, sym_id: SymbolId) -> u32 {
self.ctx
.binder
.symbols
.get(sym_id)
.map_or(0, |symbol| symbol.flags)
}
/// Safely get symbol flags with lib binders fallback.
///
/// Returns 0 if the symbol doesn't exist in any binder.
pub fn symbol_flags_with_libs(
&self,
sym_id: SymbolId,
lib_binders: &[Arc<tsz_binder::BinderState>],
) -> u32 {
self.ctx
.binder
.get_symbol_with_libs(sym_id, lib_binders)
.map_or(0, |symbol| symbol.flags)
}
// =========================================================================
// Identifier Resolution
// =========================================================================
/// Collect lib binders from `lib_contexts` for cross-arena symbol lookup.
/// This enables symbol resolution across lib.d.ts files when `lib_binders`
/// is not populated in the binder (e.g., in the driver.rs path).
pub(crate) fn get_lib_binders(&self) -> Vec<Arc<tsz_binder::BinderState>> {
self.ctx
.lib_contexts
.iter()
.map(|lc| Arc::clone(&lc.binder))
.collect()
}
/// Check if a symbol represents a class member (property, method, accessor, or constructor).
///
/// This filters out instance members that cannot be accessed as standalone values.
/// However, static members and constructors should still be accessible.
pub(crate) const fn is_class_member_symbol(flags: u32) -> bool {
// Check if it's any kind of class member
let is_member = (flags
& (symbol_flags::PROPERTY
| symbol_flags::METHOD
| symbol_flags::GET_ACCESSOR
| symbol_flags::SET_ACCESSOR
| symbol_flags::CONSTRUCTOR))
!= 0;
if !is_member {
return false;
}
// Allow constructors - they represent the class itself
if (flags & symbol_flags::CONSTRUCTOR) != 0 {
return false;
}
// Allow static members - they're accessible via the class name
if (flags & symbol_flags::STATIC) != 0 {
return false;
}
// Filter out instance members (properties, methods, accessors without STATIC)
true
}
/// Resolve an identifier node to its symbol ID.
///
/// This function walks the scope chain from the identifier's location upward,
/// checking each scope's symbol table for the name. It also checks:
/// - Module exports
/// - Type parameter scope (for generic functions, classes, type aliases)
/// - File locals (global scope from lib.d.ts)
/// - Lib binders' `file_locals`
///
/// Returns None if the identifier cannot be resolved to any symbol.
pub(crate) fn resolve_identifier_symbol(&self, idx: NodeIndex) -> Option<SymbolId> {
let result = self.resolve_identifier_symbol_inner(idx);
if let Some(sym_id) = result {
self.ctx.referenced_symbols.borrow_mut().insert(sym_id);
trace!(sym_id = %sym_id.0, idx = %idx.0, "resolve_identifier_symbol: marked referenced");
}
result
}
/// Resolve identifier for write context (assignment target).
pub(crate) fn resolve_identifier_symbol_for_write(&self, idx: NodeIndex) -> Option<SymbolId> {
let result = self.resolve_identifier_symbol_inner(idx);
if let Some(sym_id) = result {
self.ctx.written_symbols.borrow_mut().insert(sym_id);
}
result
}
fn resolve_identifier_symbol_inner(&self, idx: NodeIndex) -> Option<SymbolId> {
// Get identifier name for tracing
let ident_name = self
.ctx
.arena
.get_identifier_at(idx)
.map(|i| i.escaped_text.as_str().to_string());
let ignore_libs = !self.ctx.has_lib_loaded();
let lib_binders = if ignore_libs {
Vec::new()
} else {
self.get_lib_binders()
};
let is_from_lib = |sym_id: SymbolId| self.ctx.symbol_is_from_lib(sym_id);
let should_skip_lib_symbol = |sym_id: SymbolId| ignore_libs && is_from_lib(sym_id);
trace!(
ident_name = ?ident_name,
idx = ?idx,
ignore_libs = ignore_libs,
"Resolving identifier symbol"
);
// First try the binder's resolver which checks scope chain and file_locals
let result = self.ctx.binder.resolve_identifier_with_filter(
self.ctx.arena,
idx,
&lib_binders,
|sym_id| {
if should_skip_lib_symbol(sym_id) {
return false;
}
if let Some(symbol) = self.ctx.binder.get_symbol_with_libs(sym_id, &lib_binders) {
let is_class_member = Self::is_class_member_symbol(symbol.flags);
if is_class_member {
return is_from_lib(sym_id)
&& (symbol.flags & symbol_flags::EXPORT_VALUE) != 0;
}
}
true
},
);
let result = {
let expected_name = self
.ctx
.arena
.get_identifier_at(idx)
.map(|ident| ident.escaped_text.as_str());
result.filter(|&sym_id| {
let Some(expected_name) = expected_name else {
return false;
};
self.ctx
.binder
.get_symbol_with_libs(sym_id, &lib_binders)
.is_some_and(|symbol| symbol.escaped_name.as_str() == expected_name)
})
};
trace!(
ident_name = ?ident_name,
binder_result = ?result,
"Binder resolution result"
);
// IMPORTANT: If the binder didn't find the symbol, check lib_contexts directly as a fallback.
// The binder's method has a bug where it only queries lib_binders when lib_symbols_merged is FALSE.
// After lib symbols are merged into the main binder, lib_symbols_merged is set to TRUE,
// causing the binder to skip lib lookup entirely. By checking lib_contexts.file_locals
// directly here as a fallback, we bypass that bug and ensure global symbols are always resolved.
// This matches the pattern used successfully in generators.rs (lookup_global_type).
if result.is_none() && !ignore_libs {
// Get the identifier name
let name = if let Some(ident) = self.ctx.arena.get_identifier_at(idx) {
ident.escaped_text.as_str()
} else {
return None;
};
// Check lib_contexts directly for global symbols
for (lib_idx, lib_ctx) in self.ctx.lib_contexts.iter().enumerate() {
if let Some(lib_sym_id) = lib_ctx.binder.file_locals.get(name) {
trace!(
name = name,
lib_idx = lib_idx,
lib_sym_id = ?lib_sym_id,
"Found symbol in lib_context"
);
if !should_skip_lib_symbol(lib_sym_id) {
// Use file binder's sym_id for correct ID space after lib merge.
// Never return lib-context SymbolIds directly: they may collide with
// unrelated symbols in the current binder ID space.
let Some(file_sym_id) = self.ctx.binder.file_locals.get(name) else {
continue;
};
trace!(
name = name,
file_sym_id = ?file_sym_id,
lib_sym_id = ?lib_sym_id,
"Returning symbol from lib_contexts fallback"
);
return Some(file_sym_id);
}
}
}
}
trace!(
ident_name = ?ident_name,
final_result = ?result,
"Symbol resolution final result"
);
if let Some(ident) = self.ctx.arena.get_identifier_at(idx)
&& result.is_none()
{
let name = ident.escaped_text.as_str();
if let Some(sym_id) =
self.resolve_identifier_symbol_from_all_binders(name, |sym_id, symbol| {
if should_skip_lib_symbol(sym_id) {
return false;
}
let is_class_member = Self::is_class_member_symbol(symbol.flags);
if is_class_member {
return is_from_lib(sym_id)
&& (symbol.flags & symbol_flags::EXPORT_VALUE) != 0;
}
true
})
{
return Some(sym_id);
}
}
trace!(
ident_name = ?ident_name,
final_result = ?result,
"Symbol resolution final result"
);
if let Some(sym_id) = result
&& let Some(sym) = self.ctx.binder.get_symbol_with_libs(sym_id, &lib_binders)
{
trace!(
ident_name = ?ident_name,
sym_id = sym_id.0,
sym_name = sym.escaped_name.as_str(),
sym_flags = sym.flags,
"Symbol resolution resolved metadata"
);
}
result
}
/// Resolve an identifier symbol for type positions, skipping value-only symbols.
pub(crate) fn resolve_identifier_symbol_in_type_position(
&self,
idx: NodeIndex,
) -> TypeSymbolResolution {
let result = self.resolve_identifier_symbol_in_type_position_inner(idx);
if let TypeSymbolResolution::Type(sym_id) = result {
self.ctx.referenced_symbols.borrow_mut().insert(sym_id);
}
result
}
fn resolve_identifier_symbol_in_type_position_inner(
&self,
idx: NodeIndex,
) -> TypeSymbolResolution {
let node = match self.ctx.arena.get(idx) {
Some(node) => node,
None => return TypeSymbolResolution::NotFound,
};
let ident = match self.ctx.arena.get_identifier(node) {
Some(ident) => ident,
None => return TypeSymbolResolution::NotFound,
};
let name = ident.escaped_text.as_str();
let ignore_libs = !self.ctx.has_lib_loaded();
// Collect lib binders for cross-arena symbol lookup
let lib_binders = if ignore_libs {
Vec::new()
} else {
self.get_lib_binders()
};
let should_skip_lib_symbol =
|sym_id: SymbolId| ignore_libs && self.ctx.symbol_is_from_lib(sym_id);
let mut value_only_candidate = None;
// Check if this name exists in a local scope (namespace/module) that would shadow
// the global lib symbol. If so, we skip the early lib_contexts check and let the
// binder's scope-based resolution find the local symbol first.
let name_in_local_scope = if !ignore_libs {
self.ctx
.binder
.resolve_identifier_with_filter(
self.ctx.arena,
idx,
&lib_binders,
|_| true, // accept any symbol
)
.is_some_and(|found_sym_id| {
// Check if this symbol is different from the file_locals symbol.
// If it's different, it was found in a more local scope (namespace, etc.)
self.ctx.binder.file_locals.get(name) != Some(found_sym_id)
})
} else {
false
};
// IMPORTANT: Check lib_contexts directly BEFORE calling binder's resolve_identifier_with_filter.
// The binder's method has a bug where it only queries lib_binders when lib_symbols_merged is FALSE.
// After lib symbols are merged into the main binder, lib_symbols_merged is set to TRUE,
// causing the binder to skip lib lookup entirely. By checking lib_contexts.file_locals
// directly here, we bypass that bug and ensure global type symbols are always resolved.
// However, skip this early check when the name is declared in a local scope (namespace, etc.)
// so that local symbols can shadow global ones.
if !ignore_libs && !name_in_local_scope {
for lib_ctx in &self.ctx.lib_contexts {
if let Some(lib_sym_id) = lib_ctx.binder.file_locals.get(name) {
// After lib merge, the file binder has the same symbols with
// potentially different IDs. Use file binder's ID for returns,
// and skip symbols not present in current binder ID space.
let Some(sym_id) = self.ctx.binder.file_locals.get(name) else {
continue;
};
if !should_skip_lib_symbol(sym_id) {
// Check flags using lib binder (lib_sym_id is valid in lib binder)
let flags = lib_ctx.binder.get_symbol(lib_sym_id).map_or(0, |s| s.flags);
// Namespaces and modules are value-only but should be allowed in type position
let is_namespace_or_module = (flags
& (symbol_flags::NAMESPACE_MODULE | symbol_flags::VALUE_MODULE))
!= 0;
if is_namespace_or_module {
return TypeSymbolResolution::Type(sym_id);
}
// For ALIAS symbols, resolve to the target
if flags & symbol_flags::ALIAS != 0 {
let mut visited = Vec::new();
if let Some(target_sym_id) =
self.resolve_alias_symbol(sym_id, &mut visited)
{
// Check the target symbol's flags
let target_flags = self
.ctx
.binder
.get_symbol_with_libs(target_sym_id, &lib_binders)
.map_or(0, |s| s.flags);
if (target_flags
& (symbol_flags::NAMESPACE_MODULE | symbol_flags::VALUE_MODULE))
!= 0
{
return TypeSymbolResolution::Type(target_sym_id);
}
}
}
// Check if this is a value-only symbol
let is_value_only = (self.alias_resolves_to_value_only(sym_id, None)
|| self.symbol_is_value_only(sym_id, None))
&& !self.symbol_is_type_only(sym_id, None);
if is_value_only {
if value_only_candidate.is_none() {
value_only_candidate = Some(sym_id);
}
} else {
// Valid type symbol found in lib
return TypeSymbolResolution::Type(sym_id);
}
}
}
}
}
let mut accept_type_symbol = |sym_id: SymbolId| -> bool {
// Get symbol flags to check for special cases
let flags = self
.ctx
.binder
.get_symbol_with_libs(sym_id, &lib_binders)
.map_or(0, |s| s.flags);
// Namespaces and modules are value-only but should be allowed in type position
// because they can contain types (e.g., MyNamespace.ValueInterface)
let is_namespace_or_module =
(flags & (symbol_flags::NAMESPACE_MODULE | symbol_flags::VALUE_MODULE)) != 0;
if is_namespace_or_module {
return true;
}
// For ALIAS symbols (import equals declarations), resolve to the target
// and check if it's a namespace/module
if flags & symbol_flags::ALIAS != 0 {
let mut visited = Vec::new();
if let Some(target_sym_id) = self.resolve_alias_symbol(sym_id, &mut visited) {
let target_flags = self
.ctx
.binder
.get_symbol_with_libs(target_sym_id, &lib_binders)
.map_or(0, |s| s.flags);
if (target_flags
& (symbol_flags::NAMESPACE_MODULE | symbol_flags::VALUE_MODULE))
!= 0
{
return true;
}
}
}
let is_value_only = (self.alias_resolves_to_value_only(sym_id, None)
|| self.symbol_is_value_only(sym_id, None))
&& !self.symbol_is_type_only(sym_id, None);
if is_value_only {
if value_only_candidate.is_none() {
value_only_candidate = Some(sym_id);
}
return false;
}
true
};
let resolved = self.ctx.binder.resolve_identifier_with_filter(
self.ctx.arena,
idx,
&lib_binders,
|sym_id| {
if should_skip_lib_symbol(sym_id) {
return false;
}
if let Some(symbol) = self.ctx.binder.get_symbol_with_libs(sym_id, &lib_binders) {
let is_class_member = Self::is_class_member_symbol(symbol.flags);
if is_class_member {
return false;
}
}
accept_type_symbol(sym_id)
},
);
if resolved.is_none()
&& let Some(sym_id) =
self.resolve_identifier_symbol_from_all_binders(name, |sym_id, symbol| {
if should_skip_lib_symbol(sym_id) {
return false;
}
let is_class_member = Self::is_class_member_symbol(symbol.flags);
if is_class_member {
return false;
}
accept_type_symbol(sym_id)
})
{
let is_value_only = (self.alias_resolves_to_value_only(sym_id, None)
|| self.symbol_is_value_only(sym_id, None))
&& !self.symbol_is_type_only(sym_id, None);
if is_value_only {
return TypeSymbolResolution::ValueOnly(sym_id);
}
return TypeSymbolResolution::Type(sym_id);
}
// Guard against SymbolId renumbering from lib merging: if the resolved
// symbol's name doesn't match the requested name, the scope table has a
// stale SymbolId. Reject it and fall through to value_only_candidate.
let resolved = resolved.filter(|&sym_id| {
self.ctx
.binder
.get_symbol_with_libs(sym_id, &lib_binders)
.is_some_and(|s| s.escaped_name.as_str() == name)
});
if let Some(sym_id) = resolved {
if let Some(symbol) = self.ctx.binder.get_symbol_with_libs(sym_id, &lib_binders)
&& symbol.flags & symbol_flags::ALIAS != 0
{
// Mark the local alias as referenced (for unused-import tracking).
// When we follow the alias chain below, only the target gets returned
// and inserted into referenced_symbols by the caller. Without this,
// imports used only in type positions appear unused (false TS6133).
self.ctx.referenced_symbols.borrow_mut().insert(sym_id);
let mut visited_aliases = Vec::new();
if let Some(target_sym_id) = self.resolve_alias_symbol(sym_id, &mut visited_aliases)
{
let target_flags = self
.ctx
.binder
.get_symbol_with_libs(target_sym_id, &lib_binders)
.map_or(0, |s| s.flags);
let target_is_namespace_module = (target_flags
& (symbol_flags::NAMESPACE_MODULE | symbol_flags::VALUE_MODULE))
!= 0;
let target_is_value_only = (self
.alias_resolves_to_value_only(target_sym_id, None)
|| self.symbol_is_value_only(target_sym_id, None))
&& !self.symbol_is_type_only(target_sym_id, None);
if target_is_value_only && !target_is_namespace_module {
return TypeSymbolResolution::ValueOnly(target_sym_id);
}
return TypeSymbolResolution::Type(target_sym_id);
}
}
return TypeSymbolResolution::Type(sym_id);
}
if let Some(value_only) = value_only_candidate {
TypeSymbolResolution::ValueOnly(value_only)
} else {
TypeSymbolResolution::NotFound
}
}
/// Resolve a private identifier to its symbols across class scopes.
///
/// Private identifiers (e.g., `#foo`) are only valid within class bodies.
/// This function walks the scope chain and collects all symbols with the
/// matching private name from class scopes.
///
/// Returns a tuple of (`symbols_found`, `saw_class_scope`) where:
/// - `symbols_found`: Vec of `SymbolIds` for all matching private members
/// - `saw_class_scope`: true if any class scope was encountered
pub(crate) fn resolve_private_identifier_symbols(
&self,
idx: NodeIndex,
) -> (Vec<SymbolId>, bool) {
self.ctx
.binder
.resolve_private_identifier_symbols(self.ctx.arena, idx)
}
/// Resolve a qualified name or identifier to a symbol ID.
///
/// Handles both simple identifiers and qualified names (e.g., `A.B.C`).
/// Also resolves through alias symbols (imports).
pub(crate) fn resolve_qualified_symbol(&self, idx: NodeIndex) -> Option<SymbolId> {
let mut visited_aliases = Vec::new();
self.resolve_qualified_symbol_inner(idx, &mut visited_aliases, 0)
}
/// Resolve a qualified name or identifier for type positions.
pub(crate) fn resolve_qualified_symbol_in_type_position(
&self,
idx: NodeIndex,
) -> TypeSymbolResolution {
let mut visited_aliases = Vec::new();
self.resolve_qualified_symbol_inner_in_type_position(idx, &mut visited_aliases, 0)
}
/// Inner implementation of qualified symbol resolution for type positions.
pub(crate) fn resolve_qualified_symbol_inner_in_type_position(
&self,
idx: NodeIndex,
visited_aliases: &mut Vec<SymbolId>,
depth: usize,
) -> TypeSymbolResolution {
// Prevent stack overflow from deeply nested qualified names
const MAX_QUALIFIED_NAME_DEPTH: usize = 128;
if depth >= MAX_QUALIFIED_NAME_DEPTH {
return TypeSymbolResolution::NotFound;
}
let node = match self.ctx.arena.get(idx) {
Some(node) => node,
None => return TypeSymbolResolution::NotFound,
};
if node.kind == SyntaxKind::Identifier as u16 {
return match self.resolve_identifier_symbol_in_type_position(idx) {
TypeSymbolResolution::Type(sym_id) => {
// Preserve unresolved alias symbols in type position.
// `import X = require("...")` aliases may not resolve to a concrete
// target symbol, but `X` is still a valid namespace-like type query
// anchor (e.g., `typeof X.Member`).
let resolved = self
.resolve_alias_symbol(sym_id, visited_aliases)
.unwrap_or(sym_id);
TypeSymbolResolution::Type(resolved)
}
other => other,
};
}
if node.kind == SyntaxKind::StringLiteral as u16
|| node.kind == SyntaxKind::NoSubstitutionTemplateLiteral as u16
{
let Some(literal) = self.ctx.arena.get_literal(node) else {
return TypeSymbolResolution::NotFound;
};
if let Some(sym_id) = self.ctx.binder.file_locals.get(&literal.text) {
let is_value_only = (self
.alias_resolves_to_value_only(sym_id, Some(&literal.text))
|| self.symbol_is_value_only(sym_id, Some(&literal.text)))
&& !self.symbol_is_type_only(sym_id, Some(&literal.text));
if is_value_only {
return TypeSymbolResolution::ValueOnly(sym_id);
}
let Some(sym_id) = self.resolve_alias_symbol(sym_id, visited_aliases) else {
return TypeSymbolResolution::NotFound;
};
return TypeSymbolResolution::Type(sym_id);
}
return TypeSymbolResolution::NotFound;
}
if node.kind == tsz_parser::parser::syntax_kind_ext::PROPERTY_ACCESS_EXPRESSION {
let Some(access) = self.ctx.arena.get_access_expr(node) else {
return TypeSymbolResolution::NotFound;
};
let left_sym = match self.resolve_qualified_symbol_inner_in_type_position(
access.expression,
visited_aliases,
depth + 1,
) {
TypeSymbolResolution::Type(sym_id) => sym_id,
other => return other,
};
let left_sym = self
.resolve_alias_symbol(left_sym, visited_aliases)
.unwrap_or(left_sym);
let right_name = match self
.ctx
.arena
.get_identifier_at(access.name_or_argument)
.map(|ident| ident.escaped_text.as_str())
{
Some(name) => name,
None => return TypeSymbolResolution::NotFound,
};
let lib_binders = self.get_lib_binders();
let Some(left_symbol) = self.ctx.binder.get_symbol_with_libs(left_sym, &lib_binders)
else {
return TypeSymbolResolution::NotFound;
};
if let Some(exports) = left_symbol.exports.as_ref()
&& let Some(member_sym) = exports.get(right_name)
{
let is_value_only = (self
.alias_resolves_to_value_only(member_sym, Some(right_name))
|| self.symbol_is_value_only(member_sym, Some(right_name)))
&& !self.symbol_is_type_only(member_sym, Some(right_name));
if is_value_only {
return TypeSymbolResolution::ValueOnly(member_sym);
}
let member_sym = self
.resolve_alias_symbol(member_sym, visited_aliases)
.unwrap_or(member_sym);
return TypeSymbolResolution::Type(member_sym);
}
if let Some(ref module_specifier) = left_symbol.import_module
&& !((left_symbol.flags & symbol_flags::ALIAS) != 0
&& self
.ctx
.module_resolves_to_non_module_entity(module_specifier))
&& let Some(reexported_sym) = self.resolve_reexported_member_symbol(
module_specifier,
right_name,
visited_aliases,
)
{
let is_value_only = (self
.alias_resolves_to_value_only(reexported_sym, Some(right_name))
|| self.symbol_is_value_only(reexported_sym, Some(right_name)))
&& !self.symbol_is_type_only(reexported_sym, Some(right_name));
if is_value_only {
return TypeSymbolResolution::ValueOnly(reexported_sym);
}
return TypeSymbolResolution::Type(reexported_sym);
}
if let Some(reexported_sym) =
self.resolve_member_from_import_equals_alias(left_sym, right_name, visited_aliases)
{
let is_value_only = (self
.alias_resolves_to_value_only(reexported_sym, Some(right_name))
|| self.symbol_is_value_only(reexported_sym, Some(right_name)))
&& !self.symbol_is_type_only(reexported_sym, Some(right_name));
if is_value_only {
return TypeSymbolResolution::ValueOnly(reexported_sym);
}
return TypeSymbolResolution::Type(reexported_sym);
}
return TypeSymbolResolution::NotFound;
}
if node.kind != tsz_parser::parser::syntax_kind_ext::QUALIFIED_NAME {
return TypeSymbolResolution::NotFound;
}
let qn = match self.ctx.arena.get_qualified_name(node) {
Some(qn) => qn,
None => return TypeSymbolResolution::NotFound,
};
let left_sym = match self.resolve_qualified_symbol_inner_in_type_position(
qn.left,
visited_aliases,
depth + 1,
) {
TypeSymbolResolution::Type(sym_id) => sym_id,
other => return other,
};
let left_sym = self
.resolve_alias_symbol(left_sym, visited_aliases)
.unwrap_or(left_sym);
let right_name = match self
.ctx
.arena
.get(qn.right)
.and_then(|node| self.ctx.arena.get_identifier(node))
.map(|ident| ident.escaped_text.as_str())
{
Some(name) => name,
None => return TypeSymbolResolution::NotFound,
};
// Look up the symbol across binders (file + libs)
let lib_binders = self.get_lib_binders();
let Some(left_symbol) = self.ctx.binder.get_symbol_with_libs(left_sym, &lib_binders) else {
return TypeSymbolResolution::NotFound;
};
// First try direct exports
if let Some(exports) = left_symbol.exports.as_ref()
&& let Some(member_sym) = exports.get(right_name)
{
let is_value_only = (self.alias_resolves_to_value_only(member_sym, Some(right_name))
|| self.symbol_is_value_only(member_sym, Some(right_name)))
&& !self.symbol_is_type_only(member_sym, Some(right_name));
if is_value_only {
return TypeSymbolResolution::ValueOnly(member_sym);
}
return TypeSymbolResolution::Type(
self.resolve_alias_symbol(member_sym, visited_aliases)
.unwrap_or(member_sym),
);
}
// If not found in direct exports, check for re-exports
if let Some(ref module_specifier) = left_symbol.import_module {
if (left_symbol.flags & symbol_flags::ALIAS) != 0
&& self
.ctx
.module_resolves_to_non_module_entity(module_specifier)
{
return TypeSymbolResolution::NotFound;
}
if let Some(reexported_sym) =
self.resolve_reexported_member_symbol(module_specifier, right_name, visited_aliases)
{
let is_value_only = (self
.alias_resolves_to_value_only(reexported_sym, Some(right_name))
|| self.symbol_is_value_only(reexported_sym, Some(right_name)))
&& !self.symbol_is_type_only(reexported_sym, Some(right_name));
if is_value_only {
return TypeSymbolResolution::ValueOnly(reexported_sym);
}
return TypeSymbolResolution::Type(reexported_sym);
}
}
if let Some(reexported_sym) =
self.resolve_member_from_import_equals_alias(left_sym, right_name, visited_aliases)
{
let is_value_only = (self
.alias_resolves_to_value_only(reexported_sym, Some(right_name))
|| self.symbol_is_value_only(reexported_sym, Some(right_name)))
&& !self.symbol_is_type_only(reexported_sym, Some(right_name));
if is_value_only {
return TypeSymbolResolution::ValueOnly(reexported_sym);
}
return TypeSymbolResolution::Type(reexported_sym);
}
TypeSymbolResolution::NotFound
}
fn resolve_identifier_symbol_from_all_binders(
&self,
name: &str,
mut accept: impl FnMut(SymbolId, &tsz_binder::Symbol) -> bool,
) -> Option<SymbolId> {
let all_binders = self.ctx.all_binders.as_ref()?;
for (file_idx, binder) in all_binders.iter().enumerate() {
if let Some(sym_id) = binder.file_locals.get(name) {
let Some(sym_symbol) = binder.get_symbol(sym_id) else {
continue;
};
if !accept(sym_id, sym_symbol) {
continue;
}
if let Some(local_symbol) = self.ctx.binder.get_symbol(sym_id) {
if local_symbol.escaped_name != name {
self.ctx
.cross_file_symbol_targets
.borrow_mut()
.entry(sym_id)
.or_insert(file_idx);
}
} else {
self.ctx
.cross_file_symbol_targets
.borrow_mut()
.entry(sym_id)
.or_insert(file_idx);
}
return Some(sym_id);
}
}
None
}
/// Inner implementation of qualified symbol resolution with cycle detection.
pub(crate) fn resolve_qualified_symbol_inner(
&self,
idx: NodeIndex,
visited_aliases: &mut Vec<SymbolId>,
depth: usize,
) -> Option<SymbolId> {
// Prevent stack overflow from deeply nested qualified names
const MAX_QUALIFIED_NAME_DEPTH: usize = 128;
if depth >= MAX_QUALIFIED_NAME_DEPTH {
return None;
}
let node = self.ctx.arena.get(idx)?;
if node.kind == SyntaxKind::Identifier as u16 {
let sym_id = self.resolve_identifier_symbol(idx)?;
// Preserve alias symbols when alias resolution has no concrete target
// (e.g., `import X = require("...")` namespace-like aliases).
return self
.resolve_alias_symbol(sym_id, visited_aliases)
.or(Some(sym_id));
}
if node.kind == SyntaxKind::StringLiteral as u16
|| node.kind == SyntaxKind::NoSubstitutionTemplateLiteral as u16
{
let literal = self.ctx.arena.get_literal(node)?;
if let Some(sym_id) = self.ctx.binder.file_locals.get(&literal.text) {
return self.resolve_alias_symbol(sym_id, visited_aliases);
}
return None;
}
if node.kind == tsz_parser::parser::syntax_kind_ext::PROPERTY_ACCESS_EXPRESSION {
let access = self.ctx.arena.get_access_expr(node)?;
let left_sym =
self.resolve_qualified_symbol_inner(access.expression, visited_aliases, depth + 1)?;
let left_sym = self
.resolve_alias_symbol(left_sym, visited_aliases)
.unwrap_or(left_sym);
let right_name = self
.ctx
.arena
.get_identifier_at(access.name_or_argument)
.map(|ident| ident.escaped_text.as_str())?;
let lib_binders = self.get_lib_binders();
let left_symbol = self
.ctx
.binder
.get_symbol_with_libs(left_sym, &lib_binders)?;
if let Some(exports) = left_symbol.exports.as_ref()
&& let Some(member_sym) = exports.get(right_name)
{
return Some(
self.resolve_alias_symbol(member_sym, visited_aliases)
.unwrap_or(member_sym),
);
}
if let Some(ref module_specifier) = left_symbol.import_module {
if (left_symbol.flags & symbol_flags::ALIAS) != 0
&& self
.ctx
.module_resolves_to_non_module_entity(module_specifier)
{
return None;
}
return self.resolve_reexported_member_symbol(
module_specifier,
right_name,
visited_aliases,
);
}
if let Some(reexported_sym) =
self.resolve_member_from_import_equals_alias(left_sym, right_name, visited_aliases)
{
return Some(reexported_sym);
}
return None;
}
if node.kind != tsz_parser::parser::syntax_kind_ext::QUALIFIED_NAME {
return None;
}
let qn = self.ctx.arena.get_qualified_name(node)?;
let left_sym = self.resolve_qualified_symbol_inner(qn.left, visited_aliases, depth + 1)?;
let left_sym = self
.resolve_alias_symbol(left_sym, visited_aliases)
.unwrap_or(left_sym);
let right_name = self
.ctx
.arena
.get(qn.right)
.and_then(|node| self.ctx.arena.get_identifier(node))
.map(|ident| ident.escaped_text.as_str())?;
let lib_binders = self.get_lib_binders();
let left_symbol = self
.ctx
.binder
.get_symbol_with_libs(left_sym, &lib_binders)?;
// First try direct exports
if let Some(exports) = left_symbol.exports.as_ref()
&& let Some(member_sym) = exports.get(right_name)
{
return Some(
self.resolve_alias_symbol(member_sym, visited_aliases)
.unwrap_or(member_sym),
);
}
// If not found in direct exports, check for re-exports
// This handles cases like: export { foo } from './bar'
if let Some(ref module_specifier) = left_symbol.import_module {
if (left_symbol.flags & symbol_flags::ALIAS) != 0
&& self
.ctx
.module_resolves_to_non_module_entity(module_specifier)
{
return None;
}
if let Some(reexported_sym) =
self.resolve_reexported_member_symbol(module_specifier, right_name, visited_aliases)
{
return Some(reexported_sym);
}
}
if let Some(reexported_sym) =
self.resolve_member_from_import_equals_alias(left_sym, right_name, visited_aliases)
{
return Some(reexported_sym);
}
None
}
fn resolve_member_from_import_equals_alias(
&self,
alias_sym: SymbolId,
member_name: &str,
visited_aliases: &mut Vec<SymbolId>,
) -> Option<SymbolId> {
let symbol = self.ctx.binder.get_symbol(alias_sym)?;
if symbol.flags & symbol_flags::ALIAS == 0 {
return None;
}
let decl_idx = if symbol.value_declaration.is_some() {
symbol.value_declaration
} else {
symbol
.declarations
.iter()
.copied()
.find(|idx| idx.is_some())
.unwrap_or(NodeIndex::NONE)
};
if decl_idx.is_some()
&& let Some(decl_node) = self.ctx.arena.get(decl_idx)
&& decl_node.kind == syntax_kind_ext::IMPORT_EQUALS_DECLARATION
&& let Some(import) = self.ctx.arena.get_import_decl(decl_node)
&& let Some(module_specifier) =
self.get_require_module_specifier(import.module_specifier)
{
if self
.ctx
.module_resolves_to_non_module_entity(&module_specifier)
{
return None;
}
return self.resolve_reexported_member_symbol(
&module_specifier,
member_name,
visited_aliases,
);
}
None
}
/// Resolve a re-exported member symbol by following re-export chains.
///
/// This function handles cases where a namespace member is re-exported from
/// another module using `export { foo } from './bar'` or `export * from './bar'`.
pub(crate) fn resolve_reexported_member_symbol(
&self,
module_specifier: &str,
member_name: &str,
visited_aliases: &mut Vec<SymbolId>,
) -> Option<SymbolId> {
let mut visited_modules = rustc_hash::FxHashSet::default();
self.resolve_reexported_member_symbol_inner(
module_specifier,
member_name,
visited_aliases,
&mut visited_modules,
)
}
fn resolve_member_from_module_exports(
&self,
binder: &tsz_binder::BinderState,
exports_table: &tsz_binder::SymbolTable,
member_name: &str,
visited_aliases: &mut Vec<SymbolId>,
) -> Option<SymbolId> {
let can_resolve_aliases = std::ptr::eq(binder, self.ctx.binder);
if let Some(sym_id) = exports_table.get(member_name) {
if can_resolve_aliases {
return Some(
self.resolve_alias_symbol(sym_id, visited_aliases)
.unwrap_or(sym_id),
);
}
return Some(sym_id);
}
let export_equals_sym = exports_table.get("export=")?;
let mut candidate_symbol_ids = vec![export_equals_sym];
if can_resolve_aliases {
let resolved_export_equals = self
.resolve_alias_symbol(export_equals_sym, visited_aliases)
.unwrap_or(export_equals_sym);
if resolved_export_equals != export_equals_sym {
candidate_symbol_ids.push(resolved_export_equals);
}
}
for candidate_symbol_id in candidate_symbol_ids {
let Some(target_symbol) = binder.get_symbol(candidate_symbol_id) else {
continue;
};
if let Some(exports) = target_symbol.exports.as_ref()
&& let Some(sym_id) = exports.get(member_name)
{
if can_resolve_aliases {
return Some(
self.resolve_alias_symbol(sym_id, visited_aliases)
.unwrap_or(sym_id),
);
}
return Some(sym_id);
}
if let Some(members) = target_symbol.members.as_ref()
&& let Some(sym_id) = members.get(member_name)
{
if can_resolve_aliases {
return Some(
self.resolve_alias_symbol(sym_id, visited_aliases)
.unwrap_or(sym_id),
);
}
return Some(sym_id);
}
// Some binder states keep the namespace merge partner as a distinct symbol.
// Search same-name symbols with module namespace flags for members.
for merged_candidate_id in binder
.get_symbols()
.find_all_by_name(&target_symbol.escaped_name)
{
let Some(merged_symbol) = binder.get_symbol(merged_candidate_id) else {
continue;
};
if (merged_symbol.flags
& (symbol_flags::MODULE
| symbol_flags::NAMESPACE_MODULE
| symbol_flags::VALUE_MODULE))
== 0
{
continue;
}
if let Some(exports) = merged_symbol.exports.as_ref()
&& let Some(sym_id) = exports.get(member_name)
{
if can_resolve_aliases {
return Some(
self.resolve_alias_symbol(sym_id, visited_aliases)
.unwrap_or(sym_id),
);
}
return Some(sym_id);
}
if let Some(members) = merged_symbol.members.as_ref()
&& let Some(sym_id) = members.get(member_name)
{
if can_resolve_aliases {
return Some(
self.resolve_alias_symbol(sym_id, visited_aliases)
.unwrap_or(sym_id),
);
}
return Some(sym_id);
}
}
}
None
}
/// Inner implementation with cycle detection for module re-exports.
fn resolve_reexported_member_symbol_inner(
&self,
module_specifier: &str,
member_name: &str,
visited_aliases: &mut Vec<SymbolId>,
visited_modules: &mut rustc_hash::FxHashSet<(String, String)>,
) -> Option<SymbolId> {
// Cycle detection: check if we've already visited this (module, member) pair
let key = (module_specifier.to_string(), member_name.to_string());
if visited_modules.contains(&key) {
return None;
}
visited_modules.insert(key);
// First, check if it's a direct export from this module (ambient modules)
if let Some(module_exports) = self.ctx.binder.module_exports.get(module_specifier)
&& let Some(sym_id) = self.resolve_member_from_module_exports(
self.ctx.binder,
module_exports,
member_name,
visited_aliases,
)
{
return Some(sym_id);
}
// Cross-file resolution: use canonical file-key lookups via state_type_resolution.
if let Some(sym_id) = self.resolve_cross_file_export(module_specifier, member_name) {
return Some(
self.resolve_alias_symbol(sym_id, visited_aliases)
.unwrap_or(sym_id),
);
}
// Check for named re-exports: `export { foo } from 'bar'`
if let Some(file_reexports) = self.ctx.binder.reexports.get(module_specifier)
&& let Some((source_module, original_name)) = file_reexports.get(member_name)
{
let name_to_lookup = original_name.as_deref().unwrap_or(member_name);
return self.resolve_reexported_member_symbol_inner(
source_module,
name_to_lookup,
visited_aliases,
visited_modules,
);
}
// Check for wildcard re-exports: `export * from 'bar'`
// TSC behavior: If two `export *` declarations export the same name,
// that name is considered AMBIGUOUS and is NOT exported
// (unless explicitly re-exported by name, which is checked above).
if let Some(source_modules) = self.ctx.binder.wildcard_reexports.get(module_specifier) {
let mut found_result: Option<SymbolId> = None;
let mut found_count = 0;
for source_module in source_modules {
if let Some(sym_id) = self.resolve_reexported_member_symbol_inner(
source_module,
member_name,
visited_aliases,
visited_modules,
) {
found_count += 1;
if found_count == 1 {
found_result = Some(sym_id);
} else {
// Multiple sources export the same name - ambiguous, treat as not exported
return None;
}
}
}
if found_result.is_some() {
return found_result;
}
}
None
}
// =========================================================================
// Type Parameter Resolution
// =========================================================================
/// Look up a type parameter by name in the current type parameter scope.
///
/// Type parameters are scoped to their declaring generic (function, class, interface, etc.).
/// This function checks the current type parameter scope to resolve type parameter names.
pub(crate) fn lookup_type_parameter(&self, name: &str) -> Option<TypeId> {
self.ctx.type_parameter_scope.get(name).copied()
}
/// Get all type parameter bindings for passing to `TypeLowering`.
///
/// Returns a vector of (name, `TypeId`) pairs for all type parameters in scope.
pub(crate) fn get_type_param_bindings(&self) -> Vec<(tsz_common::interner::Atom, TypeId)> {
self.ctx
.type_parameter_scope
.iter()
.map(|(name, &type_id)| (self.ctx.types.intern_string(name), type_id))
.collect()
}
// =========================================================================
// Entity Name Resolution
// =========================================================================
/// Get the text representation of an entity name node.
///
/// Get the text representation of an expression (simple chains only).
/// Handles Identifiers and `PropertyAccessExpressions` (e.g., `a.b.c`).
pub(crate) fn expression_text(&self, idx: NodeIndex) -> Option<String> {
let node = self.ctx.arena.get(idx)?;
match node.kind {
k if k == SyntaxKind::Identifier as u16 => self
.ctx
.arena
.get_identifier(node)
.map(|ident| ident.escaped_text.clone()),
k if k == syntax_kind_ext::PROPERTY_ACCESS_EXPRESSION => {
let access = self.ctx.arena.get_access_expr(node)?;
let left = self.expression_text(access.expression)?;
let right = self.expression_text(access.name_or_argument)?;
Some(format!("{left}.{right}"))
}
_ => None,
}
}
/// Entity names can be simple identifiers or qualified names (e.g., `A.B.C`).
/// This function recursively builds the full text representation.
pub(crate) fn entity_name_text(&self, idx: NodeIndex) -> Option<String> {
let node = self.ctx.arena.get(idx)?;
if node.kind == SyntaxKind::Identifier as u16 {
return self
.ctx
.arena
.get_identifier(node)
.map(|ident| ident.escaped_text.clone());
}
if node.kind == syntax_kind_ext::QUALIFIED_NAME {
let qn = self.ctx.arena.get_qualified_name(node)?;
let left = self.entity_name_text(qn.left)?;
let right = self.entity_name_text(qn.right)?;
let mut combined = String::with_capacity(left.len() + 1 + right.len());
combined.push_str(&left);
combined.push('.');
combined.push_str(&right);
return Some(combined);
}
None
}
// =========================================================================
// Symbol Resolution for Lowering
// =========================================================================
/// Resolve a type symbol for type lowering.
///
/// Returns the symbol ID if the resolved symbol has the TYPE flag set.
/// Returns None for built-in types that have special handling in `TypeLowering`.
pub(crate) fn resolve_type_symbol_for_lowering(&self, idx: NodeIndex) -> Option<u32> {
// Skip built-in types that have special handling in TypeLowering
// These types use built-in TypeData representations instead of Refs
if let Some(node) = self.ctx.arena.get(idx)
&& let Some(ident) = self.ctx.arena.get_identifier(node)
&& is_compiler_managed_type(ident.escaped_text.as_str())
{
return None;
}
let sym_id = match self.resolve_qualified_symbol_in_type_position(idx) {
TypeSymbolResolution::Type(sym_id) => sym_id,
_ => return None,
};
let lib_binders = self.get_lib_binders();
let symbol = self.ctx.binder.get_symbol_with_libs(sym_id, &lib_binders)?;
((symbol.flags & symbol_flags::TYPE) != 0).then_some(sym_id.0)
}
/// Resolve a value symbol for type lowering.
///
/// Returns the symbol ID if the resolved symbol has VALUE or ALIAS flags set.
pub(crate) fn resolve_value_symbol_for_lowering(&self, idx: NodeIndex) -> Option<u32> {
if let Some(node) = self.ctx.arena.get(idx) {
if node.kind == SyntaxKind::Identifier as u16
&& let Some(sym_id) = self.resolve_identifier_symbol(idx)
&& self.alias_resolves_to_type_only(sym_id)
{
return None;
}
if node.kind == syntax_kind_ext::QUALIFIED_NAME {
let mut current = idx;
while let Some(node) = self.ctx.arena.get(current) {
if node.kind == SyntaxKind::Identifier as u16 {
if let Some(sym_id) = self.resolve_identifier_symbol(current)
&& self.alias_resolves_to_type_only(sym_id)
{
return None;
}
break;
}
if node.kind != syntax_kind_ext::QUALIFIED_NAME {
break;
}
let Some(qn) = self.ctx.arena.get_qualified_name(node) else {
break;
};
current = qn.left;
}
}
}
let sym_id = self.resolve_qualified_symbol(idx)?;
let lib_binders = self.get_lib_binders();
let symbol = self.ctx.binder.get_symbol_with_libs(sym_id, &lib_binders)?;
if symbol.is_type_only {
return None;
}
if (symbol.flags & (symbol_flags::VALUE | symbol_flags::ALIAS)) != 0 {
return Some(sym_id.0);
}
// The initial resolution found a TYPE-only symbol (e.g., `interface Promise<T>`
// from one lib file). But the VALUE declaration (`declare var Promise`) may
// exist in a different lib file. Search all lib binders by name for a symbol
// that has the VALUE flag. This handles declaration merging across lib files.
let name = self
.ctx
.arena
.get(idx)
.and_then(|n| self.ctx.arena.get_identifier(n))
.map(|i| i.escaped_text.as_str());
if let Some(name) = name {
// Check file_locals first (may have merged value from lib)
if let Some(val_sym_id) = self.ctx.binder.file_locals.get(name)
&& let Some(val_symbol) = self
.ctx
.binder
.get_symbol_with_libs(val_sym_id, &lib_binders)
&& (val_symbol.flags & (symbol_flags::VALUE | symbol_flags::ALIAS)) != 0
&& !val_symbol.is_type_only
{
return Some(val_sym_id.0);
}
// Search lib binders directly for a value declaration
for lib_binder in &lib_binders {
if let Some(val_sym_id) = lib_binder.file_locals.get(name)
&& let Some(val_symbol) = lib_binder.get_symbol(val_sym_id)
&& (val_symbol.flags & (symbol_flags::VALUE | symbol_flags::ALIAS)) != 0
&& !val_symbol.is_type_only
{
return Some(val_sym_id.0);
}
}
}
None
}
}