big-code-analysis 2.1.0

Tool to compute and export code metrics
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
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
// Per-language metric and AST modules deliberately consume the macro-
// generated tree-sitter token enums via `use crate::*` and `use Foo::*`
// inside match expressions — explicit imports would list dozens of
// variants per arm and obscure the per-language token sets that are the
// point of these files. Allowed at the module level rather than per
// function so the per-language impl blocks stay readable.
#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]

use std::sync::OnceLock;

use aho_corasick::AhoCorasick;
use regex::bytes::Regex;

use crate::cfg_predicate::attribute_marks_test as rust_attribute_marks_test;
use crate::macros::csharp_invocation_expr_kinds;
use crate::*;

static AHO_CORASICK: OnceLock<AhoCorasick> = OnceLock::new();
static RE: OnceLock<Regex> = OnceLock::new();

// Shared ancestor-walk scaffold behind `check_if_func!` /
// `check_if_arrow_func!`. Both decide whether a JS `function_expression`
// / `arrow_function` is a *named function* (vs. an anonymous closure) by
// counting ancestors that bind the expression to a name (`$up` kinds:
// `var x = …`, `x = …`, `label:`, object `pair`) while stopping the walk
// at frames that prove the expression is used positionally (`$stop`
// kinds: a block, return, `new`, call/arguments). A positive count, or a
// per-call `$extra` adjacency check, marks it a named function.
//
// This must stay a `macro_rules!` (not a `fn`): the `$up` / `$stop`
// variant lists are matched against each JS-family language's own
// `kind_id` enum, brought into scope by a per-language `use $language::*`
// glob at the call site. A function could not name variants that only
// exist after that glob import.
//
// Kept token-identical to the two hand-written predicates it replaced:
// the bracketed variant lists expand straight into the `matches!`
// patterns and `$extra` into the trailing `|| …` disjunct.
macro_rules! js_ancestor_walk {
    (
        $node:ident,
        $ancestors:ident,
        [$($up:ident)|+],
        [$($stop:ident)|+],
        $extra:expr $(,)?
    ) => {
        // `Self` is the enclosing `impl Checker for <Lang>Code` type, so
        // the ancestor walk's `is_else_if` filter is bound directly to the
        // language's own `Checker` (no `ParserTrait` round-trip).
        //
        // `$ancestors` is the chain the caller descended through, so each
        // step is a slice index. The walk stops at the first frame that
        // proves positional use — an enclosing block, `return`, `new`, or
        // call — so on ordinary JS it takes one or two steps. That is
        // what made the pre-#1088 cost surprising: `Node::parent` is
        // `O(depth)` per step whatever the count, so even a two-step walk
        // was quadratic over a deeply nested file, which is what the
        // `nom/nested-arrow` probe measures.
        //
        // A chain of *expression*-bodied arrows (`a => b => c => …`) is
        // the one shape no chain can flatten: no `$stop` kind intervenes,
        // so the walk itself is `O(depth)` long. The chain takes it from
        // `O(depth^2)` per node to `O(depth)`; it stays quadratic overall
        // and would need a different rule, not a cheaper lookup, to fix.
        $node.count_specific_ancestors::<Self>(
            $ancestors,
            |node| matches!(node.kind_id().into(), $($up)|+),
            |node| matches!(node.kind_id().into(), $($stop)|+),
        ) > 0
            || $extra
    };
}

// Is this expression *bound* to a name, or used positionally?
//
// `check_if_func!` and `check_if_arrow_func!` answer that question for
// the two anonymous-function forms, and until #1188 they answered it
// with two different ancestor walks. Four divergences, each measured:
//
// 1. `$stop` was `Arguments` for one and `CallExpression` for the other
//    — different tree levels, since `call_expression > arguments >
//    function_expression`. An IIFE's chain is `parenthesized_expression
//    -> call_expression` with no `arguments` on it at all, so the func
//    walk ran past the call and reached the binding: `(function(c){})(1)`
//    was a closure while `const v = (function(c){})(1)` was a function.
//    The same construct, classified by what happened one level up past
//    the call.
// 2. `Pair` was in the func `$up` only, so `({ "k": function(){} })` was
//    a function and `({ "k": () => {} })` a closure. The arrow reached
//    the same verdict for a *bare* key only, and by a different
//    mechanism — `$extra`'s `property_identifier` sibling — which a
//    string, number or computed key does not provide.
// 3. `has_sibling(PropertyIdentifier)` was in the arrow `$extra` only,
//    the exact mirror: `class C { p = () => {}; }` was a function and
//    `class C { p = function(){}; }` a closure.
// 4. `is_child(Identifier)` is in the func `$extra` only, and **must
//    stay there**. For a function expression the only possible
//    `identifier` *child* is its optional name, so it means "carries its
//    own name" and `run(function g(){})` is rightly a function. An arrow
//    stores its un-parenthesised single parameter in exactly that
//    position, so unifying it would make `run(x => x)` — the commonest
//    callback shape in any JS corpus — a function.
//
// The first three are unified below, which makes an IIFE a closure in
// both spellings: that is what a reader sees, and it matches the
// behaviour the arrow form already had. `$up` and `$stop` are
// token-identical and only `$extra` differs, for the reason in (4).
//
// The field-definition kind is threaded in per language rather than
// named here because JS and TS spell it differently (`field_definition`
// vs `public_field_definition`). Both macros take it: `$extra`'s
// `property_identifier` sibling covers a class field only when its name
// *is* an identifier, so without the `$up` entry
// `class C { ["k"] = () => 1 }`, `{ "s" = … }` and `{ #p = … }` stayed
// closures while their `function` spellings were already functions —
// divergence (3) surviving in the shapes the first fix did not reach.
macro_rules! check_if_func {
    ($node: ident, $ancestors: ident, $field_definition: ident) => {
        js_ancestor_walk!(
            $node,
            $ancestors,
            [VariableDeclarator
                | AssignmentExpression
                | LabeledStatement
                | Pair
                | $field_definition],
            [StatementBlock | ReturnStatement | NewExpression | CallExpression | Arguments],
            $node.is_child(Identifier as u16),
        )
    };
}

macro_rules! check_if_arrow_func {
    ($node: ident, $ancestors: ident, $field_definition: ident) => {
        js_ancestor_walk!(
            $node,
            $ancestors,
            [VariableDeclarator
                | AssignmentExpression
                | LabeledStatement
                | Pair
                | $field_definition],
            [StatementBlock | ReturnStatement | NewExpression | CallExpression | Arguments],
            $node.has_sibling($ancestors, PropertyIdentifier as u16),
        )
    };
}

// A generator is a function, not a closure (#1186). `function* g() {}`
// is lexically a function *declaration* — the `*` is a modifier on the
// declaration, not a different binding form — and `get_space_kind`
// already calls both generator kinds `SpaceKind::Function`. The Checker
// disagreeing with the Getter is what made `nom` report a generator as a
// closure, bill its parameters to `closure_args`, and (reaching no
// cognitive boundary arm) let it inherit the enclosing nesting.
//
// The two kinds are **not** symmetrical, which the issue's "move both"
// framing misses. `generator_function_declaration` has a required `name`
// field and mirrors `function_declaration`, so it is unconditionally a
// function. `generator_function` has an *optional* one and mirrors
// `function_expression`, so it routes through the same
// binding-site walk: making it unconditional would classify
// `run(function*(){})` as a function while `run(function(){})` stays a
// closure.
macro_rules! is_js_func {
    ($node: ident, $ancestors: ident, $field_definition: ident) => {
        match $node.kind_id().into() {
            FunctionDeclaration | GeneratorFunctionDeclaration | MethodDefinition => true,
            FunctionExpression | GeneratorFunction => {
                check_if_func!($node, $ancestors, $field_definition)
            }
            ArrowFunction => check_if_arrow_func!($node, $ancestors, $field_definition),
            _ => false,
        }
    };
}

macro_rules! is_js_closure {
    ($node: ident, $ancestors: ident, $field_definition: ident) => {
        match $node.kind_id().into() {
            FunctionExpression | GeneratorFunction => {
                !check_if_func!($node, $ancestors, $field_definition)
            }
            ArrowFunction => !check_if_arrow_func!($node, $ancestors, $field_definition),
            _ => false,
        }
    };
}

macro_rules! is_js_func_and_closure_checker {
    ($language: ident, $field_definition: ident) => {
        #[inline]
        fn is_func<'a>(node: &Node<'a>, ancestors: Ancestors<'a, '_>) -> bool {
            use $language::*;
            is_js_func!(node, ancestors, $field_definition)
        }

        #[inline]
        fn is_closure<'a>(node: &Node<'a>, ancestors: Ancestors<'a, '_>) -> bool {
            use $language::*;
            is_js_closure!(node, ancestors, $field_definition)
        }
    };
}

// Generate an `is_string` impl for a JS-family `Checker` block. The
// MozJS / JavaScript / TypeScript grammars expose `String2` as the
// anonymous `"string"` keyword alias for `String`; TSX additionally
// exposes `String3` (the JSX-attribute string production). The
// alterator flattens these aliases; the generic `string` filter must
// agree (issue #283).
macro_rules! impl_js_family_is_string {
    ($lang:ident $(, $extra:ident)* $(,)?) => {
        fn is_string(node: &Node) -> bool {
            matches!(
                node.kind_id().into(),
                $lang::String | $lang::String2 | $lang::TemplateString
                    $(| $lang::$extra)*
            )
        }
    };
}

// Generate an `is_string` impl for languages whose `is_string`
// predicate is a flat `matches!` against one or more variant
// kinds. Reduces drift risk for new alias additions and gives a
// single table that answers "which kinds count as a string for
// `find string` / `count string`?" per language (issue #301).
//
// Languages whose `is_string` needs anything beyond a flat variant
// list (e.g. JS family's `String` + `String2` + `TemplateString`
// pattern) keep their own dedicated macros or impls.
macro_rules! impl_simple_is_string {
    ($lang:ident, $first:ident $(, $rest:ident)* $(,)?) => {
        fn is_string(node: &Node) -> bool {
            matches!(
                node.kind_id().into(),
                $lang::$first $(| $lang::$rest)*
            )
        }
    };
}

// Generate an `is_else_if` impl for the grammars that model `else if` as
// an `$if_kind` node nested directly inside an `else_clause` wrapper
// (`else_clause → if`). Covers C++/JS/TS/TSX (`IfStatement`) and Rust
// (`IfExpression`). Per-call rationale (e.g. the `IfExpression` choice)
// is hoisted above each invocation per `.claude/rules/macro-comments.md`.
macro_rules! impl_is_else_if_parent_clause {
    ($lang:ident, $if_kind:ident, $else_clause:ident) => {
        #[inline]
        fn is_else_if<'a>(node: &Node<'a>, ancestors: Ancestors<'a, '_>) -> bool {
            node.kind_id() == $lang::$if_kind
                && ancestors
                    .parent(node)
                    .is_some_and(|parent| parent.kind_id() == $lang::$else_clause)
        }
    };
}

// Generate an `is_else_if` impl for the grammars that emit a bare `else`
// keyword token followed by a nested `$if_kind` sibling, with no wrapping
// clause node. Covers Java/C#/Groovy (`IfStatement`) and Kotlin
// (`IfExpression`).
macro_rules! impl_is_else_if_prev_sibling {
    ($lang:ident, $if_kind:ident, $else_kw:ident) => {
        #[inline]
        fn is_else_if<'a>(node: &Node<'a>, ancestors: Ancestors<'a, '_>) -> bool {
            node.kind_id() == $lang::$if_kind
                && ancestors
                    .previous_sibling(node)
                    .is_some_and(|prev| prev.kind_id() == $lang::$else_kw)
        }
    };
}

// Generate an `is_else_if` impl for the grammars that expose a dedicated
// elseif/elsif clause node, so the clause node itself is the else-if
// (no `if`-nesting or sibling-token shape to inspect). Flat `matches!`
// against one or more variant kinds, analogous to `impl_simple_is_string!`;
// the multi-variant form covers PHP's `ElseIfClause | ElseIfClause2`.
macro_rules! impl_is_else_if_clause {
    ($lang:ident, $first:ident $(, $rest:ident)* $(,)?) => {
        #[inline]
        fn is_else_if(node: &Node, _ancestors: Ancestors<'_, '_>) -> bool {
            matches!(
                node.kind_id().into(),
                $lang::$first $(| $lang::$rest)*
            )
        }
    };
}

#[inline]
fn get_aho_corasick_match(code: &[u8]) -> bool {
    AHO_CORASICK
        .get_or_init(|| {
            AhoCorasick::new(vec![b"<div rustbindgen"])
                .expect("constant single-needle AhoCorasick automaton always compiles")
        })
        .is_match(code)
}

/// Per-language AST classification predicates the metric walkers use to
/// recognize comments, function spaces, calls, strings, branches, and so
/// on by node kind.
///
/// Every predicate defaults to `false` — "this node is never of that
/// category." A language implements only the predicates its grammar
/// actually expresses; categories it lacks fall through to the default,
/// so adding a language no longer requires copy-pasting `-> false` stubs.
/// The flip side is that the compiler cannot flag a forgotten override, so
/// each language's per-metric tests are the safety net for completeness.
#[doc(hidden)]
pub(crate) trait Checker {
    #[inline]
    fn is_comment(_: &Node) -> bool {
        false
    }
    /// Whether this comment must survive `strip_comments`.
    ///
    /// `ancestors` is the chain the comment-removal walk descended
    /// through. Rust's impl reads the parent to keep a comment that is
    /// a macro token; reaching it with [`Node::parent`] costs
    /// `O(depth)` per comment (#1096).
    #[inline]
    fn is_useful_comment<'a>(_: &Node<'a>, _: &[u8], _: Ancestors<'a, '_>) -> bool {
        false
    }
    #[inline]
    fn is_func_space(_: &Node) -> bool {
        false
    }
    /// Whether `node` is a *named* function definition.
    ///
    /// `ancestors` is the chain the caller descended through. The
    /// JS-family grammars have no distinct production for a named
    /// function: `const f = () => …` and `g(() => …)` share one
    /// `arrow_function` node, and only what encloses it says which is
    /// which. Those steps come off the chain, because `Node::parent`
    /// costs `O(depth)` each however few of them a walk takes (#1088).
    /// Every other grammar answers `is_func` from the node's own kind
    /// and ignores the parameter.
    #[inline]
    fn is_func<'a>(_: &Node<'a>, _ancestors: Ancestors<'a, '_>) -> bool {
        false
    }
    /// Whether `node` is an anonymous function — the complement of
    /// [`is_func`] on the grammars that express both. Takes `ancestors`
    /// for the same reason, plus one of its own: Ruby's `{ … }` block is
    /// a closure only when its parent is not the `Lambda` that already
    /// counted it.
    #[inline]
    fn is_closure<'a>(_: &Node<'a>, _ancestors: Ancestors<'a, '_>) -> bool {
        false
    }
    #[inline]
    fn is_call(_: &Node) -> bool {
        false
    }
    #[inline]
    fn is_non_arg(_: &Node) -> bool {
        false
    }
    /// Whether `param` is a language's way of spelling *"this function
    /// takes nothing"* inside an otherwise ordinary parameter list.
    ///
    /// C's `int f(void)` declares zero parameters, but the grammar
    /// emits a real `parameter_declaration` for the `void`, so the
    /// negative filters in [`crate::nargs`] counted it as one — `f` and
    /// `int f(int)` both reported 1 despite one of them taking no
    /// argument at all.
    ///
    /// It takes `code` because the question is not structural: a
    /// `parameter_declaration` holding a bare `primitive_type` and no
    /// declarator is *also* how an unnamed parameter is spelled, and
    /// `void f(int)` really does take one. Only the bytes tell them
    /// apart — `.claude/rules/grammar-dispatch.md` §10.
    #[inline]
    fn is_empty_param_marker(_param: &Node, _code: &[u8]) -> bool {
        false
    }
    /// Whether `node` — the value of a function's `parameters` field —
    /// is itself a *single formal parameter* rather than a parameter
    /// *list*.
    ///
    /// `compute_args` walks the field node's children, which is right
    /// for a list and yields zero for a lone parameter that has none.
    /// Java's `lambda_expression` spells its `parameters` field three
    /// ways — `formal_parameters` and `inferred_parameters` are lists,
    /// but the un-parenthesised `x -> …` form puts a bare, childless
    /// `identifier` there — and C#'s puts a childless
    /// `implicit_parameter`. Both reported `nargs = 0` for a lambda
    /// that plainly has one argument (#1185).
    ///
    /// Gated per language rather than inferred from `child_count() == 0`
    /// so a MISSING or zero-width ERROR node under `parameters` on a
    /// broken parse cannot silently conjure an argument.
    #[inline]
    fn is_bare_param(_: &Node) -> bool {
        false
    }
    #[inline]
    fn is_string(_: &Node) -> bool {
        false
    }
    /// Whether `node` is the `if` that continues an `else if` chain,
    /// rather than a freshly nested branch.
    ///
    /// `ancestors` is the chain the caller descended through; the
    /// grammars that model `else if` as nesting need the parent (or the
    /// preceding `else` token) to tell the two apart, and resolving
    /// either from the node alone costs `O(depth)` (#1084). Languages
    /// with a dedicated `elsif` clause node ignore it.
    #[inline]
    fn is_else_if(_: &Node, _: Ancestors<'_, '_>) -> bool {
        false
    }
    #[inline]
    fn is_primitive(_node: &Node) -> bool {
        false
    }

    fn is_error(node: &Node) -> bool {
        node.has_error()
    }

    /// Return `true` to elide this node and all its descendants from
    /// every metric. Used by language modules to filter
    /// test-only / generated / preprocessor-disabled subtrees.
    ///
    /// The default returns `false` for every node, preserving the
    /// pre-#182 behavior. Language overrides drive opt-in skips
    /// (currently: `RustCode` filters `#[cfg(test)]` items, gated
    /// by the runtime `MetricsOptions::exclude_tests` flag).
    ///
    /// `ancestors` is the chain the caller descended through. Rust's
    /// override needs the parent to read the run of `#[…]` siblings
    /// before an item, and resolving siblings from the node alone
    /// costs `O(depth)` per step (#1100).
    #[inline]
    fn should_skip_subtree<'a>(
        _node: &Node<'a>,
        _code: &[u8],
        _ancestors: Ancestors<'a, '_>,
    ) -> bool {
        false
    }

    /// Source-aware variant of [`is_func_space`]. The default forwards
    /// to the byte-less predicate so languages whose function-space
    /// classification is encoded in distinct grammar productions (Java,
    /// Rust, Python, …) need no override. Languages whose function
    /// boundaries are macro-shaped — Elixir's `def` / `defp` /
    /// `defmacro` / `defmacrop` / `defmodule` — override this to
    /// disambiguate `Call` nodes by their target identifier text
    /// (#275).
    ///
    /// `ancestors` is the chain the caller descended through, so a
    /// language whose classification depends on an enclosing construct
    /// (Elixir's `quote` template) can answer without paying
    /// `Node::parent`'s `O(depth)` per step (#1084).
    #[inline]
    fn is_func_space_with_code<'a>(
        node: &Node<'a>,
        _code: &[u8],
        _ancestors: Ancestors<'a, '_>,
    ) -> bool {
        Self::is_func_space(node)
    }

    /// Source-aware variant of [`is_func`]. Same rationale as
    /// [`is_func_space_with_code`] (#275).
    #[inline]
    fn is_func_with_code<'a>(node: &Node<'a>, _code: &[u8], ancestors: Ancestors<'a, '_>) -> bool {
        Self::is_func(node, ancestors)
    }

    /// Combined predicate the walker uses to decide whether to promote
    /// `node` to a new function-space frame. Default forwards to
    /// `is_func_with_code || is_func_space_with_code` so existing
    /// languages need no override — each kept the freedom to expose
    /// `is_func` and `is_func_space` as disjoint sets (Rust includes
    /// closures in `is_func_space`, Java keeps lambdas out, …) and
    /// this method preserves that flexibility while letting Elixir
    /// halve its per-`Call` source-text lookups: a single
    /// `elixir_call_keyword` call answers both halves at once
    /// (#310 follow-on perf).
    #[inline]
    fn promotes_to_func_space_with_code<'a>(
        node: &Node<'a>,
        code: &[u8],
        ancestors: Ancestors<'a, '_>,
    ) -> bool {
        Self::is_func_with_code(node, code, ancestors)
            || Self::is_func_space_with_code(node, code, ancestors)
    }
}

mod bash;
mod c;
mod ccomment;
mod cpp;
mod csharp;
mod elixir;
mod go;
mod groovy;
mod irules;
mod java;
mod javascript;
mod kotlin;
mod lua;
mod mozcpp;
mod mozjs;
mod objc;
mod perl;
mod php;
mod preproc;
mod python;
mod ruby;
mod rust;
mod tcl;
mod tsx;
mod typescript;

/// Returns the `class_body` child of a Java `object_creation_expression`
/// when the node is an anonymous class (`new Runnable() { ... }`), or
/// `None` for a plain constructor call (`new Foo()`). Shared by
/// `JavaCode::is_func_space` and `JavaCode::get_space_kind` so both agree
/// on exactly which `object_creation_expression` nodes open a Class space
/// (#463); a lambda is a distinct `lambda_expression` node and never
/// reaches this path.
pub(crate) fn java_anonymous_class_body<'a>(node: &Node<'a>) -> Option<Node<'a>> {
    node.first_child(|id| id == Java::ClassBody as u16)
}

/// Counts the `accessor_declaration` children (`get` / `set` / `init`) of a C#
/// `property_declaration` / `indexer_declaration` by walking its
/// `accessor_list`. Returns `0` for the expression-bodied form
/// (`this[int i] => _d[i];` / `int W => _w;`), which has no accessor list.
///
/// Shared by `csharp_member_has_accessors` (here) and the npm reference
/// `csharp_count_member`, which keeps its own `.max(1)` fallback so an
/// accessor-less expression-bodied member still counts as one method (#464).
pub(crate) fn csharp_accessor_count(node: &Node) -> usize {
    node.children()
        .filter(|c| c.kind_id() == Csharp::AccessorList as u16)
        .flat_map(|list| list.children())
        .filter(|c| c.kind_id() == Csharp::AccessorDeclaration as u16)
        .count()
}

/// Returns `true` when a C# `indexer_declaration` / `property_declaration`
/// carries bodied accessors — an `accessor_list` containing at least one
/// `accessor_declaration` (`get` / `set` / `init`). Returns `false` for the
/// expression-bodied form (`this[int i] => _d[i];` / `int W => _w;`), which
/// has no accessor list and defines a single implicit getter.
///
/// Shared by `CsharpCode::is_func` / `is_func_space` and
/// `CsharpCode::get_space_kind` so all three agree on when an indexer or
/// property opens its own Function space versus deferring to its accessor
/// children. This mirrors the npm reference (`csharp_count_member`): the
/// member counts as its accessor count, falling back to 1 for the
/// accessor-less expression-bodied form (#464 indexer, #472 property).
pub(crate) fn csharp_member_has_accessors(node: &Node) -> bool {
    csharp_accessor_count(node) > 0
}

/// Whether `param` is C's `(void)` marker — the spelling for an empty
/// parameter list — rather than a parameter.
///
/// Shared by C, C++, Mozcpp and Objective-C, which all inherit the
/// idiom and all emit a real `parameter_declaration` for it.
///
/// The declarator check is what separates the marker from `void *p`,
/// which is an ordinary parameter of type `void *`, and the byte
/// comparison is what separates it from an unnamed parameter of any
/// other type: `f(int)` and `f(void)` are the same shape and different
/// arities.
pub(crate) fn c_family_void_parameter(param: &Node, code: &[u8]) -> bool {
    param.child_by_field_name("declarator").is_none()
        && param
            .child_by_field_name("type")
            .and_then(|ty| code.get(ty.start_byte()..ty.end_byte()))
            == Some(b"void".as_slice())
}

/// Strip the `#` / `#!` marker plus the `[...]` brackets from a
/// Rust `AttributeItem` / `InnerAttributeItem` token's raw text,
/// returning the inner body. Returns `None` if the input shape is
/// unexpected — callers skip silently rather than feed the matcher
/// the literal `#[...]` form.
fn rust_attribute_body<'a>(text: &'a str, marker: &str) -> Option<&'a str> {
    text.trim()
        .strip_prefix(marker)
        .and_then(|t| t.trim_start().strip_prefix('['))
        .and_then(|t| t.trim().strip_suffix(']'))
}

/// Classifies one attribute node's body, given the `#` / `#!` marker
/// its kind carries. Returns `false` for a node whose text is not
/// valid UTF-8 or does not have the `marker[...]` shape.
fn rust_attribute_node_marks_test(node: &Node, code: &[u8], marker: &str) -> bool {
    node.utf8_text(code)
        .and_then(|text| rust_attribute_body(text, marker))
        .is_some_and(rust_attribute_marks_test)
}

fn rust_item_is_test_only<'a>(node: &Node<'a>, code: &[u8], ancestors: Ancestors<'a, '_>) -> bool {
    rust_outer_attr_marks_test(node, code, ancestors) || rust_inner_attr_marks_test(node, code)
}

/// Children a parent may have before reading its child list costs more
/// than resolving the sibling from `node` itself.
///
/// The two scans below grow on different axes and neither dominates.
/// [`Node::previous_sibling`] resolves the parent by descending from
/// the root, so its cost tracks the node's *depth*; a cursor pass over
/// the parent's children tracks the parent's *width*. Measured on this
/// workspace's grammar build, one cursor sibling step costs ~68 ns
/// against ~420 ns for a whole `previous_sibling` on a shallow node —
/// and that 420 ns did not move with the sibling index, so the
/// break-even sits near six children.
///
/// This is the budget at depth 1 only; deeper nodes get
/// [`FORWARD_ATTRIBUTE_SCAN_CHILDREN_PER_DEPTH`] more per level. It
/// still bounds the shallow-and-wide shape — a generated file of
/// thousands of top-level items stays on exactly the walk it had
/// before.
const MAX_FORWARD_ATTRIBUTE_SCAN_CHILDREN: usize = 6;

/// Children the budget above gains per level of depth.
///
/// Dispatching on width alone (#1100) reads the break-even as a
/// constant, but only the forward scan's price is: a
/// [`Node::previous_sibling`] descends from the root, so every extra
/// level makes the backward walk dearer while a cursor step stays flat.
/// One child too many therefore returned the whole `O(depth²)` walk —
/// measured on nested `mod` bodies of `#[inline] fn`s, a 7-child body
/// nested 3_200 deep took 2.67 s against 0.045 s for the same shape one
/// child narrower.
///
/// Sized from a crossover sweep of a forward-only against a
/// backward-only build over that fixture family: the *marginal* cost of
/// one more level of depth measured ~120 ns against ~35 ns for a cursor
/// step, and the observed break-even tracked that ratio — near 6
/// children at depth 1, near 13 at depth 3, and past 161 at depth 100.
/// (Marginal end-to-end figures, so they run below the isolated ~68 ns
/// / ~420 ns above; it is the ratio the budget uses, not either
/// absolute.) Three per level rounds the ratio down, keeping the
/// backward walk for the shallow-but-wide shape it is genuinely
/// cheaper on.
const FORWARD_ATTRIBUTE_SCAN_CHILDREN_PER_DEPTH: usize = 3;

/// The widest parent worth reading forward from, for a node at
/// `ancestors`' depth. A node reached without a chain has no depth to
/// scale by, so the shallow cut stands.
fn forward_attribute_scan_budget(ancestors: Ancestors<'_, '_>) -> usize {
    ancestors
        .depth()
        .map_or(0, |depth| {
            depth.saturating_mul(FORWARD_ATTRIBUTE_SCAN_CHILDREN_PER_DEPTH)
        })
        .max(MAX_FORWARD_ATTRIBUTE_SCAN_CHILDREN)
}

// The tree-sitter Rust grammar exposes outer attributes (`#[...]`) as
// `AttributeItem` siblings *before* the decorated item, so what marks
// `node` test-only is the run of attributes immediately preceding it —
// a non-attribute sibling (another item, a comment) ends the run. This
// scan covers every item kind, including `mod_item`, so
// `#[cfg(test)] mod tests` (an outer attribute on the module) is caught
// here while `mod tests { #![cfg(test)] }` is caught by the inner scan.
//
// The two readings agree on every node — `rust_outer_attr_scans_agree`
// pins that — so this only picks the cheaper one.
fn rust_outer_attr_marks_test<'a>(
    node: &Node<'a>,
    code: &[u8],
    ancestors: Ancestors<'a, '_>,
) -> bool {
    match ancestors.parent(node) {
        Some(parent) if parent.child_count() <= forward_attribute_scan_budget(ancestors) => {
            rust_attribute_run_under(&parent, node, code)
        }
        _ => rust_attribute_run_before(node, code),
    }
}

/// Reads the attribute run ending at `node` forward from `parent`,
/// which the walker already holds. `O(parent.child_count())` — two
/// cursor passes over the children when a preceding run exists, one
/// otherwise — and independent of how deep `node` sits.
///
/// Two passes and not one: classifying each attribute as the first pass
/// goes by would classify every attribute in the parent rather than
/// only the adjacent run, and classification is itself `O(len)` in the
/// attribute body (#1105). Folding them into a single-pass boolean
/// would reclassify a huge `#[cfg(all(all(…)))]` once per subsequent
/// sibling and put that quadratic straight back; the second
/// [`Node::children`] cursor is the cheaper half of that trade.
fn rust_attribute_run_under(parent: &Node, node: &Node, code: &[u8]) -> bool {
    let mut run_start = None;
    for child in parent.children() {
        if child.id() == node.id() {
            break;
        }
        if child.kind_id() == Rust::AttributeItem {
            if run_start.is_none() {
                run_start = Some(child);
            }
        } else {
            run_start = None;
        }
    }
    // No preceding attribute. A `parent` that is not `node`'s parent is
    // a caller error, not a shape answered for here: the first pass then
    // runs to the end of `parent`'s children, so the answer depends on
    // whether those end in an attribute run and is meaningless either
    // way. The walker constructs its chain through `Ancestors::checked`,
    // which catches a mismatch that shows in the spans in any debug build
    // and every mismatch under `make chain-audit` (#1122).
    let Some(run_start) = run_start else {
        return false;
    };
    parent
        .children()
        .skip_while(|child| child.id() != run_start.id())
        .take_while(|child| child.id() != node.id() && child.kind_id() == Rust::AttributeItem)
        .any(|attribute| rust_attribute_node_marks_test(&attribute, code, "#"))
}

/// Reads the same run backward from `node`. Each step resolves the
/// parent by descending from the root, so this is `O(attributes x
/// depth)` — but it does not touch the siblings it does not need,
/// which is what makes it the cheaper reading under a wide parent.
fn rust_attribute_run_before(node: &Node, code: &[u8]) -> bool {
    let mut sibling = node.previous_sibling();
    while let Some(attribute) = sibling {
        if attribute.kind_id() != Rust::AttributeItem {
            break;
        }
        if rust_attribute_node_marks_test(&attribute, code, "#") {
            return true;
        }
        sibling = attribute.previous_sibling();
    }
    false
}

// `mod_item` additionally accepts inner attributes (`#![cfg(test)]`).
// The grammar nests these inside the module's `declaration_list` body,
// not as direct `mod_item` children, so descend one level via the
// `body` field before scanning. Non-module items have no inner-attribute
// test form, so this returns `false` for them immediately.
fn rust_inner_attr_marks_test(node: &Node, code: &[u8]) -> bool {
    node.kind_id() == Rust::ModItem
        && node.child_by_field_name("body").is_some_and(|body| {
            body.children()
                .filter(|child| child.kind_id() == Rust::InnerAttributeItem)
                .any(|attribute| rust_attribute_node_marks_test(&attribute, code, "#!"))
        })
}

#[cfg(test)]
#[allow(
    clippy::float_cmp,
    clippy::cast_precision_loss,
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::similar_names,
    clippy::doc_markdown,
    clippy::needless_raw_string_hashes,
    clippy::too_many_lines
)]
mod tests {
    use super::*;
    use crate::count::count;
    use crate::langs::{
        BashParser, JavascriptCode, JavascriptParser, MozjsParser, PhpParser, TsxParser,
        TypescriptParser,
    };
    use crate::test_support::for_each_node_with_chain;
    use std::fmt::Write as _;
    use std::path::PathBuf;

    fn parse(source: &str) -> BashParser {
        BashParser::new(source.as_bytes().to_vec(), &PathBuf::from("test.sh"), None)
    }

    fn count_strings(source: &str) -> usize {
        count(&parse(source), &["string".to_string()]).0
    }

    // `count`'s filter parser accepts a numeric string as a `kind_id` match
    // (parser.rs `filters`), so `has_kind` reuses the same primitive.
    fn has_kind(source: &str, kind_id: u16) -> bool {
        count(&parse(source), &[kind_id.to_string()]).0 > 0
    }

    #[test]
    fn bash_is_string_excludes_word_tokens() {
        // `echo hello world` produces three Word nodes — none of them are
        // string literals. Regression for #44 (Word must not match
        // is_string).
        assert_eq!(count_strings("echo hello world\n"), 0);
        assert_eq!(
            count_strings("if [ -f file.txt ]; then cat file.txt; fi\n"),
            0
        );
    }

    #[test]
    fn bash_is_string_matches_quoted_literals() {
        // Regular double-quoted string -> `string` (Bash::String).
        assert_eq!(count_strings("echo \"double\"\n"), 1);
        // Single-quoted string -> `raw_string` (Bash::RawString).
        assert_eq!(count_strings("echo 'single'\n"), 1);
        // ANSI-C quoting -> `ansi_c_string` (Bash::AnsiCString).
        assert_eq!(count_strings("echo $'ansi-c'\n"), 1);
    }

    #[test]
    fn bash_is_string_matches_translated_string() {
        // tree-sitter-bash only emits a visible `translated_string` node
        // in assignment-style contexts; in command arguments the `$"..."`
        // tokenizes as `$` plus a regular `string`. Use an assignment so
        // the wrapper actually appears in the AST.
        let src = "x=$\"translated\"\n";
        assert!(
            has_kind(src, Bash::TranslatedString as u16),
            "expected a translated_string node in {src:?}"
        );
        // The wrapper plus its inner `string` child both match is_string,
        // so count is 2.
        assert_eq!(count_strings(src), 2);
    }

    #[test]
    fn bash_is_string_matches_heredoc_bodies() {
        // Plain heredoc body.
        assert_eq!(
            count_strings("cat <<EOF\nhello world\nEOF\n"),
            1,
            "heredoc body should be counted as a string literal"
        );
        // Quoted-tag heredoc disables expansions but is still a string.
        assert_eq!(
            count_strings("cat <<'EOF'\nliteral $not_expanded\nEOF\n"),
            1
        );
        // Heredoc with an embedded expansion still yields exactly one
        // body node (parallel to a JS template string with `${x}`).
        assert_eq!(count_strings("cat <<EOF\nhi $name\nEOF\n"), 1);
    }

    // ===== PHP `is_string` regression tests (issue #288) =====

    fn parse_php(source: &str) -> PhpParser {
        PhpParser::new(source.as_bytes().to_vec(), &PathBuf::from("test.php"), None)
    }

    fn count_php_strings(source: &str) -> usize {
        count(&parse_php(source), &["string".to_string()]).0
    }

    #[test]
    fn php_is_string_matches_single_quoted_literal() {
        // `Php::String` is the named single-quoted literal. Inert
        // single-quoted strings have always been matched; this anchors
        // the baseline before exercising the alias kinds.
        assert_eq!(count_php_strings("<?php $x = 'single';"), 1);
    }

    #[test]
    fn php_is_string_matches_encapsed_heredoc_nowdoc_shell() {
        // `EncapsedString` (double-quoted), `Heredoc`, `Nowdoc`, and
        // `ShellCommandExpression` (backticks) must all match
        // `is_string`. Pre-#288 the alterator/checker arms were almost
        // aligned for these named literals — this test locks in the
        // shape.
        assert_eq!(count_php_strings("<?php $x = \"double\";"), 1);
        assert_eq!(
            count_php_strings("<?php $x = <<<EOT\nbody\nEOT;\n"),
            1,
            "heredoc should match is_string"
        );
        assert_eq!(
            count_php_strings("<?php $x = <<<'EOT'\nbody\nEOT;\n"),
            1,
            "nowdoc should match is_string"
        );
        assert_eq!(
            count_php_strings("<?php $x = `ls`;"),
            1,
            "shell command (backtick) should match is_string"
        );
    }

    #[test]
    fn php_is_string_matches_string_alias_kinds() {
        // Regression for #288. Before the fix, only `Php::String`
        // (kind_id 368, the named single-quoted literal) matched
        // `is_string`. The `Php::String2` (anonymous `string` type
        // keyword, kind_id 25) and `Php::String3` (the hidden `_string`
        // supertype, kind_id 378) alias kinds — both of which the
        // language enum maps to `"string"` — were missed. A function
        // with a `: string` return type produces a `Php::String2`
        // anonymous-keyword node, so we exercise it here. The named
        // `Php::String` literal in the body matches too.
        let src = "<?php function f(): string { return 'x'; }";
        // Two string-matching nodes: the `string` return-type keyword
        // (Php::String2) and the `'x'` literal (Php::String). Pre-fix
        // only the literal matched (count would be 1).
        assert_eq!(count_php_strings(src), 2);
    }

    // ===== JS-family `is_string` regression tests (issue #283) =====

    // Walk the AST and return true iff any node has `kind_id == target`.
    // Used to confirm an alias kind actually surfaces in a real parse
    // before asserting it routes through `is_string`.
    fn ast_has_kind_id<P: ParserTrait>(parser: &P, target: u16) -> bool {
        let mut stack = vec![parser.root()];
        while let Some(node) = stack.pop() {
            if node.kind_id() == target {
                return true;
            }
            for i in (0..node.child_count()).rev() {
                if let Some(c) = node.child(i) {
                    stack.push(c);
                }
            }
        }
        false
    }

    // For each language, count nodes whose kind_id is exactly `target`
    // *and* simultaneously match `is_string`. A non-zero result proves
    // both that the alias appears in the parse and that the checker
    // accepts it. Pre-fix this would be zero for the alias kinds.
    fn count_string_matches_for_kind<P: ParserTrait, F: Fn(&Node) -> bool>(
        parser: &P,
        target: u16,
        is_string: F,
    ) -> usize {
        let mut stack = vec![parser.root()];
        let mut hits = 0;
        while let Some(node) = stack.pop() {
            if node.kind_id() == target && is_string(&node) {
                hits += 1;
            }
            for i in (0..node.child_count()).rev() {
                if let Some(c) = node.child(i) {
                    stack.push(c);
                }
            }
        }
        hits
    }

    #[test]
    fn javascript_is_string_matches_string2_alias() {
        // `Javascript::String2` (kind_id 221) aliases to `"string"`
        // (see `language_javascript.rs`). The alterator already
        // flattens it (#119); the generic `string` filter must agree
        // (#283). Use a source mix that exercises both the primary
        // `String` and the anonymous `String2` productions.
        let src = "const a = 'single';\nconst b = \"double\";\nimport \"m\";\n";
        let parser = JavascriptParser::new(src.as_bytes().to_vec(), &PathBuf::from("t.js"), None);
        // First confirm String2 actually surfaces in this parse —
        // otherwise the assertion below would be vacuously true.
        assert!(
            ast_has_kind_id(&parser, Javascript::String2 as u16),
            "expected Javascript::String2 to appear in the parse",
        );
        // Then assert every String2 node matches is_string.
        assert!(
            count_string_matches_for_kind(
                &parser,
                Javascript::String2 as u16,
                JavascriptCode::is_string,
            ) > 0,
            "Javascript::String2 nodes must match is_string",
        );
    }

    #[test]
    fn mozjs_is_string_matches_string2_alias() {
        // Parallel coverage for the MozJS dialect; same `String2`
        // alias as upstream JavaScript (kind_id 220 here).
        let src = "const a = 'single';\nconst b = \"double\";\nimport \"m\";\n";
        let parser = MozjsParser::new(src.as_bytes().to_vec(), &PathBuf::from("t.js"), None);
        assert!(
            ast_has_kind_id(&parser, Mozjs::String2 as u16),
            "expected Mozjs::String2 to appear in the parse",
        );
        assert!(
            count_string_matches_for_kind(&parser, Mozjs::String2 as u16, MozjsCode::is_string) > 0,
            "Mozjs::String2 nodes must match is_string",
        );
    }

    #[test]
    fn typescript_is_string_matches_string2_alias() {
        // TypeScript exposes both the primary `String` literal and
        // a `String2` alias (kind_id 135). The latter sits among the
        // type-keyword tokens in the enum, so a `: string` annotation
        // is a reliable producer.
        let src = "const a: string = 'x';\nfunction f(): string { return 'y'; }\n";
        let parser = TypescriptParser::new(src.as_bytes().to_vec(), &PathBuf::from("t.ts"), None);
        assert!(
            ast_has_kind_id(&parser, Typescript::String2 as u16),
            "expected Typescript::String2 to appear in the parse",
        );
        assert!(
            count_string_matches_for_kind(
                &parser,
                Typescript::String2 as u16,
                TypescriptCode::is_string,
            ) > 0,
            "Typescript::String2 nodes must match is_string",
        );
    }

    #[test]
    fn tsx_is_string_matches_string2_and_string3_aliases() {
        // TSX uniquely carries two anonymous `"string"` aliases:
        // `String3` (kind_id 141, the type-annotation keyword) and
        // `String2` (kind_id 261). Both must appear in this fixture:
        // the `: string` annotation produces `String3`, and the
        // `'x'` / `"y"` / `"m"` / `"c"` literals produce `String2`.
        // Asserting presence of both *before* checking `is_string`
        // ensures a future grammar bump that stops emitting either
        // alias fails loudly here rather than silently dropping
        // coverage (which would invalidate the regression for #283).
        let src = "const a: string = 'x';\n\
                   const b = \"y\";\n\
                   import \"m\";\n\
                   const el = <div className=\"c\">{\"t\"}</div>;\n";
        let parser = TsxParser::new(src.as_bytes().to_vec(), &PathBuf::from("t.tsx"), None);
        assert!(
            ast_has_kind_id(&parser, Tsx::String3 as u16),
            "expected Tsx::String3 (type-keyword `string`) in the parse",
        );
        assert!(
            ast_has_kind_id(&parser, Tsx::String2 as u16),
            "expected Tsx::String2 (string-literal alias) in the parse",
        );
        assert!(
            count_string_matches_for_kind(&parser, Tsx::String3 as u16, TsxCode::is_string) > 0,
            "Tsx::String3 nodes must match is_string",
        );
        assert!(
            count_string_matches_for_kind(&parser, Tsx::String2 as u16, TsxCode::is_string) > 0,
            "Tsx::String2 nodes must match is_string",
        );
    }

    // Walk the AST and return the first node whose `kind_id` equals
    // `target`. Used by the `is_else_if` tests below to fish a
    // specific node out of the parse tree without depending on the
    // `count` helper above.
    fn find_first_kind<P: ParserTrait>(parser: &P, target: u16) -> Option<Node<'_>> {
        let mut stack = vec![parser.root()];
        while let Some(node) = stack.pop() {
            if node.kind_id() == target {
                return Some(node);
            }
            for i in (0..node.child_count()).rev() {
                if let Some(c) = node.child(i) {
                    stack.push(c);
                }
            }
        }
        None
    }

    /// `#[cfg(test)] mod tests { … }` carries the test marker as an
    /// *outer* attribute sibling before the `mod_item`. The outer scan
    /// must run for `mod_item` nodes too, so this case is caught by
    /// `rust_outer_attr_marks_test` — not the inner scan. Pins the key
    /// invariant of the helper split: a `mod_item`'s outer attributes are
    /// never skipped in favour of only its inner attributes.
    #[test]
    fn rust_outer_attr_on_mod_is_test_only() {
        let src = "#[cfg(test)]\nmod tests {\n    fn t() {}\n}\n";
        let parser = RustParser::new(src.as_bytes().to_vec(), &PathBuf::from("test.rs"), None);
        let code = parser.code();
        let node = find_first_kind(&parser, Rust::ModItem as u16).expect("mod_item");

        assert!(rust_item_is_test_only(&node, code, Ancestors::unknown()));
        // The marker lives on the outer scan; the inner scan sees no
        // `#![cfg(test)]` and must report false.
        assert!(rust_outer_attr_marks_test(
            &node,
            code,
            Ancestors::unknown()
        ));
        assert!(!rust_inner_attr_marks_test(&node, code));
    }

    /// `mod tests { #![cfg(test)] … }` carries the marker as an *inner*
    /// attribute nested in the module body. The outer sibling scan finds
    /// nothing; `rust_inner_attr_marks_test` descends via the `body`
    /// field and catches it.
    #[test]
    fn rust_inner_attr_in_mod_is_test_only() {
        let src = "mod tests {\n    #![cfg(test)]\n    fn t() {}\n}\n";
        let parser = RustParser::new(src.as_bytes().to_vec(), &PathBuf::from("test.rs"), None);
        let code = parser.code();
        let node = find_first_kind(&parser, Rust::ModItem as u16).expect("mod_item");

        assert!(rust_item_is_test_only(&node, code, Ancestors::unknown()));
        assert!(!rust_outer_attr_marks_test(
            &node,
            code,
            Ancestors::unknown()
        ));
        assert!(rust_inner_attr_marks_test(&node, code));
    }

    /// A plain, unattributed item is not test-only — neither scan matches.
    #[test]
    fn rust_plain_item_is_not_test_only() {
        let src = "fn foo() {}\n";
        let parser = RustParser::new(src.as_bytes().to_vec(), &PathBuf::from("test.rs"), None);
        let code = parser.code();
        let node = find_first_kind(&parser, Rust::FunctionItem as u16).expect("function_item");

        assert!(!rust_item_is_test_only(&node, code, Ancestors::unknown()));
        assert!(!rust_outer_attr_marks_test(
            &node,
            code,
            Ancestors::unknown()
        ));
        assert!(!rust_inner_attr_marks_test(&node, code));
    }

    /// Reading the `#[…]` run forward from the parent must answer
    /// exactly what walking backward from the item answers, on every
    /// node of every attribute shape.
    ///
    /// This is the acceptance criterion for #1100, not a nicety:
    /// `rust_outer_attr_marks_test` picks between the two by the
    /// parent's width against the node's depth, so a disagreement would
    /// make `exclude_tests` prune a different set of subtrees on one
    /// shape than on another and move published metric values —
    /// including as the budget is retuned. The comparison is against the
    /// backward reading directly rather than against the dispatcher,
    /// which would compare the backward walk with itself on every wide
    /// parent.
    ///
    /// The counted `marked` / `unmarked` totals are what stop it from
    /// passing vacuously — two readings that both answered `false`
    /// everywhere would agree perfectly.
    #[test]
    fn rust_outer_attr_scans_agree() {
        // One attribute run longer than the file's actual code, so the
        // run length cannot be confused with the child count.
        let long_run = format!("{}fn f() {{}}\n", "#[allow(dead_code)]\n".repeat(64));
        // A parent with hundreds of children: the shallow-but-wide
        // shape a per-step sibling scan would have been worst on.
        let mut wide = String::new();
        for i in 0..200 {
            let _ = writeln!(wide, "#[inline] fn f{i}() {{}}");
        }
        let sources = [
            // Attribute run opening the file, so the marked item is
            // its parent's first non-attribute child.
            "#[cfg(test)]\nfn a() {}\n",
            // Every recognised marker form, plus the `not(test)` form
            // that must *not* mark (#278).
            "#[test]\nfn a() {}\n#[tokio::test]\nfn b() {}\n\
             #[cfg(all(test, feature = \"x\"))]\nfn c() {}\n\
             #[cfg(not(test))]\nfn d() {}\n",
            // A run whose marker is not the attribute adjacent to the
            // item, the same run reversed, and one where an
            // intervening comment ends the run. Both orders matter:
            // only the second distinguishes a scan that stops at the
            // node it was asked about from one that reads the whole
            // run, since the query node here is itself an attribute.
            "#[cfg(test)]\n#[allow(dead_code)]\nfn a() {}\n\
             #[allow(dead_code)]\n#[cfg(test)]\nfn b() {}\n\
             #[cfg(test)]\n// break\nfn c() {}\n",
            // Nested modules, inner attributes, and an attributed item
            // inside a function body — parents other than the root.
            "mod outer {\n#![cfg(test)]\nmod inner {\n#[test]\nfn a() {}\n}\n}\n\
             fn b() {\n#[cfg(test)]\nfn c() {}\nlet x = 1;\n}\n",
            // A bare item: no parent for the root, no preceding
            // sibling for the item.
            "fn a() {}\n",
            // The dispatch boundary at depth 1, where the budget is
            // `MAX_FORWARD_ATTRIBUTE_SCAN_CHILDREN`: three attributed
            // items make a `source_file` exactly six children wide, and
            // a fourth bare item makes seven. Neither width occurred in
            // any fixture above, so `<=` against `<` went untested.
            "#[cfg(test)]\nfn a() {}\n#[inline]\nfn b() {}\n#[cfg(test)]\nfn c() {}\n",
            "#[cfg(test)]\nfn a() {}\n#[inline]\nfn b() {}\n#[cfg(test)]\nfn c() {}\nfn d() {}\n",
            // The same seven items one level down: two braces make the
            // `declaration_list` nine wide, which is exactly the budget
            // at depth 3, so the reading flips back to forward.
            "mod m {\n#[cfg(test)]\nfn a() {}\n#[inline]\nfn b() {}\n\
             #[cfg(test)]\nfn c() {}\nfn d() {}\n}\n",
            &long_run,
            &wide,
        ];

        let (mut marked, mut unmarked) = (0_usize, 0_usize);
        let (mut read_forward, mut read_backward) = (0_usize, 0_usize);
        for source in sources {
            let code = source.as_bytes();
            let visited = for_each_node_with_chain::<RustCode>(code, |node, chain| {
                let Some(parent) = chain.last() else {
                    return; // The root has no parent to read forward from.
                };
                let backward = rust_attribute_run_before(node, code);
                assert_eq!(
                    rust_attribute_run_under(parent, node, code),
                    backward,
                    "outer-attribute readings disagree on {} at row {}",
                    node.kind(),
                    node.start_row(),
                );
                assert_eq!(
                    rust_outer_attr_marks_test(node, code, Ancestors::known(chain)),
                    backward,
                    "the width dispatch changed the answer on {} at row {}",
                    node.kind(),
                    node.start_row(),
                );
                if backward {
                    marked += 1;
                } else {
                    unmarked += 1;
                }
                if parent.child_count() > forward_attribute_scan_budget(Ancestors::known(chain)) {
                    read_backward += 1;
                } else {
                    read_forward += 1;
                }
            });
            assert!(visited > 0, "fixture must have nodes to compare");
        }
        assert!(
            marked > 0 && unmarked > 0,
            "the comparison saw only one answer ({marked} marked, {unmarked} unmarked), \
             so agreeing proves nothing"
        );
        // Both arms have to fire: the dispatcher assertion above
        // compares the backward walk with itself on every node that
        // took the backward arm, so without forward readings it proves
        // nothing, and without backward ones the `wide` shape is unmet.
        assert!(
            read_forward > 0 && read_backward > 0,
            "one dispatch arm went unexercised ({read_forward} forward, \
             {read_backward} backward)"
        );
    }

    /// The budget is a width *per level of depth*, not a flat width.
    ///
    /// Pinned as a table because the shape of the formula is the whole
    /// point of #1100's follow-up: a flat cut returned the `O(depth²)`
    /// walk to any parent one child too wide, however deep it sat.
    #[cfg(feature = "rust")]
    #[test]
    fn the_forward_attribute_scan_budget_grows_with_depth() {
        assert_eq!(
            forward_attribute_scan_budget(Ancestors::unknown()),
            MAX_FORWARD_ATTRIBUTE_SCAN_CHILDREN,
            "no chain means no depth to scale by"
        );

        // `depth` reads the chain's length and nothing else, so one
        // real node repeated stands in for any ancestry — which is what
        // lets the table reach depths a fixture could not.
        let mut checked = 0_usize;
        for_each_node_with_chain::<RustCode>(b"fn a() {}\n", |node, _| {
            checked += 1;
            // The shallow cut floors the scaled one, so it governs
            // until the depth term overtakes it at depth 3.
            for (depth, want) in [(0, 6), (1, 6), (2, 6), (3, 9), (10, 30), (1_000, 3_000)] {
                let chain = vec![*node; depth];
                assert_eq!(
                    forward_attribute_scan_budget(Ancestors::known(&chain)),
                    want,
                    "budget at depth {depth}"
                );
            }
        });
        assert!(
            checked > 0,
            "fixture must yield a node to build chains from"
        );
    }

    /// The same equivalence one level up, on the hook the walker calls.
    ///
    /// `rust_item_is_test_only` folds the inner-attribute scan in, and
    /// `should_skip_subtree` adds the item-kind filter, so pinning it
    /// here is what carries the predicate-level agreement above through
    /// to the set of subtrees `exclude_tests` actually prunes.
    #[test]
    fn rust_should_skip_subtree_matches_the_backward_reading() {
        let source = "#[cfg(test)]\nmod tests {\nfn a() {}\n}\n\
                      #[allow(dead_code)]\nstatic S: i32 = 1;\n\
                      mod inner {\n#![cfg(test)]\nconst C: i32 = 1;\n}\n\
                      #[rstest]\nfn b() {}\nfn c() {}\n";
        let code = source.as_bytes();
        let mut pruned = 0_usize;
        let visited = for_each_node_with_chain::<RustCode>(code, |node, chain| {
            let reference =
                rust_attribute_run_before(node, code) || rust_inner_attr_marks_test(node, code);
            // Spelled out rather than reused from the production hook:
            // the point is to pin which kinds the prune considers, so
            // borrowing the production `matches!` would assert nothing.
            let is_item = matches!(
                node.kind_id().into(),
                Rust::ModItem
                    | Rust::FunctionItem
                    | Rust::ImplItem
                    | Rust::TraitItem
                    | Rust::ConstItem
                    | Rust::StaticItem
            );
            let skipped = RustCode::should_skip_subtree(node, code, Ancestors::known(chain));
            assert_eq!(
                skipped,
                is_item && reference,
                "prune decision moved on {} at row {}",
                node.kind(),
                node.start_row(),
            );
            pruned += usize::from(skipped);
        });
        assert!(visited > 0, "fixture must have nodes to compare");
        // `mod tests`, `mod inner`, and `fn b` — the three test-only
        // items, and no more: a hook that pruned everything would
        // satisfy the equality above only if the oracle agreed, but
        // this pins the count the fixture was written for.
        assert_eq!(pruned, 3, "expected exactly the three test-only items");
    }

    #[test]
    fn groovy_is_else_if_recognises_else_followed_by_if() {
        // Direct assertion that `GroovyCode::is_else_if` returns true
        // for an `if_statement` whose previous sibling is the `else`
        // token. Defends the sibling-token strategy against accidental
        // regression to a `false` stub (lesson 10, #115 / #239).
        let src = "if (x) { } else if (y) { } else { }";
        let parser =
            GroovyParser::new(src.as_bytes().to_vec(), &PathBuf::from("test.groovy"), None);
        let outer =
            find_first_kind(&parser, Groovy::IfStatement as u16).expect("outer if_statement");
        // Locate the inner if_statement (in the `alternative` slot of
        // the outer if, after the `else` token).
        let mut inner: Option<Node> = None;
        for i in 0..outer.child_count() {
            if let Some(c) = outer.child(i)
                && c.kind_id() == Groovy::IfStatement as u16
            {
                inner = Some(c);
                break;
            }
        }
        let inner = inner.expect("expected an inner if_statement");
        assert!(
            GroovyCode::is_else_if(&inner, Ancestors::unknown()),
            "inner if_statement after `else` must be recognised as else-if"
        );
        assert!(
            !GroovyCode::is_else_if(&outer, Ancestors::unknown()),
            "outer if_statement must not be recognised as else-if"
        );
    }

    #[test]
    fn groovy_is_else_if_false_for_standalone_if() {
        // A bare `if` (no `else` preceding it) must NOT register as
        // an else-if.
        let src = "if (x) { println(x) }";
        let parser =
            GroovyParser::new(src.as_bytes().to_vec(), &PathBuf::from("test.groovy"), None);
        let node = find_first_kind(&parser, Groovy::IfStatement as u16).expect("if_statement");
        assert!(!GroovyCode::is_else_if(&node, Ancestors::unknown()));
    }

    #[test]
    fn groovy_is_call_excludes_constructors() {
        // Regression for #430. `GroovyCode::is_call` previously matched
        // `ObjectCreationExpression`, so `new Foo()` was counted as a
        // call in Groovy but not in Java/C# (whose `is_call` is method
        // invocation only). The Java-family convention is that `is_call`
        // (the `--ops`/`call` filter) counts method/function call sites
        // only; constructors are an ABC concern. This test mixes all
        // three Groovy call-shaped forms and pins the count at 2:
        //   * `new Foo()`        -> object_creation_expression (NOT a call)
        //   * `a.bar()`          -> method_invocation           (a call)
        //   * `println "hi"`     -> command_chain               (a call)
        // Pre-fix this count was 3 (the constructor was miscounted).
        let src = "def m() {\n  def a = new Foo()\n  a.bar()\n  println \"hi\"\n}\n";
        let parser =
            GroovyParser::new(src.as_bytes().to_vec(), &PathBuf::from("test.groovy"), None);
        assert_eq!(
            count(&parser, &["call".to_string()]).0,
            2,
            "is_call must count method_invocation + command_chain only, not the constructor"
        );

        // Direct predicate assertions: the constructor node must be
        // rejected while both genuine call forms are accepted.
        let ctor = find_first_kind(&parser, Groovy::ObjectCreationExpression as u16)
            .expect("object_creation_expression");
        assert!(
            !GroovyCode::is_call(&ctor),
            "object_creation_expression must not be a call"
        );
        let method =
            find_first_kind(&parser, Groovy::MethodInvocation as u16).expect("method_invocation");
        assert!(
            GroovyCode::is_call(&method),
            "method_invocation must be a call"
        );
        let chain = find_first_kind(&parser, Groovy::CommandChain as u16).expect("command_chain");
        assert!(GroovyCode::is_call(&chain), "command_chain must be a call");
    }

    fn parse_python(src: &str) -> PythonParser {
        PythonParser::new(src.as_bytes().to_vec(), &PathBuf::from("test.py"), None)
    }

    // Walk the AST and return every node whose `kind_id` equals `target`,
    // in DFS pre-order. Used by the Python `is_else_if` tests below to
    // distinguish the outer if from the inner one in an `else: if` chain.
    fn find_all_kinds<P: ParserTrait>(parser: &P, target: u16) -> Vec<Node<'_>> {
        let mut out = Vec::new();
        let mut stack = vec![parser.root()];
        while let Some(node) = stack.pop() {
            if node.kind_id() == target {
                out.push(node);
            }
            for i in (0..node.child_count()).rev() {
                if let Some(c) = node.child(i) {
                    stack.push(c);
                }
            }
        }
        out
    }

    #[test]
    fn python_is_else_if_recognises_if_inside_else_clause() {
        // `else: if b:` chains parse as `else_clause → block → if_statement`
        // (the `block` wrapper is Python-specific). `is_else_if` must
        // walk through that wrapper. Regression for #276 (the stub
        // returned `false` unconditionally).
        let src = "if a:\n    pass\nelse:\n    if b:\n        pass\n";
        let parser = parse_python(src);
        let outer =
            find_first_kind(&parser, Python::IfStatement as u16).expect("outer if_statement");
        let inner =
            find_python_if_inside_else_block(&parser).expect("inner if_statement under else");
        assert!(
            PythonCode::is_else_if(&inner, Ancestors::unknown()),
            "if_statement inside else_clause's block must be recognised as else-if"
        );
        assert!(
            !PythonCode::is_else_if(&outer, Ancestors::unknown()),
            "outer if_statement must not be recognised as else-if"
        );
    }

    #[test]
    fn python_is_else_if_false_for_standalone_if() {
        // A bare `if` whose parent is the module / function body must
        // NOT register as an else-if.
        let src = "if a:\n    pass\n";
        let parser = parse_python(src);
        let node = find_first_kind(&parser, Python::IfStatement as u16).expect("if_statement");
        assert!(!PythonCode::is_else_if(&node, Ancestors::unknown()));
    }

    #[test]
    fn python_is_else_if_false_for_outer_if_with_elif_alternative() {
        // `elif` parses as an `ElifClause`, not an `IfStatement`, so the
        // only `IfStatement` in `if … elif …` is the outer one. Its
        // parent is the module / function body, not an `else_clause`.
        // Pins that the presence of `elif` in the AST does not trip
        // `is_else_if` for the outer `if`.
        let src = "if a:\n    pass\nelif b:\n    pass\n";
        let parser = parse_python(src);
        let outer = find_first_kind(&parser, Python::IfStatement as u16).expect("if_statement");
        assert!(!PythonCode::is_else_if(&outer, Ancestors::unknown()));
    }

    // Return the inner `if_statement` that sits directly inside an
    // `else_clause`'s `block` wrapper, or `None` if no such node exists.
    // Used by tests below instead of relying on `find_all_kinds`'s DFS
    // pre-order to land at `ifs[1]`.
    fn find_python_if_inside_else_block(parser: &PythonParser) -> Option<Node<'_>> {
        find_all_kinds(parser, Python::IfStatement as u16)
            .into_iter()
            .find(|n| {
                n.parent().is_some_and(|p| {
                    matches!(p.kind_id().into(), Python::Block | Python::Block2)
                        && p.parent()
                            .is_some_and(|gp| gp.kind_id() == Python::ElseClause)
                })
            })
    }

    #[test]
    fn python_is_else_if_false_when_else_body_has_siblings() {
        // `else: if b:` followed by another statement at the same indent
        // is a real nested-if, not a chain. The block has 2 named
        // children, so `is_else_if` must return false.
        let src = "if a:\n    pass\nelse:\n    if b:\n        pass\n    pass\n";
        let parser = parse_python(src);
        let inner =
            find_python_if_inside_else_block(&parser).expect("inner if_statement under else");
        assert!(
            !PythonCode::is_else_if(&inner, Ancestors::unknown()),
            "inner if must NOT be recognised as else-if when its block has siblings"
        );
    }

    // Drift guard for #419: tree-sitter-python lists hidden aliases for
    // both `block` and `lambda` that are NOT emitted today —
    // `Block` (135, the hidden `_block` supertype; only `Block2` 160 is
    // emitted) and `Lambda2` (197, an unseen lambda alias; only
    // `Lambda` 196 and the `lambda` keyword token `Lambda3` 73 appear).
    // Several metric sites (`is_closure` — which now routes through
    // `cognitive::python_is_lambda`, the three cognitive lambda-scope
    // sites it feeds (#422), `is_else_if`, `python_is_block` in npa,
    // npm's class-body lookup, loc's no-op arm) already enumerate these
    // aliases defensively so a future grammar bump that promotes either
    // supertype to a concrete node cannot silently undercount.
    // This test pins their current absence across representative Python
    // (function, class, if/for bodies, lambda); if a bump ever emits one,
    // the guard flips red and forces a positive assertion to be added —
    // mirroring the `Php::String3` hidden-supertype guard above.
    #[test]
    fn python_hidden_block_and_lambda_aliases_stay_unseen() {
        let src = "def f(a, b):\n    if a:\n        return b\n    for x in b:\n        print(x)\n\nclass C:\n    def m(self):\n        pass\n\ng = lambda x: x + 1\n";
        let parser = parse_python(src);

        // Confirm the LIVE aliases actually surface, so this fixture is
        // a meaningful witness (a guard that never parses a block or a
        // lambda would pass vacuously).
        assert!(
            ast_has_kind_id(&parser, Python::Block2 as u16),
            "expected Python::Block2 (160, the emitted `block`) in the parse",
        );
        assert!(
            ast_has_kind_id(&parser, Python::Lambda as u16),
            "expected Python::Lambda (196, the emitted `lambda`) in the parse",
        );

        // The hidden supertypes must NOT surface. If either now appears,
        // add a positive assertion routing it through the relevant
        // predicate (see #419).
        assert!(
            !ast_has_kind_id(&parser, Python::Block as u16),
            "Python::Block (135) is the hidden `_block` supertype; if it now appears, route it through python_is_block and assert positively (#419)",
        );
        assert!(
            !ast_has_kind_id(&parser, Python::Lambda2 as u16),
            "Python::Lambda2 (197) is an unseen lambda alias; if it now appears, cognitive::python_is_lambda (reused by is_closure and the three cognitive lambda-scope sites) must detect it and a positive closure assertion is required (#419/#422)",
        );
    }

    // #422: the cognitive lambda-alias chokepoint and `is_closure` must
    // recognise the *same* live `lambda` node. Before #422, cognitive's
    // three lambda sites compared against `Lambda` (196) only while
    // `is_closure` accepted `Lambda | Lambda2`; both now route through
    // `cognitive::python_is_lambda`, so this pins that they agree on the
    // emitted `Lambda` and that the predicate is not vacuously true (it
    // rejects the enclosing `FunctionDefinition`). The unseen `Lambda2`
    // half of the set is covered by the drift guard above.
    #[test]
    fn python_is_lambda_matches_live_lambda_and_agrees_with_is_closure() {
        use crate::metrics::cognitive::python_is_lambda;

        let parser = parse_python("def f():\n    return lambda x: x and x\n");
        let lambda = find_first_kind(&parser, Python::Lambda as u16)
            .expect("the lambda expression must parse as Python::Lambda (196)");

        assert!(
            python_is_lambda(&lambda),
            "python_is_lambda must accept the emitted Lambda node",
        );
        assert!(
            PythonCode::is_closure(&lambda, Ancestors::unknown()),
            "is_closure must agree with python_is_lambda on the same lambda node",
        );

        // Not vacuously true: a non-lambda node (the enclosing function)
        // must be rejected, so the predicate is discriminating.
        let func = find_first_kind(&parser, Python::FunctionDefinition as u16)
            .expect("the def must parse as Python::FunctionDefinition");
        assert!(
            !python_is_lambda(&func),
            "python_is_lambda must reject a non-lambda node",
        );
    }

    /// The two `is_else_if` implementations that answer a flat `false`
    /// are a behavioural claim, not a placeholder: the language has no
    /// `else if` *chain* to collapse, so every `if` it contains is a
    /// freshly nested branch and pays cognitive nesting.
    ///
    /// Neither is reachable from a metric walk — `Preproc` and
    /// `Ccomment` (which inherit the `Checker` default) have no `if`
    /// construct at all, and Elixir's branching is `cond do` or nested
    /// `if/else` — so nothing else in the suite executes them. #1084
    /// changed both signatures to take `Ancestors`, which is exactly the
    /// kind of edit that can quietly invert a one-line predicate, so pin
    /// the contract directly.
    #[test]
    fn languages_without_else_if_chains_answer_false() {
        // The `Checker` default, via the two grammars that do not
        // override it.
        let preproc = PreprocParser::new(b"#define A 1\n".to_vec(), &PathBuf::from("a.h"), None);
        let ccomment = CcommentParser::new(b"/* c */\n".to_vec(), &PathBuf::from("a.c"), None);
        assert!(
            !PreprocCode::is_else_if(&preproc.root(), Ancestors::unknown()),
            "the Checker default must answer false"
        );
        assert!(
            !CcommentCode::is_else_if(&ccomment.root(), Ancestors::unknown()),
            "the Checker default must answer false"
        );

        // Elixir's explicit override, asserted on a real `if` node so the
        // test would catch an impl that started consulting the ancestor
        // chain and got the answer wrong.
        let parser = ElixirParser::new(
            b"defmodule M do\n  def f(a) do\n    if a do\n      if a do\n        :ok\n      end\n    end\n  end\nend\n".to_vec(),
            std::path::Path::new("m.ex"),
            None,
        );
        let ifs: Vec<_> = parser
            .root()
            .preorder()
            .filter(|n| {
                n.kind() == "call"
                    && n.utf8_text(parser.code())
                        .is_some_and(|t| t.starts_with("if"))
            })
            .collect();
        assert!(
            ifs.len() >= 2,
            "fixture must contain a nested if, else the assertion below is vacuous"
        );
        for node in &ifs {
            assert!(
                !ElixirCode::is_else_if(node, Ancestors::unknown()),
                "Elixir has no else-if chain; every if is a fresh branch"
            );
        }
    }

    // Regression for #301: every language consolidated under
    // `impl_simple_is_string!` must still recognise its canonical
    // string literal via the `"string"` filter (which routes through
    // `Checker::is_string`). The positive test now drills down to
    // every individual variant of every multi-variant language so a
    // future macro invocation that drops a variant (e.g. forgetting
    // `Cpp::ConcatenatedString` after a grammar bump) fails loudly.
    //
    // JS-family languages (Mozjs/Javascript/Typescript/Tsx) keep
    // their dedicated `impl_js_family_is_string!` macro and have
    // their own alias-aware tests above; they are intentionally
    // not duplicated here.
    fn count_with_parser<P: ParserTrait>(parser: &P) -> usize {
        count(parser, &["string".to_string()]).0
    }

    // Assert that `target` kind_id appears in the parse and every
    // such node matches `is_string`. The two-step check makes test
    // failures unambiguous: a presence failure means the fixture no
    // longer produces the variant (likely grammar drift); a match
    // failure means a macro invocation dropped the variant.
    fn assert_variant_is_string<P: ParserTrait, F: Fn(&Node) -> bool>(
        parser: &P,
        target: u16,
        is_string: F,
        lang: &str,
        variant: &str,
    ) {
        assert!(
            ast_has_kind_id(parser, target),
            "{lang}::{variant} (kind_id {target}) did not appear in the parse — fixture broken",
        );
        assert!(
            count_string_matches_for_kind(parser, target, is_string) > 0,
            "{lang}::{variant} must route through is_string",
        );
    }

    // Collapses the 6-line-per-variant `assert_variant_is_string`
    // call into a single token per variant. `$lang` is the language
    // enum (e.g. `Cpp`); `$code` is the `Checker`-implementing type
    // (e.g. `CppCode`); the trailing list names the enum variants to
    // exercise. The macro feeds `stringify!` for both the language
    // and variant labels so test failures keep the same "Lang::Variant"
    // wording the helper already emits.
    macro_rules! assert_variants_is_string {
        ($parser:expr, $lang:ident, $code:ident, [$($variant:ident),+ $(,)?]) => {
            $(
                assert_variant_is_string(
                    $parser,
                    $lang::$variant as u16,
                    $code::is_string,
                    stringify!($lang),
                    stringify!($variant),
                );
            )+
        };
    }

    // Parse `$src` with `$parser_ty` and assert the generic `"string"`
    // filter yields zero matches. Used by the negative test, which
    // walks every language consolidated under `impl_simple_is_string!`
    // with identical per-language shape (parse → count → assert_eq! 0).
    macro_rules! assert_no_string_matches {
        ($parser_ty:ident, $path:expr, $src:expr, $lang:literal $(,)?) => {{
            let parser = $parser_ty::new($src.to_vec(), $path, None);
            assert_eq!(count_with_parser(&parser), 0, $lang);
        }};
    }

    #[test]
    fn simple_is_string_macro_recognises_each_language() {
        use crate::langs::{
            CcommentParser, CppParser, CsharpParser, ElixirParser, GoParser, GroovyParser,
            IrulesParser, JavaParser, KotlinParser, LuaParser, PerlParser, PreprocParser,
            PythonParser, RubyParser, RustParser, TclParser,
        };

        let path = PathBuf::from("test");

        // ---- Preproc (2 variants): StringLiteral, RawStringLiteral ----
        let src = b"#include \"foo.h\"\nR\"(raw)\"\n".to_vec();
        let parser = PreprocParser::new(src, &path, None);
        assert_variants_is_string!(
            &parser,
            Preproc,
            PreprocCode,
            [StringLiteral, RawStringLiteral]
        );

        // ---- Ccomment (2 variants): StringLiteral, RawStringLiteral ----
        // The Ccomment grammar is a stub that only emits Comment /
        // StringLiteral / RawStringLiteral; feed it both forms.
        let src = b"\"hello\"\nR\"(raw)\"\n".to_vec();
        let parser = CcommentParser::new(src, &path, None);
        assert_variants_is_string!(
            &parser,
            Ccomment,
            CcommentCode,
            [StringLiteral, RawStringLiteral]
        );

        // ---- Cpp (3 variants): StringLiteral, ConcatenatedString, RawStringLiteral ----
        // C++ string concatenation (`"a" "b"`) produces a
        // `concatenated_string` node wrapping the literals.
        let src =
            b"const char* a = \"hi\";\nconst char* b = \"a\" \"b\";\nconst char* c = R\"(raw)\";\n"
                .to_vec();
        let parser = CppParser::new(src, &path, None);
        assert_variants_is_string!(
            &parser,
            Cpp,
            CppCode,
            [StringLiteral, ConcatenatedString, RawStringLiteral]
        );

        // ---- Python (2 variants): String, ConcatenatedString ----
        // Python concatenates adjacent string literals into a
        // `concatenated_string` node.
        let src = b"a = \"hi\"\nb = \"a\" \"b\"\n".to_vec();
        let parser = PythonParser::new(src, &path, None);
        assert_variants_is_string!(&parser, Python, PythonCode, [String, ConcatenatedString]);

        // ---- Java (2 variants): StringLiteral, MultilineStringLiteral ----
        // `Java::MultilineStringLiteral` maps to `_multiline_string_literal`
        // (leading-underscore hidden supertype) and does NOT surface as a
        // concrete kind_id in observed parses. Triple-quoted text blocks
        // instead produce regular `StringLiteral` nodes. The variant is
        // intentionally listed in the macro so a future grammar revision
        // that promotes it can't bypass `is_string`; presence is asserted
        // below to flag drift.
        let src = b"class C { String a = \"hi\"; String b = \"\"\"\nmulti\n\"\"\"; }\n".to_vec();
        let parser = JavaParser::new(src, &path, None);
        assert_variants_is_string!(&parser, Java, JavaCode, [StringLiteral]);
        assert!(
            !ast_has_kind_id(&parser, Java::MultilineStringLiteral as u16),
            "Java::MultilineStringLiteral is documented as the hidden _multiline_string_literal supertype; if it now appears in parses, replace this with a positive variant assertion",
        );

        // ---- Csharp (4 variants): StringLiteral, VerbatimStringLiteral,
        // RawStringLiteral, InterpolatedStringExpression ----
        // Verbatim: `@"..."`; raw: triple-double-quote; interpolated: `$"..."`.
        let src = b"class C { string a = \"hi\"; string b = @\"verb\"; string c = \"\"\"raw\"\"\"; string d = $\"int{1}\"; }\n".to_vec();
        let parser = CsharpParser::new(src, &path, None);
        assert_variants_is_string!(
            &parser,
            Csharp,
            CsharpCode,
            [
                StringLiteral,
                VerbatimStringLiteral,
                RawStringLiteral,
                InterpolatedStringExpression,
            ]
        );

        // ---- Rust (2 variants): StringLiteral, RawStringLiteral ----
        let src = b"fn main() { let a = \"hi\"; let b = r\"raw\"; }\n".to_vec();
        let parser = RustParser::new(src, &path, None);
        assert_variants_is_string!(&parser, Rust, RustCode, [StringLiteral, RawStringLiteral]);

        // ---- Go (2 variants): InterpretedStringLiteral, RawStringLiteral ----
        // Backtick-delimited string is the raw form.
        let src = b"package main\nfunc main() { _ = \"hi\"; _ = `raw` }\n".to_vec();
        let parser = GoParser::new(src, &path, None);
        assert_variants_is_string!(
            &parser,
            Go,
            GoCode,
            [InterpretedStringLiteral, RawStringLiteral]
        );

        // ---- Kotlin (2 variants): StringLiteral, MultilineStringLiteral ----
        let src = b"fun main() { val a = \"hi\"; val b = \"\"\"multi\"\"\" }\n".to_vec();
        let parser = KotlinParser::new(src, &path, None);
        assert_variants_is_string!(
            &parser,
            Kotlin,
            KotlinCode,
            [StringLiteral, MultilineStringLiteral]
        );

        // ---- Lua (1 variant): String ----
        let src = b"local a = \"hi\"\nlocal b = [[long]]\n".to_vec();
        let parser = LuaParser::new(src, &path, None);
        assert_variants_is_string!(&parser, Lua, LuaCode, [String]);

        // ---- Perl (7 variants): StringSingleQuoted, StringDoubleQuoted,
        // StringQQuoted, StringQqQuoted, BacktickQuoted, CommandQxQuoted,
        // HeredocBodyStatement ----
        let src = b"my $a = 'single';\nmy $b = \"double\";\nmy $c = q(qquoted);\nmy $d = qq(qqquoted);\nmy $e = `cmd`;\nmy $f = qx(qxcmd);\nmy $g = <<EOT;\nbody\nEOT\n".to_vec();
        let parser = PerlParser::new(src, &path, None);
        assert_variants_is_string!(
            &parser,
            Perl,
            PerlCode,
            [
                StringSingleQuoted,
                StringDoubleQuoted,
                StringQQuoted,
                StringQqQuoted,
                BacktickQuoted,
                CommandQxQuoted,
                HeredocBodyStatement,
            ]
        );

        // ---- Bash (5 variants): String, RawString, AnsiCString,
        // TranslatedString, HeredocBody2 ----
        // TranslatedString surfaces as a wrapper node only in
        // assignment-style contexts (see `bash_is_string_matches_translated_string`).
        let src = b"a=\"d\"\nb='r'\nc=$'ansi'\nd=$\"t\"\ncat <<EOF\nbody\nEOF\n".to_vec();
        let parser = crate::langs::BashParser::new(src, &path, None);
        assert_variants_is_string!(
            &parser,
            Bash,
            BashCode,
            [
                String,
                RawString,
                AnsiCString,
                TranslatedString,
                HeredocBody2
            ]
        );

        // ---- Tcl (3 variants): QuotedWord, BracedWord, BracedWordSimple ----
        // Tcl uses two `braced_word` rules: the named rule `braced_word`
        // (an argument-position braced expression that admits commands
        // and substitutions, e.g. `proc ... { body }`'s `body` arg) and
        // `braced_word_simple` (a plain inert braced word like
        // `{braced}` in `set v {braced}`). The fixture below mixes a
        // `proc ... {body}` (BracedWord), a simple `set` assignment
        // (BracedWordSimple), and a `set` to a quoted literal
        // (QuotedWord).
        let src = b"set a \"quoted\"\nset b {braced}\nproc p {x y} { return $x }\n".to_vec();
        let parser = TclParser::new(src, &path, None);
        assert_variants_is_string!(
            &parser,
            Tcl,
            TclCode,
            [QuotedWord, BracedWordSimple, BracedWord]
        );

        // ---- iRules (3 variants): QuotedWord, BracedWord, BracedWordSimple ----
        // Same Tcl-family braced-word split as Tcl above: the `proc`
        // body is a named `braced_word` (BracedWord), the simple `set`
        // assignment is a `braced_word_simple` (BracedWordSimple), and
        // the quoted literal is a `QuotedWord`.
        let src = b"set a \"quoted\"\nset b {braced}\nproc p {x y} { return $x }\n".to_vec();
        let parser = IrulesParser::new(src, &path, None);
        assert_variants_is_string!(
            &parser,
            Irules,
            IrulesCode,
            [QuotedWord, BracedWordSimple, BracedWord]
        );

        // ---- Php (7 variants): String, String2, String3,
        // EncapsedString, Heredoc, Nowdoc, ShellCommandExpression ----
        // String2 is the `string` type-keyword (`: string` return
        // type, exercised here). String3 is the hidden `_string`
        // supertype (kind_id => "_string" — name starts with `_`),
        // which tree-sitter does NOT emit as a concrete node — see
        // its empirical absence asserted below.
        let src = b"<?php function f(): string { $a = 'single'; $b = \"double\"; $c = <<<EOT\nbody\nEOT;\n$d = <<<'EOT'\nnow\nEOT;\n$e = `ls`; return $a; }\n".to_vec();
        let parser = PhpParser::new(src, &path, None);
        assert_variants_is_string!(&parser, Php, PhpCode, [String, String2]);
        // `Php::String3` is the hidden `_string` supertype — never
        // surfaces as a concrete kind_id in observed parses; the
        // checker still lists it so future grammar revisions that
        // promote it cannot silently bypass `is_string`. Verified
        // unreachable empirically (assertion below proves the
        // fixture does not produce it; the variant is intentionally
        // unverifiable through a positive test until then).
        assert!(
            !ast_has_kind_id(&parser, Php::String3 as u16),
            "Php::String3 is documented as the hidden _string supertype; if it now appears in parses, add a positive variant assertion",
        );
        assert_variants_is_string!(
            &parser,
            Php,
            PhpCode,
            [EncapsedString, Heredoc, Nowdoc, ShellCommandExpression]
        );

        // ---- Elixir (3 variants): String, Charlist, Sigil ----
        // Charlists use single quotes; sigils use `~s(...)` etc.
        let src = b"a = \"hi\"\nb = 'charlist'\nc = ~s(sigil)\n".to_vec();
        let parser = ElixirParser::new(src, &path, None);
        assert_variants_is_string!(&parser, Elixir, ElixirCode, [String, Charlist, Sigil]);

        // ---- Ruby (11 variants): String, ChainedString, BareString,
        // Subshell, Regex, HeredocBody, DelimitedSymbol, SimpleSymbol,
        // StringArray, SymbolArray, Character ----
        // ChainedString: two adjacent string literals (`"a" "b"`).
        // BareString: an unquoted element inside `%w[...]` / inside
        // a string array context. `%w[bare1 bare2]` emits a
        // StringArray whose children are BareString nodes — both
        // surface in the parse. Use a script that produces every
        // form so the per-variant assertion can hit each.
        let src = b"a = \"hi\"\nb = \"x\" \"y\"\nc = `cmd`\nd = /re/\ne = <<EOT\nbody\nEOT\nf = :sym\ng = :\"dsym\"\nh = %w[bare1 bare2]\ni = %i[s1 s2]\nj = ?A\n".to_vec();
        let parser = RubyParser::new(src, &path, None);
        assert_variants_is_string!(
            &parser,
            Ruby,
            RubyCode,
            [
                String,
                ChainedString,
                BareString,
                Subshell,
                Regex,
                HeredocBody,
                DelimitedSymbol,
                SimpleSymbol,
                StringArray,
                SymbolArray,
                Character,
            ]
        );

        // ---- Groovy (1 variant): StringLiteral ----
        // The dekobon Groovy grammar consolidates every string shape
        // (single / double / triple-quoted, slashy `/.../`, dollar-
        // slashy `$/.../$`, GString-interpolated) under one
        // `string_literal` rule, so a single variant suffices —
        // unlike Java, character literals are not a separate node.
        let src =
            b"def m() { def a = \"hi\"; def b = \"\"\"multi\"\"\"; def c = /pat/ }\n".to_vec();
        let parser = GroovyParser::new(src, &path, None);
        assert_variants_is_string!(&parser, Groovy, GroovyCode, [StringLiteral]);
    }

    #[test]
    fn simple_is_string_macro_rejects_non_string_nodes() {
        // Pure-identifier source must produce zero string matches.
        // Catches a regression where a macro invocation accidentally
        // included a too-broad variant (e.g. `Identifier`). Covers
        // every language consolidated under `impl_simple_is_string!`.
        use crate::langs::{
            BashParser, CcommentParser, CppParser, CsharpParser, ElixirParser, GoParser,
            GroovyParser, IrulesParser, JavaParser, KotlinParser, LuaParser, PerlParser,
            PreprocParser, PythonParser, RubyParser, RustParser, TclParser,
        };

        let path = PathBuf::from("test");

        // Each fixture is the most minimal identifier-only input that
        // still parses for the target language. Per-language comments
        // below flag the few cases where the input choice is load-
        // bearing (Bash `s=$y` to avoid `string`-kind expansion nodes,
        // Tcl `set x $y` to keep the bareword as `Word`, etc.).
        assert_no_string_matches!(PreprocParser, &path, b"#define FOO 1\n", "Preproc");
        // Ccomment: input that lexes as a single line comment only.
        assert_no_string_matches!(CcommentParser, &path, b"// just a comment\n", "Ccomment");
        assert_no_string_matches!(CppParser, &path, b"int main() { return x; }\n", "Cpp");
        assert_no_string_matches!(PythonParser, &path, b"x = y\n", "Python");
        assert_no_string_matches!(JavaParser, &path, b"class C { int x = y; }\n", "Java");
        // Csharp: identifier-only field initializer.
        assert_no_string_matches!(CsharpParser, &path, b"class C { int x = y; }\n", "Csharp");
        assert_no_string_matches!(RustParser, &path, b"fn main() { let x = y; }\n", "Rust");
        assert_no_string_matches!(
            GoParser,
            &path,
            b"package main\nfunc main() { _ = x }\n",
            "Go"
        );
        assert_no_string_matches!(KotlinParser, &path, b"fun main() { val x = y }\n", "Kotlin");
        // Perl: identifier-only assignment with no quoted forms.
        assert_no_string_matches!(PerlParser, &path, b"my $x = $y;\n", "Perl");
        assert_no_string_matches!(LuaParser, &path, b"local x = y\n", "Lua");
        // Bash: assignment of one variable to another, no literals.
        // `s=$y` produces only Variable/SimpleExpansion nodes; no
        // string-kind node should appear.
        assert_no_string_matches!(BashParser, &path, b"s=$y\n", "Bash");
        // Tcl: `set` of an unquoted identifier word. The unquoted
        // bareword surfaces as `Word`, not any of the three string
        // kinds.
        assert_no_string_matches!(TclParser, &path, b"set x $y\n", "Tcl");
        // iRules: same Tcl-family `set` of an unquoted identifier word —
        // the bareword surfaces as `Word`, not any string kind.
        assert_no_string_matches!(IrulesParser, &path, b"set x $y\n", "Irules");
        // Php: identifier-only assignment.
        assert_no_string_matches!(PhpParser, &path, b"<?php $x = $y;\n", "Php");
        // Elixir: integer assignment, no string/charlist/sigil.
        assert_no_string_matches!(ElixirParser, &path, b"x = 1\n", "Elixir");
        assert_no_string_matches!(RubyParser, &path, b"x = y\n", "Ruby");
        // Groovy: identifier-only method body.
        assert_no_string_matches!(GroovyParser, &path, b"def m() { def x = y }\n", "Groovy");
    }

    #[test]
    fn mozjs_parses_using_declaration() {
        // Drift marker for the JS-base-grammar bump 0.23.1 -> 0.25.0
        // (#407): the `using` / `await using` explicit-resource-
        // management declaration is a 0.25.0 grammar feature. The
        // bundled mozjs parser was previously stale at 0.23.1 while
        // its marker claimed 0.25.0 (the #400 baseline lie), so this
        // node could not appear. Asserting it surfaces pins the regen
        // against reversion — it fails against the pre-#407 parser.
        let src = "function f() {\n  using r = acquire();\n  return r;\n}\n";
        let parser = MozjsParser::new(src.as_bytes().to_vec(), &PathBuf::from("t.js"), None);
        assert!(
            ast_has_kind_id(&parser, Mozjs::UsingDeclaration as u16),
            "expected Mozjs::UsingDeclaration to appear in the parse",
        );
    }

    #[test]
    fn go_rune_literal_is_not_a_string() {
        // #699 verdict: Go `RuneLiteral` is an operand in `get_op_type`
        // and is flattened by the alterator, but it is deliberately
        // excluded from `is_string` — a rune is a character (`int32`),
        // not a string, exactly as Rust/Cpp `CharLiteral` are operand +
        // flattened yet absent from their `is_string`. This pins the
        // exclusion: the fixture must produce a `rune_literal` (proving
        // the surrounding grammar still emits it) while `GoCode::is_string`
        // returns false for it. The flattening half lives in
        // `alterator.rs::go_rune_literal_flattened_but_not_a_string_kind`.
        use crate::langs::GoParser;
        let path = PathBuf::from("test.go");
        let src = b"package main\nfunc main() { r := 'x'; _ = r }\n".to_vec();
        let parser = GoParser::new(src, &path, None);
        let rune_id = Go::RuneLiteral as u16;
        assert!(
            ast_has_kind_id(&parser, rune_id),
            "fixture should produce a rune_literal node",
        );
        assert_eq!(
            count_string_matches_for_kind(&parser, rune_id, GoCode::is_string),
            0,
            "Go rune_literal must not match is_string (a rune is a char, not a string)",
        );
    }

    /// Asserts `is_func` / `is_closure` answer the same off a known
    /// chain as off a `Node::parent` climb, for every node of `code`.
    /// Returns the `(functions, closures)` the chain path counted, so a
    /// caller can additionally pin what the predicates actually decided.
    ///
    /// Both predicates fold their ancestor lookups into one boolean, so
    /// a wrong ancestor does not fail — it silently moves a function
    /// into the closure column. The counters keep a fixture honest: both
    /// answers have to occur, for both predicates, or the parity holds
    /// only over nodes the predicates never classify.
    fn assert_func_parity<L: crate::traits::LanguageInfo + Checker>(
        label: &str,
        code: &[u8],
    ) -> (usize, usize) {
        let mut funcs = 0;
        let mut closures = 0;
        let visited = for_each_node_with_chain::<L>(code, |node, chain| {
            let known = Ancestors::known(chain);
            let climbing = Ancestors::unknown();
            assert_eq!(
                L::is_func(node, known),
                L::is_func(node, climbing),
                "{label}: is_func disagrees on {} at row {}",
                node.kind(),
                node.start_row()
            );
            assert_eq!(
                L::is_closure(node, known),
                L::is_closure(node, climbing),
                "{label}: is_closure disagrees on {} at row {}",
                node.kind(),
                node.start_row()
            );
            funcs += usize::from(L::is_func(node, known));
            closures += usize::from(L::is_closure(node, known));
        });
        assert!(visited > 20, "{label}: fixture is too small to prove much");
        assert!(
            funcs > 0 && closures > 0,
            "{label}: fixture must contain both a named function and a closure, \
             else parity holds over nodes the walk never classifies: \
             {funcs} functions, {closures} closures"
        );
        (funcs, closures)
    }

    /// The JS-family `is_func` / `is_closure` must classify a node the
    /// same whether they read the walker's ancestor chain or climb with
    /// `Node::parent` (#1088).
    ///
    /// These are the predicates with the longest ancestor lookup: an
    /// upward walk, its `is_else_if` filter, and a `has_sibling`
    /// adjacency check.
    #[test]
    fn js_func_and_closure_agree_between_known_and_climbing() {
        // One of each shape the walk distinguishes: an arrow bound to a
        // name, an arrow passed positionally, a `function` expression
        // named by an object `pair`, and a nested arrow whose only
        // enclosing frame is a `statement_block`.
        //
        // The returned object literal is load-bearing. It is the one
        // shape here where the trailing `has_sibling` adjacency check
        // decides the answer: the upward walk stops at the `return`
        // having counted no binding, so whether the arrow is a *named*
        // function rests entirely on the `property_identifier` beside
        // it. Everywhere else a binding is found first and the adjacency
        // check is never reached, which would leave a wrong parent
        // lookup invisible.
        let code = concat!(
            "const f = a => { g(() => 1); a => 2; };\n",
            "const o = { m: function () { return 1; } };\n",
            "function h() { return { k: (a) => a + 1 }; }\n",
            "function i() { return function () { return 2; }; }\n",
        )
        .as_bytes();
        // `is_js_func_and_closure_checker!` expands separately against
        // each grammar's own `kind_id` enum, so the four impls are four
        // distinct variant lists rather than one shared body. Checking
        // only JavaScript would leave a drifted id in any of the other
        // three uncovered.
        assert_func_parity::<crate::langs::JavascriptCode>("javascript", code);
        assert_func_parity::<crate::langs::MozjsCode>("mozjs", code);
        assert_func_parity::<crate::langs::TypescriptCode>("typescript", code);
        assert_func_parity::<crate::langs::TsxCode>("tsx", code);
    }

    /// Ruby's `is_closure` is the one non-JS predicate that needs an
    /// ancestor: a `{ … }` block is a closure unless its parent is the
    /// `Lambda` that already counted it (#465), so it reads the chain
    /// for the same reason the JS family does (#1088).
    ///
    /// The stabby lambda is the discriminating shape. Its body block is
    /// the only node here whose answer depends on the parent lookup —
    /// every other block's parent is a `Call`, which the arm accepts
    /// whether the lookup is right or wrong.
    #[test]
    fn ruby_block_closure_agrees_between_known_and_climbing() {
        let code = concat!(
            "def m\n",
            "  f = ->(z) { z + 1 }\n",
            "  [1].each { |x| x }\n",
            "  lambda { 2 }\n",
            "end\n",
        )
        .as_bytes();
        let (funcs, closures) = assert_func_parity::<crate::langs::RubyCode>("ruby", code);
        assert_eq!(funcs, 1, "only `def m` is a named function");
        assert_eq!(
            closures, 3,
            "the stabby lambda, the `each` block and the `lambda` block \
             are three closures — the stabby lambda's own body block must \
             not add a fourth, which is the answer the parent lookup decides"
        );
    }
}