tsz-cli 0.1.9

CLI binaries for the tsz TypeScript compiler
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
//! Diagnostics collection, per-file checking, export hash computation,
//! and binder construction helpers for the compilation driver.

use super::*;

/// Load lib.d.ts files and create `LibContext` objects for the checker.
///
/// This function reuses already-loaded lib files from the binding phase, avoiding a second
/// parse/bind pass during checker setup.
pub(super) fn load_lib_files_for_contexts(lib_files: &[Arc<LibFile>]) -> Vec<LibContext> {
    if lib_files.is_empty() {
        return Vec::new();
    }

    let lib_contexts: Vec<LibContext> = lib_files
        .iter()
        .map(|lib| LibContext {
            arena: Arc::clone(&lib.arena),
            binder: Arc::clone(&lib.binder),
        })
        .collect();

    // Merge all lib binders into a single binder to avoid duplicate SymbolIds
    // (e.g., "Intl" declarations across multiple lib files).
    use tsz::binder::state::LibContext as BinderLibContext;
    let mut merged_binder = tsz::binder::BinderState::new();
    let binder_lib_contexts: Vec<_> = lib_contexts
        .iter()
        .map(|ctx| BinderLibContext {
            arena: Arc::clone(&ctx.arena),
            binder: Arc::clone(&ctx.binder),
        })
        .collect();
    merged_binder.merge_lib_contexts_into_binder(&binder_lib_contexts);

    vec![LibContext {
        // Keep a lib arena available for declaration lookups.
        arena: Arc::clone(&lib_contexts[0].arena),
        binder: Arc::new(merged_binder),
    }]
}

pub(super) fn collect_diagnostics(
    program: &MergedProgram,
    options: &ResolvedCompilerOptions,
    base_dir: &Path,
    cache: Option<&mut CompilationCache>,
    lib_contexts: &[LibContext],
) -> Vec<Diagnostic> {
    let _collect_span =
        tracing::info_span!("collect_diagnostics", files = program.files.len()).entered();
    let mut diagnostics: Vec<Diagnostic> = Vec::new();
    let mut used_paths = FxHashSet::default();
    let mut cache = cache;
    let mut resolution_cache = ModuleResolutionCache::default();
    let mut program_paths = FxHashSet::default();
    let mut canonical_to_file_name: FxHashMap<PathBuf, String> = FxHashMap::default();
    let mut canonical_to_file_idx: FxHashMap<PathBuf, usize> = FxHashMap::default();

    {
        let _span = tracing::info_span!("build_program_path_maps").entered();
        for (idx, file) in program.files.iter().enumerate() {
            let canonical = canonicalize_or_owned(Path::new(&file.file_name));
            program_paths.insert(canonical.clone());
            canonical_to_file_name.insert(canonical.clone(), file.file_name.clone());
            canonical_to_file_idx.insert(canonical, idx);
        }
    }

    // Pre-create all binders for cross-file resolution
    let all_binders: Arc<Vec<Arc<BinderState>>> = Arc::new({
        let _span = tracing::info_span!("build_cross_file_binders").entered();
        program
            .files
            .iter()
            .enumerate()
            .map(|(file_idx, file)| {
                Arc::new(create_cross_file_lookup_binder(file, program, file_idx))
            })
            .collect()
    });

    // Extract is_external_module from BoundFile to preserve state across file bindings
    // This fixes TS2664 which requires accurate per-file is_external_module values
    let is_external_module_by_file: Arc<rustc_hash::FxHashMap<String, bool>> = Arc::new(
        program
            .files
            .iter()
            .map(|file| (file.file_name.clone(), file.is_external_module))
            .collect(),
    );

    // Collect all arenas for cross-file resolution
    let all_arenas: Arc<Vec<Arc<NodeArena>>> = Arc::new({
        let _span = tracing::info_span!("collect_all_arenas").entered();
        program
            .files
            .iter()
            .map(|file| Arc::clone(&file.arena))
            .collect()
    });

    // Create ModuleResolver instance for proper error reporting (TS2834, TS2835, TS2792, etc.)
    let mut module_resolver = ModuleResolver::new(options);

    // Build resolved_module_paths map: (source_file_idx, specifier) -> target_file_idx
    // Also build resolved_module_errors map for specific error codes
    let mut resolved_module_paths: FxHashMap<(usize, String), usize> = FxHashMap::default();
    let mut resolved_module_specifiers: FxHashSet<(usize, String)> = FxHashSet::default();
    let mut resolved_module_errors: FxHashMap<
        (usize, String),
        tsz::checker::context::ResolutionError,
    > = FxHashMap::default();

    {
        let _span = tracing::info_span!("build_resolved_module_maps").entered();
        for (file_idx, file) in program.files.iter().enumerate() {
            let module_specifiers = collect_module_specifiers(&file.arena, file.source_file);
            let file_path = Path::new(&file.file_name);

            for (specifier, specifier_node, import_kind) in &module_specifiers {
                // Get span from the specifier node
                let span = if let Some(spec_node) = file.arena.get(*specifier_node) {
                    Span::new(spec_node.pos, spec_node.end)
                } else {
                    Span::new(0, 0) // Fallback for invalid nodes
                };

                // Always try ModuleResolver first to get specific error types (TS2834/TS2835/TS2792)
                match module_resolver.resolve_with_kind(specifier, file_path, span, *import_kind) {
                    Ok(resolved_module) => {
                        resolved_module_specifiers.insert((file_idx, specifier.clone()));
                        let canonical = canonicalize_or_owned(&resolved_module.resolved_path);
                        if let Some(&target_idx) = canonical_to_file_idx.get(&canonical) {
                            resolved_module_paths.insert((file_idx, specifier.clone()), target_idx);
                        }
                    }
                    Err(failure) => {
                        let mut resolved_override: Option<PathBuf> = None;
                        if let tsz::module_resolver::ResolutionFailure::JsxNotEnabled {
                            resolved_path,
                            ..
                        } = &failure
                        {
                            resolved_override = Some(resolved_path.clone());
                        }
                        // Check if this is NotFound and the old
                        // resolver would find it (virtual test files). In that case,
                        // validate Node16 rules before accepting the fallback.
                        if failure.is_not_found()
                            && let Some(resolved) = resolve_module_specifier(
                                file_path,
                                specifier,
                                options,
                                base_dir,
                                &mut resolution_cache,
                                &program_paths,
                            )
                        {
                            if std::env::var_os("TSZ_DEBUG_RESOLVE").is_some() {
                                tracing::debug!(
                                    "module specifier fallback success: file={} spec={} -> {}",
                                    file_path.display(),
                                    specifier,
                                    resolved.display()
                                );
                            }
                            // Validate Node16/NodeNext extension requirements for virtual files
                            let resolution_kind = options.effective_module_resolution();
                            let is_node16_or_next = matches!(
                                resolution_kind,
                                crate::config::ModuleResolutionKind::Node16
                                    | crate::config::ModuleResolutionKind::NodeNext
                            );

                            if is_node16_or_next {
                                // Check if importing file is ESM (by extension or path)
                                let file_path_str = file_path.to_string_lossy();
                                let importing_ext =
                                    tsz::module_resolver::ModuleExtension::from_path(file_path);
                                let is_esm = importing_ext.forces_esm()
                                    || file_path_str.ends_with(".mts")
                                    || file_path_str.ends_with(".mjs");

                                // Check if specifier has an extension
                                let specifier_has_extension =
                                    Path::new(specifier).extension().is_some();

                                // In Node16/NodeNext ESM mode, relative imports must have explicit extensions
                                // If the import is extensionless, TypeScript treats it as "cannot find module" (TS2307)
                                // even though the file exists, because ESM requires explicit extensions
                                if is_esm && !specifier_has_extension && specifier.starts_with('.')
                                {
                                    // Emit TS2307 error - module cannot be found with the exact specifier
                                    // (even though the file exists, ESM requires explicit extension)
                                    resolved_module_errors.insert(
                                            (file_idx, specifier.clone()),
                                            tsz::checker::context::ResolutionError {
                                                code: tsz::module_resolver::CANNOT_FIND_MODULE,
                                                message: format!(
                                                    "Cannot find module '{specifier}' or its corresponding type declarations."
                                                ),
                                            },
                                        );
                                    continue; // Don't add to resolved_modules - this is an error
                                }
                            }

                            // Fallback succeeded and passed validation - add to resolved paths
                            let canonical = canonicalize_or_owned(&resolved);
                            if let Some(&target_idx) = canonical_to_file_idx.get(&canonical) {
                                resolved_module_paths
                                    .insert((file_idx, specifier.clone()), target_idx);
                            }
                            continue; // Virtual file found and validated, skip error
                        }

                        // Check if this is a JSON module import without resolveJsonModule enabled
                        // If so, emit TS2732 instead of TS2307
                        let failure_to_use = if matches!(
                            failure,
                            tsz::module_resolver::ResolutionFailure::NotFound { .. }
                        ) && specifier.ends_with(".json")
                            && !options.resolve_json_module
                        {
                            // Create TS2732 error for JSON import without resolveJsonModule
                            tsz::module_resolver::ResolutionFailure::JsonModuleWithoutResolveJsonModule {
                                specifier: specifier.clone(),
                                containing_file: file_path.to_string_lossy().to_string(),
                                span,
                            }
                        } else {
                            failure
                        };

                        if std::env::var_os("TSZ_DEBUG_RESOLVE").is_some() {
                            tracing::debug!(
                                "module specifier resolution failed: file={} spec={} failure={:?}",
                                file_path.display(),
                                specifier,
                                failure_to_use
                            );
                        }

                        // Untyped JS module handling: When resolution fails but a JS
                        // file exists for this specifier (without declaration files),
                        // TypeScript treats it as an untyped module:
                        // - With noImplicitAny: emit TS7016 ("Could not find a declaration file")
                        // - Without noImplicitAny: silently treat as `any` (no error)
                        if matches!(
                            failure_to_use,
                            tsz::module_resolver::ResolutionFailure::NotFound { .. }
                                | tsz::module_resolver::ResolutionFailure::PackageJsonError { .. }
                        ) && let Some(js_path) =
                            module_resolver.probe_js_file(specifier, file_path, span, *import_kind)
                        {
                            if options.checker.no_implicit_any {
                                resolved_module_errors.insert(
                                        (file_idx, specifier.clone()),
                                        tsz::checker::context::ResolutionError {
                                            code: 7016,
                                            message: format!(
                                                "Could not find a declaration file for module '{}'. '{}' implicitly has an 'any' type.",
                                                specifier, js_path.display()
                                            ),
                                        },
                                    );
                            }
                            // Mark the module as "resolved" so the checker doesn't
                            // independently emit TS2307. The module is treated as
                            // untyped (type `any`) — no target file index needed.
                            resolved_module_specifiers.insert((file_idx, specifier.clone()));
                            continue;
                        }

                        // Convert ResolutionFailure to Diagnostic to get the error code and message
                        let mut diagnostic = failure_to_use.to_diagnostic();

                        // TypeScript emits TS2792 (instead of TS2307) for certain module kinds.
                        // These are "classic-like" module systems (AMD, System, UMD) and ES modules.
                        use tsz_common::common::ModuleKind;
                        let module_kind_prefers_2792 = matches!(
                            options.checker.module,
                            ModuleKind::System
                                | ModuleKind::AMD
                                | ModuleKind::UMD
                                | ModuleKind::ES2015
                                | ModuleKind::ES2020
                                | ModuleKind::ES2022
                                | ModuleKind::ESNext
                                | ModuleKind::Preserve
                        );
                        let has_explicit_extension =
                            std::path::Path::new(specifier).extension().is_some();
                        if diagnostic.code == tsz::module_resolver::CANNOT_FIND_MODULE
                            && !has_explicit_extension
                            && module_kind_prefers_2792
                            && options.effective_module_resolution()
                                != tsz::config::ModuleResolutionKind::Bundler
                        {
                            diagnostic.code = tsz::module_resolver::MODULE_RESOLUTION_MODE_MISMATCH;
                            diagnostic.message = format!(
                                "Cannot find module '{specifier}'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?"
                            );
                        }

                        resolved_module_errors.insert(
                            (file_idx, specifier.clone()),
                            tsz::checker::context::ResolutionError {
                                code: diagnostic.code,
                                message: diagnostic.message,
                            },
                        );

                        if resolved_override.is_some() {
                            // Mark as resolved to suppress TS2307, but don't map
                            // to a target file. For JsxNotEnabled, the resolved
                            // file shouldn't have its exports validated (which
                            // would cause spurious TS1192/TS2306 errors).
                            resolved_module_specifiers.insert((file_idx, specifier.clone()));
                        }
                    }
                }
            }
        }
    }

    let resolved_module_paths = Arc::new(resolved_module_paths);
    let resolved_module_specifiers = Arc::new(resolved_module_specifiers);
    let resolved_module_errors = Arc::new(resolved_module_errors);

    // Create a shared QueryCache for memoized evaluate_type/is_subtype_of calls.
    let query_cache = QueryCache::new(&program.type_interner);

    // Prime Array<T> base type with global augmentations before any file checks.
    if !program.files.is_empty() && !lib_contexts.is_empty() {
        let prime_idx = 0;
        let file = &program.files[prime_idx];
        let binder = parallel::create_binder_from_bound_file(file, program, prime_idx);
        let mut checker = CheckerState::with_options(
            &file.arena,
            &binder,
            &query_cache,
            file.file_name.clone(),
            &options.checker,
        );
        checker.ctx.set_lib_contexts(lib_contexts.to_vec());
        checker.ctx.set_actual_lib_file_count(lib_contexts.len());
        checker.ctx.set_all_arenas(Arc::clone(&all_arenas));
        checker.ctx.set_all_binders(Arc::clone(&all_binders));
        checker.prime_boxed_types();
    }

    // --- SMART INVALIDATION: Work Queue Algorithm ---
    // Only type-check files that have changed or depend on files with changed export signatures

    let mut work_queue: VecDeque<usize> = VecDeque::new();
    let mut checked_files: FxHashSet<usize> = FxHashSet::default();

    // Mark all files as used for cache cleanup
    for (idx, file) in program.files.iter().enumerate() {
        let file_path = PathBuf::from(&file.file_name);
        used_paths.insert(file_path.clone());

        // Check if file has cached type information
        // If no cache or cache miss, file needs to be checked
        let needs_check = cache
            .as_deref()
            .is_none_or(|c| !c.type_caches.contains_key(&file_path)); // No cache at all -> check everything

        if needs_check {
            work_queue.push_back(idx);
            checked_files.insert(idx);
        }
    }

    // --- FILE CHECKING ---
    //
    // Two paths:
    // 1. Non-cached (first build, CI): Check ALL files in parallel using rayon.
    //    No dependency cascade needed since we're checking everything.
    // 2. Cached (watch mode): Sequential work queue with export-hash-based
    //    dependency cascade for incremental invalidation.

    if cache.is_none() {
        // --- PARALLEL PATH: No cache, check all files concurrently ---
        let _parallel_span =
            tracing::info_span!("parallel_check_files", files = work_queue.len()).entered();

        // Pre-compute per-file module bridging (sequential, fast — uses resolved_module_paths)
        let per_file_binders: Vec<BinderState> = {
            let _prep_span = tracing::info_span!("prepare_binders").entered();
            work_queue
                .iter()
                .map(|&file_idx| {
                    let file = &program.files[file_idx];
                    let mut binder = create_binder_from_bound_file(file, program, file_idx);
                    let module_specifiers =
                        collect_module_specifiers(&file.arena, file.source_file);

                    // Bridge raw module specifiers to resolved export tables using
                    // the pre-computed resolved_module_paths map (no FS calls needed).
                    for (specifier, _, _) in &module_specifiers {
                        if let Some(&target_idx) =
                            resolved_module_paths.get(&(file_idx, specifier.clone()))
                        {
                            let target_file_name = &program.files[target_idx].file_name;
                            if let Some(exports) =
                                binder.module_exports.get(target_file_name).cloned()
                            {
                                binder.module_exports.insert(specifier.clone(), exports);
                            }
                            if let Some(wildcards) =
                                binder.wildcard_reexports.get(target_file_name).cloned()
                            {
                                binder
                                    .wildcard_reexports
                                    .insert(specifier.clone(), wildcards);
                            }
                            if let Some(reexports) = binder.reexports.get(target_file_name).cloned()
                            {
                                binder.reexports.insert(specifier.clone(), reexports);
                            }
                            if let Some(source_modules) =
                                binder.wildcard_reexports.get(target_file_name).cloned()
                            {
                                for source_module in source_modules {
                                    if let Some(&source_idx) = resolved_module_paths
                                        .get(&(target_idx, source_module.clone()))
                                    {
                                        let source_file_name = &program.files[source_idx].file_name;
                                        if let Some(exports) =
                                            binder.module_exports.get(source_file_name).cloned()
                                        {
                                            binder
                                                .module_exports
                                                .insert(source_module.clone(), exports);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    binder
                })
                .collect()
        };

        let work_items: Vec<usize> = work_queue.into_iter().collect();
        let no_check = options.no_check;
        let check_js = options.check_js;
        let skip_lib_check = options.skip_lib_check;
        let compiler_options = options.checker.clone();
        let lib_ctx_for_parallel = lib_contexts.to_vec();

        // Check all files in parallel — each file gets its own CheckerState.
        // TypeInterner (DashMap) and QueryCache (RwLock) are already thread-safe.
        #[cfg(not(target_arch = "wasm32"))]
        let file_results: Vec<Vec<Diagnostic>> = {
            use rayon::iter::{
                IndexedParallelIterator, IntoParallelIterator, IntoParallelRefIterator,
                ParallelIterator,
            };
            if work_items.len() <= 1 {
                work_items
                    .iter()
                    .zip(per_file_binders)
                    .map(|(&file_idx, binder)| {
                        let context = CheckFileForParallelContext {
                            file_idx,
                            binder,
                            program,
                            query_cache: &query_cache,
                            compiler_options: &compiler_options,
                            lib_contexts: &lib_ctx_for_parallel,
                            all_arenas: &all_arenas,
                            all_binders: &all_binders,
                            resolved_module_paths: &resolved_module_paths,
                            resolved_module_specifiers: &resolved_module_specifiers,
                            resolved_module_errors: &resolved_module_errors,
                            is_external_module_by_file: &is_external_module_by_file,
                            no_check,
                            check_js,
                            skip_lib_check,
                        };
                        check_file_for_parallel(context)
                    })
                    .collect()
            } else {
                tsz::parallel::ensure_rayon_global_pool();
                work_items
                    .par_iter()
                    .zip(per_file_binders.into_par_iter())
                    .map(|(&file_idx, binder)| {
                        let context = CheckFileForParallelContext {
                            file_idx,
                            binder,
                            program,
                            query_cache: &query_cache,
                            compiler_options: &compiler_options,
                            lib_contexts: &lib_ctx_for_parallel,
                            all_arenas: &all_arenas,
                            all_binders: &all_binders,
                            resolved_module_paths: &resolved_module_paths,
                            resolved_module_specifiers: &resolved_module_specifiers,
                            resolved_module_errors: &resolved_module_errors,
                            is_external_module_by_file: &is_external_module_by_file,
                            no_check,
                            check_js,
                            skip_lib_check,
                        };
                        check_file_for_parallel(context)
                    })
                    .collect()
            }
        };

        #[cfg(target_arch = "wasm32")]
        let file_results: Vec<Vec<Diagnostic>> = work_items
            .iter()
            .zip(per_file_binders.into_iter())
            .map(|(&file_idx, binder)| {
                let context = CheckFileForParallelContext {
                    file_idx,
                    binder,
                    program,
                    query_cache: &query_cache,
                    compiler_options: &compiler_options,
                    lib_contexts: &lib_ctx_for_parallel,
                    all_arenas: &all_arenas,
                    all_binders: &all_binders,
                    resolved_module_paths: &resolved_module_paths,
                    resolved_module_specifiers: &resolved_module_specifiers,
                    resolved_module_errors: &resolved_module_errors,
                    is_external_module_by_file: &is_external_module_by_file,
                    no_check,
                    check_js,
                    skip_lib_check,
                };
                check_file_for_parallel(context)
            })
            .collect();

        for file_diags in file_results {
            diagnostics.extend(file_diags);
        }
    } else {
        // --- SEQUENTIAL PATH: Cached build with dependency cascade ---

        // Process files in the work queue
        while let Some(file_idx) = work_queue.pop_front() {
            let file = &program.files[file_idx];
            let file_path = PathBuf::from(&file.file_name);

            let mut binder = create_binder_from_bound_file(file, program, file_idx);
            let module_specifiers = collect_module_specifiers(&file.arena, file.source_file);

            // Bridge multi-file module resolution for ES module imports.
            for (specifier, _, _) in &module_specifiers {
                if let Some(resolved) = resolve_module_specifier(
                    Path::new(&file.file_name),
                    specifier,
                    options,
                    base_dir,
                    &mut resolution_cache,
                    &program_paths,
                ) {
                    let canonical = canonicalize_or_owned(&resolved);
                    if let Some(target_file_name) = canonical_to_file_name.get(&canonical)
                        && let Some(exports) = binder.module_exports.get(target_file_name).cloned()
                    {
                        binder.module_exports.insert(specifier.clone(), exports);
                        if let Some(wildcards) =
                            binder.wildcard_reexports.get(target_file_name).cloned()
                        {
                            binder
                                .wildcard_reexports
                                .insert(specifier.clone(), wildcards);
                        }
                        if let Some(reexports) = binder.reexports.get(target_file_name).cloned() {
                            binder.reexports.insert(specifier.clone(), reexports);
                        }
                        if let Some(&target_idx) = canonical_to_file_idx.get(&canonical)
                            && let Some(source_modules) =
                                binder.wildcard_reexports.get(target_file_name).cloned()
                        {
                            for source_module in source_modules {
                                if let Some(&source_idx) =
                                    resolved_module_paths.get(&(target_idx, source_module.clone()))
                                {
                                    let source_file_name = &program.files[source_idx].file_name;
                                    if let Some(exports) =
                                        binder.module_exports.get(source_file_name).cloned()
                                    {
                                        binder
                                            .module_exports
                                            .insert(source_module.clone(), exports);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            let cached = cache
                .as_deref_mut()
                .and_then(|cache| cache.type_caches.remove(&file_path));
            let compiler_options = options.checker.clone();
            let mut checker = if let Some(cached) = cached {
                CheckerState::with_cache(
                    &file.arena,
                    &binder,
                    &query_cache,
                    file.file_name.clone(),
                    cached,
                    compiler_options,
                )
            } else {
                CheckerState::new(
                    &file.arena,
                    &binder,
                    &query_cache,
                    file.file_name.clone(),
                    compiler_options,
                )
            };
            checker.ctx.report_unresolved_imports = true;
            if !lib_contexts.is_empty() {
                checker.ctx.set_lib_contexts(lib_contexts.to_vec());
                checker.ctx.set_actual_lib_file_count(lib_contexts.len());
            }
            checker.ctx.set_all_arenas(Arc::clone(&all_arenas));
            checker.ctx.set_all_binders(Arc::clone(&all_binders));
            checker
                .ctx
                .set_resolved_module_paths(Arc::clone(&resolved_module_paths));
            checker
                .ctx
                .set_resolved_module_errors(Arc::clone(&resolved_module_errors));
            checker.ctx.set_current_file_idx(file_idx);
            checker.ctx.is_external_module_by_file = Some(Arc::clone(&is_external_module_by_file));

            // Build resolved_modules set for backward compatibility
            let mut resolved_modules = rustc_hash::FxHashSet::default();
            for (specifier, _, _) in &module_specifiers {
                if resolved_module_specifiers.contains(&(file_idx, specifier.clone()))
                    || resolved_module_paths.contains_key(&(file_idx, specifier.clone()))
                {
                    resolved_modules.insert(specifier.clone());
                } else if !resolved_module_errors.contains_key(&(file_idx, specifier.clone()))
                    && let Some(resolved) = resolve_module_specifier(
                        Path::new(&file.file_name),
                        specifier,
                        options,
                        base_dir,
                        &mut resolution_cache,
                        &program_paths,
                    )
                {
                    let canonical = canonicalize_or_owned(&resolved);
                    if program_paths.contains(&canonical) {
                        resolved_modules.insert(specifier.clone());
                    }
                }
            }
            checker.ctx.resolved_modules = Some(resolved_modules);
            checker.ctx.has_parse_errors = !file.parse_diagnostics.is_empty();
            // TS1009 (Trailing comma not allowed) is emitted by our parser but is a
            // checker grammar error in TSC (not in parseDiagnostics). Exclude it from
            // has_syntax_parse_errors so we match TSC's hasParseDiagnostics() behavior,
            // which is used to suppress TS1108/TS1105 grammar errors.
            checker.ctx.has_syntax_parse_errors = file
                .parse_diagnostics
                .iter()
                .any(|d| d.code != 1185 && d.code != 1009);
            checker.ctx.syntax_parse_error_positions = file
                .parse_diagnostics
                .iter()
                .filter(|d| d.code != 1185 && d.code != 1009)
                .map(|d| d.start)
                .collect();
            // Track whether the file has "real" syntax errors (actual parse
            // failures like missing tokens or invalid characters) vs grammar
            // checks (strict mode violations, decorator errors, etc.).
            // Real syntax errors broadly suppress TS2304 to match tsc behavior.
            checker.ctx.has_real_syntax_errors = file
                .parse_diagnostics
                .iter()
                .any(|d| is_real_syntax_error(d.code));
            let mut file_diagnostics = Vec::new();
            for parse_diagnostic in &file.parse_diagnostics {
                file_diagnostics.push(parse_diagnostic_to_checker(
                    &file.file_name,
                    parse_diagnostic,
                ));
            }
            // skipLibCheck: skip type checking of declaration files (.d.ts)
            if options.skip_lib_check && file.file_name.ends_with(".d.ts") {
                diagnostics.extend(file_diagnostics);
                continue;
            }

            // Note: We always run checking for all files (JS and TS).
            // TypeScript reports syntax/semantic errors like TS1210 (strict mode violations)
            // even for JS files without checkJs. Only type-level errors are gated by checkJs.
            if !options.no_check {
                let _check_span =
                    tracing::info_span!("check_file", file = %file.file_name).entered();
                checker.check_source_file(file.source_file);

                // Filter diagnostics for JS files without checkJs
                let is_js = is_js_file(Path::new(&file.file_name));
                let has_ts_check_pragma = js_file_has_ts_check_pragma(file);
                let has_ts_nocheck_pragma = js_file_has_ts_nocheck_pragma(file);
                let should_filter_type_errors =
                    is_js && (has_ts_nocheck_pragma || (!options.check_js && !has_ts_check_pragma));
                let mut checker_diagnostics = std::mem::take(&mut checker.ctx.diagnostics);

                if should_filter_type_errors {
                    // Keep syntax/semantic diagnostics and JS grammar diagnostics (TS8xxx).
                    checker_diagnostics
                        .retain(|diag| diag.code < 2000 || (8000..9000).contains(&diag.code));
                }

                file_diagnostics.extend(checker_diagnostics);
            }

            // Update the cache and check for export hash changes
            if let Some(c) = cache.as_deref_mut() {
                let new_hash = compute_export_hash(program, file, file_idx, &mut checker);
                let old_hash = c.export_hashes.get(&file_path).copied();

                c.type_caches
                    .insert(file_path.clone(), checker.extract_cache());
                c.diagnostics
                    .insert(file_path.clone(), file_diagnostics.clone());
                c.export_hashes.insert(file_path.clone(), new_hash);

                if old_hash != Some(new_hash)
                    && let Some(dependents) = c.reverse_dependencies.get(&file_path)
                {
                    for dep_path in dependents {
                        if let Some(&dep_idx) = canonical_to_file_idx.get(dep_path)
                            && checked_files.insert(dep_idx)
                        {
                            work_queue.push_back(dep_idx);
                            c.type_caches.remove(dep_path);
                            c.diagnostics.remove(dep_path);
                        }
                    }
                }
            } else {
                diagnostics.extend(file_diagnostics);
            }
        }
    }

    // Collect diagnostics from cache for all files
    // This includes both checked files (now in cache) and unchecked files (cached from previous run)
    if let Some(c) = cache.as_deref() {
        for file in &program.files {
            let file_path = PathBuf::from(&file.file_name);
            if let Some(cached_diags) = c.diagnostics.get(&file_path) {
                diagnostics.extend(cached_diags.clone());
            }
        }
    }

    // Cleanup unused entries from the cache
    if let Some(c) = cache {
        c.type_caches.retain(|path, _| used_paths.contains(path));
        c.diagnostics.retain(|path, _| used_paths.contains(path));
        c.export_hashes.retain(|path, _| used_paths.contains(path));
    }

    diagnostics.extend(detect_missing_tslib_helper_diagnostics(program, options));

    diagnostics
}

pub(super) fn detect_missing_tslib_helper_diagnostics(
    program: &MergedProgram,
    options: &ResolvedCompilerOptions,
) -> Vec<Diagnostic> {
    if !options.import_helpers {
        return Vec::new();
    }

    let tslib_file = program.files.iter().find(|file| {
        let path = file.file_name.replace('\\', "/");
        // Match tslib by directory or filename: the package's main declaration
        // file may be `tslib.d.ts` or `index.d.ts` inside a `tslib/` directory.
        path.contains("/tslib/")
            || Path::new(&file.file_name)
                .file_name()
                .and_then(|name| name.to_str())
                .is_some_and(|name| name.eq_ignore_ascii_case("tslib.d.ts"))
    });

    let mut result = Vec::new();

    match tslib_file {
        None => {
            // tslib module not found at all → TS2354 for each file needing helpers
            for file in &program.files {
                if file.file_name.ends_with(".d.ts") {
                    continue;
                }
                if let Some((_helper_name, start, length)) = first_required_helper(file) {
                    result.push(Diagnostic::error(
                        file.file_name.clone(),
                        start,
                        length,
                        "This syntax requires an imported helper but module 'tslib' cannot be found.".to_string(),
                        2354,
                    ));
                }
            }
        }
        Some(tslib_file) => {
            // tslib found but check if specific helper exists → TS2343
            let tslib_exports_empty = program
                .module_exports
                .get(&tslib_file.file_name)
                .is_none_or(tsz_binder::SymbolTable::is_empty);

            if !tslib_exports_empty {
                return Vec::new();
            }

            for file in &program.files {
                if file.file_name == tslib_file.file_name || file.file_name.ends_with(".d.ts") {
                    continue;
                }

                if let Some((helper_name, start, length)) = first_required_helper(file) {
                    result.push(Diagnostic::error(
                        file.file_name.clone(),
                        start,
                        length,
                        format!(
                            "This syntax requires an imported helper named '{helper_name}' which does not exist in 'tslib'. Consider upgrading your version of 'tslib'."
                        ),
                        2343,
                    ));
                }
            }
        }
    }

    result
}

pub(super) fn first_required_helper(file: &BoundFile) -> Option<(&'static str, u32, u32)> {
    let mut saw_await: Option<(u32, u32)> = None;
    let mut saw_yield: Option<(u32, u32)> = None;

    for node_idx_raw in 0..file.arena.len() {
        let node_idx = NodeIndex(node_idx_raw as u32);
        let Some(node) = file.arena.get(node_idx) else {
            continue;
        };

        if node.kind == SyntaxKind::PrivateIdentifier as u16 {
            return Some((
                "__classPrivateFieldSet",
                node.pos,
                node.end.saturating_sub(node.pos),
            ));
        }

        if node.kind == syntax_kind_ext::DECORATOR {
            return Some(("__decorate", node.pos, node.end.saturating_sub(node.pos)));
        }

        if node.kind == syntax_kind_ext::CLASS_DECLARATION
            && let Some(class_data) = file.arena.get_class(node)
            && class_data.heritage_clauses.is_some()
        {
            return Some(("__extends", node.pos, node.end.saturating_sub(node.pos)));
        }

        if node.kind == syntax_kind_ext::AWAIT_EXPRESSION {
            saw_await = Some((node.pos, node.end.saturating_sub(node.pos)));
        }
        if node.kind == syntax_kind_ext::YIELD_EXPRESSION {
            saw_yield = Some((node.pos, node.end.saturating_sub(node.pos)));
        }
    }

    if let (Some((start, length)), Some(_)) = (saw_await, saw_yield) {
        return Some(("__asyncGenerator", start, length));
    }

    None
}

pub(super) struct CheckFileForParallelContext<'a> {
    file_idx: usize,
    binder: BinderState,
    program: &'a MergedProgram,
    query_cache: &'a QueryCache<'a>,
    compiler_options: &'a tsz_common::CheckerOptions,
    lib_contexts: &'a [LibContext],
    all_arenas: &'a Arc<Vec<Arc<tsz::parser::node::NodeArena>>>,
    all_binders: &'a Arc<Vec<Arc<BinderState>>>,
    resolved_module_paths: &'a Arc<FxHashMap<(usize, String), usize>>,
    resolved_module_specifiers: &'a Arc<FxHashSet<(usize, String)>>,
    resolved_module_errors:
        &'a Arc<FxHashMap<(usize, String), tsz::checker::context::ResolutionError>>,
    is_external_module_by_file: &'a Arc<FxHashMap<String, bool>>,
    no_check: bool,
    check_js: bool,
    skip_lib_check: bool,
}

/// Check a single file for the parallel checking path.
///
/// This is extracted from the work queue loop so it can be called from rayon's `par_iter`.
/// Each invocation creates its own `CheckerState` (with its own mutable context),
/// while sharing thread-safe structures (`TypeInterner` via `DashMap`, `QueryCache` via `RwLock`).
pub(super) fn check_file_for_parallel<'a>(
    context: CheckFileForParallelContext<'a>,
) -> Vec<Diagnostic> {
    let CheckFileForParallelContext {
        file_idx,
        binder,
        program,
        query_cache,
        compiler_options,
        lib_contexts,
        all_arenas,
        all_binders,
        resolved_module_paths,
        resolved_module_specifiers,
        resolved_module_errors,
        is_external_module_by_file,
        no_check,
        check_js,
        skip_lib_check,
    } = context;
    let file = &program.files[file_idx];

    // skipLibCheck: skip type checking of declaration files (.d.ts)
    if skip_lib_check && file.file_name.ends_with(".d.ts") {
        return Vec::new();
    }
    let module_specifiers = collect_module_specifiers(&file.arena, file.source_file);

    // Build resolved_modules from the pre-computed resolved module maps
    let resolved_modules: FxHashSet<String> = module_specifiers
        .iter()
        .filter(|(specifier, _, _)| {
            resolved_module_paths.contains_key(&(file_idx, specifier.clone()))
                || resolved_module_specifiers.contains(&(file_idx, specifier.clone()))
        })
        .map(|(specifier, _, _)| specifier.clone())
        .collect();

    let mut checker = CheckerState::with_options(
        &file.arena,
        &binder,
        query_cache,
        file.file_name.clone(),
        compiler_options,
    );
    checker.ctx.report_unresolved_imports = true;

    if !lib_contexts.is_empty() {
        checker.ctx.set_lib_contexts(lib_contexts.to_vec());
        checker.ctx.set_actual_lib_file_count(lib_contexts.len());
    }

    checker.ctx.set_all_arenas(Arc::clone(all_arenas));
    checker.ctx.set_all_binders(Arc::clone(all_binders));
    checker
        .ctx
        .set_resolved_module_paths(Arc::clone(resolved_module_paths));
    checker
        .ctx
        .set_resolved_module_errors(Arc::clone(resolved_module_errors));
    checker.ctx.set_current_file_idx(file_idx);
    checker.ctx.is_external_module_by_file = Some(Arc::clone(is_external_module_by_file));
    checker.ctx.resolved_modules = Some(resolved_modules);
    checker.ctx.has_parse_errors = !file.parse_diagnostics.is_empty();
    // TS1009 (Trailing comma not allowed) is emitted by our parser but is a
    // checker grammar error in TSC (not in parseDiagnostics). Exclude it from
    // has_syntax_parse_errors so we match TSC's hasParseDiagnostics() behavior.
    checker.ctx.has_syntax_parse_errors = file
        .parse_diagnostics
        .iter()
        .any(|d| d.code != 1185 && d.code != 1009);
    checker.ctx.syntax_parse_error_positions = file
        .parse_diagnostics
        .iter()
        .filter(|d| d.code != 1185 && d.code != 1009)
        .map(|d| d.start)
        .collect();
    checker.ctx.has_real_syntax_errors = file
        .parse_diagnostics
        .iter()
        .any(|d| is_real_syntax_error(d.code));

    // Collect parse diagnostics
    let mut file_diagnostics: Vec<Diagnostic> = file
        .parse_diagnostics
        .iter()
        .map(|d| parse_diagnostic_to_checker(&file.file_name, d))
        .collect();

    // Note: We always run checking for all files (JS and TS).
    // TypeScript reports syntax/semantic errors like TS1210 (strict mode violations)
    // even for JS files without checkJs. Only type-level errors are gated by checkJs.
    if !no_check {
        checker.check_source_file(file.source_file);

        // Filter diagnostics for JS files without checkJs
        let is_js = is_js_file(Path::new(&file.file_name));
        let has_ts_check_pragma = js_file_has_ts_check_pragma(file);
        let has_ts_nocheck_pragma = js_file_has_ts_nocheck_pragma(file);
        let should_filter_type_errors =
            is_js && (has_ts_nocheck_pragma || (!check_js && !has_ts_check_pragma));
        let mut checker_diagnostics = std::mem::take(&mut checker.ctx.diagnostics);

        if should_filter_type_errors {
            // Keep syntax/semantic diagnostics and JS grammar diagnostics (TS8xxx).
            checker_diagnostics
                .retain(|diag| diag.code < 2000 || (8000..9000).contains(&diag.code));
        }

        file_diagnostics.extend(checker_diagnostics);
    }

    file_diagnostics
}

pub(super) fn compute_export_hash(
    program: &MergedProgram,
    file: &BoundFile,
    file_idx: usize,
    checker: &mut CheckerState,
) -> u64 {
    let mut formatter = TypeFormatter::with_symbols(&program.type_interner, &program.symbols);
    let mut hasher = FxHasher::default();
    let mut type_str_cache: FxHashMap<TypeId, String> = FxHashMap::default();

    if let Some(file_locals) = program.file_locals.get(file_idx) {
        let mut exports: Vec<(&String, SymbolId)> = file_locals
            .iter()
            .filter_map(|(name, &sym_id)| {
                is_exported_symbol(&program.symbols, sym_id).then_some((name, sym_id))
            })
            .collect();
        exports.sort_by(|left, right| left.0.cmp(right.0));

        for (name, sym_id) in exports {
            name.hash(&mut hasher);
            let type_id = checker.get_type_of_symbol(sym_id);
            let type_str = type_str_cache
                .entry(type_id)
                .or_insert_with(|| formatter.format(type_id));
            type_str.hash(&mut hasher);
        }
    }

    let mut export_signatures = Vec::new();
    collect_export_signatures(file, checker, &mut formatter, &mut export_signatures);
    export_signatures.sort();
    for signature in export_signatures {
        signature.hash(&mut hasher);
    }

    hasher.finish()
}

pub(super) fn js_file_has_ts_check_pragma(file: &BoundFile) -> bool {
    let Some(source) = file.arena.get_source_file_at(file.source_file) else {
        return false;
    };
    let text = source.text.as_ref().to_ascii_lowercase();
    let ts_check = text.rfind("@ts-check");
    let ts_no_check = text.rfind("@ts-nocheck");
    match (ts_check, ts_no_check) {
        (Some(check_idx), Some(no_check_idx)) => check_idx > no_check_idx,
        (Some(_), None) => true,
        _ => false,
    }
}

pub(super) fn js_file_has_ts_nocheck_pragma(file: &BoundFile) -> bool {
    let Some(source) = file.arena.get_source_file_at(file.source_file) else {
        return false;
    };
    source
        .text
        .as_ref()
        .to_ascii_lowercase()
        .contains("@ts-nocheck")
}

pub(super) fn is_exported_symbol(symbols: &tsz::binder::SymbolArena, sym_id: SymbolId) -> bool {
    let Some(symbol) = symbols.get(sym_id) else {
        return false;
    };
    symbol.is_exported || (symbol.flags & symbol_flags::EXPORT_VALUE) != 0
}

pub(super) fn collect_export_signatures(
    file: &BoundFile,
    checker: &mut CheckerState,
    formatter: &mut TypeFormatter,
    signatures: &mut Vec<String>,
) {
    let arena = &file.arena;
    let Some(source) = arena.get_source_file_at(file.source_file) else {
        return;
    };

    for &stmt_idx in &source.statements.nodes {
        let Some(stmt) = arena.get(stmt_idx) else {
            continue;
        };

        if let Some(export_decl) = arena.get_export_decl(stmt) {
            if export_decl.is_default_export {
                if let Some(signature) =
                    export_default_signature(export_decl.export_clause, checker, formatter)
                {
                    signatures.push(signature);
                }
                continue;
            }

            if export_decl.module_specifier.is_none() {
                if export_decl.export_clause.is_some() {
                    let clause_node = export_decl.export_clause;
                    if arena.get_named_imports_at(clause_node).is_some() {
                        collect_local_named_export_signatures(
                            arena,
                            file.source_file,
                            clause_node,
                            checker,
                            formatter,
                            export_type_prefix(export_decl.is_type_only),
                            signatures,
                        );
                    } else {
                        collect_exported_declaration_signatures(
                            arena,
                            clause_node,
                            checker,
                            formatter,
                            export_type_prefix(export_decl.is_type_only),
                            signatures,
                        );
                    }
                }
                continue;
            }

            let module_spec = arena
                .get_literal_text(export_decl.module_specifier)
                .unwrap_or("")
                .to_string();
            if export_decl.export_clause.is_none() {
                signatures.push(format!(
                    "{}*|{}",
                    export_type_prefix(export_decl.is_type_only),
                    module_spec
                ));
                continue;
            }

            let clause_node = export_decl.export_clause;
            if let Some(named) = arena.get_named_imports_at(clause_node) {
                let mut specifiers = Vec::new();
                for &spec_idx in &named.elements.nodes {
                    let Some(spec) = arena.get_specifier_at(spec_idx) else {
                        continue;
                    };
                    let name = arena.get_identifier_text(spec.name).unwrap_or("");
                    if spec.property_name.is_none() {
                        specifiers.push(name.to_string());
                    } else {
                        let property = arena.get_identifier_text(spec.property_name).unwrap_or("");
                        specifiers.push(format!("{property} as {name}"));
                    }
                }
                specifiers.sort();
                signatures.push(format!(
                    "{}{{{}}}|{}",
                    export_type_prefix(export_decl.is_type_only),
                    specifiers.join(","),
                    module_spec
                ));
            } else if let Some(name) = arena.get_identifier_text(clause_node) {
                signatures.push(format!(
                    "{}* as {}|{}",
                    export_type_prefix(export_decl.is_type_only),
                    name,
                    module_spec
                ));
            }

            continue;
        }

        if let Some(export_assignment) = arena.get_export_assignment(stmt)
            && export_assignment.expression.is_some()
        {
            let type_id = checker.get_type_of_node(export_assignment.expression);
            let type_str = formatter.format(type_id);
            signatures.push(format!("export=:{type_str}"));
        }
    }
}

pub(super) fn collect_local_named_export_signatures(
    arena: &NodeArena,
    source_file: NodeIndex,
    named_idx: NodeIndex,
    checker: &mut CheckerState,
    formatter: &mut TypeFormatter,
    type_prefix: &str,
    signatures: &mut Vec<String>,
) {
    let Some(named) = arena.get_named_imports_at(named_idx) else {
        return;
    };

    for &spec_idx in &named.elements.nodes {
        let Some(spec) = arena.get_specifier_at(spec_idx) else {
            continue;
        };
        let exported_name = if spec.name.is_some() {
            arena.get_identifier_text(spec.name).unwrap_or("")
        } else {
            arena.get_identifier_text(spec.property_name).unwrap_or("")
        };
        if exported_name.is_empty() {
            continue;
        }
        let local_name = if spec.property_name.is_some() {
            arena.get_identifier_text(spec.property_name).unwrap_or("")
        } else {
            exported_name
        };
        let type_id = find_local_declaration(arena, source_file, local_name)
            .map_or(TypeId::ANY, |decl_idx| checker.get_type_of_node(decl_idx));
        let type_str = formatter.format(type_id);
        signatures.push(format!("{type_prefix}{exported_name}:{type_str}"));
    }
}

pub(super) fn collect_exported_declaration_signatures(
    arena: &NodeArena,
    decl_idx: NodeIndex,
    checker: &mut CheckerState,
    formatter: &mut TypeFormatter,
    type_prefix: &str,
    signatures: &mut Vec<String>,
) {
    let Some(node) = arena.get(decl_idx) else {
        return;
    };

    if node.kind == syntax_kind_ext::VARIABLE_STATEMENT {
        if let Some(var_stmt) = arena.get_variable(node) {
            for &list_idx in &var_stmt.declarations.nodes {
                collect_exported_declaration_signatures(
                    arena,
                    list_idx,
                    checker,
                    formatter,
                    type_prefix,
                    signatures,
                );
            }
        }
        return;
    }

    if node.kind == syntax_kind_ext::VARIABLE_DECLARATION_LIST {
        if let Some(list) = arena.get_variable(node) {
            for &decl_idx in &list.declarations.nodes {
                collect_exported_declaration_signatures(
                    arena,
                    decl_idx,
                    checker,
                    formatter,
                    type_prefix,
                    signatures,
                );
            }
        }
        return;
    }

    if let Some(var_decl) = arena.get_variable_declaration(node) {
        if let Some(name) = arena.get_identifier_text(var_decl.name) {
            push_exported_signature(name, decl_idx, checker, formatter, type_prefix, signatures);
        }
        return;
    }

    if let Some(func) = arena.get_function(node) {
        if let Some(name) = arena.get_identifier_text(func.name) {
            push_exported_signature(name, decl_idx, checker, formatter, type_prefix, signatures);
        }
        return;
    }

    if let Some(class) = arena.get_class(node) {
        if let Some(name) = arena.get_identifier_text(class.name) {
            push_exported_signature(name, decl_idx, checker, formatter, type_prefix, signatures);
        }
        return;
    }

    if let Some(interface) = arena.get_interface(node) {
        if let Some(name) = arena.get_identifier_text(interface.name) {
            push_exported_signature(name, decl_idx, checker, formatter, type_prefix, signatures);
        }
        return;
    }

    if let Some(type_alias) = arena.get_type_alias(node) {
        if let Some(name) = arena.get_identifier_text(type_alias.name) {
            push_exported_signature(name, decl_idx, checker, formatter, type_prefix, signatures);
        }
        return;
    }

    if let Some(enum_decl) = arena.get_enum(node) {
        if let Some(name) = arena.get_identifier_text(enum_decl.name) {
            push_exported_signature(name, decl_idx, checker, formatter, type_prefix, signatures);
        }
        return;
    }

    if let Some(module_decl) = arena.get_module(node) {
        let name = arena
            .get_identifier_text(module_decl.name)
            .or_else(|| arena.get_literal_text(module_decl.name));
        if let Some(name) = name {
            push_exported_signature(name, decl_idx, checker, formatter, type_prefix, signatures);
        }
    }
}

pub(super) fn push_exported_signature(
    name: &str,
    decl_idx: NodeIndex,
    checker: &mut CheckerState,
    formatter: &mut TypeFormatter,
    type_prefix: &str,
    signatures: &mut Vec<String>,
) {
    let type_id = checker.get_type_of_node(decl_idx);
    let type_str = formatter.format(type_id);
    signatures.push(format!("{type_prefix}{name}:{type_str}"));
}

pub(super) fn find_local_declaration(
    arena: &NodeArena,
    source_file: NodeIndex,
    name: &str,
) -> Option<NodeIndex> {
    let source = arena.get_source_file_at(source_file)?;

    for &stmt_idx in &source.statements.nodes {
        let Some(stmt) = arena.get(stmt_idx) else {
            continue;
        };
        if let Some(export_decl) = arena.get_export_decl(stmt) {
            if export_decl.export_clause.is_none() {
                continue;
            }
            let clause_idx = export_decl.export_clause;
            if arena.get_named_imports_at(clause_idx).is_some() {
                continue;
            }
            if let Some(found) = find_local_declaration_in_node(arena, clause_idx, name) {
                return Some(found);
            }
            continue;
        }

        if let Some(found) = find_local_declaration_in_node(arena, stmt_idx, name) {
            return Some(found);
        }
    }

    None
}

pub(super) fn find_local_declaration_in_node(
    arena: &NodeArena,
    node_idx: NodeIndex,
    name: &str,
) -> Option<NodeIndex> {
    let node = arena.get(node_idx)?;

    if let Some(var_decl) = arena.get_variable_declaration(node) {
        if let Some(decl_name) = arena.get_identifier_text(var_decl.name)
            && decl_name == name
        {
            return Some(node_idx);
        }
        return None;
    }

    if node.kind == syntax_kind_ext::VARIABLE_STATEMENT {
        if let Some(var_stmt) = arena.get_variable(node) {
            for &list_idx in &var_stmt.declarations.nodes {
                if let Some(found) = find_local_declaration_in_node(arena, list_idx, name) {
                    return Some(found);
                }
            }
        }
        return None;
    }

    if node.kind == syntax_kind_ext::VARIABLE_DECLARATION_LIST {
        if let Some(list) = arena.get_variable(node) {
            for &decl_idx in &list.declarations.nodes {
                if let Some(found) = find_local_declaration_in_node(arena, decl_idx, name) {
                    return Some(found);
                }
            }
        }
        return None;
    }

    if let Some(func) = arena.get_function(node) {
        if let Some(decl_name) = arena.get_identifier_text(func.name)
            && decl_name == name
        {
            return Some(node_idx);
        }
        return None;
    }

    if let Some(class) = arena.get_class(node) {
        if let Some(decl_name) = arena.get_identifier_text(class.name)
            && decl_name == name
        {
            return Some(node_idx);
        }
        return None;
    }

    if let Some(interface) = arena.get_interface(node) {
        if let Some(decl_name) = arena.get_identifier_text(interface.name)
            && decl_name == name
        {
            return Some(node_idx);
        }
        return None;
    }

    if let Some(type_alias) = arena.get_type_alias(node) {
        if let Some(decl_name) = arena.get_identifier_text(type_alias.name)
            && decl_name == name
        {
            return Some(node_idx);
        }
        return None;
    }

    if let Some(enum_decl) = arena.get_enum(node) {
        if let Some(decl_name) = arena.get_identifier_text(enum_decl.name)
            && decl_name == name
        {
            return Some(node_idx);
        }
        return None;
    }

    if let Some(module_decl) = arena.get_module(node) {
        let decl_name = arena
            .get_identifier_text(module_decl.name)
            .or_else(|| arena.get_literal_text(module_decl.name));
        if let Some(decl_name) = decl_name
            && decl_name == name
        {
            return Some(node_idx);
        }
    }

    None
}

pub(super) fn export_default_signature(
    export_clause: NodeIndex,
    checker: &mut CheckerState,
    formatter: &mut TypeFormatter,
) -> Option<String> {
    if export_clause.is_none() {
        return None;
    }
    let type_id = if let Some(sym_id) = checker.ctx.binder.get_node_symbol(export_clause) {
        checker.get_type_of_symbol(sym_id)
    } else {
        checker.get_type_of_node(export_clause)
    };
    let type_str = formatter.format(type_id);
    Some(format!("default:{type_str}"))
}

pub(super) const fn export_type_prefix(is_type_only: bool) -> &'static str {
    if is_type_only { "type:" } else { "" }
}

pub(super) fn parse_diagnostic_to_checker(
    file_name: &str,
    diagnostic: &ParseDiagnostic,
) -> Diagnostic {
    Diagnostic::error(
        file_name.to_string(),
        diagnostic.start,
        diagnostic.length,
        diagnostic.message.clone(),
        diagnostic.code,
    )
}

pub(super) fn create_binder_from_bound_file(
    file: &BoundFile,
    program: &MergedProgram,
    file_idx: usize,
) -> BinderState {
    let mut file_locals = SymbolTable::new();

    if file_idx < program.file_locals.len() {
        for (name, &sym_id) in program.file_locals[file_idx].iter() {
            file_locals.set(name.clone(), sym_id);
        }
    }

    for (name, &sym_id) in program.globals.iter() {
        if !file_locals.has(name) {
            file_locals.set(name.clone(), sym_id);
        }
    }

    // Merge module augmentations from all files
    // When checking a file, we need access to augmentations from all other files
    let mut merged_module_augmentations: rustc_hash::FxHashMap<
        String,
        Vec<tsz::binder::ModuleAugmentation>,
    > = rustc_hash::FxHashMap::default();

    for other_file in &program.files {
        for (spec, augs) in &other_file.module_augmentations {
            merged_module_augmentations
                .entry(spec.clone())
                .or_default()
                .extend(augs.clone());
        }
    }

    // Merge global augmentations from all files
    // Each augmentation is tagged with its source arena for cross-file resolution.
    let mut merged_global_augmentations: rustc_hash::FxHashMap<
        String,
        Vec<tsz::binder::GlobalAugmentation>,
    > = rustc_hash::FxHashMap::default();

    for other_file in &program.files {
        for (name, decls) in &other_file.global_augmentations {
            merged_global_augmentations
                .entry(name.clone())
                .or_default()
                .extend(decls.iter().map(|aug| {
                    tsz::binder::GlobalAugmentation::with_arena(
                        aug.node,
                        Arc::clone(&other_file.arena),
                    )
                }));
        }
    }

    let mut binder = BinderState::from_bound_state_with_scopes_and_augmentations(
        BinderOptions::default(),
        program.symbols.clone(),
        file_locals,
        file.node_symbols.clone(),
        BinderStateScopeInputs {
            scopes: file.scopes.clone(),
            node_scope_ids: file.node_scope_ids.clone(),
            global_augmentations: merged_global_augmentations,
            module_augmentations: merged_module_augmentations,
            module_exports: program.module_exports.clone(),
            reexports: program.reexports.clone(),
            wildcard_reexports: program.wildcard_reexports.clone(),
            symbol_arenas: program.symbol_arenas.clone(),
            declaration_arenas: program.declaration_arenas.clone(),
            shorthand_ambient_modules: program.shorthand_ambient_modules.clone(),
            modules_with_export_equals: Default::default(),
            flow_nodes: file.flow_nodes.clone(),
            node_flow: file.node_flow.clone(),
            switch_clause_to_switch: file.switch_clause_to_switch.clone(),
            expando_properties: file.expando_properties.clone(),
        },
    );

    binder.declared_modules = program.declared_modules.clone();
    // Restore is_external_module from BoundFile to preserve per-file state
    binder.is_external_module = file.is_external_module;
    // Track lib-originating symbols so unused checking can skip them
    binder.lib_symbol_ids = program.lib_symbol_ids.clone();
    binder
}

/// Build a lightweight binder for cross-file symbol/export lookups.
///
/// This avoids cloning per-file node/scope/flow structures when populating
/// `CheckerContext::all_binders`, which only needs symbol/file-local/module-export data.
pub(super) fn create_cross_file_lookup_binder(
    file: &BoundFile,
    program: &MergedProgram,
    file_idx: usize,
) -> BinderState {
    let mut file_locals = SymbolTable::new();

    if file_idx < program.file_locals.len() {
        for (name, &sym_id) in program.file_locals[file_idx].iter() {
            file_locals.set(name.clone(), sym_id);
        }
    }

    for (name, &sym_id) in program.globals.iter() {
        if !file_locals.has(name) {
            file_locals.set(name.clone(), sym_id);
        }
    }

    // Keep cross-file binders intentionally tiny: they are only used to map
    // import targets to that file's locals/exports.
    let mut binder = BinderState::new();
    binder.file_locals = file_locals;
    binder.node_symbols = file.node_symbols.clone();
    binder.scopes = file.scopes.clone();
    binder.node_scope_ids = file.node_scope_ids.clone();
    binder.declared_modules = program.declared_modules.clone();
    binder.shorthand_ambient_modules = program.shorthand_ambient_modules.clone();
    if let Some(exports) = program.module_exports.get(&file.file_name) {
        binder
            .module_exports
            .insert(file.file_name.clone(), exports.clone());
    }
    for module_name in program
        .declared_modules
        .iter()
        .chain(program.shorthand_ambient_modules.iter())
    {
        if let Some(exports) = program.module_exports.get(module_name) {
            binder
                .module_exports
                .insert(module_name.clone(), exports.clone());
        }
    }
    // Copy re-export data for cross-file import validation.
    // Without this, `resolve_import_in_file` can't follow wildcard/named
    // re-export chains across binder boundaries.
    if let Some(wildcards) = program.wildcard_reexports.get(&file.file_name) {
        binder
            .wildcard_reexports
            .insert(file.file_name.clone(), wildcards.clone());
    }
    if let Some(reexports) = program.reexports.get(&file.file_name) {
        binder
            .reexports
            .insert(file.file_name.clone(), reexports.clone());
    }
    binder.is_external_module = file.is_external_module;
    binder
}

/// Classify a parse diagnostic code as a "real" syntax error (actual parse failure)
/// vs a grammar/semantic check emitted during parsing.
///
/// Real syntax errors indicate that the parser couldn't parse the source normally
/// and had to recover. tsc propagates `ThisNodeHasError` flags from these errors
/// to suppress cascading semantic errors like TS2304.
///
/// Grammar checks (e.g., strict mode violations, decorator errors) are emitted
/// during parsing but don't indicate parse failure — tsc still emits TS2304 for
/// undeclared names in these files.
fn is_real_syntax_error(code: u32) -> bool {
    matches!(
        code,
        1005  // '{0}' expected
        | 1009 // Trailing comma not allowed (sometimes real syntax error)
        | 1014 // A rest parameter must be last in a parameter list
        | 1036 // Statements are not allowed in ambient contexts
        | 1109 // Expression expected
        | 1110 // Type expected
        | 1126 // Unexpected end of text
        | 1127 // Invalid character
        | 1128 // Declaration or statement expected
        | 1129 // '{' or ';' expected
        | 1130 // '}' expected
        | 1131 // Property assignment expected
        | 1134 // Variable declaration expected
        | 1135 // Argument expression expected
        | 1136 // Property or signature expected
        | 1137 // Expression or comma expected
        | 1138 // Parameter declaration expected
        | 1141 // Type parameter declaration expected
        | 1146 // Declaration expected
        | 1155 // 'const' declarations must be initialized
        | 1160 // Unterminated template literal
        | 1161 // Unterminated regular expression literal
        | 1002 // Unterminated string literal
        | 1003 // Identifier expected
        | 1006 // A file cannot have a reference to itself
        | 1007 // The parser expected to find a '}' 
        | 1010 // 'while' expected
        | 1011 // '(' or '<' expected
        | 1012 // '{' expected
        | 1035 // Only ambient modules can use quoted names
        | 1101 // 'with' statements are not allowed in strict mode
        | 1103 // A character literal must contain exactly one character
        | 1121 // Octal literals are not allowed in strict mode
        | 1124 // Digit expected
        | 1144 // '{' or ';' expected
        | 1145 // '{' or JSX element expected
        | 1147 // Import declarations in a namespace cannot reference a module
        | 1164 // Computed property names are not allowed in enums
        | 1185 // Merge conflict marker encountered
        | 1191 // An import declaration cannot have modifiers
        | 1313 // 'else' is not allowed after rest element
        | 1351 // An identifier or keyword cannot immediately follow a numeric literal
        | 1357 // A default clause cannot appear more than once
        | 1378 // Top-level 'for await' loops are only allowed...
        | 1432 // 'await' expressions are only allowed within async functions
        | 1434 // Top-level 'await' expressions are only allowed...
    )
}