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
//! Class member declaration and accessibility validation helpers.
use crate::state::{CheckerState, MemberAccessInfo, MemberAccessLevel, MemberLookup};
use tsz_parser::parser::NodeIndex;
use tsz_parser::parser::node_flags;
use tsz_parser::parser::syntax_kind_ext;
use tsz_solver::TypeId;
impl<'a> CheckerState<'a> {
pub(crate) fn check_async_modifier_on_declaration(
&mut self,
modifiers: &Option<tsz_parser::parser::NodeList>,
) {
use crate::diagnostics::diagnostic_codes;
if let Some(async_mod_idx) = self.find_async_modifier(modifiers) {
self.error_at_node(
async_mod_idx,
"'async' modifier cannot be used here.",
diagnostic_codes::MODIFIER_CANNOT_BE_USED_HERE,
);
}
}
pub(crate) fn lookup_member_access_in_class(
&self,
class_idx: NodeIndex,
name: &str,
is_static: bool,
) -> MemberLookup {
let Some(node) = self.ctx.arena.get(class_idx) else {
return MemberLookup::NotFound;
};
let Some(class) = self.ctx.arena.get_class(node) else {
return MemberLookup::NotFound;
};
let mut accessor_access: Option<MemberAccessLevel> = None;
for &member_idx in &class.members.nodes {
let Some(member_node) = self.ctx.arena.get(member_idx) else {
continue;
};
match member_node.kind {
k if k == syntax_kind_ext::PROPERTY_DECLARATION => {
let Some(prop) = self.ctx.arena.get_property_decl(member_node) else {
continue;
};
if self.has_static_modifier(&prop.modifiers) != is_static {
continue;
}
let Some(prop_name) = self.get_property_name(prop.name) else {
continue;
};
if prop_name == name {
let access_level = if self.is_private_identifier_name(prop.name) {
Some(MemberAccessLevel::Private)
} else {
self.member_access_level_from_modifiers(&prop.modifiers)
};
return match access_level {
Some(level) => MemberLookup::Restricted(level),
None => MemberLookup::Public,
};
}
}
k if k == syntax_kind_ext::METHOD_DECLARATION => {
let Some(method) = self.ctx.arena.get_method_decl(member_node) else {
continue;
};
if self.has_static_modifier(&method.modifiers) != is_static {
continue;
}
let Some(method_name) = self.get_property_name(method.name) else {
continue;
};
if method_name == name {
let access_level = if self.is_private_identifier_name(method.name) {
Some(MemberAccessLevel::Private)
} else {
self.member_access_level_from_modifiers(&method.modifiers)
};
return match access_level {
Some(level) => MemberLookup::Restricted(level),
None => MemberLookup::Public,
};
}
}
k if k == syntax_kind_ext::GET_ACCESSOR || k == syntax_kind_ext::SET_ACCESSOR => {
let Some(accessor) = self.ctx.arena.get_accessor(member_node) else {
continue;
};
if self.has_static_modifier(&accessor.modifiers) != is_static {
continue;
}
let Some(accessor_name) = self.get_property_name(accessor.name) else {
continue;
};
if accessor_name == name {
let access_level = if self.is_private_identifier_name(accessor.name) {
Some(MemberAccessLevel::Private)
} else {
self.member_access_level_from_modifiers(&accessor.modifiers)
};
// Don't return immediately - a getter/setter pair may have
// different visibility. Use the most permissive level (tsc
// allows reads when getter is public even if setter is private).
match access_level {
Some(MemberAccessLevel::Private) | None => return MemberLookup::Public,
Some(level) => {
accessor_access = Some(match accessor_access {
// First accessor found
None | Some(MemberAccessLevel::Private) => level,
// If either accessor is non-private, use the most permissive level
Some(prev) => prev,
});
}
}
}
}
k if k == syntax_kind_ext::CONSTRUCTOR => {
if is_static {
continue;
}
let Some(ctor) = self.ctx.arena.get_constructor(member_node) else {
continue;
};
if ctor.body.is_none() {
continue;
}
for ¶m_idx in &ctor.parameters.nodes {
let Some(param_node) = self.ctx.arena.get(param_idx) else {
continue;
};
let Some(param) = self.ctx.arena.get_parameter(param_node) else {
continue;
};
if !self.has_parameter_property_modifier(¶m.modifiers) {
continue;
}
let Some(param_name) = self.get_property_name(param.name) else {
continue;
};
if param_name == name {
return match self.member_access_level_from_modifiers(¶m.modifiers) {
Some(level) => MemberLookup::Restricted(level),
None => MemberLookup::Public,
};
}
}
}
_ => {}
}
}
// If we found accessor(s) but didn't early-return Public, return
// the most permissive access level across getter/setter pair.
if let Some(level) = accessor_access {
return MemberLookup::Restricted(level);
}
MemberLookup::NotFound
}
pub(crate) fn find_member_access_info(
&self,
class_idx: NodeIndex,
name: &str,
is_static: bool,
) -> Option<MemberAccessInfo> {
use rustc_hash::FxHashSet;
let mut current = class_idx;
let mut visited: FxHashSet<NodeIndex> = FxHashSet::default();
while visited.insert(current) {
match self.lookup_member_access_in_class(current, name, is_static) {
MemberLookup::Restricted(level) => {
return Some(MemberAccessInfo {
level,
declaring_class_idx: current,
declaring_class_name: self.get_class_name_from_decl(current),
});
}
MemberLookup::Public => return None,
MemberLookup::NotFound => {
let base_idx = self.get_base_class_idx(current)?;
current = base_idx;
}
}
}
None
}
pub(crate) fn is_method_member_in_class_hierarchy(
&self,
class_idx: NodeIndex,
name: &str,
is_static: bool,
) -> Option<bool> {
use rustc_hash::FxHashSet;
let mut current = class_idx;
let mut visited: FxHashSet<NodeIndex> = FxHashSet::default();
while visited.insert(current) {
let node = self.ctx.arena.get(current)?;
let class = self.ctx.arena.get_class(node)?;
for &member_idx in &class.members.nodes {
let Some(member_node) = self.ctx.arena.get(member_idx) else {
continue;
};
match member_node.kind {
k if k == syntax_kind_ext::METHOD_DECLARATION => {
let Some(method) = self.ctx.arena.get_method_decl(member_node) else {
continue;
};
if self.has_static_modifier(&method.modifiers) != is_static {
continue;
}
if let Some(method_name) = self.get_property_name(method.name)
&& method_name == name
{
return Some(true);
}
}
k if k == syntax_kind_ext::PROPERTY_DECLARATION => {
let Some(prop) = self.ctx.arena.get_property_decl(member_node) else {
continue;
};
if self.has_static_modifier(&prop.modifiers) != is_static {
continue;
}
if let Some(prop_name) = self.get_property_name(prop.name)
&& prop_name == name
{
// Auto-accessors (`accessor x`) behave like accessor members
// for super-property access and should not trigger TS2855.
if self.has_accessor_modifier(&prop.modifiers) {
return Some(true);
}
// Ambient class members (from `declare class`) are not
// class field definitions — they don't use [[Define]]
// semantics, so super access is valid.
if self.is_ambient_declaration(current) {
return Some(true);
}
return Some(false);
}
}
k if k == syntax_kind_ext::GET_ACCESSOR
|| k == syntax_kind_ext::SET_ACCESSOR =>
{
let Some(accessor) = self.ctx.arena.get_accessor(member_node) else {
continue;
};
if self.has_static_modifier(&accessor.modifiers) != is_static {
continue;
}
if let Some(accessor_name) = self.get_property_name(accessor.name)
&& accessor_name == name
{
// In ES2015+, getters/setters are prototype methods accessible via super.
// In ES5/ES3, they are property descriptors and not accessible via super.
if self.ctx.compiler_options.target.is_es5() {
return Some(false);
}
return Some(true);
}
}
k if k == syntax_kind_ext::CONSTRUCTOR => {
if is_static {
continue;
}
let Some(ctor) = self.ctx.arena.get_constructor(member_node) else {
continue;
};
if ctor.body.is_none() {
continue;
}
for ¶m_idx in &ctor.parameters.nodes {
let Some(param_node) = self.ctx.arena.get(param_idx) else {
continue;
};
let Some(param) = self.ctx.arena.get_parameter(param_node) else {
continue;
};
if !self.has_parameter_property_modifier(¶m.modifiers) {
continue;
}
if let Some(param_name) = self.get_property_name(param.name)
&& param_name == name
{
return Some(false);
}
}
}
_ => {}
}
}
let base_idx = self.get_base_class_idx(current)?;
current = base_idx;
}
None
}
/// Recursively check a type node for parameter properties in function types.
/// Function types (like `(x: T) => R` or `new (x: T) => R`) cannot have parameter properties.
/// Walk a type node and emit TS2304 for unresolved type names inside complex types.
/// Check type for missing names, but skip top-level `TYPE_REFERENCE` nodes.
/// This is used when the caller will separately check `TYPE_REFERENCE` nodes
/// to avoid duplicate error emissions.
pub(crate) fn check_type_for_missing_names_skip_top_level_ref(&mut self, type_idx: NodeIndex) {
let Some(node) = self.ctx.arena.get(type_idx) else {
return;
};
use tsz_parser::parser::syntax_kind_ext;
// Skip TYPE_REFERENCE at top level to avoid duplicates
if node.kind == syntax_kind_ext::TYPE_REFERENCE {
return;
}
// For all other types, use the normal check
self.check_type_for_missing_names(type_idx);
}
pub(crate) fn check_type_for_missing_names(&mut self, type_idx: NodeIndex) {
let Some(node) = self.ctx.arena.get(type_idx) else {
return;
};
let factory = self.ctx.types.factory();
match node.kind {
k if k == syntax_kind_ext::TYPE_REFERENCE => {
let _ = self.get_type_from_type_reference(type_idx);
}
k if k == syntax_kind_ext::TYPE_QUERY => {
let _ = self.get_type_from_type_query(type_idx);
}
k if k == syntax_kind_ext::TYPE_LITERAL => {
if let Some(type_lit) = self.ctx.arena.get_type_literal(node) {
for &member_idx in &type_lit.members.nodes {
self.check_type_member_for_missing_names(member_idx);
}
}
}
k if k == syntax_kind_ext::FUNCTION_TYPE || k == syntax_kind_ext::CONSTRUCTOR_TYPE => {
if let Some(func_type) = self.ctx.arena.get_function_type(node) {
let updates =
self.push_missing_name_type_parameters(&func_type.type_parameters);
self.check_type_parameters_for_missing_names(&func_type.type_parameters);
self.check_duplicate_type_parameters(&func_type.type_parameters);
self.check_duplicate_parameters(&func_type.parameters, false);
for ¶m_idx in &func_type.parameters.nodes {
self.check_parameter_type_for_missing_names(param_idx);
}
let typeof_param_names =
self.push_typeof_params_from_ast_nodes(&func_type.parameters.nodes);
if func_type.type_annotation.is_some() {
self.check_type_for_missing_names(func_type.type_annotation);
}
self.pop_typeof_params_from_ast(typeof_param_names);
self.pop_type_parameters(updates);
}
}
k if k == syntax_kind_ext::ARRAY_TYPE => {
if let Some(arr) = self.ctx.arena.get_array_type(node) {
self.check_type_for_missing_names(arr.element_type);
}
}
k if k == syntax_kind_ext::TUPLE_TYPE => {
if let Some(tuple) = self.ctx.arena.get_tuple_type(node) {
for &elem_idx in &tuple.elements.nodes {
self.check_tuple_element_for_missing_names(elem_idx);
}
}
}
k if k == syntax_kind_ext::OPTIONAL_TYPE
|| k == syntax_kind_ext::REST_TYPE
|| k == syntax_kind_ext::PARENTHESIZED_TYPE =>
{
if let Some(wrapped) = self.ctx.arena.get_wrapped_type(node) {
self.check_type_for_missing_names(wrapped.type_node);
}
}
k if k == syntax_kind_ext::UNION_TYPE || k == syntax_kind_ext::INTERSECTION_TYPE => {
if let Some(composite) = self.ctx.arena.get_composite_type(node) {
for &member_idx in &composite.types.nodes {
self.check_type_for_missing_names(member_idx);
}
}
}
k if k == syntax_kind_ext::CONDITIONAL_TYPE => {
if let Some(cond) = self.ctx.arena.get_conditional_type(node) {
// Check check_type and extends_type first (infer type params not in scope yet)
self.check_type_for_missing_names(cond.check_type);
self.check_type_for_missing_names(cond.extends_type);
// Collect infer type parameters from extends_type and add them to scope for true_type
let infer_params = self.collect_infer_type_parameters(cond.extends_type);
let mut param_bindings = Vec::new();
for param_name in &infer_params {
let atom = self.ctx.types.intern_string(param_name);
let type_id = factory.type_param(tsz_solver::TypeParamInfo {
name: atom,
constraint: None,
default: None,
is_const: false,
});
let previous = self
.ctx
.type_parameter_scope
.insert(param_name.clone(), type_id);
param_bindings.push((param_name.clone(), previous));
}
// Check true_type with infer type parameters in scope
self.check_type_for_missing_names(cond.true_type);
// Remove infer type parameters from scope
for (name, previous) in param_bindings.into_iter().rev() {
if let Some(prev_type) = previous {
self.ctx.type_parameter_scope.insert(name, prev_type);
} else {
self.ctx.type_parameter_scope.remove(&name);
}
}
// Check false_type (infer type params not in scope)
self.check_type_for_missing_names(cond.false_type);
}
}
k if k == syntax_kind_ext::INFER_TYPE => {
if let Some(infer) = self.ctx.arena.get_infer_type(node) {
self.check_type_parameter_node_for_missing_names(infer.type_parameter);
}
}
k if k == syntax_kind_ext::TYPE_OPERATOR => {
if let Some(op) = self.ctx.arena.get_type_operator(node) {
self.check_type_for_missing_names(op.type_node);
}
}
k if k == syntax_kind_ext::INDEXED_ACCESS_TYPE => {
if let Some(indexed) = self.ctx.arena.get_indexed_access_type(node) {
self.check_type_for_missing_names(indexed.object_type);
self.check_type_for_missing_names(indexed.index_type);
}
}
k if k == syntax_kind_ext::MAPPED_TYPE => {
if let Some(mapped) = self.ctx.arena.get_mapped_type(node) {
// TS7039: Mapped object type implicitly has an 'any' template type.
if self.ctx.no_implicit_any() && mapped.type_node.is_none() {
let pos = node.pos;
let len = node.end.saturating_sub(node.pos);
self.ctx.error(
pos,
len,
"Mapped object type implicitly has an 'any' template type.".to_string(),
7039,
);
}
self.check_type_parameter_node_for_missing_names(mapped.type_parameter);
let mut param_binding: Option<(String, Option<TypeId>)> = None;
if let Some(param_node) = self.ctx.arena.get(mapped.type_parameter)
&& let Some(param) = self.ctx.arena.get_type_parameter(param_node)
&& let Some(name_node) = self.ctx.arena.get(param.name)
&& let Some(ident) = self.ctx.arena.get_identifier(name_node)
{
let name = ident.escaped_text.clone();
let atom = self.ctx.types.intern_string(&name);
let type_id = factory.type_param(tsz_solver::TypeParamInfo {
name: atom,
constraint: None,
default: None,
is_const: false,
});
let previous = self.ctx.type_parameter_scope.insert(name.clone(), type_id);
param_binding = Some((name, previous));
}
if mapped.name_type.is_some() {
self.check_type_for_missing_names(mapped.name_type);
}
if mapped.type_node.is_some() {
self.check_type_for_missing_names(mapped.type_node);
} else if self.ctx.no_implicit_any() {
// TS7039: Mapped object type implicitly has an 'any' template type
use crate::diagnostics::{diagnostic_codes, diagnostic_messages};
self.error_at_node(
type_idx,
diagnostic_messages::MAPPED_OBJECT_TYPE_IMPLICITLY_HAS_AN_ANY_TEMPLATE_TYPE,
diagnostic_codes::MAPPED_OBJECT_TYPE_IMPLICITLY_HAS_AN_ANY_TEMPLATE_TYPE,
);
}
if let Some(ref members) = mapped.members {
for &member_idx in &members.nodes {
self.check_type_member_for_missing_names(member_idx);
}
}
if let Some((name, previous)) = param_binding {
if let Some(prev_type) = previous {
self.ctx.type_parameter_scope.insert(name, prev_type);
} else {
self.ctx.type_parameter_scope.remove(&name);
}
}
}
}
k if k == syntax_kind_ext::TYPE_PREDICATE => {
if let Some(pred) = self.ctx.arena.get_type_predicate(node)
&& pred.type_node.is_some()
{
self.check_type_for_missing_names(pred.type_node);
}
}
k if k == syntax_kind_ext::TEMPLATE_LITERAL_TYPE => {
if let Some(template) = self.ctx.arena.get_template_literal_type(node) {
for &span_idx in &template.template_spans.nodes {
let Some(span_node) = self.ctx.arena.get(span_idx) else {
continue;
};
let Some(span) = self.ctx.arena.get_template_span(span_node) else {
continue;
};
self.check_type_for_missing_names(span.expression);
}
}
}
_ => {}
}
}
pub(crate) fn push_missing_name_type_parameters(
&mut self,
type_parameters: &Option<tsz_parser::parser::NodeList>,
) -> Vec<(String, Option<TypeId>, bool)> {
use tsz_solver::TypeParamInfo;
let Some(list) = type_parameters else {
return Vec::new();
};
let factory = self.ctx.types.factory();
let mut updates = Vec::new();
for ¶m_idx in &list.nodes {
let Some(param_node) = self.ctx.arena.get(param_idx) else {
continue;
};
let Some(param) = self.ctx.arena.get_type_parameter(param_node) else {
continue;
};
let Some(name_node) = self.ctx.arena.get(param.name) else {
continue;
};
let Some(ident) = self.ctx.arena.get_identifier(name_node) else {
continue;
};
let name = ident.escaped_text.clone();
let atom = self.ctx.types.intern_string(&name);
let type_id = factory.type_param(TypeParamInfo {
name: atom,
constraint: None,
default: None,
is_const: false,
});
let previous = self.ctx.type_parameter_scope.insert(name.clone(), type_id);
updates.push((name, previous, false));
}
updates
}
/// Push parameter names from an AST `Option<NodeList>` (signature parameters) into
/// `typeof_param_scope` so that `typeof paramName` in return types resolves without TS2304.
/// Returns the names pushed so they can be popped later.
fn push_typeof_params_from_ast_params(
&mut self,
params: &Option<tsz_parser::parser::NodeList>,
) -> Vec<String> {
let Some(list) = params else {
return Vec::new();
};
self.push_typeof_params_from_ast_nodes(&list.nodes)
}
/// Push parameter names from a slice of parameter `NodeIndex` values into `typeof_param_scope`.
fn push_typeof_params_from_ast_nodes(&mut self, nodes: &[NodeIndex]) -> Vec<String> {
let mut names = Vec::new();
for ¶m_idx in nodes {
let Some(param_node) = self.ctx.arena.get(param_idx) else {
continue;
};
let Some(param) = self.ctx.arena.get_parameter(param_node) else {
continue;
};
// Only handle simple identifier binding names (covers the common case).
let Some(name_node) = self.ctx.arena.get(param.name) else {
continue;
};
let Some(ident) = self.ctx.arena.get_identifier(name_node) else {
continue;
};
let name = ident.escaped_text.clone();
self.ctx
.typeof_param_scope
.insert(name.clone(), TypeId::ANY);
names.push(name);
}
names
}
/// Pop parameter names previously pushed by `push_typeof_params_from_ast_*`.
fn pop_typeof_params_from_ast(&mut self, names: Vec<String>) {
for name in names {
self.ctx.typeof_param_scope.remove(&name);
}
}
pub(crate) fn check_type_member_for_missing_names(&mut self, member_idx: NodeIndex) {
let Some(member_node) = self.ctx.arena.get(member_idx) else {
return;
};
if let Some(sig) = self.ctx.arena.get_signature(member_node) {
self.check_computed_property_name(sig.name);
let updates = self.push_missing_name_type_parameters(&sig.type_parameters);
self.check_type_parameters_for_missing_names(&sig.type_parameters);
self.check_duplicate_type_parameters(&sig.type_parameters);
if let Some(ref params) = sig.parameters {
self.check_duplicate_parameters(params, false);
for ¶m_idx in ¶ms.nodes {
self.check_parameter_type_for_missing_names(param_idx);
}
}
// Push parameter names into typeof_param_scope so that `typeof paramName`
// in return type annotations can resolve without emitting TS2304.
let typeof_param_names = self.push_typeof_params_from_ast_params(&sig.parameters);
if sig.type_annotation.is_some() {
self.check_type_for_missing_names(sig.type_annotation);
}
self.pop_typeof_params_from_ast(typeof_param_names);
self.pop_type_parameters(updates);
return;
}
if let Some(index_sig) = self.ctx.arena.get_index_signature(member_node) {
for ¶m_idx in &index_sig.parameters.nodes {
self.check_parameter_type_for_missing_names(param_idx);
}
if index_sig.type_annotation.is_some() {
self.check_type_for_missing_names(index_sig.type_annotation);
}
}
}
/// Check a type literal member for parameter properties (call/construct signatures).
pub(crate) fn check_type_member_for_parameter_properties(&mut self, member_idx: NodeIndex) {
let Some(node) = self.ctx.arena.get(member_idx) else {
return;
};
// Check call signatures and construct signatures for parameter properties
if node.kind == syntax_kind_ext::CALL_SIGNATURE
|| node.kind == syntax_kind_ext::CONSTRUCT_SIGNATURE
{
if let Some(sig) = self.ctx.arena.get_signature(node) {
if let Some(params) = &sig.parameters {
self.check_strict_mode_reserved_parameter_names(
¶ms.nodes,
member_idx,
false,
);
self.check_parameter_properties(¶ms.nodes);
// TS2371: Parameter initializers not allowed in call/construct signatures
self.check_non_impl_parameter_initializers(¶ms.nodes, false, false);
for (pi, ¶m_idx) in params.nodes.iter().enumerate() {
if let Some(param_node) = self.ctx.arena.get(param_idx)
&& let Some(param) = self.ctx.arena.get_parameter(param_node)
{
if param.type_annotation.is_some() {
self.check_type_for_parameter_properties(param.type_annotation);
}
self.maybe_report_implicit_any_parameter(param, false, pi);
}
}
}
// Recursively check the return type
self.check_type_for_parameter_properties(sig.type_annotation);
// TS7013/TS7020: Check for implicit any return type on construct/call signatures
if self.ctx.no_implicit_any() && sig.type_annotation.is_none() {
use crate::diagnostics::diagnostic_codes;
if node.kind == syntax_kind_ext::CONSTRUCT_SIGNATURE {
self.error_at_node(
member_idx,
"Construct signature, which lacks return-type annotation, implicitly has an 'any' return type.",
diagnostic_codes::CONSTRUCT_SIGNATURE_WHICH_LACKS_RETURN_TYPE_ANNOTATION_IMPLICITLY_HAS_AN_ANY_RET,
);
} else {
self.error_at_node(
member_idx,
"Call signature, which lacks return-type annotation, implicitly has an 'any' return type.",
diagnostic_codes::CALL_SIGNATURE_WHICH_LACKS_RETURN_TYPE_ANNOTATION_IMPLICITLY_HAS_AN_ANY_RETURN_T,
);
}
}
}
}
// Check method signatures in type literals
else if node.kind == syntax_kind_ext::METHOD_SIGNATURE {
if let Some(sig) = self.ctx.arena.get_signature(node) {
if let Some(params) = &sig.parameters {
self.check_strict_mode_reserved_parameter_names(
¶ms.nodes,
member_idx,
false,
);
self.check_parameter_properties(¶ms.nodes);
// TS2371: Parameter initializers not allowed in method signatures
self.check_non_impl_parameter_initializers(¶ms.nodes, false, false);
for (pi, ¶m_idx) in params.nodes.iter().enumerate() {
if let Some(param_node) = self.ctx.arena.get(param_idx)
&& let Some(param) = self.ctx.arena.get_parameter(param_node)
{
if param.type_annotation.is_some() {
self.check_type_for_parameter_properties(param.type_annotation);
}
self.maybe_report_implicit_any_parameter(param, false, pi);
}
}
}
self.check_type_for_parameter_properties(sig.type_annotation);
if self.ctx.no_implicit_any()
&& sig.type_annotation.is_none()
&& let Some(name) = self.property_name_for_error(sig.name)
{
use crate::diagnostics::diagnostic_codes;
self.error_at_node_msg(
sig.name,
diagnostic_codes::WHICH_LACKS_RETURN_TYPE_ANNOTATION_IMPLICITLY_HAS_AN_RETURN_TYPE,
&[&name, "any"],
);
}
}
}
// Check property signatures for implicit any (error 7008)
else if node.kind == syntax_kind_ext::PROPERTY_SIGNATURE {
if let Some(sig) = self.ctx.arena.get_signature(node) {
if sig.type_annotation.is_some() {
self.check_type_for_parameter_properties(sig.type_annotation);
}
// Property signature without type annotation implicitly has 'any' type
// Only emit TS7008 when noImplicitAny is enabled
if self.ctx.no_implicit_any()
&& sig.type_annotation.is_none()
&& let Some(member_name) = self.get_property_name(sig.name)
{
use crate::diagnostics::diagnostic_codes;
self.error_at_node_msg(
sig.name,
diagnostic_codes::MEMBER_IMPLICITLY_HAS_AN_TYPE,
&[&member_name, "any"],
);
}
}
}
// Check accessors in type literals/interfaces - cannot have body (error 1183)
else if (node.kind == syntax_kind_ext::GET_ACCESSOR
|| node.kind == syntax_kind_ext::SET_ACCESSOR)
&& let Some(accessor) = self.ctx.arena.get_accessor(node)
{
// Accessors in type literals and interfaces cannot have implementations
if accessor.body.is_some() {
use crate::diagnostics::diagnostic_codes;
// Report error on the body
self.error_at_node(
accessor.body,
"An implementation cannot be declared in ambient contexts.",
diagnostic_codes::AN_IMPLEMENTATION_CANNOT_BE_DECLARED_IN_AMBIENT_CONTEXTS,
);
}
}
}
/// Check that all method/constructor overload signatures have implementations.
/// Reports errors 2389, 2390, 2391, 1042.
pub(crate) fn check_class_member_implementations(&mut self, members: &[NodeIndex]) {
use crate::diagnostics::diagnostic_codes;
let mut i = 0;
while i < members.len() {
let member_idx = members[i];
let Some(node) = self.ctx.arena.get(member_idx) else {
i += 1;
continue;
};
match node.kind {
// TS1042: 'async' modifier cannot be used on getters/setters
syntax_kind_ext::GET_ACCESSOR | syntax_kind_ext::SET_ACCESSOR => {
if let Some(accessor) = self.ctx.arena.get_accessor(node)
&& self.has_async_modifier(&accessor.modifiers)
{
self.error_at_node(
member_idx,
"'async' modifier cannot be used here.",
diagnostic_codes::MODIFIER_CANNOT_BE_USED_HERE,
);
}
}
syntax_kind_ext::CONSTRUCTOR => {
if let Some(ctor) = self.ctx.arena.get_constructor(node)
&& ctor.body.is_none()
{
// Constructor overload signature - check for implementation
let has_impl = self.find_constructor_impl(members, i + 1);
if !has_impl {
self.error_at_node(
member_idx,
"Constructor implementation is missing.",
diagnostic_codes::CONSTRUCTOR_IMPLEMENTATION_IS_MISSING,
);
}
}
}
syntax_kind_ext::METHOD_DECLARATION => {
if let Some(method) = self.ctx.arena.get_method_decl(node) {
let flags = u32::from(node.flags);
if (flags & node_flags::THIS_NODE_HAS_ERROR) != 0
|| (flags & node_flags::THIS_NODE_OR_ANY_SUB_NODES_HAS_ERROR) != 0
{
continue;
}
// Abstract methods don't need implementations (they're meant for derived classes).
// Optional methods (g?(): T) also don't need implementations —
// they are standalone declarations, not overload signatures.
let is_abstract = self.has_abstract_modifier(&method.modifiers);
if method.body.is_none() && !is_abstract && !method.question_token {
// Method overload signature - check for implementation.
// TSC only reports TS2391 on the LAST overload in a consecutive
// group with the same name, so skip ahead to find it.
let method_name = self.get_method_name_from_node(member_idx);
if let Some(name) = method_name {
// Advance past consecutive bodyless method overloads with the same name.
let mut last_overload_i = i;
let mut j = i + 1;
while j < members.len() {
let next_idx = members[j];
let Some(next_node) = self.ctx.arena.get(next_idx) else {
break;
};
if next_node.kind == syntax_kind_ext::METHOD_DECLARATION
&& let Some(next_method) =
self.ctx.arena.get_method_decl(next_node)
&& next_method.body.is_none()
{
let next_name = self.get_method_name_from_node(next_idx);
if next_name.as_deref() == Some(name.as_str()) {
last_overload_i = j;
j += 1;
continue;
}
}
break;
}
// Report at the last overload in the group
let report_member_idx = members[last_overload_i];
let report_error_node = self
.ctx
.arena
.get(report_member_idx)
.and_then(|n| self.ctx.arena.get_method_decl(n))
.map(|m| m.name)
.filter(|n| n.is_some())
.unwrap_or(report_member_idx);
let (has_impl, impl_name, impl_idx) =
self.find_method_impl(members, last_overload_i + 1, &name);
if !has_impl {
self.error_at_node(
report_error_node,
"Function implementation is missing or not immediately following the declaration.",
diagnostic_codes::FUNCTION_IMPLEMENTATION_IS_MISSING_OR_NOT_IMMEDIATELY_FOLLOWING_THE_DECLARATION
);
} else if let Some(actual_name) = impl_name
&& actual_name != name
{
// Implementation has wrong name — report at the
// implementation's name node, and only on the last
// overload (the one immediately preceding the implementation).
let impl_member_idx = impl_idx.unwrap_or(last_overload_i + 1);
if impl_member_idx == last_overload_i + 1 {
let impl_node_idx = members[impl_member_idx];
let expected_display = self
.get_method_name_for_diagnostic(report_member_idx)
.unwrap_or_else(|| name.clone());
let impl_error_node = self
.ctx
.arena
.get(impl_node_idx)
.and_then(|n| self.ctx.arena.get_method_decl(n))
.map(|m| m.name)
.filter(|n| n.is_some())
.unwrap_or(impl_node_idx);
self.error_at_node(
impl_error_node,
&format!(
"Function implementation name must be '{expected_display}'."
),
diagnostic_codes::FUNCTION_IMPLEMENTATION_NAME_MUST_BE,
);
}
}
// Skip past all overloads we already processed
i = last_overload_i + 1;
continue;
}
}
}
}
_ => {}
}
i += 1;
}
}
/// Check that consecutive method declarations with the same name have consistent
/// static/instance modifiers (TS2387/TS2388).
///
/// TSC rule: for each consecutive pair of same-name method declarations within
/// an overload group, if their static-ness differs, emit an error on the second:
/// - TS2387 if it's instance but should be static
/// - TS2388 if it's static but shouldn't be
///
/// An overload group ends when we encounter an implementation (method with body).
/// After an implementation, the next declaration starts a new group even if
/// it has the same name.
pub(crate) fn check_static_instance_overload_consistency(&mut self, members: &[NodeIndex]) {
use crate::diagnostics::{diagnostic_codes, diagnostic_messages};
let mut prev_name: Option<String> = None;
let mut prev_is_static = false;
let mut prev_had_body = false;
for &member_idx in members {
let Some(node) = self.ctx.arena.get(member_idx) else {
prev_name = None;
prev_had_body = false;
continue;
};
if node.kind != syntax_kind_ext::METHOD_DECLARATION {
prev_name = None;
prev_had_body = false;
continue;
}
let Some(method) = self.ctx.arena.get_method_decl(node) else {
prev_name = None;
prev_had_body = false;
continue;
};
let cur_name = self.get_method_name_from_node(member_idx);
let cur_is_static = self.has_static_modifier(&method.modifiers);
let cur_has_body = method.body.is_some();
// Only compare within the same overload group.
// After an implementation (body), start a new group.
if !prev_had_body
&& let (Some(prev), Some(cur)) = (&prev_name, &cur_name)
&& prev == cur
&& cur_is_static != prev_is_static
{
let error_node = if method.name.is_some() {
method.name
} else {
member_idx
};
if cur_is_static {
self.error_at_node(
error_node,
diagnostic_messages::FUNCTION_OVERLOAD_MUST_NOT_BE_STATIC,
diagnostic_codes::FUNCTION_OVERLOAD_MUST_NOT_BE_STATIC,
);
} else {
self.error_at_node(
error_node,
diagnostic_messages::FUNCTION_OVERLOAD_MUST_BE_STATIC,
diagnostic_codes::FUNCTION_OVERLOAD_MUST_BE_STATIC,
);
}
}
prev_name = cur_name;
prev_is_static = cur_is_static;
prev_had_body = cur_has_body;
}
}
/// Report an error at a specific node.
/// Check an expression node for TS1359: await outside async function.
/// Recursively checks the expression tree for await expressions.
/// Report an error with context about a related symbol.
/// Check a class member (property, method, constructor, accessor).
pub(crate) fn check_class_member(&mut self, member_idx: NodeIndex) {
let Some(node) = self.ctx.arena.get(member_idx) else {
return;
};
let mut pushed_this = false;
if let Some(this_type) = self.class_member_this_type(member_idx) {
self.ctx.this_type_stack.push(this_type);
pushed_this = true;
}
let is_static_member = match node.kind {
k if k == syntax_kind_ext::PROPERTY_DECLARATION => self
.ctx
.arena
.get_property_decl(node)
.is_some_and(|decl| self.has_static_modifier(&decl.modifiers)),
k if k == syntax_kind_ext::METHOD_DECLARATION => self
.ctx
.arena
.get_method_decl(node)
.is_some_and(|decl| self.has_static_modifier(&decl.modifiers)),
k if k == syntax_kind_ext::GET_ACCESSOR || k == syntax_kind_ext::SET_ACCESSOR => self
.ctx
.arena
.get_accessor(node)
.is_some_and(|decl| self.has_static_modifier(&decl.modifiers)),
k if k == syntax_kind_ext::CLASS_STATIC_BLOCK_DECLARATION => true,
_ => false,
};
let prev_in_static_member = self
.ctx
.enclosing_class
.as_ref()
.map(|c| c.in_static_member)
.unwrap_or(false);
if let Some(ref mut class_info) = self.ctx.enclosing_class {
class_info.in_static_member = is_static_member;
}
self.check_class_member_name(member_idx);
self.check_class_member_decorator_expressions(member_idx);
// TS2302: Static members cannot reference class type parameters
self.check_static_member_for_class_type_param_refs(member_idx);
match node.kind {
syntax_kind_ext::PROPERTY_DECLARATION => {
self.check_property_declaration(member_idx);
}
syntax_kind_ext::METHOD_DECLARATION => {
self.check_method_declaration(member_idx);
}
syntax_kind_ext::CONSTRUCTOR => {
self.check_constructor_declaration(member_idx);
}
syntax_kind_ext::GET_ACCESSOR | syntax_kind_ext::SET_ACCESSOR => {
self.check_accessor_declaration(member_idx);
}
syntax_kind_ext::CLASS_STATIC_BLOCK_DECLARATION => {
// Static blocks contain statements that must be type-checked
if let Some(block) = self.ctx.arena.get_block(node) {
let prev_unreachable = self.ctx.is_unreachable;
let prev_reported = self.ctx.has_reported_unreachable;
// Check each statement in the block
for &stmt_idx in &block.statements.nodes {
self.check_statement(stmt_idx);
if !self.statement_falls_through(stmt_idx) {
self.ctx.is_unreachable = true;
}
}
self.ctx.is_unreachable = prev_unreachable;
self.ctx.has_reported_unreachable = prev_reported;
}
}
syntax_kind_ext::INDEX_SIGNATURE => {
// Index signatures are metadata used during type resolution, not members
// with their own types. They're handled separately by get_index_signatures.
// TS1071: Accessibility modifiers cannot appear on index signatures.
if let Some(index_sig) = self.ctx.arena.get_index_signature(node)
&& let Some(ref mods) = index_sig.modifiers
{
use crate::diagnostics::diagnostic_codes;
use tsz_scanner::SyntaxKind;
for &mod_idx in &mods.nodes {
if let Some(mod_node) = self.ctx.arena.get(mod_idx) {
let modifier_name = match mod_node.kind {
k if k == SyntaxKind::PublicKeyword as u16 => Some("public"),
k if k == SyntaxKind::PrivateKeyword as u16 => Some("private"),
k if k == SyntaxKind::ProtectedKeyword as u16 => Some("protected"),
k if k == SyntaxKind::ExportKeyword as u16 => Some("export"),
_ => None,
};
if let Some(name) = modifier_name {
self.error_at_node(
mod_idx,
&format!(
"'{name}' modifier cannot appear on an index signature."
),
diagnostic_codes::MODIFIER_CANNOT_APPEAR_ON_AN_INDEX_SIGNATURE,
);
}
}
}
}
}
_ => {
// Other class member types (semicolons, etc.)
self.get_type_of_node(member_idx);
}
}
if pushed_this {
self.ctx.this_type_stack.pop();
}
if let Some(ref mut class_info) = self.ctx.enclosing_class {
class_info.in_static_member = prev_in_static_member;
}
}
fn check_class_member_decorator_expressions(&mut self, member_idx: NodeIndex) {
let Some(node) = self.ctx.arena.get(member_idx) else {
return;
};
let (modifiers, parameters) = match node.kind {
k if k == syntax_kind_ext::PROPERTY_DECLARATION => self
.ctx
.arena
.get_property_decl(node)
.map_or((None, None), |decl| (decl.modifiers.as_ref(), None)),
k if k == syntax_kind_ext::METHOD_DECLARATION => self
.ctx
.arena
.get_method_decl(node)
.map_or((None, None), |decl| {
(decl.modifiers.as_ref(), Some(&decl.parameters))
}),
k if k == syntax_kind_ext::GET_ACCESSOR || k == syntax_kind_ext::SET_ACCESSOR => self
.ctx
.arena
.get_accessor(node)
.map_or((None, None), |decl| {
(decl.modifiers.as_ref(), Some(&decl.parameters))
}),
k if k == syntax_kind_ext::CONSTRUCTOR => self
.ctx
.arena
.get_constructor(node)
.map_or((None, None), |decl| {
(decl.modifiers.as_ref(), Some(&decl.parameters))
}),
_ => (None, None),
};
let is_abstract = modifiers.is_some_and(|m| {
m.nodes.iter().any(|&mod_idx| {
self.ctx
.arena
.get(mod_idx)
.is_some_and(|n| n.kind == tsz_scanner::SyntaxKind::AbstractKeyword as u16)
})
});
let is_ambient =
self.ctx
.enclosing_class
.as_ref()
.is_some_and(|c| c.is_declared)
|| modifiers.is_some_and(|m| {
m.nodes.iter().any(|&n| {
self.ctx.arena.get(n).is_some_and(|n| {
n.kind == tsz_scanner::SyntaxKind::DeclareKeyword as u16
})
})
});
let is_ambient_field = is_ambient && node.kind == syntax_kind_ext::PROPERTY_DECLARATION;
if let Some(modifiers) = modifiers {
for &modifier_idx in &modifiers.nodes {
let Some(modifier_node) = self.ctx.arena.get(modifier_idx) else {
continue;
};
if modifier_node.kind != syntax_kind_ext::DECORATOR {
continue;
}
if is_abstract
|| (!self.ctx.compiler_options.experimental_decorators && is_ambient_field)
{
use crate::diagnostics::diagnostic_codes;
if is_abstract && node.kind == syntax_kind_ext::METHOD_DECLARATION {
self.error_at_node(
modifier_idx,
"A decorator can only decorate a method implementation, not an overload.",
diagnostic_codes::A_DECORATOR_CAN_ONLY_DECORATE_A_METHOD_IMPLEMENTATION_NOT_AN_OVERLOAD,
);
} else {
self.error_at_node(
modifier_idx,
"Decorators are not valid here.",
diagnostic_codes::DECORATORS_ARE_NOT_VALID_HERE,
);
}
}
let Some(decorator) = self.ctx.arena.get_decorator(modifier_node) else {
continue;
};
self.get_type_of_node(decorator.expression);
}
}
if let Some(parameters) = parameters {
for ¶m_idx in ¶meters.nodes {
let Some(param_node) = self.ctx.arena.get(param_idx) else {
continue;
};
let Some(param) = self.ctx.arena.get_parameter(param_node) else {
continue;
};
if let Some(param_modifiers) = ¶m.modifiers {
for &modifier_idx in ¶m_modifiers.nodes {
let Some(modifier_node) = self.ctx.arena.get(modifier_idx) else {
continue;
};
if modifier_node.kind != syntax_kind_ext::DECORATOR {
continue;
}
if !self.ctx.compiler_options.experimental_decorators {
use crate::diagnostics::diagnostic_codes;
self.error_at_node(
modifier_idx,
"Decorators are not valid here.",
diagnostic_codes::DECORATORS_ARE_NOT_VALID_HERE,
);
}
if let Some(decorator) = self.ctx.arena.get_decorator(modifier_node) {
self.get_type_of_node(decorator.expression);
}
}
}
}
}
}
/// Check if a type node references class type parameters (TS2302).
/// Called for static members to ensure they don't reference the enclosing class's type params.
fn check_type_node_for_class_type_param_refs(
&mut self,
type_idx: NodeIndex,
class_type_param_names: &[String],
) {
use crate::diagnostics::diagnostic_codes;
if type_idx.is_none() || class_type_param_names.is_empty() {
return;
}
let Some(node) = self.ctx.arena.get(type_idx) else {
return;
};
match node.kind {
k if k == syntax_kind_ext::TYPE_REFERENCE => {
if let Some(type_ref) = self.ctx.arena.get_type_ref(node) {
// Check if type_name is an identifier matching a class type param
if let Some(name_node) = self.ctx.arena.get(type_ref.type_name)
&& let Some(ident) = self.ctx.arena.get_identifier(name_node)
&& class_type_param_names.contains(&ident.escaped_text)
{
self.error_at_node(
type_idx,
"Static members cannot reference class type parameters.",
diagnostic_codes::STATIC_MEMBERS_CANNOT_REFERENCE_CLASS_TYPE_PARAMETERS,
);
}
// Also check type arguments
if let Some(type_ref) = self.ctx.arena.get_type_ref(node)
&& let Some(ref type_args) = type_ref.type_arguments
{
for &arg_idx in &type_args.nodes {
self.check_type_node_for_class_type_param_refs(
arg_idx,
class_type_param_names,
);
}
}
}
}
k if k == syntax_kind_ext::ARRAY_TYPE => {
if let Some(arr) = self.ctx.arena.get_array_type(node) {
self.check_type_node_for_class_type_param_refs(
arr.element_type,
class_type_param_names,
);
}
}
k if k == syntax_kind_ext::TUPLE_TYPE => {
if let Some(tuple) = self.ctx.arena.get_tuple_type(node) {
for &elem_idx in &tuple.elements.nodes {
self.check_type_node_for_class_type_param_refs(
elem_idx,
class_type_param_names,
);
}
}
}
k if k == syntax_kind_ext::UNION_TYPE || k == syntax_kind_ext::INTERSECTION_TYPE => {
if let Some(composite) = self.ctx.arena.get_composite_type(node) {
for &member_idx in &composite.types.nodes {
self.check_type_node_for_class_type_param_refs(
member_idx,
class_type_param_names,
);
}
}
}
k if k == syntax_kind_ext::FUNCTION_TYPE || k == syntax_kind_ext::CONSTRUCTOR_TYPE => {
if let Some(func_type) = self.ctx.arena.get_function_type(node) {
// Exclude function type's own type parameters (they shadow class ones)
let own_params = self.collect_type_param_names(&func_type.type_parameters);
let filtered: Vec<String> = class_type_param_names
.iter()
.filter(|n| !own_params.contains(n))
.cloned()
.collect();
let names_to_check = if own_params.is_empty() {
class_type_param_names
} else if filtered.is_empty() {
return;
} else {
&filtered
};
for ¶m_idx in &func_type.parameters.nodes {
if let Some(param_node) = self.ctx.arena.get(param_idx)
&& let Some(param) = self.ctx.arena.get_parameter(param_node)
{
self.check_type_node_for_class_type_param_refs(
param.type_annotation,
names_to_check,
);
}
}
self.check_type_node_for_class_type_param_refs(
func_type.type_annotation,
names_to_check,
);
}
}
k if k == syntax_kind_ext::OPTIONAL_TYPE
|| k == syntax_kind_ext::REST_TYPE
|| k == syntax_kind_ext::PARENTHESIZED_TYPE =>
{
if let Some(wrapped) = self.ctx.arena.get_wrapped_type(node) {
self.check_type_node_for_class_type_param_refs(
wrapped.type_node,
class_type_param_names,
);
}
}
k if k == syntax_kind_ext::TYPE_LITERAL => {
if let Some(type_lit) = self.ctx.arena.get_type_literal(node) {
for &member_idx in &type_lit.members.nodes {
self.check_type_member_for_class_type_param_refs(
member_idx,
class_type_param_names,
);
}
}
}
_ => {}
}
}
/// Check a type literal member for class type parameter references.
fn check_type_member_for_class_type_param_refs(
&mut self,
member_idx: NodeIndex,
class_type_param_names: &[String],
) {
let Some(node) = self.ctx.arena.get(member_idx) else {
return;
};
if let Some(sig) = self.ctx.arena.get_signature(node) {
if let Some(ref params) = sig.parameters {
for ¶m_idx in ¶ms.nodes {
if let Some(param_node) = self.ctx.arena.get(param_idx)
&& let Some(param) = self.ctx.arena.get_parameter(param_node)
{
self.check_type_node_for_class_type_param_refs(
param.type_annotation,
class_type_param_names,
);
}
}
}
self.check_type_node_for_class_type_param_refs(
sig.type_annotation,
class_type_param_names,
);
}
}
/// Check a static class member for references to class type parameters (TS2302).
/// Collect type parameter names from a type parameter list.
fn collect_type_param_names(
&self,
type_parameters: &Option<tsz_parser::parser::NodeList>,
) -> Vec<String> {
let Some(list) = type_parameters else {
return Vec::new();
};
let mut names = Vec::new();
for ¶m_idx in &list.nodes {
if let Some(node) = self.ctx.arena.get(param_idx)
&& let Some(param) = self.ctx.arena.get_type_parameter(node)
&& let Some(name_node) = self.ctx.arena.get(param.name)
&& let Some(ident) = self.ctx.arena.get_identifier(name_node)
{
names.push(ident.escaped_text.clone());
}
}
names
}
/// Check a static class member for references to class type parameters (TS2302).
fn check_static_member_for_class_type_param_refs(&mut self, member_idx: NodeIndex) {
let class_type_param_names: Vec<String> = self
.ctx
.enclosing_class
.as_ref()
.map(|c| c.type_param_names.clone())
.unwrap_or_default();
if class_type_param_names.is_empty() {
return;
}
let Some(node) = self.ctx.arena.get(member_idx) else {
return;
};
match node.kind {
k if k == syntax_kind_ext::PROPERTY_DECLARATION => {
if let Some(prop) = self.ctx.arena.get_property_decl(node)
&& self.has_static_modifier(&prop.modifiers)
{
self.check_type_node_for_class_type_param_refs(
prop.type_annotation,
&class_type_param_names,
);
}
}
k if k == syntax_kind_ext::METHOD_DECLARATION => {
if let Some(method) = self.ctx.arena.get_method_decl(node)
&& self.has_static_modifier(&method.modifiers)
{
self.check_callable_for_class_type_param_refs(
&class_type_param_names,
&method.type_parameters,
&method.parameters,
method.type_annotation,
);
}
}
k if k == syntax_kind_ext::GET_ACCESSOR || k == syntax_kind_ext::SET_ACCESSOR => {
if let Some(accessor) = self.ctx.arena.get_accessor(node)
&& self.has_static_modifier(&accessor.modifiers)
{
self.check_callable_for_class_type_param_refs(
&class_type_param_names,
&accessor.type_parameters,
&accessor.parameters,
accessor.type_annotation,
);
}
}
_ => {}
}
}
/// Shared logic for checking a callable member (method/accessor) for class
/// type parameter references in its parameters and return type (TS2302).
fn check_callable_for_class_type_param_refs(
&mut self,
class_type_param_names: &[String],
type_parameters: &Option<tsz_parser::parser::NodeList>,
parameters: &tsz_parser::parser::NodeList,
type_annotation: NodeIndex,
) {
// Exclude the member's own type parameters (they shadow class ones)
let own_params = self.collect_type_param_names(type_parameters);
let filtered: Vec<String> = class_type_param_names
.iter()
.filter(|n| !own_params.contains(n))
.cloned()
.collect();
if filtered.is_empty() {
return;
}
for ¶m_idx in ¶meters.nodes {
if let Some(param_node) = self.ctx.arena.get(param_idx)
&& let Some(param) = self.ctx.arena.get_parameter(param_node)
{
self.check_type_node_for_class_type_param_refs(param.type_annotation, &filtered);
}
}
self.check_type_node_for_class_type_param_refs(type_annotation, &filtered);
}
/// Check that all method overload signatures in a group share the same abstract modifier
/// (TS2512: Overload signatures must all be abstract or non-abstract).
pub(crate) fn check_abstract_overload_consistency(
&mut self,
members: &[tsz_parser::parser::NodeIndex],
) {
use crate::diagnostics::{diagnostic_codes, diagnostic_messages};
use tsz_parser::parser::syntax_kind_ext;
let mut i = 0;
while i < members.len() {
let start_idx = i;
let start_member_idx = members[start_idx];
let Some(node) = self.ctx.arena.get(start_member_idx) else {
i += 1;
continue;
};
if node.kind != syntax_kind_ext::METHOD_DECLARATION {
i += 1;
continue;
}
let start_name = self.get_method_name_from_node(start_member_idx);
if start_name.is_none() {
i += 1;
continue;
}
// Collect the group of methods with the same name
let mut group = Vec::new();
let mut impl_index_in_group = None;
for &member_idx in members.iter().skip(start_idx) {
let Some(cur_node) = self.ctx.arena.get(member_idx) else {
break;
};
if cur_node.kind != syntax_kind_ext::METHOD_DECLARATION {
break;
}
let cur_name = self.get_method_name_from_node(member_idx);
if cur_name != start_name {
break;
}
let Some(method) = self.ctx.arena.get_method_decl(cur_node) else {
break;
};
let is_abstract = self.has_abstract_modifier(&method.modifiers);
let has_body = method.body.is_some();
let error_node = method.name;
if has_body && impl_index_in_group.is_none() {
impl_index_in_group = Some(group.len());
}
group.push((member_idx, is_abstract, has_body, error_node));
}
// Determine the "truth" abstractness for the group
if group.len() > 1 {
let truth_abstract = if let Some(idx) = impl_index_in_group {
group[idx].1
} else {
group[0].1
};
// Report TS2512 for any signature that differs
for &(_member_idx, is_abstract, _has_body, error_node) in &group {
if is_abstract != truth_abstract {
self.error_at_node(
error_node,
diagnostic_messages::OVERLOAD_SIGNATURES_MUST_ALL_BE_ABSTRACT_OR_NON_ABSTRACT,
diagnostic_codes::OVERLOAD_SIGNATURES_MUST_ALL_BE_ABSTRACT_OR_NON_ABSTRACT,
);
}
}
}
i += group.len();
}
}
/// Check that all declarations of an abstract method are consecutive (TS2516).
pub(crate) fn check_abstract_method_consecutive_declarations(
&mut self,
members: &[tsz_parser::parser::NodeIndex],
) {
use crate::diagnostics::{diagnostic_codes, diagnostic_messages};
use rustc_hash::{FxHashMap, FxHashSet};
use tsz_parser::parser::syntax_kind_ext;
// Map from (method_name, is_static) to the node index of its FIRST abstract declaration.
let mut first_abstract_decl: FxHashMap<(String, bool), tsz_parser::parser::NodeIndex> =
FxHashMap::default();
// Track the methods we've already emitted TS2516 for, to avoid duplicate errors.
let mut reported_methods: FxHashSet<(String, bool)> = FxHashSet::default();
let mut last_seen_method: Option<(String, bool)> = None;
for &member_idx in members {
let Some(node) = self.ctx.arena.get(member_idx) else {
last_seen_method = None;
continue;
};
// Only care about method declarations.
if node.kind != syntax_kind_ext::METHOD_DECLARATION {
last_seen_method = None;
continue;
}
let Some(method) = self.ctx.arena.get_method_decl(node) else {
last_seen_method = None;
continue;
};
let name = self.get_method_name_from_node(member_idx);
let is_static = self.has_static_modifier(&method.modifiers);
let is_abstract = self.has_abstract_modifier(&method.modifiers);
if let Some(name_str) = name {
let method_key = (name_str, is_static);
if is_abstract {
if let Some(&first_decl_node) = first_abstract_decl.get(&method_key) {
// We have seen an abstract declaration of this method before.
// If the last seen method wasn't this one, we have a discontinuity!
if last_seen_method.as_ref() != Some(&method_key)
&& reported_methods.insert(method_key.clone())
{
self.error_at_node(
first_decl_node,
diagnostic_messages::ALL_DECLARATIONS_OF_AN_ABSTRACT_METHOD_MUST_BE_CONSECUTIVE,
diagnostic_codes::ALL_DECLARATIONS_OF_AN_ABSTRACT_METHOD_MUST_BE_CONSECUTIVE,
);
}
} else {
// First time seeing an abstract declaration for this method key.
first_abstract_decl.insert(method_key.clone(), method.name);
}
}
// Update the last seen method key (even if it's non-abstract, as long as it's the same method group).
last_seen_method = Some(method_key);
} else {
last_seen_method = None;
}
}
}
pub(crate) fn check_for_static_member_class_type_param_reference(
&mut self,
sym_id: tsz_binder::SymbolId,
error_node: NodeIndex,
) {
use crate::diagnostics::diagnostic_codes;
use tsz_binder::symbol_flags;
// Must be in a class and inside a static member
let Some(enclosing_class) = self.ctx.enclosing_class.as_ref() else {
return;
};
if !enclosing_class.in_static_member {
return;
}
// Must be a type parameter
let Some(symbol) = self.ctx.binder.get_symbol(sym_id) else {
return;
};
if symbol.flags & symbol_flags::TYPE_PARAMETER == 0 {
return;
}
// Must be a type parameter of the enclosing class
let class_sym_id = self
.ctx
.binder
.node_symbols
.get(&enclosing_class.class_idx.0)
.copied();
// Is sym_id a type parameter of class_sym?
if let Some(class_sym) = class_sym_id
&& symbol.parent == class_sym
{
self.error_at_node(
error_node,
"Static members cannot reference class type parameters.",
diagnostic_codes::STATIC_MEMBERS_CANNOT_REFERENCE_CLASS_TYPE_PARAMETERS,
);
}
}
}