tldr-core 0.4.0

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
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
//! Cognitive complexity calculation module (Session 15, Phase 3)
//!
//! Implements SonarQube's cognitive complexity algorithm with threshold checking.
//!
//! # Algorithm Overview (per SonarSource whitepaper)
//!
//! Cognitive complexity increments for:
//! - Control flow structures: +1 for if, elif, for, while, catch, switch, ?:
//! - Nesting penalty: +1 per nesting level for nested control structures
//! - Logical operators: +1 for sequences of &&, ||
//! - Recursion: +1 for recursive calls
//! - Break/continue to label: +1 (not applicable in Python)
//!
//! Important deviations from cyclomatic complexity:
//! - `else` adds +0 (linear flow per SonarSource Cognitive Complexity v1.4)
//! - `else if` adds +1 total (the inner `if`, NOT +2)
//! - `elif` adds +1 (Python's distinct `elif_clause`)
//! - Nesting increases cognitive load exponentially
//!
//! # References
//! - [SonarSource Cognitive Complexity Whitepaper](https://www.sonarsource.com/docs/CognitiveComplexity.pdf)

use std::path::Path;

use serde::{Deserialize, Serialize};
use tree_sitter::Node;

use crate::ast::function_finder::{get_function_body, get_function_name, get_function_node_kinds};
use crate::ast::parser::{parse, parse_file};
use crate::metrics::types::{CognitiveContributor, CognitiveInfo};
use crate::types::Language;
use crate::TldrResult;

/// Maximum nesting depth to prevent infinite loops
const MAX_NESTING_DEPTH: usize = 100;

/// Default threshold for cognitive complexity warning
pub const DEFAULT_THRESHOLD: u32 = 15;

/// Default threshold for severe violations
pub const DEFAULT_HIGH_THRESHOLD: u32 = 25;

/// Result of cognitive complexity analysis for a file
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CognitiveReport {
    /// Functions analyzed with their complexity scores
    pub functions: Vec<FunctionCognitive>,
    /// Functions that exceed the threshold
    pub violations: Vec<ViolationEntry>,
    /// Summary statistics
    pub summary: CognitiveSummary,
    /// Warnings encountered during analysis
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub warnings: Vec<String>,
}

/// Cognitive complexity for a single function
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionCognitive {
    /// Function name
    pub name: String,
    /// File path
    pub file: String,
    /// Line number where function starts
    pub line: u32,
    /// Cognitive complexity score
    pub cognitive: u32,
    /// Cyclomatic complexity (optional, for comparison)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cyclomatic: Option<u32>,
    /// Maximum nesting depth in this function
    pub max_nesting: u32,
    /// Nesting penalty portion of the score
    pub nesting_penalty: u32,
    /// Threshold status
    pub threshold_status: ThresholdStatus,
    /// Detailed contributors (optional, when show_contributors is true)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub contributors: Option<Vec<CognitiveContributor>>,
}

/// Threshold violation entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ViolationEntry {
    /// Function name
    pub name: String,
    /// File path
    pub file: String,
    /// Line number
    pub line: u32,
    /// Cognitive complexity score
    pub cognitive: u32,
    /// Severity level
    pub severity: String,
}

/// Summary statistics for cognitive complexity
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CognitiveSummary {
    /// Total number of functions analyzed
    pub total_functions: usize,
    /// Sum of all cognitive complexity scores
    pub total_cognitive: u32,
    /// Average cognitive complexity
    pub avg_cognitive: f64,
    /// Maximum cognitive complexity found
    pub max_cognitive: u32,
    /// Number of violations (exceeds threshold)
    pub violations_count: usize,
    /// Number of severe violations (exceeds high threshold)
    pub severe_violations_count: usize,
    /// Compliance rate (percentage of functions under threshold)
    pub compliance_rate: f64,
}

/// Threshold status for a function
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ThresholdStatus {
    /// Below threshold
    Ok,
    /// Approaching threshold (>= 80% of threshold)
    Warning,
    /// Exceeds threshold
    Violation,
    /// Exceeds high threshold
    Severe,
}

impl ThresholdStatus {
    /// Determine status based on score and thresholds
    pub fn from_score(score: u32, threshold: u32, high_threshold: u32) -> Self {
        if score >= high_threshold {
            ThresholdStatus::Severe
        } else if score >= threshold {
            ThresholdStatus::Violation
        } else if score >= (threshold * 4 / 5) {
            // >= 80% of threshold
            ThresholdStatus::Warning
        } else {
            ThresholdStatus::Ok
        }
    }
}

/// Options for cognitive complexity analysis
#[derive(Debug, Clone, Default)]
pub struct CognitiveOptions {
    /// Filter to specific function name
    pub function_filter: Option<String>,
    /// Threshold for violations
    pub threshold: u32,
    /// High threshold for severe violations
    pub high_threshold: u32,
    /// Include contributor breakdown
    pub show_contributors: bool,
    /// Include cyclomatic comparison
    pub include_cyclomatic: bool,
    /// Maximum functions to return (0 = all)
    pub top: usize,
}

impl CognitiveOptions {
    /// Create default options with standard thresholds
    pub fn new() -> Self {
        Self {
            function_filter: None,
            threshold: DEFAULT_THRESHOLD,
            high_threshold: DEFAULT_HIGH_THRESHOLD,
            show_contributors: false,
            include_cyclomatic: false,
            top: 50,
        }
    }

    /// Set function filter
    pub fn with_function(mut self, function: Option<String>) -> Self {
        self.function_filter = function;
        self
    }

    /// Set threshold
    pub fn with_threshold(mut self, threshold: u32) -> Self {
        self.threshold = threshold;
        self
    }

    /// Set high threshold
    pub fn with_high_threshold(mut self, high_threshold: u32) -> Self {
        self.high_threshold = high_threshold;
        self
    }

    /// Enable contributor breakdown
    pub fn with_contributors(mut self, show: bool) -> Self {
        self.show_contributors = show;
        self
    }

    /// Enable cyclomatic comparison
    pub fn with_cyclomatic(mut self, include: bool) -> Self {
        self.include_cyclomatic = include;
        self
    }

    /// Set top limit
    pub fn with_top(mut self, top: usize) -> Self {
        self.top = top;
        self
    }
}

/// Analyze cognitive complexity for a file
///
/// # Arguments
/// * `path` - Path to the source file
/// * `options` - Analysis options
///
/// # Returns
/// * `Ok(CognitiveReport)` - Analysis results
/// * `Err(TldrError)` - On parse or file errors
pub fn analyze_cognitive(path: &Path, options: &CognitiveOptions) -> TldrResult<CognitiveReport> {
    // Parse the file
    let (tree, source, language) = parse_file(path)?;
    let root = tree.root_node();

    let file_path = path.to_string_lossy().to_string();

    // Find all functions
    let mut functions = find_all_functions(root, language, &source, &file_path, options)?;

    // sibling-resolver-gaps-v1 (P14.AGG14-19): the AST walker above
    // recognizes only the canonical JS function nodes
    // (`function_declaration`, `arrow_function`, `method_definition`,
    // `function`, `generator_function*`). It misses the CommonJS
    // pattern `app.X = function name() {}` because the
    // `assignment_expression` is the immediate parent of the
    // function expression — the function expression itself is a
    // `function` node, but its tree-sitter `name` field is empty when
    // the assigned function is anonymous OR it's the inner name (which
    // doesn't reflect the outer `app.X` exported binding). For
    // express's `application.js` cognitive returned only 2 functions
    // while halstead returned 19. Augment by reusing the AST extractor
    // (the same source halstead/complexity siblings rely on) to fill
    // in CommonJS-style assignments. The extractor knows the
    // `app.X = function ...` shape and emits the qualified name.
    if matches!(language, Language::JavaScript | Language::TypeScript) {
        augment_cognitive_with_extractor_functions(
            path,
            language,
            &source,
            &file_path,
            options,
            &mut functions,
        )?;
    }

    // Apply function filter if specified
    if let Some(ref filter) = options.function_filter {
        functions.retain(|f| f.name.contains(filter) || f.name == *filter);
    }

    // Sort by cognitive complexity descending
    functions.sort_by(|a, b| b.cognitive.cmp(&a.cognitive));

    // Apply top limit
    if options.top > 0 && functions.len() > options.top {
        functions.truncate(options.top);
    }

    // Build violations list
    let violations: Vec<ViolationEntry> = functions
        .iter()
        .filter(|f| {
            f.threshold_status == ThresholdStatus::Violation
                || f.threshold_status == ThresholdStatus::Severe
        })
        .map(|f| ViolationEntry {
            name: f.name.clone(),
            file: f.file.clone(),
            line: f.line,
            cognitive: f.cognitive,
            severity: match f.threshold_status {
                ThresholdStatus::Severe => "severe".to_string(),
                ThresholdStatus::Violation => "violation".to_string(),
                _ => "warning".to_string(),
            },
        })
        .collect();

    // Calculate summary
    let summary = calculate_summary(&functions, options.threshold, options.high_threshold);

    Ok(CognitiveReport {
        functions,
        violations,
        summary,
        warnings: Vec::new(),
    })
}

/// Analyze cognitive complexity from source code string
pub fn analyze_cognitive_source(
    source: &str,
    language: Language,
    file_name: &str,
    options: &CognitiveOptions,
) -> TldrResult<CognitiveReport> {
    let tree = parse(source, language)?;
    let root = tree.root_node();

    let mut functions = find_all_functions(root, language, source, file_name, options)?;

    // Apply function filter if specified
    if let Some(ref filter) = options.function_filter {
        functions.retain(|f| f.name.contains(filter) || f.name == *filter);
    }

    // Sort by cognitive complexity descending
    functions.sort_by(|a, b| b.cognitive.cmp(&a.cognitive));

    // Apply top limit
    if options.top > 0 && functions.len() > options.top {
        functions.truncate(options.top);
    }

    // Build violations list
    let violations: Vec<ViolationEntry> = functions
        .iter()
        .filter(|f| {
            f.threshold_status == ThresholdStatus::Violation
                || f.threshold_status == ThresholdStatus::Severe
        })
        .map(|f| ViolationEntry {
            name: f.name.clone(),
            file: f.file.clone(),
            line: f.line,
            cognitive: f.cognitive,
            severity: match f.threshold_status {
                ThresholdStatus::Severe => "severe".to_string(),
                ThresholdStatus::Violation => "violation".to_string(),
                _ => "warning".to_string(),
            },
        })
        .collect();

    // Calculate summary
    let summary = calculate_summary(&functions, options.threshold, options.high_threshold);

    Ok(CognitiveReport {
        functions,
        violations,
        summary,
        warnings: Vec::new(),
    })
}

/// Find all functions in the AST and calculate their cognitive complexity
fn find_all_functions(
    root: Node,
    language: Language,
    source: &str,
    file_path: &str,
    options: &CognitiveOptions,
) -> TldrResult<Vec<FunctionCognitive>> {
    let func_kinds = get_function_node_kinds(language);
    let mut functions = Vec::new();

    let mut cursor = root.walk();
    let mut stack = vec![root];

    while let Some(node) = stack.pop() {
        if func_kinds.contains(&node.kind()) {
            if let Some(name) = get_function_name(node, language, source) {
                let mut calculator = CognitiveCalculator::new(name.clone(), source, language);
                calculator.analyze_function(node)?;

                // Extract values before consuming calculator
                let max_nesting = calculator.max_nesting;
                let cyclomatic_val = calculator.cyclomatic;

                let info = calculator.into_info();
                let cognitive = info.score;
                let nesting_penalty = info.nesting_penalty;

                let threshold_status = ThresholdStatus::from_score(
                    cognitive,
                    options.threshold,
                    options.high_threshold,
                );

                let cyclomatic = if options.include_cyclomatic {
                    Some(cyclomatic_val)
                } else {
                    None
                };

                let contributors = if options.show_contributors {
                    info.contributors
                } else {
                    None
                };

                functions.push(FunctionCognitive {
                    name,
                    file: file_path.to_string(),
                    line: node.start_position().row as u32 + 1,
                    cognitive,
                    cyclomatic,
                    max_nesting,
                    nesting_penalty,
                    threshold_status,
                    contributors,
                });
            }
        }

        // Add children to stack
        cursor.reset(node);
        if cursor.goto_first_child() {
            loop {
                stack.push(cursor.node());
                if !cursor.goto_next_sibling() {
                    break;
                }
            }
        }
    }

    // p19-secondary-fixes-v1 (BUG-P19-03): OCaml's
    // `get_function_node_kinds` lists BOTH `value_definition` (the outer
    // wrapper) and `let_binding` (the inner definition). The walker
    // matches both and emits the same function twice with identical
    // (name, line). Dedup by (name, line) preserving first-seen entry.
    if matches!(language, Language::Ocaml) {
        let mut seen: std::collections::HashSet<(String, u32)> =
            std::collections::HashSet::new();
        functions.retain(|f| seen.insert((f.name.clone(), f.line)));
    }

    Ok(functions)
}

/// sibling-resolver-gaps-v1 (P14.AGG14-19): augment the cognitive
/// function list with any function the AST extractor sees that the
/// raw tree-sitter walker missed. Targets the JS CommonJS pattern
/// `app.X = function name(){}` (and module-level
/// `module.exports.foo = function(){}`), which `extract_file` already
/// resolves (halstead and complexity siblings rely on this). For each
/// extractor function whose name is not yet present in `functions`,
/// locate the matching node via `find_function_node` and run the
/// same cognitive calculator — preserving exact metric semantics.
fn augment_cognitive_with_extractor_functions(
    path: &Path,
    language: Language,
    source: &str,
    file_path: &str,
    options: &CognitiveOptions,
    functions: &mut Vec<FunctionCognitive>,
) -> TldrResult<()> {
    use crate::ast::function_finder::find_function_node;
    use crate::extract_file;

    let module = match extract_file(path, None) {
        Ok(m) => m,
        Err(_) => return Ok(()),
    };

    let tree = parse(source, language)?;
    let root = tree.root_node();

    let existing: std::collections::HashSet<String> =
        functions.iter().map(|f| f.name.clone()).collect();

    let mut candidates: Vec<(String, u32)> = Vec::new();
    for f in &module.functions {
        if !existing.contains(&f.name) {
            candidates.push((f.name.clone(), f.line_number));
        }
    }
    for class in &module.classes {
        for m in &class.methods {
            if !existing.contains(&m.name) {
                candidates.push((m.name.clone(), m.line_number));
            }
        }
    }

    for (name, line) in candidates {
        let func_node = match find_function_node(root, &name, language, source) {
            Some(n) => n,
            None => continue,
        };
        let mut calculator = CognitiveCalculator::new(name.clone(), source, language);
        if calculator.analyze_function(func_node).is_err() {
            continue;
        }
        let max_nesting = calculator.max_nesting;
        let cyclomatic_val = calculator.cyclomatic;
        let info = calculator.into_info();
        let cognitive = info.score;
        let nesting_penalty = info.nesting_penalty;

        let threshold_status =
            ThresholdStatus::from_score(cognitive, options.threshold, options.high_threshold);

        let cyclomatic = if options.include_cyclomatic {
            Some(cyclomatic_val)
        } else {
            None
        };

        let contributors = if options.show_contributors {
            info.contributors
        } else {
            None
        };

        functions.push(FunctionCognitive {
            name,
            file: file_path.to_string(),
            line,
            cognitive,
            cyclomatic,
            max_nesting,
            nesting_penalty,
            threshold_status,
            contributors,
        });
    }
    Ok(())
}

/// Calculate summary statistics
fn calculate_summary(
    functions: &[FunctionCognitive],
    threshold: u32,
    high_threshold: u32,
) -> CognitiveSummary {
    if functions.is_empty() {
        return CognitiveSummary::default();
    }

    let total_cognitive: u32 = functions.iter().map(|f| f.cognitive).sum();
    let max_cognitive = functions.iter().map(|f| f.cognitive).max().unwrap_or(0);
    let avg_cognitive = total_cognitive as f64 / functions.len() as f64;

    let violations_count = functions
        .iter()
        .filter(|f| f.cognitive >= threshold)
        .count();
    let severe_violations_count = functions
        .iter()
        .filter(|f| f.cognitive >= high_threshold)
        .count();

    let compliant = functions.len() - violations_count;
    let compliance_rate = (compliant as f64 / functions.len() as f64) * 100.0;

    CognitiveSummary {
        total_functions: functions.len(),
        total_cognitive,
        avg_cognitive,
        max_cognitive,
        violations_count,
        severe_violations_count,
        compliance_rate,
    }
}

/// Result of running the canonical cognitive calculator on a single
/// function node.  Used by `tldr complexity` (cross-command-consistency-v1)
/// so it shares one implementation with `tldr cognitive`.
#[derive(Debug, Clone, Copy, Default)]
pub struct CognitiveScore {
    /// SonarSource cognitive complexity score
    pub cognitive: u32,
    /// Maximum nesting depth observed
    pub max_nesting: u32,
    /// Nesting-penalty portion of the score
    pub nesting_penalty: u32,
}

/// Run the canonical SonarSource cognitive calculator on a single function.
///
/// BUG-7 (cross-command-consistency-v1): both `tldr complexity` and
/// `tldr cognitive` must report the same number for the same function.
/// This helper runs the cognitive calculator that powers `tldr cognitive`
/// so `tldr complexity` can delegate to it instead of carrying a second,
/// drifting implementation.
pub fn calculate_cognitive_for_function(
    function_name: &str,
    source: &str,
    language: Language,
    func_node: Node,
) -> CognitiveScore {
    let mut calc = CognitiveCalculator::new(function_name.to_string(), source, language);
    if calc.analyze_function(func_node).is_err() {
        return CognitiveScore::default();
    }
    let max_nesting = calc.max_nesting;
    let nesting_penalty = calc.nesting_penalty;
    let info = calc.into_info();
    CognitiveScore {
        cognitive: info.score,
        max_nesting,
        nesting_penalty,
    }
}

/// Calculator for cognitive complexity metrics
struct CognitiveCalculator<'a> {
    function_name: String,
    source: &'a str,
    language: Language,
    cognitive: u32,
    cyclomatic: u32,
    max_nesting: u32,
    current_nesting: u32,
    nesting_penalty: u32,
    contributors: Vec<CognitiveContributor>,
    /// Track previous logical operator to detect sequences
    prev_logical_op: Option<String>,
}

impl<'a> CognitiveCalculator<'a> {
    fn new(function_name: String, source: &'a str, language: Language) -> Self {
        Self {
            function_name,
            source,
            language,
            cognitive: 0,
            cyclomatic: 1, // Base complexity is 1
            max_nesting: 0,
            current_nesting: 0,
            nesting_penalty: 0,
            contributors: Vec::new(),
            prev_logical_op: None,
        }
    }

    fn analyze_function(&mut self, func_node: Node) -> TldrResult<()> {
        // Get function body
        let body = get_function_body(func_node, self.language);

        if let Some(body_node) = body {
            self.analyze_node(body_node, 0)?;
        }

        Ok(())
    }

    fn analyze_node(&mut self, node: Node, depth: usize) -> TldrResult<()> {
        if depth > MAX_NESTING_DEPTH {
            return Ok(());
        }

        let kind = node.kind();
        let line = node.start_position().row as u32 + 1;

        // Check if this is a nesting-increasing structure
        let increases_nesting = self.increases_nesting_node(node);

        if increases_nesting {
            self.current_nesting += 1;
            self.max_nesting = self.max_nesting.max(self.current_nesting);
        }

        // Calculate cognitive complexity increment
        self.count_cognitive_increment(node, line);

        // Calculate cyclomatic increment (for comparison)
        self.count_cyclomatic_increment(node);

        // Recurse into children
        let mut cursor = node.walk();
        if cursor.goto_first_child() {
            loop {
                self.analyze_node(cursor.node(), depth + 1)?;
                if !cursor.goto_next_sibling() {
                    break;
                }
            }
        }

        if increases_nesting {
            self.current_nesting -= 1;
        }

        // Reset logical operator tracking at statement boundaries
        if is_statement(kind) {
            self.prev_logical_op = None;
        }

        Ok(())
    }

    /// Check if a node kind increases nesting level
    fn increases_nesting(&self, kind: &str) -> bool {
        // Language-agnostic control-flow node kinds. These have the
        // `_statement` / `_clause` suffix and are unambiguous across grammars.
        let generic = matches!(
            kind,
            "if_statement"
                | "for_statement"
                | "for_in_statement"
                | "while_statement"
                | "try_statement"
                | "with_statement"
                | "match_statement"
                | "switch_statement"
                | "switch_body"
                | "lambda"
                | "lambda_expression"
                | "catch_clause"
                | "except_clause"
                | "except_handler"
        );

        if generic {
            return true;
        }

        // cognitive-else-counting-fix-v1: Ruby tree-sitter grammar exposes
        // control-flow nodes without the `_statement` suffix used by
        // Python/JS/Java (e.g. bare `"if"`, `"while"`, `"case"`). These bare
        // keyword names ALSO appear as token-kind leaves in Python/JS/TS/C/
        // Rust trees (the literal `if` / `for` / `while` keyword inside an
        // `if_statement` / `for_statement`). Matching them unconditionally
        // double-counted every control structure in non-Ruby code. Gate the
        // Ruby cognates on `Language::Ruby` so they only fire for actual
        // Ruby ASTs.
        if matches!(self.language, Language::Ruby) {
            return matches!(
                kind,
                "if" | "unless"
                    | "while"
                    | "until"
                    | "for"
                    | "case"
                    | "begin"
                    | "rescue"
                    | "if_modifier"
                    | "unless_modifier"
                    | "while_modifier"
                    | "until_modifier"
            );
        }

        // pattern-match-arm-undercount-v1 (P19.BUG-01 family + BUG-P19-02):
        // Rust / Scala / Kotlin / OCaml grammars expose control-flow nodes
        // as `*_expression` (rather than `*_statement`). Without this branch
        // the cognitive walker never enters those expressions as a nesting
        // step, leaving `max_nesting=0` and `nesting_penalty=0` everywhere
        // in the corpus (the dominant symptom of BUG-P19-02 on Rust).
        match self.language {
            Language::Rust => {
                if matches!(
                    kind,
                    "if_expression"
                        | "for_expression"
                        | "while_expression"
                        | "loop_expression"
                        | "match_expression"
                        | "closure_expression"
                ) {
                    return true;
                }
            }
            Language::Scala => {
                if matches!(
                    kind,
                    "if_expression"
                        | "for_expression"
                        | "while_expression"
                        | "match_expression"
                        | "try_expression"
                        | "case_block"
                ) {
                    return true;
                }
            }
            Language::Kotlin => {
                if matches!(
                    kind,
                    "if_expression"
                        | "when_expression"
                        | "try_expression"
                        | "do_while_statement"
                ) {
                    return true;
                }
            }
            Language::Ocaml => {
                if matches!(
                    kind,
                    "match_expression"
                        | "function_expression"
                        | "if_expression"
                        | "for_expression"
                        | "while_expression"
                        | "try_expression"
                ) {
                    return true;
                }
            }
            Language::Elixir => {
                // Anonymous `fn` blocks contain `stab_clause` arms and
                // dispatch like `case`/`cond`/`with`; treat as nesting.
                if matches!(kind, "anonymous_function") {
                    return true;
                }
                // `call` nodes for case/cond/with handled in node-aware
                // wrapper `increases_nesting_node`.
            }
            _ => {}
        }

        false
    }

    /// Node-aware nesting check. Wraps `increases_nesting(&str)` and adds
    /// language-specific node-shape checks that need access to the node
    /// (rather than just its kind), such as Elixir's case/cond/with
    /// dispatch calls.
    fn increases_nesting_node(&self, node: Node) -> bool {
        if self.increases_nesting(node.kind()) {
            return true;
        }
        if matches!(self.language, Language::Elixir)
            && node.kind() == "call"
            && self.is_elixir_dispatch_call(node)
        {
            return true;
        }
        false
    }

    /// Detect Elixir `case`/`cond`/`with` dispatch construct.
    ///
    /// The Elixir tree-sitter grammar models these as a `call` whose first
    /// child is an `identifier` token containing the keyword. They wrap a
    /// `do_block` whose direct children are `stab_clause` arms.
    fn is_elixir_dispatch_call(&self, node: Node) -> bool {
        let mut cursor = node.walk();
        if !cursor.goto_first_child() {
            return false;
        }
        let head = cursor.node();
        if head.kind() != "identifier" {
            return false;
        }
        let text = match head.utf8_text(self.source.as_bytes()) {
            Ok(t) => t,
            Err(_) => return false,
        };
        matches!(text, "case" | "cond" | "with")
    }

    /// Count cognitive complexity increment for a node
    ///
    /// Per SonarSource algorithm:
    /// - if/elif/for/while/catch/switch/?:: +1 base + nesting level
    /// - else: +0 (linear flow, no cognitive increment)
    /// - &&/||: +1 for each sequence change
    /// - recursion: +1
    fn count_cognitive_increment(&mut self, node: Node, line: u32) {
        let kind = node.kind();

        // Per SonarSource Cognitive Complexity v1.4: `else if` adds +1, NOT
        // +2. We score the inner `if_statement` directly (which is exactly +1
        // base, no nesting penalty since the parent else_clause is treated as
        // linear flow), and score `else_clause` as +0. This produces the
        // correct +1 for `else if` without double-counting.

        // Control structures that add base increment + nesting.
        //
        // Per SonarSource Cognitive Complexity v1.4 (and the module-level
        // doc-comment): `else` is linear flow and adds +0. Only `if`, `elif`,
        // for/while/catch/switch/?: increment.
        let base_increment = match kind {
            // if adds +1 base + nesting
            "if_statement" | "if_expression" => Some((1, "if")),
            // elif adds +1 base + nesting (Python's elif_clause)
            "elif_clause" => Some((1, "elif")),
            // for/while add +1 base + nesting
            "for_statement" | "for_in_statement" => Some((1, "for")),
            "while_statement" => Some((1, "while")),
            // pattern-match-arm-undercount-v1: Rust/Scala/Kotlin/OCaml
            // expose loops as `*_expression` rather than `*_statement`.
            // Without these the loop construct itself is invisible to the
            // cognitive walker on those languages.
            "for_expression" | "while_expression" | "loop_expression"
                if matches!(
                    self.language,
                    Language::Rust | Language::Scala | Language::Ocaml
                ) =>
            {
                Some((1, "for"))
            }
            // Kotlin do-while
            "do_while_statement" if matches!(self.language, Language::Kotlin) => {
                Some((1, "while"))
            }
            // catch/except add +1 base + nesting
            "except_clause" | "catch_clause" | "except_handler" => Some((1, "catch")),
            // try_expression (scala/kotlin/ocaml) — credited so the catch
            // arms underneath get nesting; cognitive +1 matches `try`.
            "try_expression"
                if matches!(
                    self.language,
                    Language::Scala | Language::Kotlin | Language::Ocaml
                ) =>
            {
                Some((1, "try"))
            }
            // switch/match add +1 base + nesting
            "match_statement" | "switch_statement" => Some((1, "switch")),
            // pattern-match-arm-undercount-v1: Rust/Scala/OCaml/Kotlin
            // expose match/when as `*_expression`. Credit the construct
            // itself with +1 and let each non-catchall arm add +1 below.
            "match_expression"
                if matches!(
                    self.language,
                    Language::Rust | Language::Scala | Language::Ocaml
                ) =>
            {
                Some((1, "match"))
            }
            "function_expression" if matches!(self.language, Language::Ocaml) => {
                // OCaml `function | ...` form. Same dispatch as `match`.
                Some((1, "match"))
            }
            "when_expression" if matches!(self.language, Language::Kotlin) => {
                Some((1, "when"))
            }
            // ternary adds +1 (no nesting penalty per SonarQube - chains are flat)
            "conditional_expression" | "ternary_expression" => Some((1, "?:")),
            // P12.AGG12-10 + cognitive-else-counting-fix-v1: Ruby AST kinds.
            // `if`/`unless` are SonarSource "if" cognates; `while`/`until` are
            // loops; `for` is a loop; `case` is a switch; `begin` is the
            // equivalent of `try`; `rescue` is the catch clause; the
            // *_modifier suffix variants are the trailing-conditional
            // shorthand and count exactly the same as their statement form.
            //
            // CRITICAL: gate on Language::Ruby. In Python/JS/TS/C/Rust ASTs
            // the literal `if`/`for`/`while` keyword appears as a token-kind
            // child of `if_statement` / `for_statement` etc. Matching them
            // unconditionally double-counted every control structure (e.g.
            // a single `if x: ...` scored 3 instead of 1).
            "if" if matches!(self.language, Language::Ruby) => Some((1, "if")),
            "unless" if matches!(self.language, Language::Ruby) => Some((1, "if")),
            "while" | "until" if matches!(self.language, Language::Ruby) => Some((1, "while")),
            "for" if matches!(self.language, Language::Ruby) => Some((1, "for")),
            "case" if matches!(self.language, Language::Ruby) => Some((1, "switch")),
            "rescue" if matches!(self.language, Language::Ruby) => Some((1, "catch")),
            "if_modifier" => Some((1, "if")),
            "unless_modifier" => Some((1, "if")),
            "while_modifier" | "until_modifier" => Some((1, "while")),
            // pattern-match-arm-undercount-v1 (P19.BUG-01 family):
            // Per-arm credit for switch/match/when/case dispatch
            // constructs across c/cpp/rust/scala/kotlin/ocaml/elixir.
            // SonarSource v1.4 strictly credits the dispatch construct
            // once; we extend with +1 per non-catchall arm so 5-arm
            // matches in Rust/Scala/etc. surface a meaningful cognitive
            // score (the pre-fix repro showed 8-arm `parse` cog=0 on
            // Rust). Catchall arms (`_`, `default`, `else`) are excluded
            // so an `if/else` cognate inside the dispatch remains
            // linear-flow.
            "case_statement"
                if matches!(self.language, Language::C | Language::Cpp)
                    && !is_default_case_statement(node) =>
            {
                Some((1, "case"))
            }
            "match_arm"
                if matches!(self.language, Language::Rust)
                    && !is_rust_wildcard_arm(node, self.source) =>
            {
                Some((1, "arm"))
            }
            "case_clause"
                if matches!(self.language, Language::Scala)
                    && !is_scala_wildcard_arm(node, self.source) =>
            {
                Some((1, "case"))
            }
            "when_entry"
                if matches!(self.language, Language::Kotlin)
                    && !is_kotlin_else_when_entry(node) =>
            {
                Some((1, "when"))
            }
            "match_case"
                if matches!(self.language, Language::Ocaml)
                    && !is_ocaml_wildcard_match_case(node, self.source) =>
            {
                Some((1, "arm"))
            }
            "stab_clause"
                if matches!(self.language, Language::Elixir)
                    && !is_elixir_catchall_stab_clause(node, self.source) =>
            {
                Some((1, "case"))
            }
            _ => None,
        };

        if let Some((base, construct)) = base_increment {
            // Nesting penalty only applies to nested control structures.
            // The current_nesting is already incremented for this node, so we
            // use saturating_sub(1).
            // Exceptions per SonarSource spec (no nesting penalty):
            //   - ternary (?:)
            //   - `else if` — an if_statement that is a direct child of an
            //     else_clause; treated as a flat sibling of the parent if,
            //     not a nested construct.
            let is_else_if = construct == "if"
                && node
                    .parent()
                    .map(|p| p.kind() == "else_clause")
                    .unwrap_or(false);

            let nesting_increment =
                if construct != "?:" && !is_else_if && self.current_nesting > 1 {
                    self.current_nesting.saturating_sub(1)
                } else {
                    0
                };

            let total = base + nesting_increment;
            self.cognitive += total;
            self.nesting_penalty += nesting_increment;

            self.contributors.push(CognitiveContributor {
                line,
                construct: construct.to_string(),
                base_increment: base,
                nesting_increment,
                nesting_level: self.current_nesting,
            });
        }

        // Logical operators: +1 for sequence of same type, +1 when type changes
        if kind == "boolean_operator" || kind == "binary_expression" {
            if let Some(op) = self.get_logical_operator(node) {
                // Only add if different from previous or first in sequence
                let should_add = match &self.prev_logical_op {
                    None => true,              // First in sequence
                    Some(prev) => *prev != op, // Different operator
                };

                if should_add {
                    self.cognitive += 1;
                    self.contributors.push(CognitiveContributor {
                        line,
                        construct: op.clone(),
                        base_increment: 1,
                        nesting_increment: 0,
                        nesting_level: self.current_nesting,
                    });
                }

                self.prev_logical_op = Some(op);
            }
        }

        // Recursion: +1 for calling the same function
        if kind == "call" || kind == "call_expression" {
            if let Some(callee) = self.get_callee_name(node) {
                if callee == self.function_name {
                    self.cognitive += 1;
                    self.contributors.push(CognitiveContributor {
                        line,
                        construct: "recursion".to_string(),
                        base_increment: 1,
                        nesting_increment: 0,
                        nesting_level: self.current_nesting,
                    });
                }
            }
        }

        // Break/continue to label: +1 (not common in Python)
        if kind == "break_statement" || kind == "continue_statement" {
            // Check if it has a label (language specific)
            // For now, only count labeled breaks/continues
            if node.named_child_count() > 0 {
                self.cognitive += 1;
                self.contributors.push(CognitiveContributor {
                    line,
                    construct: if kind == "break_statement" {
                        "break_label"
                    } else {
                        "continue_label"
                    }
                    .to_string(),
                    base_increment: 1,
                    nesting_increment: 0,
                    nesting_level: self.current_nesting,
                });
            }
        }
    }

    /// Get logical operator from node
    fn get_logical_operator(&self, node: Node) -> Option<String> {
        if let Some(op_node) = node.child_by_field_name("operator") {
            let op_text = op_node.utf8_text(self.source.as_bytes()).ok()?;
            if matches!(op_text, "and" | "or" | "&&" | "||") {
                return Some(op_text.to_string());
            }
        }
        None
    }

    /// Count cyclomatic complexity increment (for comparison)
    fn count_cyclomatic_increment(&mut self, node: Node) {
        let kind = node.kind();

        match kind {
            "if_statement" | "if_expression" | "elif_clause" => self.cyclomatic += 1,
            "for_statement" | "for_in_statement" | "while_statement" => self.cyclomatic += 1,
            "except_clause" | "catch_clause" | "except_handler" => self.cyclomatic += 1,
            "case_clause" | "match_arm" | "switch_case" => self.cyclomatic += 1,
            "conditional_expression" | "ternary_expression" => self.cyclomatic += 1,
            // pattern-match-arm-undercount-v1: cyclomatic +1 per non-catchall
            // arm for c/cpp `case_statement`, kotlin `when_entry`, ocaml
            // `match_case`, elixir `stab_clause`. (rust `match_arm` /
            // scala `case_clause` already credited above.)
            "case_statement"
                if matches!(self.language, Language::C | Language::Cpp)
                    && !is_default_case_statement(node) =>
            {
                self.cyclomatic += 1
            }
            "when_entry"
                if matches!(self.language, Language::Kotlin)
                    && !is_kotlin_else_when_entry(node) =>
            {
                self.cyclomatic += 1
            }
            "match_case"
                if matches!(self.language, Language::Ocaml)
                    && !is_ocaml_wildcard_match_case(node, self.source) =>
            {
                self.cyclomatic += 1
            }
            "stab_clause"
                if matches!(self.language, Language::Elixir)
                    && !is_elixir_catchall_stab_clause(node, self.source) =>
            {
                self.cyclomatic += 1
            }
            // Loop/match expressions on Rust/Scala/Kotlin/OCaml.
            "for_expression" | "while_expression" | "loop_expression"
                if matches!(
                    self.language,
                    Language::Rust | Language::Scala | Language::Ocaml
                ) =>
            {
                self.cyclomatic += 1
            }
            "match_expression"
                if matches!(
                    self.language,
                    Language::Rust | Language::Scala | Language::Ocaml
                ) =>
            {
                self.cyclomatic += 1
            }
            "when_expression" if matches!(self.language, Language::Kotlin) => {
                self.cyclomatic += 1
            }
            "do_while_statement" if matches!(self.language, Language::Kotlin) => {
                self.cyclomatic += 1
            }
            // Ruby AST kinds (P12.AGG12-10 + cognitive-else-counting-fix-v1).
            // Gate bare-keyword cognates on Language::Ruby — in other
            // grammars these are literal-token leaves of statement nodes and
            // would double-count cyclomatic complexity. The `*_modifier`
            // forms are Ruby-only by construction so are safe.
            "if" | "unless" if matches!(self.language, Language::Ruby) => self.cyclomatic += 1,
            "while" | "until" if matches!(self.language, Language::Ruby) => self.cyclomatic += 1,
            "for" if matches!(self.language, Language::Ruby) => self.cyclomatic += 1,
            "rescue" if matches!(self.language, Language::Ruby) => self.cyclomatic += 1,
            "if_modifier" | "unless_modifier" => self.cyclomatic += 1,
            "while_modifier" | "until_modifier" => self.cyclomatic += 1,
            "when" if matches!(self.language, Language::Ruby) => self.cyclomatic += 1, // case-arm cognate
            "boolean_operator" | "binary_expression" => {
                if self.get_logical_operator(node).is_some() {
                    self.cyclomatic += 1;
                }
            }
            _ => {}
        }
    }

    /// Get callee name from call node
    fn get_callee_name(&self, call_node: Node) -> Option<String> {
        let func_node = call_node
            .child_by_field_name("function")
            .or_else(|| call_node.child(0))?;

        match func_node.kind() {
            "identifier" => Some(
                func_node
                    .utf8_text(self.source.as_bytes())
                    .ok()?
                    .to_string(),
            ),
            _ => None,
        }
    }

    fn into_info(self) -> CognitiveInfo {
        CognitiveInfo {
            score: self.cognitive,
            nesting_penalty: self.nesting_penalty,
            threshold_violations: Vec::new(),
            contributors: if self.contributors.is_empty() {
                None
            } else {
                Some(self.contributors)
            },
        }
    }
}

/// Check if a node kind represents a statement
fn is_statement(kind: &str) -> bool {
    kind.ends_with("_statement") || kind.ends_with("_definition") || kind.ends_with("_declaration")
}

// pattern-match-arm-undercount-v1 (P19.BUG-01 family): helpers to detect
// catchall arms in each grammar so the cognitive walker can credit
// non-catchall arms only.

/// C / C++: a `case_statement` whose first child is the `default` keyword
/// is the catchall arm and is NOT credited.
fn is_default_case_statement(node: Node) -> bool {
    let mut cursor = node.walk();
    if !cursor.goto_first_child() {
        return false;
    }
    cursor.node().kind() == "default"
}

/// Rust: a `match_arm` whose `match_pattern` child is a wildcard `_` is
/// the catchall arm and is NOT credited.
fn is_rust_wildcard_arm(node: Node, source: &str) -> bool {
    let mut cursor = node.walk();
    if !cursor.goto_first_child() {
        return false;
    }
    loop {
        let child = cursor.node();
        if child.kind() == "match_pattern" {
            // A match_pattern containing a single wildcard `_` is the catchall.
            let mut pcursor = child.walk();
            if pcursor.goto_first_child() {
                let inner = pcursor.node();
                if inner.kind() == "_" {
                    return true;
                }
                let text = inner.utf8_text(source.as_bytes()).unwrap_or("");
                if text == "_" {
                    return true;
                }
            }
            return false;
        }
        if !cursor.goto_next_sibling() {
            break;
        }
    }
    false
}

/// Scala: a `case_clause` whose pattern is a single `wildcard` is the
/// catchall arm and is NOT credited.
fn is_scala_wildcard_arm(node: Node, source: &str) -> bool {
    let mut cursor = node.walk();
    if !cursor.goto_first_child() {
        return false;
    }
    loop {
        let child = cursor.node();
        let kind = child.kind();
        // Skip the leading `case` keyword.
        if kind == "case" {
            if !cursor.goto_next_sibling() {
                return false;
            }
            continue;
        }
        if kind == "wildcard" {
            return true;
        }
        // The first non-`case` named child is the pattern; if it is
        // anything other than `wildcard`, this is a credited arm.
        let text = child.utf8_text(source.as_bytes()).unwrap_or("");
        return text == "_";
    }
    false
}

/// Kotlin: a `when_entry` whose first child is the `else` token is the
/// catchall arm and is NOT credited.
fn is_kotlin_else_when_entry(node: Node) -> bool {
    let mut cursor = node.walk();
    if !cursor.goto_first_child() {
        return false;
    }
    loop {
        let child = cursor.node();
        let kind = child.kind();
        if kind == "else" {
            return true;
        }
        // The first significant child decides; arrows / numbers / etc. mean not-else.
        if kind != "(" && kind != " " {
            return false;
        }
        if !cursor.goto_next_sibling() {
            break;
        }
    }
    false
}

/// OCaml: a `match_case` whose first pattern child is a `value_pattern`
/// containing only `_` is the catchall arm and is NOT credited.
fn is_ocaml_wildcard_match_case(node: Node, source: &str) -> bool {
    let mut cursor = node.walk();
    if !cursor.goto_first_child() {
        return false;
    }
    let first = cursor.node();
    let kind = first.kind();
    if kind == "value_pattern" {
        let text = first.utf8_text(source.as_bytes()).unwrap_or("");
        if text == "_" {
            return true;
        }
    }
    false
}

/// Elixir: a `stab_clause` whose `arguments` child is a single
/// identifier `_` (or `true` in `cond`) is the catchall arm and is NOT
/// credited.
fn is_elixir_catchall_stab_clause(node: Node, source: &str) -> bool {
    // Locate the `arguments` child.
    let mut cursor = node.walk();
    if !cursor.goto_first_child() {
        return false;
    }
    loop {
        let child = cursor.node();
        if child.kind() == "arguments" {
            // A single `identifier` child with text `_` is the catchall.
            let mut acursor = child.walk();
            if acursor.goto_first_child() {
                let inner = acursor.node();
                let text = inner.utf8_text(source.as_bytes()).unwrap_or("");
                if inner.kind() == "identifier" && text == "_" {
                    return true;
                }
                if inner.kind() == "boolean" && text == "true" {
                    // `cond` catchall is conventionally `true ->`.
                    return true;
                }
            }
            return false;
        }
        if !cursor.goto_next_sibling() {
            break;
        }
    }
    false
}

/// Merge multiple cognitive reports into one.
///
/// Combines functions from all reports, sorts by cognitive score descending,
/// applies top-N limit, rebuilds violations, and recalculates summary.
pub fn merge_cognitive_reports(
    reports: Vec<CognitiveReport>,
    options: &CognitiveOptions,
) -> CognitiveReport {
    if reports.is_empty() {
        return CognitiveReport {
            functions: vec![],
            violations: vec![],
            summary: CognitiveSummary::default(),
            warnings: vec![],
        };
    }

    // 1. Flatten all functions from all reports
    let mut functions: Vec<FunctionCognitive> = reports
        .iter()
        .flat_map(|r| r.functions.iter().cloned())
        .collect();

    // 2. Merge warnings from all reports
    let warnings: Vec<String> = reports.into_iter().flat_map(|r| r.warnings).collect();

    // 3. Sort by cognitive score descending
    functions.sort_by(|a, b| b.cognitive.cmp(&a.cognitive));

    // 4. Apply top-N limit
    if options.top > 0 && functions.len() > options.top {
        functions.truncate(options.top);
    }

    // 5. Rebuild violations from the (potentially truncated) function list
    let violations: Vec<ViolationEntry> = functions
        .iter()
        .filter(|f| {
            f.threshold_status == ThresholdStatus::Violation
                || f.threshold_status == ThresholdStatus::Severe
        })
        .map(|f| ViolationEntry {
            name: f.name.clone(),
            file: f.file.clone(),
            line: f.line,
            cognitive: f.cognitive,
            severity: match f.threshold_status {
                ThresholdStatus::Severe => "severe".to_string(),
                ThresholdStatus::Violation => "violation".to_string(),
                _ => "warning".to_string(),
            },
        })
        .collect();

    // 6. Recalculate summary
    let summary = calculate_summary(&functions, options.threshold, options.high_threshold);

    CognitiveReport {
        functions,
        violations,
        summary,
        warnings,
    }
}

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

    /// Test simple function with no control flow (cognitive = 0)
    #[test]
    fn test_simple_function_zero_complexity() {
        let source = r#"
def simple_function(x, y):
    result = x + y
    return result
"#;
        let options = CognitiveOptions::new();
        let report =
            analyze_cognitive_source(source, Language::Python, "test.py", &options).unwrap();

        let func = report
            .functions
            .iter()
            .find(|f| f.name == "simple_function")
            .unwrap();
        assert_eq!(
            func.cognitive, 0,
            "Simple function should have cognitive = 0"
        );
    }

    /// Test single if statement (cognitive = 1)
    #[test]
    fn test_single_if() {
        let source = r#"
def check_positive(x):
    if x > 0:
        return True
    return False
"#;
        let options = CognitiveOptions::new();
        let report =
            analyze_cognitive_source(source, Language::Python, "test.py", &options).unwrap();

        let func = report
            .functions
            .iter()
            .find(|f| f.name == "check_positive")
            .unwrap();
        assert_eq!(func.cognitive, 1, "Single if should have cognitive = 1");
    }

    /// Test nested if (cognitive = 3: if=1 + nested_if=1+1_nesting)
    #[test]
    fn test_nested_if() {
        let source = r#"
def check_nested(x, y):
    if x > 0:
        if y > 0:
            return "both positive"
    return "not both positive"
"#;
        let options = CognitiveOptions::new();
        let report =
            analyze_cognitive_source(source, Language::Python, "test.py", &options).unwrap();

        let func = report
            .functions
            .iter()
            .find(|f| f.name == "check_nested")
            .unwrap();
        assert_eq!(
            func.cognitive, 3,
            "Nested if should have cognitive = 3 (1 + 1 + 1 nesting)"
        );
    }

    /// Test loop with nested condition (cognitive = 3)
    #[test]
    fn test_loop_with_nested_condition() {
        let source = r#"
def process_items(items):
    result = []
    for item in items:
        if item > 0:
            result.append(item)
    return result
"#;
        let options = CognitiveOptions::new();
        let report =
            analyze_cognitive_source(source, Language::Python, "test.py", &options).unwrap();

        let func = report
            .functions
            .iter()
            .find(|f| f.name == "process_items")
            .unwrap();
        assert_eq!(
            func.cognitive, 3,
            "Loop with nested if should have cognitive = 3"
        );
    }

    /// Test multiple functions are all analyzed
    #[test]
    fn test_multiple_functions() {
        let source = r#"
def simple():
    return 1

def with_if(x):
    if x:
        return x
    return 0

def with_nested(x, y):
    if x:
        if y:
            return x + y
    return 0

def complex_function(data, threshold, flag):
    result = 0
    for item in data:
        if item > threshold:
            if flag:
                while item > 0:
                    result += 1
                    item -= 1
            else:
                result -= 1
    return result
"#;
        let options = CognitiveOptions::new();
        let report =
            analyze_cognitive_source(source, Language::Python, "test.py", &options).unwrap();

        assert!(
            report.functions.len() >= 4,
            "Should analyze all 4 functions"
        );
    }

    /// Test threshold violations are detected
    #[test]
    fn test_threshold_violations() {
        let source = r#"
def complex_function(data, threshold, flag):
    result = 0
    for item in data:
        if item > threshold:
            if flag:
                while item > 0:
                    if result > 100:
                        result += 1
                    item -= 1
            else:
                result -= 1
        else:
            for x in range(10):
                if x > 5:
                    result += x
    return result
"#;
        let options = CognitiveOptions::new().with_threshold(5);
        let report =
            analyze_cognitive_source(source, Language::Python, "test.py", &options).unwrap();

        assert!(
            !report.violations.is_empty(),
            "Should detect threshold violations"
        );
    }

    /// Test that `else` adds +0 (linear flow) per SonarSource Cognitive
    /// Complexity v1.4. Only `if` and `elif` increment.
    #[test]
    fn test_else_not_counted() {
        let source = r#"
def with_else(x):
    if x > 0:
        return 1
    else:
        return -1
"#;
        let options = CognitiveOptions::new();
        let report =
            analyze_cognitive_source(source, Language::Python, "test.py", &options).unwrap();

        let func = report
            .functions
            .iter()
            .find(|f| f.name == "with_else")
            .unwrap();
        // if adds +1, else adds +0 (linear flow per SonarSource v1.4)
        assert_eq!(
            func.cognitive, 1,
            "else should NOT add to cognitive complexity per SonarSource v1.4"
        );
    }

    /// Test logical operators add complexity
    #[test]
    fn test_logical_operators() {
        let source = r#"
def with_logic(a, b, c):
    if a and b:
        return 1
    if a or c:
        return 2
    return 0
"#;
        let options = CognitiveOptions::new();
        let report =
            analyze_cognitive_source(source, Language::Python, "test.py", &options).unwrap();

        let func = report
            .functions
            .iter()
            .find(|f| f.name == "with_logic")
            .unwrap();
        // 2 ifs (each +1) + 2 logical operators (each +1) = 4
        assert!(func.cognitive >= 4, "Should count logical operators");
    }

    /// Test else-if chains don't double-count in JavaScript
    /// Per SonarSource Cognitive Complexity v1.4: `else` adds +0,
    /// `else if` adds +1 (not +2), so `if-else if-else` scores 2.
    #[test]
    fn test_else_if_no_double_count_javascript() {
        let js_code = r#"
function test(x) {
    if (x > 0) {       // +1
        return 1;
    } else if (x < 0) { // +1 (else-if: +1, NOT +2; else adds 0)
        return -1;
    } else {            // +0 (else is linear flow per SonarSource v1.4)
        return 0;
    }
}
"#;
        let options = CognitiveOptions::new();
        let report =
            analyze_cognitive_source(js_code, Language::JavaScript, "test.js", &options).unwrap();

        let func = report.functions.iter().find(|f| f.name == "test").unwrap();
        assert_eq!(
            func.cognitive, 2,
            "if-else if-else should score 2 per SonarSource (if=+1, else-if=+1, else=+0)"
        );
    }

    /// Test else-if chains in TypeScript
    #[test]
    fn test_else_if_no_double_count_typescript() {
        let ts_code = r#"
function test(x: number): number {
    if (x > 0) {       // +1
        return 1;
    } else if (x < 0) { // +1 (NOT +2)
        return -1;
    } else {            // +0 (else is linear flow)
        return 0;
    }
}
"#;
        let options = CognitiveOptions::new();
        let report =
            analyze_cognitive_source(ts_code, Language::TypeScript, "test.ts", &options).unwrap();

        let func = report.functions.iter().find(|f| f.name == "test").unwrap();
        assert_eq!(func.cognitive, 2, "if-else if-else should score 2");
    }

    /// Test else-if chains in Rust
    #[test]
    fn test_rust_else_if_scoring() {
        let rust_code = r#"
fn test(x: i32) -> i32 {
    if x > 0 {         // +1
        1
    } else if x < 0 {  // +1
        -1
    } else {            // +0
        0
    }
}
"#;
        let options = CognitiveOptions::new();
        let report =
            analyze_cognitive_source(rust_code, Language::Rust, "test.rs", &options).unwrap();

        let func = report.functions.iter().find(|f| f.name == "test").unwrap();
        assert_eq!(func.cognitive, 2, "Rust if-else if-else should score 2");
    }

    /// Test Python elif still works correctly (already used elif_clause)
    #[test]
    fn test_python_elif_still_correct() {
        let py_code = r#"
def test(x):
    if x > 0:     # +1
        return 1
    elif x < 0:   # +1
        return -1
    else:          # +0 (else is linear flow per SonarSource v1.4)
        return 0
"#;
        let options = CognitiveOptions::new();
        let report =
            analyze_cognitive_source(py_code, Language::Python, "test.py", &options).unwrap();

        let func = report.functions.iter().find(|f| f.name == "test").unwrap();
        assert_eq!(func.cognitive, 2, "Python if-elif-else should score 2");
    }

    /// Test multiple else-if chains
    #[test]
    fn test_multiple_else_if_chains() {
        let js_code = r#"
function classify(x) {
    if (x > 100) {      // +1
        return "high";
    } else if (x > 50) { // +1
        return "medium";
    } else if (x > 0) {  // +1
        return "low";
    } else {             // +0
        return "negative";
    }
}
"#;
        let options = CognitiveOptions::new();
        let report =
            analyze_cognitive_source(js_code, Language::JavaScript, "test.js", &options).unwrap();

        let func = report
            .functions
            .iter()
            .find(|f| f.name == "classify")
            .unwrap();
        assert_eq!(
            func.cognitive, 3,
            "Multiple else-if should each score +1; else=0; total = 3"
        );
    }

    /// Test else-if in C language
    #[test]
    fn test_c_else_if_no_double_count() {
        let c_code = r#"
int test(int x) {
    if (x > 0) {       // +1
        return 1;
    } else if (x < 0) { // +1 (NOT +2)
        return -1;
    } else {            // +0
        return 0;
    }
}
"#;
        let options = CognitiveOptions::new();
        let report = analyze_cognitive_source(c_code, Language::C, "test.c", &options).unwrap();

        let func = report.functions.iter().find(|f| f.name == "test").unwrap();
        assert_eq!(func.cognitive, 2, "C if-else if-else should score 2");
    }

    // -------------------------------------------------------------------------
    // Merge Cognitive Reports Tests
    // -------------------------------------------------------------------------

    /// Helper to create a synthetic FunctionCognitive for testing
    fn make_cognitive_function(
        name: &str,
        file: &str,
        line: u32,
        cognitive: u32,
    ) -> FunctionCognitive {
        FunctionCognitive {
            name: name.to_string(),
            file: file.to_string(),
            line,
            cognitive,
            cyclomatic: None,
            max_nesting: 0,
            nesting_penalty: 0,
            threshold_status: ThresholdStatus::from_score(
                cognitive,
                DEFAULT_THRESHOLD,
                DEFAULT_HIGH_THRESHOLD,
            ),
            contributors: None,
        }
    }

    /// Helper to create a synthetic CognitiveReport for testing
    fn make_cognitive_report(functions: Vec<FunctionCognitive>) -> CognitiveReport {
        let violations: Vec<ViolationEntry> = functions
            .iter()
            .filter(|f| f.cognitive >= DEFAULT_THRESHOLD)
            .map(|f| ViolationEntry {
                name: f.name.clone(),
                file: f.file.clone(),
                line: f.line,
                cognitive: f.cognitive,
                severity: if f.cognitive >= DEFAULT_HIGH_THRESHOLD {
                    "severe".to_string()
                } else {
                    "warning".to_string()
                },
            })
            .collect();
        let summary = calculate_summary(&functions, DEFAULT_THRESHOLD, DEFAULT_HIGH_THRESHOLD);
        CognitiveReport {
            functions,
            violations,
            summary,
            warnings: vec![],
        }
    }

    #[test]
    fn test_merge_cognitive_reports_combines_functions() {
        let report1 = make_cognitive_report(vec![
            make_cognitive_function("foo", "a.py", 1, 5),
            make_cognitive_function("bar", "a.py", 10, 20),
        ]);
        let report2 = make_cognitive_report(vec![make_cognitive_function("baz", "b.py", 1, 10)]);

        let options = CognitiveOptions::new();
        let merged = merge_cognitive_reports(vec![report1, report2], &options);

        assert_eq!(
            merged.functions.len(),
            3,
            "Merged report should contain all 3 functions from both reports"
        );

        // Verify all function names are present
        let names: Vec<&str> = merged.functions.iter().map(|f| f.name.as_str()).collect();
        assert!(names.contains(&"foo"), "Should contain 'foo'");
        assert!(names.contains(&"bar"), "Should contain 'bar'");
        assert!(names.contains(&"baz"), "Should contain 'baz'");
    }

    #[test]
    fn test_merge_cognitive_reports_recalculates_summary() {
        let report1 = make_cognitive_report(vec![
            make_cognitive_function("foo", "a.py", 1, 5),
            make_cognitive_function("bar", "a.py", 10, 20),
        ]);
        let report2 = make_cognitive_report(vec![make_cognitive_function("baz", "b.py", 1, 10)]);

        let options = CognitiveOptions::new();
        let merged = merge_cognitive_reports(vec![report1, report2], &options);

        // Summary should reflect all 3 functions
        assert_eq!(
            merged.summary.total_functions, 3,
            "Summary should count all 3 functions"
        );
        assert_eq!(
            merged.summary.total_cognitive, 35,
            "Total cognitive should be 5+20+10=35"
        );
        assert_eq!(
            merged.summary.max_cognitive, 20,
            "Max cognitive should be 20"
        );
    }

    #[test]
    fn test_merge_cognitive_reports_empty() {
        let options = CognitiveOptions::new();
        let merged = merge_cognitive_reports(vec![], &options);

        assert!(
            merged.functions.is_empty(),
            "Empty merge should have no functions"
        );
        assert!(
            merged.violations.is_empty(),
            "Empty merge should have no violations"
        );
        assert_eq!(
            merged.summary.total_functions, 0,
            "Empty merge should have 0 total_functions"
        );
    }
}