tldr-core 0.1.3

Core analysis engine for TLDR code analysis tool
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
//! JavaScript error analyzers -- 4 analyzers for Node.js runtime errors.
//!
//! Each analyzer is a pure function that takes a `ParsedError`, source code,
//! and a tree-sitter `Tree`, and returns an `Option<Diagnosis>`.
//!
//! # Analyzer Inventory (4 total)
//!
//! | # | Pattern                                      | Analyzer                      | Fix                                                | Confidence |
//! |---|----------------------------------------------|-------------------------------|----------------------------------------------------|------------|
//! | 1 | `ReferenceError: X is not defined`           | analyze_reference_error       | Inject `require()` or `import` for known modules   | MEDIUM     |
//! | 2 | `TypeError: X is not a function`             | analyze_type_error_not_function| Check if X exists as property; suggest access      | MEDIUM     |
//! | 3 | `TypeError: Cannot read properties of undefined`| analyze_type_error_undefined | Add optional chaining `?.` or null guard          | MEDIUM     |
//! | 4 | `SyntaxError`                                | analyze_syntax_error          | Common: missing comma, unclosed bracket, etc.      | LOW        |

use regex::Regex;
use tree_sitter::Tree;

use super::types::{Diagnosis, EditKind, Fix, FixConfidence, FixLocation, ParsedError, TextEdit};

// ============================================================================
// Known-module lookup table (data, not code)
// ============================================================================

/// Common Node.js builtin modules: maps a name to the `require()` statement.
///
/// Used by the ReferenceError analyzer to inject the correct require/import
/// when a module-level name is used without importing it.
static KNOWN_MODULES: &[(&str, &str)] = &[
    ("fs", "const fs = require('fs');"),
    ("path", "const path = require('path');"),
    ("os", "const os = require('os');"),
    ("http", "const http = require('http');"),
    ("https", "const https = require('https');"),
    ("url", "const url = require('url');"),
    ("crypto", "const crypto = require('crypto');"),
    ("util", "const util = require('util');"),
    ("stream", "const stream = require('stream');"),
    ("events", "const events = require('events');"),
    ("child_process", "const child_process = require('child_process');"),
    ("buffer", "const { Buffer } = require('buffer');"),
    ("Buffer", "const { Buffer } = require('buffer');"),
    ("querystring", "const querystring = require('querystring');"),
    ("assert", "const assert = require('assert');"),
    ("zlib", "const zlib = require('zlib');"),
    ("net", "const net = require('net');"),
    ("dns", "const dns = require('dns');"),
    ("tls", "const tls = require('tls');"),
    ("readline", "const readline = require('readline');"),
    ("cluster", "const cluster = require('cluster');"),
    ("worker_threads", "const { Worker } = require('worker_threads');"),
    ("process", "const process = require('process');"),
    ("timers", "const timers = require('timers');"),
    // Common npm packages
    ("express", "const express = require('express');"),
    ("lodash", "const _ = require('lodash');"),
    ("_", "const _ = require('lodash');"),
    ("axios", "const axios = require('axios');"),
    ("moment", "const moment = require('moment');"),
    ("chalk", "const chalk = require('chalk');"),
    ("commander", "const { Command } = require('commander');"),
    ("mongoose", "const mongoose = require('mongoose');"),
    ("pg", "const { Pool } = require('pg');"),
    ("redis", "const redis = require('redis');"),
    ("winston", "const winston = require('winston');"),
    ("dotenv", "const dotenv = require('dotenv');"),
    ("cors", "const cors = require('cors');"),
    ("helmet", "const helmet = require('helmet');"),
    ("jsonwebtoken", "const jwt = require('jsonwebtoken');"),
    ("jwt", "const jwt = require('jsonwebtoken');"),
    ("bcrypt", "const bcrypt = require('bcrypt');"),
    ("supertest", "const supertest = require('supertest');"),
    ("yargs", "const yargs = require('yargs');"),
    ("pino", "const pino = require('pino');"),
];

/// Common property-vs-method confusion patterns for TypeError.
///
/// Maps a misused name to (correct_access, description).
static PROPERTY_CORRECTIONS: &[(&str, &str, &str)] = &[
    ("length", ".length", "Access as a property, not a function call"),
    ("size", ".size", "Access as a property, not a function call"),
    ("name", ".name", "Access as a property, not a function call"),
    ("message", ".message", "Access as a property, not a function call"),
    ("constructor", ".constructor", "Access as a property, not a function call"),
    ("prototype", ".prototype", "Access as a property, not a function call"),
    ("__proto__", ".__proto__", "Access as a property, not a function call"),
    ("then", ".then()", "Call as a method on a Promise"),
    ("catch", ".catch()", "Call as a method on a Promise"),
    ("toString", ".toString()", "Call toString as a method"),
    ("valueOf", ".valueOf()", "Call valueOf as a method"),
];

// ============================================================================
// Top-level dispatcher
// ============================================================================

/// Dispatch to the correct JavaScript analyzer based on error pattern.
///
/// Returns `Some(Diagnosis)` if an analyzer handled the error, `None` otherwise.
pub fn diagnose_javascript(
    error: &ParsedError,
    source: &str,
    _tree: &Tree,
    _api_surface: Option<&()>,
) -> Option<Diagnosis> {
    let error_type = error.error_type.as_str();
    let msg = &error.message;

    match error_type {
        "ReferenceError" => analyze_reference_error(error, source),
        "TypeError" => {
            // Dispatch to the correct TypeError sub-analyzer
            if msg.contains("is not a function") {
                analyze_type_error_not_function(error, source)
            } else if msg.contains("Cannot read propert")
                || msg.contains("cannot read propert")
            {
                analyze_type_error_undefined(error, source)
            } else {
                // Generic TypeError -- not one of our specific patterns
                None
            }
        }
        "SyntaxError" => analyze_syntax_error(error, source),
        _ => None,
    }
}

/// Check whether a given error type has a registered JavaScript analyzer.
pub fn has_analyzer(error_type: &str) -> bool {
    matches!(
        error_type,
        "ReferenceError" | "TypeError:not_a_function" | "TypeError:undefined_property" | "SyntaxError"
    )
}

// ============================================================================
// Analyzer 1: ReferenceError -- X is not defined
// ============================================================================

/// Analyze `ReferenceError: X is not defined`.
///
/// This usually means a module-level identifier is used without requiring/importing it.
/// The fix is to inject the appropriate `require()` or `import` statement.
///
/// Handles:
/// - Known Node.js builtins via KNOWN_MODULES table
/// - Known npm packages via KNOWN_MODULES table
/// - Fallback: suggest require with inferred module name
fn analyze_reference_error(error: &ParsedError, source: &str) -> Option<Diagnosis> {
    let name = extract_js_name(&error.message, "is not defined")?;

    // Look up in KNOWN_MODULES table
    let require_stmt = KNOWN_MODULES
        .iter()
        .find(|(n, _)| *n == name)
        .map(|(_, stmt)| stmt.to_string())
        .unwrap_or_else(|| {
            // Fallback: generate a default require
            format!("const {} = require('{}');", name, name.to_lowercase())
        });

    let (new_text, insert_line) = inject_require_statement(source, &require_stmt)?;
    let edit_kind = require_edit_kind(source);

    let is_known = KNOWN_MODULES.iter().any(|(n, _)| *n == name);

    Some(Diagnosis {
        language: "javascript".to_string(),
        error_code: "ReferenceError".to_string(),
        message: format!(
            "'{}' is not defined -- missing require: {}",
            name, require_stmt
        ),
        location: error.line.map(|l| FixLocation {
            file: error.file.clone().unwrap_or_default(),
            line: l,
            column: error.column,
        }),
        confidence: if is_known {
            FixConfidence::Medium
        } else {
            FixConfidence::Low
        },
        fix: Some(Fix {
            description: format!("Add `{}`", require_stmt),
            edits: vec![TextEdit {
                line: insert_line,
                column: None,
                kind: edit_kind,
                new_text,
            }],
        }),
    })
}

// ============================================================================
// Analyzer 2: TypeError -- X is not a function
// ============================================================================

/// Analyze `TypeError: X is not a function`.
///
/// Checks if X exists as a property (not method) and suggests the correct
/// access pattern. For example, `arr.length()` should be `arr.length`.
fn analyze_type_error_not_function(error: &ParsedError, source: &str) -> Option<Diagnosis> {
    let name = extract_not_a_function_name(&error.message)?;

    // Check if it's a known property-vs-method confusion
    let correction = PROPERTY_CORRECTIONS
        .iter()
        .find(|(n, _, _)| *n == name);

    if let Some((_prop_name, correct_access, description)) = correction {
        if let Some(line_no) = error.line {
            let lines: Vec<&str> = source.lines().collect();
            if line_no > 0 && line_no <= lines.len() {
                let old_line = lines[line_no - 1];

                // Look for the pattern: name() and replace with name (remove parens)
                let call_pattern = format!(".{}()", name);
                let property_pattern = correct_access.to_string();

                if old_line.contains(&call_pattern) {
                    let new_line = old_line.replace(&call_pattern, &property_pattern);
                    return Some(Diagnosis {
                        language: "javascript".to_string(),
                        error_code: "TypeError".to_string(),
                        message: format!(
                            "'{}' is not a function -- {}",
                            name, description
                        ),
                        location: Some(FixLocation {
                            file: error.file.clone().unwrap_or_default(),
                            line: line_no,
                            column: error.column,
                        }),
                        confidence: FixConfidence::Medium,
                        fix: Some(Fix {
                            description: format!(
                                "Replace `.{}()` with `{}` at line {}",
                                name, correct_access, line_no
                            ),
                            edits: vec![TextEdit {
                                line: line_no,
                                column: None,
                                kind: EditKind::ReplaceLine,
                                new_text: new_line,
                            }],
                        }),
                    });
                }
            }
        }
    }

    // Fallback: unrecognized pattern -- still produce a diagnosis
    Some(Diagnosis {
        language: "javascript".to_string(),
        error_code: "TypeError".to_string(),
        message: format!(
            "'{}' is not a function -- check if it's a property or method name is misspelled",
            name
        ),
        location: error.line.map(|l| FixLocation {
            file: error.file.clone().unwrap_or_default(),
            line: l,
            column: error.column,
        }),
        confidence: FixConfidence::Low,
        fix: None,
    })
}

// ============================================================================
// Analyzer 3: TypeError -- Cannot read properties of undefined
// ============================================================================

/// Analyze `TypeError: Cannot read properties of undefined (reading 'X')`.
///
/// Fix: Add optional chaining `?.` or a null guard before the property access
/// on the offending line.
fn analyze_type_error_undefined(error: &ParsedError, source: &str) -> Option<Diagnosis> {
    let property = extract_reading_property(&error.message)?;

    if let Some(line_no) = error.line {
        let lines: Vec<&str> = source.lines().collect();
        if line_no > 0 && line_no <= lines.len() {
            let old_line = lines[line_no - 1];

            // Find the dot-access pattern `.property` and replace with `?.property`
            let dot_access = format!(".{}", property);
            let optional_access = format!("?.{}", property);

            if old_line.contains(&dot_access) && !old_line.contains(&optional_access) {
                // Replace the first occurrence of .property with ?.property
                let new_line = old_line.replacen(&dot_access, &optional_access, 1);

                return Some(Diagnosis {
                    language: "javascript".to_string(),
                    error_code: "TypeError".to_string(),
                    message: format!(
                        "Cannot read property '{}' of undefined -- add optional chaining `?.`",
                        property
                    ),
                    location: Some(FixLocation {
                        file: error.file.clone().unwrap_or_default(),
                        line: line_no,
                        column: error.column,
                    }),
                    confidence: FixConfidence::Medium,
                    fix: Some(Fix {
                        description: format!(
                            "Replace `.{}` with `?.{}` at line {}",
                            property, property, line_no
                        ),
                        edits: vec![TextEdit {
                            line: line_no,
                            column: None,
                            kind: EditKind::ReplaceLine,
                            new_text: new_line,
                        }],
                    }),
                });
            }

            // Handle bracket notation: [property] -> ?.[property]
            let bracket_access = format!("[\"{}\"]", property);
            let optional_bracket = format!("?.[\"{}\"]", property);

            if old_line.contains(&bracket_access) && !old_line.contains(&optional_bracket) {
                let new_line = old_line.replacen(&bracket_access, &optional_bracket, 1);

                return Some(Diagnosis {
                    language: "javascript".to_string(),
                    error_code: "TypeError".to_string(),
                    message: format!(
                        "Cannot read property '{}' of undefined -- add optional chaining `?.`",
                        property
                    ),
                    location: Some(FixLocation {
                        file: error.file.clone().unwrap_or_default(),
                        line: line_no,
                        column: error.column,
                    }),
                    confidence: FixConfidence::Medium,
                    fix: Some(Fix {
                        description: format!(
                            "Add optional chaining before `[\"{}\"]` at line {}",
                            property, line_no
                        ),
                        edits: vec![TextEdit {
                            line: line_no,
                            column: None,
                            kind: EditKind::ReplaceLine,
                            new_text: new_line,
                        }],
                    }),
                });
            }
        }
    }

    // Fallback: produce diagnostic without fix
    Some(Diagnosis {
        language: "javascript".to_string(),
        error_code: "TypeError".to_string(),
        message: format!(
            "Cannot read property '{}' of undefined -- add null check or optional chaining",
            property
        ),
        location: error.line.map(|l| FixLocation {
            file: error.file.clone().unwrap_or_default(),
            line: l,
            column: error.column,
        }),
        confidence: FixConfidence::Low,
        fix: None,
    })
}

// ============================================================================
// Analyzer 4: SyntaxError
// ============================================================================

/// Analyze `SyntaxError` patterns from Node.js.
///
/// Handles common SyntaxError patterns:
/// - Missing comma in object/array literal
/// - Unclosed bracket/brace/paren
/// - Unexpected token
/// - Unexpected end of input
///
/// SyntaxErrors are mostly diagnostic (low confidence) since the fix depends
/// heavily on context. We provide useful guidance rather than auto-fixes.
fn analyze_syntax_error(error: &ParsedError, source: &str) -> Option<Diagnosis> {
    let msg = &error.message;

    // Pattern: "Unexpected token X"
    if let Some(token) = extract_unexpected_token(msg) {
        return analyze_unexpected_token(error, source, &token);
    }

    // Pattern: "Unexpected end of input"
    if msg.contains("Unexpected end of input") {
        return analyze_unexpected_end(error, source);
    }

    // Pattern: "Unexpected identifier"
    if msg.contains("Unexpected identifier") {
        return analyze_unexpected_identifier(error, source);
    }

    // Pattern: "Missing initializer in const declaration"
    if msg.contains("Missing initializer in const") {
        return analyze_missing_initializer(error, source);
    }

    // Generic SyntaxError -- produce a diagnostic
    Some(Diagnosis {
        language: "javascript".to_string(),
        error_code: "SyntaxError".to_string(),
        message: format!("SyntaxError: {} -- review code at the error location", msg),
        location: error.line.map(|l| FixLocation {
            file: error.file.clone().unwrap_or_default(),
            line: l,
            column: error.column,
        }),
        confidence: FixConfidence::Low,
        fix: None,
    })
}

/// Analyze `Unexpected token X` SyntaxError.
///
/// Common patterns:
/// - Unexpected `}` -> missing opening brace or extra closing brace
/// - Unexpected `)` -> mismatched parentheses
/// - Unexpected `]` -> mismatched brackets
/// - Unexpected `,` at start of object -> trailing comma in previous line
fn analyze_unexpected_token(
    error: &ParsedError,
    source: &str,
    token: &str,
) -> Option<Diagnosis> {
    if let Some(line_no) = error.line {
        let lines: Vec<&str> = source.lines().collect();
        if line_no > 0 && line_no <= lines.len() {
            // Pattern: unexpected comma after opening brace/bracket or before closing
            // Likely a trailing comma issue on the previous line
            if token == "," && line_no > 1 {
                let prev_line = lines[line_no - 2];
                // Check if previous line ends with an unterminated expression
                let prev_trimmed = prev_line.trim_end();
                if !prev_trimmed.ends_with(',')
                    && !prev_trimmed.ends_with('{')
                    && !prev_trimmed.ends_with('[')
                    && !prev_trimmed.ends_with('(')
                    && !prev_trimmed.is_empty()
                {
                    let new_prev = format!("{},", prev_trimmed);
                    return Some(Diagnosis {
                        language: "javascript".to_string(),
                        error_code: "SyntaxError".to_string(),
                        message: format!(
                            "Unexpected token '{}' -- possibly missing comma on previous line",
                            token
                        ),
                        location: Some(FixLocation {
                            file: error.file.clone().unwrap_or_default(),
                            line: line_no,
                            column: error.column,
                        }),
                        confidence: FixConfidence::Low,
                        fix: Some(Fix {
                            description: format!(
                                "Add missing comma at end of line {}",
                                line_no - 1
                            ),
                            edits: vec![TextEdit {
                                line: line_no - 1,
                                column: None,
                                kind: EditKind::ReplaceLine,
                                new_text: new_prev,
                            }],
                        }),
                    });
                }
            }

            // Pattern: unexpected closing delimiter -- count brackets to diagnose
            if token == "}" || token == ")" || token == "]" {
                let (opens, closes) = count_delimiters(source, token.chars().next().unwrap());
                if closes > opens {
                    return Some(Diagnosis {
                        language: "javascript".to_string(),
                        error_code: "SyntaxError".to_string(),
                        message: format!(
                            "Unexpected '{}' -- extra closing delimiter (found {} opens, {} closes)",
                            token, opens, closes
                        ),
                        location: Some(FixLocation {
                            file: error.file.clone().unwrap_or_default(),
                            line: line_no,
                            column: error.column,
                        }),
                        confidence: FixConfidence::Low,
                        fix: None,
                    });
                }
            }

            // Generic unexpected token
            return Some(Diagnosis {
                language: "javascript".to_string(),
                error_code: "SyntaxError".to_string(),
                message: format!(
                    "Unexpected token '{}' at line {} -- check for missing semicolons, commas, or brackets",
                    token, line_no
                ),
                location: Some(FixLocation {
                    file: error.file.clone().unwrap_or_default(),
                    line: line_no,
                    column: error.column,
                }),
                confidence: FixConfidence::Low,
                fix: None,
            });
        }
    }

    Some(Diagnosis {
        language: "javascript".to_string(),
        error_code: "SyntaxError".to_string(),
        message: format!("Unexpected token '{}' -- review syntax near error location", token),
        location: error.line.map(|l| FixLocation {
            file: error.file.clone().unwrap_or_default(),
            line: l,
            column: error.column,
        }),
        confidence: FixConfidence::Low,
        fix: None,
    })
}

/// Analyze `Unexpected end of input` SyntaxError.
///
/// This typically means an unclosed bracket, brace, paren, or string literal.
fn analyze_unexpected_end(error: &ParsedError, source: &str) -> Option<Diagnosis> {
    // Count unmatched delimiters
    let mut brace_depth = 0i32;
    let mut paren_depth = 0i32;
    let mut bracket_depth = 0i32;

    for ch in source.chars() {
        match ch {
            '{' => brace_depth += 1,
            '}' => brace_depth -= 1,
            '(' => paren_depth += 1,
            ')' => paren_depth -= 1,
            '[' => bracket_depth += 1,
            ']' => bracket_depth -= 1,
            _ => {}
        }
    }

    let mut missing = Vec::new();
    if brace_depth > 0 {
        missing.push(format!("{} unclosed `{}`", brace_depth, '{'));
    }
    if paren_depth > 0 {
        missing.push(format!("{} unclosed `(`", paren_depth));
    }
    if bracket_depth > 0 {
        missing.push(format!("{} unclosed `[`", bracket_depth));
    }

    let detail = if missing.is_empty() {
        "possibly unclosed string literal or template literal".to_string()
    } else {
        missing.join(", ")
    };

    let total_lines = source.lines().count();

    // If there's a single unclosed brace, suggest adding `}` at the end
    let fix = if brace_depth == 1 && paren_depth == 0 && bracket_depth == 0 {
        Some(Fix {
            description: format!("Add closing `}}` at end of file (line {})", total_lines),
            edits: vec![TextEdit {
                line: total_lines,
                column: None,
                kind: EditKind::InsertAfter,
                new_text: "}".to_string(),
            }],
        })
    } else {
        None
    };

    Some(Diagnosis {
        language: "javascript".to_string(),
        error_code: "SyntaxError".to_string(),
        message: format!("Unexpected end of input -- {}", detail),
        location: error.line.map(|l| FixLocation {
            file: error.file.clone().unwrap_or_default(),
            line: l,
            column: error.column,
        }),
        confidence: FixConfidence::Low,
        fix,
    })
}

/// Analyze `Unexpected identifier` SyntaxError.
///
/// This often means a missing semicolon, comma, or operator on the previous line.
fn analyze_unexpected_identifier(error: &ParsedError, source: &str) -> Option<Diagnosis> {
    if let Some(line_no) = error.line {
        let lines: Vec<&str> = source.lines().collect();
        if line_no > 1 && line_no <= lines.len() {
            let prev_line = lines[line_no - 2].trim_end();

            // Check if previous line looks like it's missing a semicolon
            if !prev_line.ends_with(';')
                && !prev_line.ends_with('{')
                && !prev_line.ends_with('}')
                && !prev_line.ends_with(',')
                && !prev_line.ends_with('(')
                && !prev_line.is_empty()
                && !prev_line.starts_with("//")
                && !prev_line.starts_with("/*")
            {
                return Some(Diagnosis {
                    language: "javascript".to_string(),
                    error_code: "SyntaxError".to_string(),
                    message: format!(
                        "Unexpected identifier at line {} -- possibly missing semicolon on line {}",
                        line_no,
                        line_no - 1
                    ),
                    location: Some(FixLocation {
                        file: error.file.clone().unwrap_or_default(),
                        line: line_no,
                        column: error.column,
                    }),
                    confidence: FixConfidence::Low,
                    fix: None,
                });
            }
        }
    }

    Some(Diagnosis {
        language: "javascript".to_string(),
        error_code: "SyntaxError".to_string(),
        message: "Unexpected identifier -- check for missing operators, semicolons, or commas"
            .to_string(),
        location: error.line.map(|l| FixLocation {
            file: error.file.clone().unwrap_or_default(),
            line: l,
            column: error.column,
        }),
        confidence: FixConfidence::Low,
        fix: None,
    })
}

/// Analyze `Missing initializer in const declaration` SyntaxError.
fn analyze_missing_initializer(error: &ParsedError, source: &str) -> Option<Diagnosis> {
    if let Some(line_no) = error.line {
        let lines: Vec<&str> = source.lines().collect();
        if line_no > 0 && line_no <= lines.len() {
            let old_line = lines[line_no - 1];
            let trimmed = old_line.trim();

            // Pattern: `const x;` -> suggest `const x = undefined;` or switch to `let`
            if trimmed.starts_with("const ") && trimmed.ends_with(';') {
                let var_name: String = trimmed
                    .trim_start_matches("const ")
                    .trim_end_matches(';')
                    .trim()
                    .to_string();

                let new_line = old_line.replace(
                    trimmed,
                    &format!("let {};", var_name),
                );

                return Some(Diagnosis {
                    language: "javascript".to_string(),
                    error_code: "SyntaxError".to_string(),
                    message: format!(
                        "Missing initializer in const declaration '{}' -- use `let` instead or add an initializer",
                        var_name
                    ),
                    location: Some(FixLocation {
                        file: error.file.clone().unwrap_or_default(),
                        line: line_no,
                        column: error.column,
                    }),
                    confidence: FixConfidence::Medium,
                    fix: Some(Fix {
                        description: format!(
                            "Change `const {}` to `let {}` at line {}",
                            var_name, var_name, line_no
                        ),
                        edits: vec![TextEdit {
                            line: line_no,
                            column: None,
                            kind: EditKind::ReplaceLine,
                            new_text: new_line,
                        }],
                    }),
                });
            }
        }
    }

    Some(Diagnosis {
        language: "javascript".to_string(),
        error_code: "SyntaxError".to_string(),
        message: "Missing initializer in const declaration -- add `= value` or use `let`".to_string(),
        location: error.line.map(|l| FixLocation {
            file: error.file.clone().unwrap_or_default(),
            line: l,
            column: error.column,
        }),
        confidence: FixConfidence::Low,
        fix: None,
    })
}

// ============================================================================
// Helper functions
// ============================================================================

/// Extract the undefined name from a ReferenceError message.
///
/// "X is not defined" -> "X"
fn extract_js_name(msg: &str, suffix: &str) -> Option<String> {
    let re = Regex::new(&format!(r"(\w+)\s+{}", regex::escape(suffix))).ok()?;
    re.captures(msg)
        .and_then(|caps| caps.get(1))
        .map(|m| m.as_str().to_string())
}

/// Extract the name from "X is not a function" TypeError message.
///
/// Handles patterns:
/// - "someObj.length is not a function" -> "length"
/// - "someFunc is not a function" -> "someFunc"
fn extract_not_a_function_name(msg: &str) -> Option<String> {
    let re = Regex::new(r"(?:(\w+)\.)?(\w+)\s+is not a function").ok()?;
    re.captures(msg)
        .and_then(|caps| caps.get(2))
        .map(|m| m.as_str().to_string())
}

/// Extract the property name from "Cannot read properties of undefined (reading 'X')".
fn extract_reading_property(msg: &str) -> Option<String> {
    let re = Regex::new(r"reading '(\w+)'").ok()?;
    re.captures(msg)
        .and_then(|caps| caps.get(1))
        .map(|m| m.as_str().to_string())
}

/// Extract the unexpected token from a SyntaxError message.
///
/// "Unexpected token }" -> "}"
/// "Unexpected token ','" -> ","
fn extract_unexpected_token(msg: &str) -> Option<String> {
    // Try quoted form first: "Unexpected token 'X'"
    let re_quoted = Regex::new(r"Unexpected token '([^']+)'").ok()?;
    if let Some(caps) = re_quoted.captures(msg) {
        return caps.get(1).map(|m| m.as_str().to_string());
    }

    // Try unquoted form: "Unexpected token X"
    let re_unquoted = Regex::new(r"Unexpected token\s+(\S+)").ok()?;
    re_unquoted
        .captures(msg)
        .and_then(|caps| caps.get(1))
        .map(|m| m.as_str().to_string())
}

/// Count opening and closing delimiters in source code.
///
/// Returns (open_count, close_count) for the matching pair.
fn count_delimiters(source: &str, close_char: char) -> (usize, usize) {
    let open_char = match close_char {
        '}' => '{',
        ')' => '(',
        ']' => '[',
        _ => return (0, 0),
    };

    let mut opens = 0usize;
    let mut closes = 0usize;
    let mut in_string = false;
    let mut string_char = '"';
    let mut prev_char = '\0';

    for ch in source.chars() {
        if in_string {
            if ch == string_char && prev_char != '\\' {
                in_string = false;
            }
        } else {
            match ch {
                '"' | '\'' | '`' => {
                    in_string = true;
                    string_char = ch;
                }
                c if c == open_char => opens += 1,
                c if c == close_char => closes += 1,
                _ => {}
            }
        }
        prev_char = ch;
    }

    (opens, closes)
}

/// Inject a require/import statement at the top of a JavaScript file.
///
/// Places the new statement after the last existing `require` or `import` line,
/// or at the very top of the file if there are none. Returns `None` if the
/// statement is already present.
fn inject_require_statement(source: &str, stmt: &str) -> Option<(String, usize)> {
    // Already present -- no edit needed
    if source.contains(stmt) {
        return None;
    }

    let lines: Vec<&str> = source.lines().collect();

    // Find the last require/import line
    let mut last_import_line: Option<usize> = None;
    for (i, line) in lines.iter().enumerate() {
        let trimmed = line.trim();
        if trimmed.starts_with("const ") && trimmed.contains("require(")
            || trimmed.starts_with("var ") && trimmed.contains("require(")
            || trimmed.starts_with("let ") && trimmed.contains("require(")
            || trimmed.starts_with("import ")
            || trimmed.starts_with("import{")
        {
            last_import_line = Some(i);
        }
    }

    // Insert after the last import/require, or at the top
    let insert_after_line = last_import_line.unwrap_or(0);
    let line_1indexed = insert_after_line + 1;

    Some((stmt.to_string(), line_1indexed))
}

/// Determine the EditKind for a require injection based on existing imports.
fn require_edit_kind(source: &str) -> EditKind {
    let has_imports = source.lines().any(|l| {
        let trimmed = l.trim();
        (trimmed.starts_with("const ") && trimmed.contains("require("))
            || (trimmed.starts_with("var ") && trimmed.contains("require("))
            || (trimmed.starts_with("let ") && trimmed.contains("require("))
            || trimmed.starts_with("import ")
            || trimmed.starts_with("import{")
    });

    if has_imports {
        EditKind::InsertAfter
    } else {
        EditKind::InsertBefore
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    // ---- Validation gate: all 4 JS analyzers registered ----

    #[test]
    fn test_all_4_js_analyzers_registered() {
        // Each of the 4 analyzer patterns must be reachable through the dispatcher
        let patterns = [
            "ReferenceError",
            "TypeError:not_a_function",
            "TypeError:undefined_property",
            "SyntaxError",
        ];
        for pattern in &patterns {
            assert!(
                has_analyzer(pattern),
                "Analyzer for '{}' should be registered",
                pattern
            );
        }
    }

    #[test]
    fn test_unknown_js_error_not_handled() {
        assert!(!has_analyzer("RangeError"));
        assert!(!has_analyzer(""));
        assert!(!has_analyzer("CustomError"));
    }

    // ---- Analyzer 1: ReferenceError ----

    #[test]
    fn test_reference_error_known_module_fs() {
        let source = "const data = fs.readFileSync('file.txt');\n";
        let error = ParsedError {
            error_type: "ReferenceError".to_string(),
            message: "fs is not defined".to_string(),
            file: Some(PathBuf::from("app.js")),
            line: Some(1),
            column: Some(14),
            language: "javascript".to_string(),
            raw_text: "ReferenceError: fs is not defined".to_string(),
            function_name: None,
            offending_line: Some("const data = fs.readFileSync('file.txt');".to_string()),
        };

        let diag = analyze_reference_error(&error, source);
        assert!(diag.is_some(), "Should diagnose ReferenceError for fs");
        let d = diag.unwrap();
        assert_eq!(d.error_code, "ReferenceError");
        assert_eq!(d.confidence, FixConfidence::Medium);
        assert!(d.fix.is_some());
        let fix = d.fix.unwrap();
        assert!(
            fix.edits[0].new_text.contains("require('fs')"),
            "Fix should inject fs require, got: {}",
            fix.edits[0].new_text
        );
    }

    #[test]
    fn test_reference_error_known_module_path() {
        let source = "const dir = path.join(__dirname, 'data');\n";
        let error = ParsedError {
            error_type: "ReferenceError".to_string(),
            message: "path is not defined".to_string(),
            file: Some(PathBuf::from("app.js")),
            line: Some(1),
            column: Some(13),
            language: "javascript".to_string(),
            raw_text: "ReferenceError: path is not defined".to_string(),
            function_name: None,
            offending_line: None,
        };

        let diag = analyze_reference_error(&error, source);
        assert!(diag.is_some());
        let d = diag.unwrap();
        assert!(d.fix.is_some());
        assert!(d.fix.unwrap().edits[0].new_text.contains("require('path')"));
    }

    #[test]
    fn test_reference_error_unknown_module() {
        let source = "const x = someLib.doStuff();\n";
        let error = ParsedError {
            error_type: "ReferenceError".to_string(),
            message: "someLib is not defined".to_string(),
            file: None,
            line: Some(1),
            column: None,
            language: "javascript".to_string(),
            raw_text: String::new(),
            function_name: None,
            offending_line: None,
        };

        let diag = analyze_reference_error(&error, source);
        assert!(diag.is_some());
        let d = diag.unwrap();
        assert_eq!(d.confidence, FixConfidence::Low);
        assert!(d.fix.is_some());
        assert!(d.fix.unwrap().edits[0].new_text.contains("require('somelib')"));
    }

    #[test]
    fn test_reference_error_already_required() {
        let source = "const fs = require('fs');\nconst data = fs.readFileSync('file.txt');\n";
        let error = ParsedError {
            error_type: "ReferenceError".to_string(),
            message: "fs is not defined".to_string(),
            file: None,
            line: Some(2),
            column: None,
            language: "javascript".to_string(),
            raw_text: String::new(),
            function_name: None,
            offending_line: None,
        };

        let diag = analyze_reference_error(&error, source);
        // require already present -> inject_require_statement returns None -> no diagnosis
        assert!(
            diag.is_none(),
            "Should not produce a diagnosis when require already present"
        );
    }

    // ---- Analyzer 2: TypeError: not a function ----

    #[test]
    fn test_type_error_not_a_function_length() {
        let source = "const arr = [1, 2, 3];\nconst len = arr.length();\n";
        let error = ParsedError {
            error_type: "TypeError".to_string(),
            message: "arr.length is not a function".to_string(),
            file: Some(PathBuf::from("app.js")),
            line: Some(2),
            column: Some(17),
            language: "javascript".to_string(),
            raw_text: String::new(),
            function_name: None,
            offending_line: None,
        };

        let diag = analyze_type_error_not_function(&error, source);
        assert!(diag.is_some(), "Should diagnose length() typo");
        let d = diag.unwrap();
        assert_eq!(d.error_code, "TypeError");
        assert_eq!(d.confidence, FixConfidence::Medium);
        assert!(d.fix.is_some());
        let fix = d.fix.unwrap();
        assert!(
            fix.edits[0].new_text.contains("arr.length"),
            "Fix should remove () from .length(), got: {}",
            fix.edits[0].new_text
        );
        assert!(
            !fix.edits[0].new_text.contains("arr.length()"),
            "Fix should NOT contain .length(), got: {}",
            fix.edits[0].new_text
        );
    }

    #[test]
    fn test_type_error_not_a_function_unknown() {
        let source = "const result = obj.customThing();\n";
        let error = ParsedError {
            error_type: "TypeError".to_string(),
            message: "obj.customThing is not a function".to_string(),
            file: None,
            line: Some(1),
            column: None,
            language: "javascript".to_string(),
            raw_text: String::new(),
            function_name: None,
            offending_line: None,
        };

        let diag = analyze_type_error_not_function(&error, source);
        assert!(diag.is_some());
        let d = diag.unwrap();
        assert_eq!(d.confidence, FixConfidence::Low);
        assert!(d.fix.is_none());
    }

    // ---- Analyzer 3: TypeError: Cannot read properties of undefined ----

    #[test]
    fn test_type_error_undefined_property_dot_access() {
        let source = "const user = getUser();\nconst name = user.profile.name;\n";
        let error = ParsedError {
            error_type: "TypeError".to_string(),
            message: "Cannot read properties of undefined (reading 'name')".to_string(),
            file: Some(PathBuf::from("app.js")),
            line: Some(2),
            column: Some(26),
            language: "javascript".to_string(),
            raw_text: String::new(),
            function_name: None,
            offending_line: None,
        };

        let diag = analyze_type_error_undefined(&error, source);
        assert!(diag.is_some(), "Should diagnose undefined property access");
        let d = diag.unwrap();
        assert_eq!(d.error_code, "TypeError");
        assert_eq!(d.confidence, FixConfidence::Medium);
        assert!(d.fix.is_some());
        let fix = d.fix.unwrap();
        assert!(
            fix.edits[0].new_text.contains("?.name"),
            "Fix should add optional chaining, got: {}",
            fix.edits[0].new_text
        );
    }

    #[test]
    fn test_type_error_undefined_property_foo_optional_chaining() {
        // Canonical form: "Cannot read properties of undefined (reading 'foo')"
        // with a line that contains `.foo` — should produce a ReplaceLine edit
        // with `?.foo` substituted, at Medium confidence.
        let source = "const bar = getValue();\nconst x = bar.foo;\n";
        let error = ParsedError {
            error_type: "TypeError".to_string(),
            message: "Cannot read properties of undefined (reading 'foo')".to_string(),
            file: Some(PathBuf::from("app.js")),
            line: Some(2),
            column: Some(11),
            language: "javascript".to_string(),
            raw_text: "TypeError: Cannot read properties of undefined (reading 'foo')".to_string(),
            function_name: None,
            offending_line: Some("const x = bar.foo;".to_string()),
        };

        let diag = analyze_type_error_undefined(&error, source);
        assert!(diag.is_some(), "Should diagnose undefined property 'foo'");
        let d = diag.unwrap();
        assert_eq!(d.error_code, "TypeError");
        assert_eq!(
            d.confidence,
            FixConfidence::Medium,
            "Confidence should be Medium (not Low) when fix is applicable"
        );
        assert!(d.fix.is_some(), "Should produce a fix edit");
        let fix = d.fix.unwrap();
        assert_eq!(fix.edits.len(), 1);
        let edit = &fix.edits[0];
        assert_eq!(edit.line, 2);
        assert_eq!(edit.kind, EditKind::ReplaceLine);
        assert!(
            edit.new_text.contains("?.foo"),
            "Fix should insert optional chaining `?.foo`, got: {}",
            edit.new_text
        );
        assert!(
            !edit.new_text.contains(".foo") || edit.new_text.contains("?.foo"),
            "The resulting line must use `?.foo` not bare `.foo`"
        );
    }

    #[test]
    fn test_type_error_undefined_property_no_fix() {
        let source = "const x = getValue();\n";
        let error = ParsedError {
            error_type: "TypeError".to_string(),
            message: "Cannot read properties of undefined (reading 'foo')".to_string(),
            file: None,
            line: None,
            column: None,
            language: "javascript".to_string(),
            raw_text: String::new(),
            function_name: None,
            offending_line: None,
        };

        let diag = analyze_type_error_undefined(&error, source);
        assert!(diag.is_some());
        let d = diag.unwrap();
        assert_eq!(d.confidence, FixConfidence::Low);
        assert!(d.fix.is_none());
    }

    // ---- Analyzer 4: SyntaxError ----

    #[test]
    fn test_syntax_error_unexpected_token() {
        let source = "const obj = {\n  name: 'test'\n  age: 30\n};\n";
        let error = ParsedError {
            error_type: "SyntaxError".to_string(),
            message: "Unexpected identifier".to_string(),
            file: Some(PathBuf::from("app.js")),
            line: Some(3),
            column: Some(2),
            language: "javascript".to_string(),
            raw_text: String::new(),
            function_name: None,
            offending_line: None,
        };

        let diag = analyze_syntax_error(&error, source);
        assert!(diag.is_some(), "Should diagnose SyntaxError");
        let d = diag.unwrap();
        assert_eq!(d.error_code, "SyntaxError");
        assert_eq!(d.confidence, FixConfidence::Low);
    }

    #[test]
    fn test_syntax_error_unexpected_end_of_input() {
        let source = "function foo() {\n  const x = 1;\n";
        let error = ParsedError {
            error_type: "SyntaxError".to_string(),
            message: "Unexpected end of input".to_string(),
            file: Some(PathBuf::from("app.js")),
            line: Some(2),
            column: None,
            language: "javascript".to_string(),
            raw_text: String::new(),
            function_name: None,
            offending_line: None,
        };

        let diag = analyze_syntax_error(&error, source);
        assert!(diag.is_some(), "Should diagnose unexpected end");
        let d = diag.unwrap();
        assert_eq!(d.error_code, "SyntaxError");
        assert!(d.message.contains("unclosed"));
        // Should suggest adding closing brace
        assert!(d.fix.is_some(), "Should suggest adding closing brace");
        let fix = d.fix.unwrap();
        assert_eq!(fix.edits[0].new_text, "}");
    }

    #[test]
    fn test_syntax_error_missing_initializer() {
        let source = "const x;\nconsole.log(x);\n";
        let error = ParsedError {
            error_type: "SyntaxError".to_string(),
            message: "Missing initializer in const declaration".to_string(),
            file: Some(PathBuf::from("app.js")),
            line: Some(1),
            column: Some(7),
            language: "javascript".to_string(),
            raw_text: String::new(),
            function_name: None,
            offending_line: None,
        };

        let diag = analyze_syntax_error(&error, source);
        assert!(diag.is_some());
        let d = diag.unwrap();
        assert_eq!(d.error_code, "SyntaxError");
        assert_eq!(d.confidence, FixConfidence::Medium);
        assert!(d.fix.is_some());
        let fix = d.fix.unwrap();
        assert!(
            fix.edits[0].new_text.contains("let x;"),
            "Fix should change const to let, got: {}",
            fix.edits[0].new_text
        );
    }

    // ---- Dispatcher ----

    #[test]
    fn test_diagnose_js_dispatches_reference_error() {
        let source = "const data = fs.readFileSync('file.txt');\n";
        let tree = crate::ast::parser::parse(source, crate::Language::JavaScript).unwrap();
        let error = ParsedError {
            error_type: "ReferenceError".to_string(),
            message: "fs is not defined".to_string(),
            file: None,
            line: Some(1),
            column: None,
            language: "javascript".to_string(),
            raw_text: String::new(),
            function_name: None,
            offending_line: None,
        };

        let diag = diagnose_javascript(&error, source, &tree, None);
        assert!(diag.is_some());
        assert_eq!(diag.unwrap().error_code, "ReferenceError");
    }

    #[test]
    fn test_diagnose_js_dispatches_type_error_not_function() {
        let source = "const arr = [1, 2, 3];\nconst len = arr.length();\n";
        let tree = crate::ast::parser::parse(source, crate::Language::JavaScript).unwrap();
        let error = ParsedError {
            error_type: "TypeError".to_string(),
            message: "arr.length is not a function".to_string(),
            file: None,
            line: Some(2),
            column: None,
            language: "javascript".to_string(),
            raw_text: String::new(),
            function_name: None,
            offending_line: None,
        };

        let diag = diagnose_javascript(&error, source, &tree, None);
        assert!(diag.is_some());
        assert_eq!(diag.unwrap().error_code, "TypeError");
    }

    #[test]
    fn test_diagnose_js_dispatches_type_error_undefined() {
        let source = "const name = obj.profile.name;\n";
        let tree = crate::ast::parser::parse(source, crate::Language::JavaScript).unwrap();
        let error = ParsedError {
            error_type: "TypeError".to_string(),
            message: "Cannot read properties of undefined (reading 'name')".to_string(),
            file: None,
            line: Some(1),
            column: None,
            language: "javascript".to_string(),
            raw_text: String::new(),
            function_name: None,
            offending_line: None,
        };

        let diag = diagnose_javascript(&error, source, &tree, None);
        assert!(diag.is_some());
        assert_eq!(diag.unwrap().error_code, "TypeError");
    }

    #[test]
    fn test_diagnose_js_dispatches_syntax_error() {
        let source = "const x = {\n";
        let tree = crate::ast::parser::parse(source, crate::Language::JavaScript).unwrap();
        let error = ParsedError {
            error_type: "SyntaxError".to_string(),
            message: "Unexpected end of input".to_string(),
            file: None,
            line: Some(1),
            column: None,
            language: "javascript".to_string(),
            raw_text: String::new(),
            function_name: None,
            offending_line: None,
        };

        let diag = diagnose_javascript(&error, source, &tree, None);
        assert!(diag.is_some());
        assert_eq!(diag.unwrap().error_code, "SyntaxError");
    }

    #[test]
    fn test_diagnose_js_unknown_error_returns_none() {
        let source = "const x = 1;\n";
        let tree = crate::ast::parser::parse(source, crate::Language::JavaScript).unwrap();
        let error = ParsedError {
            error_type: "RangeError".to_string(),
            message: "Maximum call stack size exceeded".to_string(),
            file: None,
            line: None,
            column: None,
            language: "javascript".to_string(),
            raw_text: String::new(),
            function_name: None,
            offending_line: None,
        };

        let diag = diagnose_javascript(&error, source, &tree, None);
        assert!(diag.is_none());
    }

    // ---- Helper function tests ----

    #[test]
    fn test_extract_js_name() {
        assert_eq!(
            extract_js_name("fs is not defined", "is not defined"),
            Some("fs".to_string())
        );
        assert_eq!(
            extract_js_name("path is not defined", "is not defined"),
            Some("path".to_string())
        );
        assert_eq!(extract_js_name("random text", "is not defined"), None);
    }

    #[test]
    fn test_extract_not_a_function_name() {
        assert_eq!(
            extract_not_a_function_name("arr.length is not a function"),
            Some("length".to_string())
        );
        assert_eq!(
            extract_not_a_function_name("someFunc is not a function"),
            Some("someFunc".to_string())
        );
        assert_eq!(extract_not_a_function_name("random text"), None);
    }

    #[test]
    fn test_extract_reading_property() {
        assert_eq!(
            extract_reading_property("Cannot read properties of undefined (reading 'name')"),
            Some("name".to_string())
        );
        assert_eq!(
            extract_reading_property("Cannot read properties of null (reading 'foo')"),
            Some("foo".to_string())
        );
        assert_eq!(extract_reading_property("random text"), None);
    }

    #[test]
    fn test_extract_unexpected_token() {
        assert_eq!(
            extract_unexpected_token("Unexpected token '}'"),
            Some("}".to_string())
        );
        assert_eq!(
            extract_unexpected_token("Unexpected token }"),
            Some("}".to_string())
        );
        assert_eq!(
            extract_unexpected_token("Unexpected token ','"),
            Some(",".to_string())
        );
    }

    #[test]
    fn test_count_delimiters() {
        let source = "function foo() { if (x) { return [1]; } }";
        let (opens, closes) = count_delimiters(source, '}');
        assert_eq!(opens, 2);
        assert_eq!(closes, 2);
    }

    #[test]
    fn test_count_delimiters_unmatched() {
        let source = "function foo() { if (x) { return 1; }";
        let (opens, closes) = count_delimiters(source, '}');
        assert_eq!(opens, 2);
        assert_eq!(closes, 1);
    }

    #[test]
    fn test_inject_require_no_existing() {
        let source = "const x = 1;\n";
        let result = inject_require_statement(source, "const fs = require('fs');");
        assert!(result.is_some());
        let (text, line) = result.unwrap();
        assert!(text.contains("require('fs')"));
        assert_eq!(line, 1);
    }

    #[test]
    fn test_inject_require_after_existing() {
        let source = "const path = require('path');\n\nconst x = 1;\n";
        let result = inject_require_statement(source, "const fs = require('fs');");
        assert!(result.is_some());
        let (text, line) = result.unwrap();
        assert!(text.contains("require('fs')"));
        assert_eq!(line, 1); // After the first require
    }

    #[test]
    fn test_inject_require_already_present() {
        let source = "const fs = require('fs');\nconst data = fs.readFileSync('file.txt');\n";
        let result = inject_require_statement(source, "const fs = require('fs');");
        assert!(result.is_none(), "Should return None when require already present");
    }
}