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
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
//! Composition tree derivation from component source profiles.
//!
//! Builds a `CompositionTree` for a component family by combining:
//! 1. Family member identification (from index file exports)
//! 2. Children slot tracing (where `{children}` lands in each component)
//! 3. BEM token analysis (structural parent-child relationships)
//! 4. Rendered components (which components each family member renders internally)
//!
//! The resulting tree describes the expected JSX composition structure
//! for consumers of the component family.
use crate::css_profile::CssBlockProfile;
use crate::sd_types::{
ChildRelationship, ComponentSourceProfile, CompositionEdge, CompositionTree, EdgeStrength,
};
use std::collections::{HashMap, HashSet};
use tracing::debug;
// ── Evidence-based composition tree builder ─────────────────────────────
/// Context for projecting a delegate family's composition tree edges onto
/// a wrapper family. Used when a family like Dropdown wraps another family
/// like Menu — each Dropdown component is a thin wrapper around a Menu
/// counterpart (DropdownList wraps MenuList, DropdownItem wraps MenuItem).
///
/// The delegate tree's edges are projected onto the wrapper family so that
/// composition constraints (context, DOM nesting, CSS) are inherited.
pub struct DelegateContext<'a> {
/// The delegate family's resolved composition tree.
pub delegate_tree: &'a CompositionTree,
/// Mapping: wrapper component name → delegate component name.
/// E.g., "DropdownList" → "MenuList", "DropdownItem" → "MenuItem".
pub wrapper_to_delegate: HashMap<String, String>,
}
/// Build a composition tree using CSS structure, React patterns, and HTML
/// semantics instead of BEM-based edge creation.
///
/// BEM determines family membership only. All parent-child edges come from:
/// 1. Internal rendering (A renders B in JSX)
/// 1.5. Delegate tree projection (edges from a delegate family's tree)
/// 2. CSS direct-child selectors (`.A > .B`)
/// 3. CSS grid parent-child (`A` has grid-template, `B` has grid-column)
/// 4. CSS flex context (A wraps children in flex container, B is not a grid child)
/// 5. CSS descendant selectors (`.A .B`)
/// 5.5. CSS layout children (shared CSS rule with flex-wrap/gap implies containment)
/// 6. React context (A provides, B consumes)
/// 7. DOM nesting (A wraps children in `<ul>`, B renders `<li>`)
/// 8. cloneElement threading (A injects props into children that B declares)
/// 8.5. BEM element orphan fallback (orphan BEM elements → root→member)
/// 8.6. Secondary BEM block sub-root fallback
/// 8.7. Prop-passed detection (ReactNode/ReactElement props → PropPassed edges)
/// 9. Suppress root edges when intermediate exists
/// 10. Drop unconnected members (exported orphans are retained)
///
/// **Signal combining**: When multiple steps detect the same (parent, child)
/// pair, their strengths are combined via `EdgeStrength::combine()` (OR per
/// dimension). This ensures that CSS `>` (Structural) + DOM nesting (Required)
/// produces Required, rather than the first signal winning and the second
/// being discarded.
///
/// Record a signal for a (parent, child) edge. If the edge already exists,
/// combines the new strength with the existing one. If it's new, creates it.
///
/// Relationship priority: Internal > PropPassed > DirectChild.
/// Evidence strings are concatenated with " + " to preserve the audit trail.
#[allow(clippy::too_many_arguments)]
fn record_signal(
tree: &mut CompositionTree,
edge_map: &mut HashMap<(String, String), usize>,
parent: String,
child: String,
strength: EdgeStrength,
relationship: ChildRelationship,
evidence: String,
prop_name: Option<String>,
) {
let key = (parent.clone(), child.clone());
if let Some(&idx) = edge_map.get(&key) {
// Upgrade existing edge
let edge = &mut tree.edges[idx];
edge.strength = edge.strength.combine(&strength);
edge.required = edge.strength.parent_requires_child();
// Upgrade relationship if new one is more specific
if relationship == ChildRelationship::Internal
|| (relationship == ChildRelationship::PropPassed
&& edge.relationship == ChildRelationship::DirectChild)
{
edge.relationship = relationship;
}
if let Some(pn) = prop_name {
edge.prop_name = Some(pn);
}
// Append evidence
if let Some(ref mut ev) = edge.bem_evidence {
ev.push_str(" + ");
ev.push_str(&evidence);
}
} else {
let idx = tree.edges.len();
tree.edges.push(CompositionEdge {
parent,
child,
relationship,
required: strength.parent_requires_child(),
bem_evidence: Some(evidence),
strength,
prop_name,
});
edge_map.insert(key, idx);
}
}
pub fn build_composition_tree_v2(
profiles: &HashMap<String, ComponentSourceProfile>,
family_exports: &[String],
css_profiles: Option<&HashMap<String, CssBlockProfile>>,
primary_css_block: Option<&str>,
delegate_contexts: &[DelegateContext<'_>],
// Barrel-file exports — components exported in `index.ts`. Members in
// this set are retained even with zero edges (as orphans). If `None`,
// Step 10 drops all zero-edge members (legacy behavior).
exported_members: Option<&[String]>,
) -> Option<CompositionTree> {
if family_exports.is_empty() {
return None;
}
let root = family_exports[0].clone();
let family_set: HashSet<&str> = family_exports.iter().map(|s| s.as_str()).collect();
let mut tree = CompositionTree {
root: root.clone(),
family_members: family_exports.to_vec(),
edges: Vec::new(),
};
// Track existing edges for O(1) lookup. Maps (parent, child) to the
// edge's index in tree.edges. When multiple signals target the same
// pair, their strengths are combined (ORed per dimension) instead of
// the first signal winning.
let mut edge_map: HashMap<(String, String), usize> = HashMap::new();
// Resolve the primary CSS profile from the profiles map.
let css_profile = primary_css_block.and_then(|key| css_profiles?.get(key));
// Build CSS element → component mapping for CSS-based steps.
// Maps a CSS BEM element name (e.g., "content-section") to the component
// that uses the corresponding `styles.xxx` token.
let css_to_component = if let Some(css_prof) = css_profile {
build_css_element_to_component_map(profiles, family_exports, &css_prof.block)
} else {
HashMap::new()
};
// ── Step 1: Internal rendering ──────────────────────────────────
for parent_name in family_exports {
let Some(parent_profile) = profiles.get(parent_name) else {
continue;
};
for rendered in &parent_profile.rendered_components {
if family_set.contains(rendered.name.as_str()) {
let strength = if rendered.conditional {
EdgeStrength::Allowed
} else {
EdgeStrength::Wrapper
};
let evidence = if rendered.conditional {
"conditionally rendered".to_string()
} else {
"internally rendered".to_string()
};
record_signal(
&mut tree,
&mut edge_map,
parent_name.clone(),
rendered.name.clone(),
strength,
ChildRelationship::Internal,
evidence,
None,
);
}
}
}
// ── Step 1.5: Delegate tree projection ──────────────────────────
// For wrapper families (e.g., Dropdown wraps Menu), project edges
// from the delegate family's tree onto this tree. Each edge in the
// delegate tree where BOTH parent and child have wrapper mappings
// produces a corresponding edge in this tree.
//
// This runs before Step 10 so projected edges prevent members from
// being dropped. Strength is Allowed because the delegation itself
// is a design choice — the underlying constraints are Required in
// the delegate family but optional at the wrapper level.
for ctx in delegate_contexts {
// Build reverse map: delegate component → wrapper component
let delegate_to_wrapper: HashMap<&str, &str> = ctx
.wrapper_to_delegate
.iter()
.map(|(w, d)| (d.as_str(), w.as_str()))
.collect();
for edge in &ctx.delegate_tree.edges {
let Some(&wrapper_parent) = delegate_to_wrapper.get(edge.parent.as_str()) else {
continue;
};
let Some(&wrapper_child) = delegate_to_wrapper.get(edge.child.as_str()) else {
continue;
};
// Both must be in this family
if !family_set.contains(wrapper_parent) || !family_set.contains(wrapper_child) {
continue;
}
// Skip self-edges
if wrapper_parent == wrapper_child {
continue;
}
debug!(
parent = %wrapper_parent,
child = %wrapper_child,
delegate_parent = %edge.parent,
delegate_child = %edge.child,
delegate_family = %ctx.delegate_tree.root,
"delegate tree projection"
);
record_signal(
&mut tree,
&mut edge_map,
wrapper_parent.to_string(),
wrapper_child.to_string(),
edge.strength.clone(),
edge.relationship.clone(),
format!(
"Delegate projection from {} tree: {} wraps {}, {} wraps {}",
ctx.delegate_tree.root, wrapper_parent, edge.parent, wrapper_child, edge.child,
),
None,
);
}
}
if let Some(css_prof) = css_profile {
// ── Step 2: CSS direct-child selectors ──────────────────────
for (css_parent, css_child) in &css_prof.direct_child_nesting {
let Some(parent_comps) = css_to_component.get(css_parent.as_str()) else {
continue;
};
let Some(child_comps) = css_to_component.get(css_child.as_str()) else {
continue;
};
// When an element maps to multiple components, all edges from
// that element are Allowed — the CSS class is ambiguous across
// components and could be either one.
let parent_ambiguous = parent_comps.len() > 1;
let child_ambiguous = child_comps.len() > 1;
for parent_comp in parent_comps {
for child_comp in child_comps {
if parent_comp == child_comp {
continue;
}
// If the reverse edge already exists (child→parent from
// a prior step), this creates a bidirectional pair.
// Bidirectional CSS relationships represent optional
// recursive nesting (e.g., WizardNavItem > WizardNav
// for sub-navigation), not mandatory containment.
let reverse_key = (child_comp.clone(), parent_comp.clone());
let has_reverse = edge_map.contains_key(&reverse_key);
let strength = if *child_comp == root
|| parent_ambiguous
|| child_ambiguous
|| has_reverse
{
EdgeStrength::Allowed
} else {
EdgeStrength::Structural
};
record_signal(
&mut tree,
&mut edge_map,
parent_comp.clone(),
child_comp.clone(),
strength,
ChildRelationship::DirectChild,
format!("CSS direct child: .{} > .{}", css_parent, css_child),
None,
);
}
}
}
// ── Step 3: CSS grid parent-child ───────────────────────────
// Find grid containers (has_grid_template) and grid children
// (has_grid_column/grid_row). Map to components.
// With multi-component mapping, an element may map to multiple
// components — expand each to (element, component) pairs.
let grid_containers: Vec<(&str, String)> = css_prof
.elements
.iter()
.filter(|(_, info)| info.has_grid_template && info.display_values.contains("grid"))
.flat_map(|(el, _)| {
css_to_component
.get(el.as_str())
.into_iter()
.flat_map(move |comps| {
comps.iter().map(move |comp| (el.as_str(), comp.clone()))
})
})
.collect();
for (child_el, child_info) in &css_prof.elements {
if !child_info.has_grid_column && !child_info.has_grid_row {
continue;
}
let Some(child_comps) = css_to_component.get(child_el.as_str()) else {
continue;
};
let child_ambiguous = child_comps.len() > 1;
for child_comp in child_comps {
// Find the best grid container for this child.
let mut best_parent: Option<&str> = None;
for (container_el, container_comp) in &grid_containers {
if *container_comp == *child_comp {
continue;
}
if css_prof
.direct_child_nesting
.contains(&(container_el.to_string(), child_el.clone()))
{
best_parent = Some(container_comp);
break;
}
}
if best_parent.is_none() {
for (container_el, container_comp) in &grid_containers {
if *container_comp == *child_comp {
continue;
}
if css_prof
.descendant_nesting
.contains(&(container_el.to_string(), child_el.clone()))
{
best_parent = Some(container_comp);
break;
}
}
}
if best_parent.is_none() && grid_containers.len() == 1 {
let (_, ref container_comp) = grid_containers[0];
if *container_comp != *child_comp {
best_parent = Some(container_comp);
}
}
if let Some(parent_comp) = best_parent {
let strength = if *child_comp == root || child_ambiguous {
EdgeStrength::Allowed
} else {
EdgeStrength::Structural
};
record_signal(
&mut tree,
&mut edge_map,
parent_comp.to_string(),
child_comp.clone(),
strength,
ChildRelationship::DirectChild,
format!(
"CSS grid: {} has grid-template, {} has grid-column/row",
parent_comp, child_comp
),
None,
);
}
}
}
// Step 3b: Implicit grid children — elements inside a non-root
// grid container that don't have explicit grid-column/grid-row.
// Example: DescriptionListTerm and DescriptionListDescription are
// implicit grid children of DescriptionListGroup (which has
// grid-template-rows).
//
// Only applies to non-root grid containers (containers that are
// themselves grid children of the root grid).
let non_root_grid_containers: Vec<(&str, String)> = grid_containers
.iter()
.filter(|(el, _)| {
// Must not be root and must itself be a grid child
!el.is_empty()
&& css_prof
.elements
.get(*el)
.is_some_and(|info| info.has_grid_column || info.has_grid_row)
})
.cloned()
.collect();
if !non_root_grid_containers.is_empty() {
for (child_el, child_info) in &css_prof.elements {
// Skip elements that already have grid positioning (handled above)
if child_info.has_grid_column || child_info.has_grid_row {
continue;
}
// Skip root element
if child_el.is_empty() {
continue;
}
// Skip elements that are grid containers themselves
if child_info.has_grid_template {
continue;
}
let Some(child_comps) = css_to_component.get(child_el.as_str()) else {
continue;
};
let child_ambiguous = child_comps.len() > 1;
for child_comp in child_comps {
// Skip if already has a non-root parent
if tree
.edges
.iter()
.any(|e| e.child == *child_comp && e.parent != root)
{
continue;
}
let mut best_parent: Option<&str> = None;
for (container_el, container_comp) in &non_root_grid_containers {
if *container_comp == *child_comp {
continue;
}
if css_prof
.direct_child_nesting
.contains(&(container_el.to_string(), child_el.clone()))
|| css_prof
.descendant_nesting
.contains(&(container_el.to_string(), child_el.clone()))
{
best_parent = Some(container_comp);
break;
}
}
if best_parent.is_none() && non_root_grid_containers.len() == 1 {
let (_, ref comp) = non_root_grid_containers[0];
if *comp != *child_comp {
best_parent = Some(comp);
}
}
if let Some(parent_comp) = best_parent {
let strength = if *child_comp == root || child_ambiguous {
EdgeStrength::Allowed
} else {
EdgeStrength::Structural
};
record_signal(
&mut tree,
&mut edge_map,
parent_comp.to_string(),
child_comp.clone(),
strength,
ChildRelationship::DirectChild,
format!(
"CSS grid: {} is grid container, {} is implicit grid child",
parent_comp, child_comp
),
None,
);
}
}
}
}
// ── Step 3c: Re-parent through display:contents intermediaries ──
//
// When a family member's CSS element is a "mode-switcher" (switches
// between `display: contents` and `display: flex`), it acts as an
// invisible grid-passthrough in one mode and a visible flex container
// in the other. Grid children that have `grid_column_reverts`
// (their grid-column is `initial`/`unset`/`revert` in some mode)
// are actually DOM children of the mode-switcher, not direct grid
// children of the root.
//
// Example: Masthead (grid root) → MastheadMain (mode-switcher:
// display:contents in stack, display:flex in inline). MastheadBrand
// has grid-column that reverts to `initial` in inline mode — it's
// actually inside MastheadMain, not a direct child of Masthead.
//
// We also use `variable_child_refs` (from CSS custom property naming
// like `--masthead__main--toggle--GridColumn`) and `has_containment`
// (from `:has()` selectors) to assign non-grid children to the
// mode-switcher.
{
// Find mode-switcher CSS elements and map them to components
let mode_switcher_components: Vec<(String, String)> = css_prof
.elements
.iter()
.filter(|(_, info)| info.is_mode_switcher)
.filter_map(|(element, _)| {
let comps = css_to_component.get(element.as_str())?;
// Only take family members
comps
.iter()
.find(|c| family_set.contains(c.as_str()) && **c != root)
.map(|c| (c.clone(), element.clone()))
})
.collect();
for (switcher_comp, switcher_element) in &mode_switcher_components {
// 1. Re-parent grid children with grid_column_reverts from
// root → switcher
for (element_name, info) in &css_prof.elements {
if !info.grid_column_reverts || !info.has_grid_column {
continue;
}
// Don't re-parent the mode-switcher to itself
if element_name == switcher_element {
continue;
}
// Find the component for this CSS element
let Some(child_comps) = css_to_component.get(element_name.as_str()) else {
continue;
};
for child_comp in child_comps {
if !family_set.contains(child_comp.as_str()) || child_comp == &root {
continue;
}
// Check if this child currently has a root→child edge
let has_root_edge = tree
.edges
.iter()
.any(|e| e.parent == root && e.child == *child_comp);
if !has_root_edge {
continue;
}
// Re-parent: remove root→child edge, add switcher→child
if let Some(idx) = tree
.edges
.iter()
.position(|e| e.parent == root && e.child == *child_comp)
{
tree.edges.remove(idx);
// Remove from edge_map too
edge_map.remove(&(root.clone(), child_comp.clone()));
}
record_signal(
&mut tree,
&mut edge_map,
switcher_comp.clone(),
child_comp.clone(),
EdgeStrength::Structural,
ChildRelationship::DirectChild,
format!(
"CSS display:contents re-parent: {} has grid_column_reverts, \
{} is mode-switcher (display:contents ↔ flex)",
child_comp, switcher_comp
),
None,
);
}
}
// 2. Assign non-grid children via variable_child_refs
// e.g., main.variable_child_refs = {"toggle", "content"}
// → MastheadToggle goes under MastheadMain
//
// Guard: skip children whose CSS element has grid-column
// WITHOUT grid_column_reverts — those are genuine grid
// children of the root, not nested inside the mode-switcher.
// (e.g., MastheadContent has grid-column that never reverts,
// so it stays as a direct child of Masthead even though the
// variable --masthead__main--toggle--content-- references it.)
if let Some(switcher_info) = css_prof.elements.get(switcher_element.as_str()) {
for child_ref in &switcher_info.variable_child_refs {
// Skip if the child CSS element is a genuine grid child
if let Some(child_css) = css_prof.elements.get(child_ref.as_str()) {
if child_css.has_grid_column && !child_css.grid_column_reverts {
continue;
}
}
// Map the child_ref CSS element to a component
let Some(child_comps) = css_to_component.get(child_ref.as_str()) else {
continue;
};
for child_comp in child_comps {
if !family_set.contains(child_comp.as_str())
|| child_comp == &root
|| child_comp == switcher_comp
{
continue;
}
// Skip children that already have a non-root parent
// (don't override Step 1 internal rendering or
// earlier re-parenting)
let has_non_root_parent = tree
.edges
.iter()
.any(|e| e.child == *child_comp && e.parent != root);
if has_non_root_parent {
continue;
}
record_signal(
&mut tree,
&mut edge_map,
switcher_comp.clone(),
child_comp.clone(),
EdgeStrength::Allowed,
ChildRelationship::DirectChild,
format!(
"CSS variable nesting: --{}__{}--{}-- references {} inside {}",
css_prof.block,
switcher_element,
child_ref,
child_comp,
switcher_comp
),
None,
);
}
}
}
}
// Note: has_containment from :has() selectors is NOT consumed here.
// The :has() signal requires more careful handling (bidirectional
// false positives, incorrect element-to-component mapping) and
// should be added as a separate, guarded step in the future.
}
// ── Step 4: CSS flex context ────────────────────────────────
// Only fires when the ROOT component's CSS slot is a grid container.
// In that case, family members WITHOUT grid positioning can't be
// direct children of root — they need a flex intermediary.
//
// Example: Toolbar root is display:grid. ToolbarContent wraps
// children in content-section (display:flex). ToolbarItem has no
// grid-column so it goes under ToolbarContent, not Toolbar.
let root_is_grid = {
let root_css = css_prof.elements.get("");
root_css
.is_some_and(|info| info.display_values.contains("grid") && info.has_grid_template)
};
if root_is_grid {
// Find non-root components whose children_slot is a flex container
let flex_parents: Vec<(String, String)> = family_exports
.iter()
.filter(|name| **name != root)
.filter_map(|name| {
let prof = profiles.get(name)?;
if !prof.has_children_prop {
return None;
}
let innermost_token = prof
.children_slot_detail
.iter()
.rev()
.find_map(|(_, token)| token.as_ref())?;
let block_camel = &css_prof.block;
let element_camel = innermost_token.strip_prefix(block_camel.as_str())?;
if element_camel.is_empty() {
return None;
}
let element_camel_lower = {
let mut s = element_camel.to_string();
if let Some(c) = s.get_mut(0..1) {
c.make_ascii_lowercase();
}
s
};
let element_kebab = camel_to_kebab(&element_camel_lower);
let css_info = css_prof.elements.get(&element_kebab)?;
if css_info.display_values.contains("flex") {
Some((name.clone(), element_kebab))
} else {
None
}
})
.collect();
if !flex_parents.is_empty() {
for child_name in family_exports {
if child_name == &root {
continue;
}
// Skip children that already have a non-root parent
if tree
.edges
.iter()
.any(|e| e.child == *child_name && e.parent != root)
{
continue;
}
// Skip flex parents themselves (they're grid children of root)
if flex_parents.iter().any(|(p, _)| p == child_name) {
continue;
}
// Skip children whose CSS element has grid positioning
let child_is_grid = profiles.get(child_name).is_some_and(|cp| {
cp.css_tokens_used.iter().any(|token| {
// Strip "styles." prefix, skip modifiers
let raw = if let Some(rest) = token.strip_prefix("styles.") {
if rest.starts_with("modifiers.") {
return false;
}
rest
} else {
token.as_str()
};
let block_camel = &css_prof.block;
if let Some(suffix) = raw.strip_prefix(block_camel.as_str()) {
if suffix.is_empty() {
return false;
}
let mut el = suffix.to_string();
if let Some(c) = el.get_mut(0..1) {
c.make_ascii_lowercase();
}
let el_kebab = camel_to_kebab(&el);
if let Some(info) = css_prof.elements.get(&el_kebab) {
return info.has_grid_column || info.has_grid_row;
}
}
false
})
});
if child_is_grid {
continue;
}
// Determine if the child has explicit sizing (width/max-height).
// Sized children belong in rigid containers, not wrapping toolbars.
let child_has_sizing = profiles.get(child_name).is_some_and(|cp| {
cp.css_tokens_used.iter().any(|token| {
let raw = if let Some(rest) = token.strip_prefix("styles.") {
if rest.starts_with("modifiers.") {
return false;
}
rest
} else {
token.as_str()
};
let block_camel = &css_prof.block;
if let Some(suffix) = raw.strip_prefix(block_camel.as_str()) {
if suffix.is_empty() {
return false;
}
let mut el = suffix.to_string();
if let Some(c) = el.get_mut(0..1) {
c.make_ascii_lowercase();
}
let el_kebab = camel_to_kebab(&el);
if let Some(info) = css_prof.elements.get(&el_kebab) {
return info.has_sizing;
}
}
false
})
});
// Match to best flex parent. Tiebreaker order:
// 1. Existing edge from another signal
// 2. Rigid container (flex_shrink_zero && !flex_wrap) preferred
// for children with has_sizing — sized elements like logos
// belong in rigid brand containers, not wrapping toolbars
// 3. Longest CSS element name (fallback)
let best = flex_parents
.iter()
.filter(|(p, _)| p != child_name)
.max_by_key(|(p, el)| {
let has_other_edge = tree
.edges
.iter()
.any(|e| e.parent == *p && e.child == *child_name);
let rigidity = if child_has_sizing {
let info = css_prof.elements.get(el.as_str());
let is_rigid =
info.is_some_and(|i| i.flex_shrink_zero && !i.flex_wrap);
is_rigid as usize
} else {
0
};
(has_other_edge as usize, rigidity, el.len())
});
if let Some((best_parent, _)) = best {
record_signal(
&mut tree,
&mut edge_map,
best_parent.clone(),
child_name.clone(),
EdgeStrength::Allowed,
ChildRelationship::DirectChild,
format!(
"CSS flex context: {} wraps children in flex, root is grid",
best_parent
),
None,
);
}
}
}
}
// ── Step 5: CSS descendant selectors ────────────────────────
for (css_parent, css_child) in &css_prof.descendant_nesting {
let Some(parent_comps) = css_to_component.get(css_parent.as_str()) else {
continue;
};
let Some(child_comps) = css_to_component.get(css_child.as_str()) else {
continue;
};
for parent_comp in parent_comps {
for child_comp in child_comps {
if parent_comp == child_comp {
continue;
}
// When the parent is the family root and the child is a
// BEM element that acts as a generic wrapper (renders
// div/span and accepts children), annotate the evidence
// with "BEM element" so downstream heuristics (e.g.,
// ExclusiveWrapper) can identify it regardless of which
// signal step connected it first.
let is_bem_wrapper = *parent_comp == root
&& child_comp.starts_with(&root)
&& child_comp.len() > root.len()
&& profiles.get(child_comp).is_some_and(|p| {
p.has_children_prop
&& p.children_slot_path
.first()
.is_some_and(|tag| matches!(tag.as_str(), "div" | "span"))
});
let evidence = if is_bem_wrapper {
format!(
"CSS descendant (BEM element): .{} .{}",
css_parent, css_child
)
} else {
format!("CSS descendant: .{} .{}", css_parent, css_child)
};
record_signal(
&mut tree,
&mut edge_map,
parent_comp.clone(),
child_comp.clone(),
EdgeStrength::Allowed,
ChildRelationship::DirectChild,
evidence,
None,
);
}
}
}
// ── Step 5.5: CSS layout children ───────────────────────────
// Consume `layout_children` from the CSS profile — pairs of BEM
// elements where one is a layout container (has flex-wrap/gap/grid)
// and the other is a co-rule sibling. Maps both to components and
// creates an edge.
//
// This data was previously computed but never consumed. It catches
// intermediate nesting within families (e.g., EmptyStateFooter →
// EmptyStateActions from a shared CSS rule with flex-wrap).
for (css_container, css_child) in &css_prof.layout_children {
let Some(container_comps) = css_to_component.get(css_container.as_str()) else {
continue;
};
let Some(child_comps) = css_to_component.get(css_child.as_str()) else {
continue;
};
for container_comp in container_comps {
for child_comp in child_comps {
if container_comp == child_comp {
continue;
}
record_signal(
&mut tree,
&mut edge_map,
container_comp.clone(),
child_comp.clone(),
EdgeStrength::Allowed,
ChildRelationship::DirectChild,
format!(
"CSS layout container: .{} wraps .{} (shared CSS rule with flex-wrap/gap)",
css_container, css_child
),
None,
);
}
}
}
}
// ── Step 6: React context ───────────────────────────────────────
infer_context_nesting(&mut tree, &mut edge_map, profiles, family_exports);
// ── Step 7: DOM nesting ─────────────────────────────────────────
infer_dom_nesting(&mut tree, &mut edge_map, profiles, family_exports);
// ── Step 8: cloneElement threading ──────────────────────────────
infer_clone_element_nesting(&mut tree, &mut edge_map, profiles, family_exports);
// ── Step 8.5: BEM element orphan fallback ──────────────────────
// For family members with zero incoming edges after all structural
// signals, connect them to the root if they are BEM elements of the
// root's block. This catches children-passthrough families where the
// parent renders `{children}` and sub-components are placed by the
// consumer in JSX (e.g., EmptyState → EmptyStateBody).
//
// Guards:
// 1. Zero incoming edges (orphan gate — prevents creating wrong edges
// for already-connected components in Category 3 families)
// 2. Member appears in css_element_to_component_map (has BEM element
// CSS tokens of the root's block)
// 3. BEM independence check: member must NOT have its own distinct
// BEM block (prevents false edges for collision families like
// Label/LabelGroup, Menu/MenuToggle)
// 4. Root has has_children_prop (root must accept children)
{
let root_has_children = profiles.get(&root).is_some_and(|p| p.has_children_prop);
let root_bem_block = profiles
.get(&root)
.and_then(|p| p.bem_block.as_deref())
.map(|s| s.to_string());
if root_has_children && !css_to_component.is_empty() {
// Collect all members that currently have incoming edges (owned to avoid borrow)
let parented: HashSet<String> = tree.edges.iter().map(|e| e.child.clone()).collect();
// Collect the set of components that are BEM elements (values in the map)
let bem_element_components: HashSet<&str> = css_to_component
.values()
.flat_map(|comps| comps.iter().map(|s| s.as_str()))
.collect();
let mut fallback_edges = Vec::new();
for member in family_exports {
if member == &root {
continue;
}
// Guard 1: only orphans (no incoming edges)
if parented.contains(member) {
continue;
}
// Guard 2: must be a BEM element of the root's block
if !bem_element_components.contains(member.as_str()) {
continue;
}
// Guard 3: BEM independence — skip if member has its own
// distinct BEM block (e.g., LabelGroup has block "labelGroup"
// which differs from Label's "label")
if let Some(member_bem) = profiles.get(member).and_then(|p| p.bem_block.as_deref())
{
if let Some(ref root_block) = root_bem_block {
if member_bem != root_block.as_str() {
debug!(
root = %root,
member = %member,
member_bem = %member_bem,
root_block = %root_block,
"BEM orphan fallback: skipping independent block"
);
continue;
}
}
}
debug!(
root = %root,
member = %member,
"BEM orphan fallback: connecting orphan to root"
);
fallback_edges.push((root.clone(), member.clone()));
}
for (parent, child) in fallback_edges {
record_signal(
&mut tree,
&mut edge_map,
parent.clone(),
child.clone(),
EdgeStrength::Allowed,
ChildRelationship::DirectChild,
format!(
"BEM element fallback: {} is a BEM element of {}'s block with no other parent",
child, parent
),
None,
);
}
}
}
// ── Step 8.6: Secondary BEM block sub-root fallback ───────────
// Some families have components that use a different BEM block than the
// root (e.g., Modal root uses "backdrop" while ModalBody uses "modalBox",
// Tabs root uses "tabs" while TabContentBody uses "tabContent").
//
// For each secondary block:
// 1. Build a secondary css_to_component map for that block
// 2. Find the sub-root: the component that maps to element "" (root)
// of the secondary block
// 3. Run Step 8.5 logic using the sub-root: orphan members whose
// bem_block matches the secondary block get an Allowed edge to the
// sub-root
//
// After collapse_internal_nodes, if the sub-root is internal (non-exported),
// edges propagate to the family root automatically.
if let Some(css_profs) = css_profiles {
// Collect all distinct BEM blocks used by family members that
// differ from the root's BEM block. These need sub-root fallback
// because the root's Step 8.5 only connects orphans whose
// bem_block matches the root's block.
//
// NOTE: we compare against the root's block, NOT the primary
// CSS profile key. The primary CSS key may differ from the root's
// block (e.g., Modal: root block = "backdrop", primary CSS key =
// "modalBox" via dominant vote). The sub-root fallback is about
// which components can't be reached from the root — that's
// determined by the root's block, not the CSS file selection.
let root_block = profiles
.get(&root)
.and_then(|p| p.bem_block.as_deref())
.unwrap_or("");
let mut secondary_blocks: HashSet<&str> = HashSet::new();
for name in family_exports {
if let Some(prof) = profiles.get(name) {
if let Some(ref block) = prof.bem_block {
if block != root_block {
secondary_blocks.insert(block.as_str());
}
}
}
}
for sec_block in &secondary_blocks {
// Only process if we have a CSS profile for this block
if !css_profs.contains_key(*sec_block) {
continue;
}
// Build secondary CSS element → component map
let sec_css_to_component =
build_css_element_to_component_map(profiles, family_exports, sec_block);
// Find the sub-root: component(s) that map to element "" (the
// block root) in the secondary map
let sub_roots: Vec<&str> = sec_css_to_component
.get("")
.into_iter()
.flat_map(|comps| comps.iter().map(|s| s.as_str()))
.collect();
// Find the best sub-root: prefer one with has_children_prop
let sub_root = sub_roots
.iter()
.find(|name| profiles.get(**name).is_some_and(|p| p.has_children_prop))
.or(sub_roots.first())
.copied();
let Some(sub_root) = sub_root else {
continue;
};
let sub_root_has_children = profiles.get(sub_root).is_some_and(|p| p.has_children_prop);
if !sub_root_has_children {
continue;
}
// Collect BEM element components from the secondary map
let sec_bem_components: HashSet<&str> = sec_css_to_component
.values()
.flat_map(|comps| comps.iter().map(|s| s.as_str()))
.collect();
// Refresh parented set (may have changed from primary Step 8.5)
let parented: HashSet<String> = tree.edges.iter().map(|e| e.child.clone()).collect();
let mut sec_fallback_edges = Vec::new();
for member in family_exports {
if member == sub_root || member == &root {
continue;
}
// Guard 1: only orphans
if parented.contains(member) {
continue;
}
// Guard 2: must be in the secondary CSS element map
if !sec_bem_components.contains(member.as_str()) {
continue;
}
// Guard 3: member's BEM block must match this secondary block
if let Some(member_bem) = profiles.get(member).and_then(|p| p.bem_block.as_deref())
{
if member_bem != *sec_block {
continue;
}
} else {
continue;
}
debug!(
sub_root = %sub_root,
member = %member,
secondary_block = %sec_block,
"Secondary block fallback: connecting orphan to sub-root"
);
sec_fallback_edges.push((
sub_root.to_string(),
member.clone(),
(*sec_block).to_string(),
));
}
for (parent, child, block) in sec_fallback_edges {
record_signal(
&mut tree,
&mut edge_map,
parent.clone(),
child.clone(),
// CHP=YES: BEM element CSS classes are designed to be styled inside their
// block's container. Placing them outside breaks styling.
// PMC=NO: the parent doesn't necessarily require every BEM element child.
EdgeStrength::Structural,
ChildRelationship::DirectChild,
format!(
"Secondary block fallback: {} is a BEM element of {}'s block ({})",
child, parent, block
),
None,
);
}
}
}
// ── Step 8.7: Prop-passed detection ───────────────────────────
// Detect components passed via named ReactNode/ReactElement props
// rather than as JSX children. For each family member, check if any
// other family member has a ReactNode prop whose name correlates
// with the component name (e.g., Alert.actionLinks ↔ AlertActionLink).
//
// This step both:
// - Creates new PropPassed edges for orphan components
// - Reclassifies existing DirectChild edges to PropPassed when a
// prop name match is found
{
let parented: HashSet<String> = tree.edges.iter().map(|e| e.child.clone()).collect();
let mut new_prop_edges = Vec::new();
let mut reclassify: Vec<(String, String, String)> = Vec::new(); // (parent, child, prop_name)
for child_name in family_exports {
if child_name == &root {
continue;
}
let child_lower = child_name.to_lowercase();
for parent_name in family_exports {
if parent_name == child_name {
continue;
}
let Some(parent_prof) = profiles.get(parent_name) else {
continue;
};
let parent_lower = parent_name.to_lowercase();
// Strip parent name prefix from child to get suffix
let suffix = if child_lower.starts_with(&parent_lower) {
&child_lower[parent_lower.len()..]
} else {
continue; // child name doesn't start with parent name
};
if suffix.is_empty() {
continue;
}
// Check parent's prop_types for ReactNode/ReactElement props
for (prop_name, prop_type) in &parent_prof.prop_types {
if prop_name == "children" {
continue;
}
if !prop_type.contains("ReactNode")
&& !prop_type.contains("ReactElement")
&& !prop_type.contains("ComponentType")
{
continue;
}
let prop_lower = prop_name.to_lowercase();
// Match: suffix starts with prop name or prop name
// starts with suffix (case-insensitive)
if suffix.starts_with(&prop_lower) || prop_lower.starts_with(suffix) {
// Check if edge already exists
let edge_exists = tree
.edges
.iter()
.any(|e| e.parent == *parent_name && e.child == *child_name);
if edge_exists {
// Reclassify existing edge to PropPassed
reclassify.push((
parent_name.clone(),
child_name.clone(),
prop_name.clone(),
));
} else if !parented.contains(child_name) {
// Create new PropPassed edge for orphan
debug!(
parent = %parent_name,
child = %child_name,
prop = %prop_name,
"Prop-passed detection: {} accepts {} via prop '{}'",
parent_name, child_name, prop_name
);
new_prop_edges.push((
parent_name.clone(),
child_name.clone(),
prop_name.clone(),
prop_type.clone(),
));
}
break; // Found a match for this parent, no need to check more props
}
}
}
}
for (parent, child, prop, ptype) in new_prop_edges {
record_signal(
&mut tree,
&mut edge_map,
parent.clone(),
child.clone(),
EdgeStrength::Allowed,
ChildRelationship::PropPassed,
format!(
"Prop-passed: {} accepts {} via `{}` prop ({})",
parent, child, prop, ptype
),
Some(prop),
);
}
// Reclassify existing edges
for (parent, child, prop) in reclassify {
if let Some(edge) = tree
.edges
.iter_mut()
.find(|e| e.parent == parent && e.child == child)
{
edge.relationship = ChildRelationship::PropPassed;
edge.prop_name = Some(prop.clone());
edge.bem_evidence = Some(format!(
"Prop-passed (reclassified): {} accepts {} via `{}` prop",
parent, child, prop
));
}
}
}
// ── Step 8.8: Downgrade bidirectional CHP cycles ──────────────
// When A→B and B→A both have CHP=YES (Required or Structural),
// this represents recursive nesting (e.g., WizardNavItem contains
// a nested WizardNav for sub-navigation). The weaker direction
// (lower EdgeStrength ordinal) is downgraded to Allowed — recursive
// nesting is optional, not a structural constraint.
downgrade_bidirectional_chp_cycles(&mut tree);
// ── Step 9: Dedup ──────────────────────────────────────────────
deduplicate_edges(&mut tree);
// ── Step 9.5: Pure composition wrapper PMC upgrade ─────────────
// When a parent is a "pure composition wrapper" (no Internal outgoing
// edges — it only accepts consumer-placed children via {children})
// AND it has structural evidence of being a layout container (CSS grid
// with grid-template, or DOM nesting with a pure container tag), then
// the parent exists to contain its children. Empty usage is meaningless.
//
// Add PMC=YES (via Wrapper) to Structural DirectChild edges from such
// parents. Only edges with CHP=YES (Structural) are upgraded — weak
// signals like CSS descendant and BEM fallback (Allowed, CHP=NO) are
// skipped because they represent deep-descendant matches, not real
// direct parent-child layout dependencies.
//
// combine() handles the rest:
// Structural.combine(Wrapper) = Required (gains PMC)
// Required stays Required (no-op)
//
// The requiresChild rule uses OR semantics ("parent must contain at
// least one of X, Y, Z"), so upgrading ALL structural children is
// acceptable even when individual children are optional.
{
// Collect parent-level info from edge evidence
let mut parent_has_internal: HashSet<String> = HashSet::new();
let mut parent_has_grid: HashSet<String> = HashSet::new();
let mut parent_has_pure_container: HashSet<String> = HashSet::new();
for edge in &tree.edges {
if edge.relationship == ChildRelationship::Internal {
parent_has_internal.insert(edge.parent.clone());
}
if let Some(ref ev) = edge.bem_evidence {
if ev.contains("CSS grid:") && ev.contains("grid-template") {
parent_has_grid.insert(edge.parent.clone());
}
if ev.contains("wraps children in <") {
// Extract the tag and check if it's a pure container
if let Some(start) = ev.find("wraps children in <") {
let after = &ev[start + 19..];
if let Some(end) = after.find('>') {
let tag = &after[..end];
// Note: `select` is intentionally excluded.
// `is_pure_container_tag()` already excludes it
// because `<select>` can be empty and `<option>`
// can appear directly in `<select>` without
// `<optgroup>`. Upgrading edges from `<select>`
// parents would make FormSelectOptionGroup
// Required (PMC=YES) when it should be Structural
// (PMC=NO) — optgroup is an optional wrapper.
// `optgroup` is kept because an `<optgroup>`
// without `<option>` children is meaningless.
if matches!(
tag,
"ul" | "ol"
| "tbody"
| "thead"
| "tfoot"
| "tr"
| "dl"
| "table"
| "optgroup"
) {
parent_has_pure_container.insert(edge.parent.clone());
}
}
}
}
}
}
// Upgrade Structural edges from qualifying parents
for edge in &mut tree.edges {
if edge.relationship != ChildRelationship::DirectChild {
continue;
}
// Only upgrade edges with CHP=YES (Structural or Required).
// Skip Allowed edges — they're weak signals (CSS descendant,
// BEM fallback) that don't represent real layout dependencies.
if !edge.strength.child_requires_parent() {
continue;
}
if parent_has_internal.contains(&edge.parent) {
continue;
}
let has_signal = parent_has_grid.contains(&edge.parent)
|| parent_has_pure_container.contains(&edge.parent);
if !has_signal {
continue;
}
edge.strength = edge.strength.combine(&EdgeStrength::Wrapper);
edge.required = edge.strength.parent_requires_child();
}
}
// ── Step 9.6: Suppress root shortcuts ───────────────────────────
// Must run AFTER Step 9.5 so that Required wrappers (from PMC upgrade)
// are available for the wrapper-grandchild suppression path.
suppress_root_edges_with_intermediate(&mut tree);
// ── Step 10: Drop unconnected members ───────────────────────────
// Members with no edges at all are dropped from the tree, UNLESS
// they are barrel-file exports (exported_members). Exported orphans
// are retained — they're part of the family's public API even if
// no structural signal links them (e.g., convenience composites
// like LoginForm, orchestrators like MenuContainer).
//
// Non-exported members with zero edges are internal noise (context
// objects, type exports, helper components) and are dropped.
//
// Members with outgoing edges but no incoming edges are secondary
// roots — top-level containers within the family. These are retained
// so that collapse_internal_nodes can properly handle them.
let parented: HashSet<&str> = tree.edges.iter().map(|e| e.child.as_str()).collect();
let parenting: HashSet<&str> = tree.edges.iter().map(|e| e.parent.as_str()).collect();
let exported_set: HashSet<&str> = exported_members
.map(|e| e.iter().map(|s| s.as_str()).collect())
.unwrap_or_default();
tree.family_members.retain(|m| {
m == &root
|| parented.contains(m.as_str())
|| parenting.contains(m.as_str())
|| exported_set.contains(m.as_str())
});
Some(tree)
}
/// Infer parent→child edges from cloneElement prop injection chains.
///
/// If component A uses `cloneElement(child, { prop1 })` and family member B
/// declares `prop1` in its interface, then B is a child of A.
///
/// Two filters prevent false edges from shared prop vocabularies:
///
/// 1. **Skip reverse-of-existing**: If B→A already exists from a prior step
/// (e.g., Step 1 internal rendering), don't create A→B from cloneElement.
/// The prior edge establishes the direction; adding the reverse creates a
/// false cycle.
///
/// 2. **Remove bidirectional pairs**: After creating all cloneElement edges,
/// if both A→B and B→A exist from cloneElement, both are removed. This
/// indicates a peer relationship (shared prop vocabulary) rather than a
/// parent-child hierarchy. E.g., chart sub-components that all inject
/// the same layout props (height, width, theme) into non-family primitives.
fn infer_clone_element_nesting(
tree: &mut CompositionTree,
edge_map: &mut HashMap<(String, String), usize>,
profiles: &HashMap<String, ComponentSourceProfile>,
family_exports: &[String],
) {
// Collect existing edges from prior steps to detect reverse conflicts
let prior_edges: HashSet<(String, String)> = edge_map.keys().cloned().collect();
// Collect candidate signals: (parent, child, evidence, children_is_react_element)
let mut candidates: Vec<(String, String, String, bool)> = Vec::new();
for parent_name in family_exports {
let Some(parent_profile) = profiles.get(parent_name) else {
continue;
};
if parent_profile.clone_element_injections.is_empty() {
continue;
}
// Skip components that don't accept children — their cloneElement
// calls target internally-created elements (e.g., dropdown items),
// not consumer-provided children.
if !parent_profile.has_children_prop {
continue;
}
let universal_props: HashSet<&str> =
["children", "className", "style", "id", "key", "ref"].into();
let injected_props: HashSet<&str> = parent_profile
.clone_element_injections
.iter()
.flat_map(|inj| inj.injected_props.iter().map(|s| s.as_str()))
.filter(|p| !universal_props.contains(p))
.collect();
if injected_props.is_empty() {
continue;
}
for child_name in family_exports {
if child_name == parent_name {
continue;
}
// Filter 1: skip if reverse edge already exists from a prior step.
if prior_edges.contains(&(child_name.clone(), parent_name.clone())) {
continue;
}
let Some(child_profile) = profiles.get(child_name) else {
continue;
};
let matching_props: Vec<&str> = injected_props
.iter()
.filter(|prop| child_profile.all_props.contains(**prop))
.copied()
.collect();
if !matching_props.is_empty() {
// Check if the parent's `children` prop is typed as ReactElement
// (singular, specific) vs ReactNode (plural, generic). Parents
// that accept ReactElement are purpose-built wrappers for a
// specific child type (PMC=YES, CHP=NO → Wrapper). Parents
// that accept ReactNode are generic containers (CHP=YES,
// PMC=NO → Structural).
let children_type = parent_profile
.prop_types
.get("children")
.map(|t| t.as_str())
.unwrap_or("");
let is_react_element =
children_type.contains("ReactElement") && !children_type.contains("ReactNode");
candidates.push((
parent_name.clone(),
child_name.clone(),
format!(
"cloneElement: {} injects [{}], {} declares them",
parent_name,
matching_props.join(", "),
child_name
),
is_react_element,
));
}
}
}
// Filter 2: remove bidirectional cloneElement pairs.
let clone_pairs: HashSet<(String, String)> = candidates
.iter()
.map(|(p, c, _, _)| (p.clone(), c.clone()))
.collect();
candidates.retain(|(p, c, _, _)| !clone_pairs.contains(&(c.clone(), p.clone())));
for (parent, child, evidence, is_react_element) in candidates {
// Determine edge strength based on the parent's children prop type:
// - ReactElement (singular): parent is a purpose-built wrapper for a
// specific child type. PMC=YES (parent needs child), CHP=NO (child
// works standalone). Example: ChartDonutThreshold wraps
// ChartDonutUtilization.
// - ReactNode (generic): parent is a generic container that processes
// whatever children it receives. CHP=YES (child relies on injected
// props), PMC=NO (parent doesn't demand specific child).
// Example: AlertGroup, DataListItem, Breadcrumb.
let strength = if is_react_element {
EdgeStrength::Wrapper
} else {
EdgeStrength::Structural
};
record_signal(
tree,
edge_map,
parent,
child,
strength,
ChildRelationship::DirectChild,
evidence,
None,
);
}
}
/// Build a mapping from CSS BEM element names to component names.
///
/// Uses `css_tokens_used` on each component to determine which CSS elements
/// it renders. The mapping strips the BEM block prefix from tokens.
///
/// Example: ToolbarContent uses `styles.toolbarContentSection`. Block is
/// "toolbar". Strip prefix → "ContentSection" → kebab → "content-section".
/// Maps "content-section" → "ToolbarContent".
fn build_css_element_to_component_map(
profiles: &HashMap<String, ComponentSourceProfile>,
family_exports: &[String],
block_name: &str,
) -> HashMap<String, HashSet<String>> {
let mut map: HashMap<String, HashSet<String>> = HashMap::new();
let root_name = family_exports.first().map(|s| s.as_str());
for comp_name in family_exports {
let Some(profile) = profiles.get(comp_name) else {
continue;
};
for token in &profile.css_tokens_used {
// Tokens are stored as "styles.drawerBody" or "styles.modifiers.expanded".
// Strip the "styles." prefix to get the raw token (e.g., "drawerBody").
// Skip modifier tokens ("styles.modifiers.*") — they don't map to BEM elements.
let raw_token = if let Some(rest) = token.strip_prefix("styles.") {
if rest.starts_with("modifiers.") {
continue;
}
rest
} else {
token.as_str()
};
if let Some(suffix) = raw_token.strip_prefix(block_name) {
let element_name = if suffix.is_empty() {
// Root element — token matches block exactly
String::new()
} else {
// Element — strip block prefix, lowercase first char,
// convert to kebab-case
let mut camel = suffix.to_string();
if let Some(c) = camel.get_mut(0..1) {
c.make_ascii_lowercase();
}
camel_to_kebab(&camel)
};
// For non-root elements, skip the root component claiming
// child tokens when a dedicated component already exists.
// The root often uses child CSS tokens (e.g., JumpLinks uses
// `styles.jumpLinksList`) because it renders those elements
// internally, but JumpLinksList is the dedicated component.
//
// For the root element (""), all components are allowed
// (both root and sub-components may use the block token).
if !element_name.is_empty()
&& root_name == Some(comp_name.as_str())
&& map.contains_key(&element_name)
{
// Root trying to claim a non-root element that already
// has a dedicated component — skip.
continue;
}
map.entry(element_name)
.or_default()
.insert(comp_name.clone());
}
}
}
map
}
/// Convert camelCase to kebab-case.
fn camel_to_kebab(s: &str) -> String {
let mut result = String::with_capacity(s.len() + 4);
for (i, ch) in s.chars().enumerate() {
if ch.is_uppercase() && i > 0 {
result.push('-');
}
result.push(ch.to_ascii_lowercase());
}
result
}
/// Check if an edge from parent to child already exists in the tree.
/// Infer parent→child edges from HTML DOM nesting rules.
///
/// For each family member that wraps `{children}` in an HTML element,
/// check if any other family member renders a compatible child element
/// as its root. For example, if component A wraps children in `<ul>` and
/// component B renders `<li>` as its outermost element, then B should be
/// nested inside A.
///
/// This catches relationships that BEM can't express because BEM elements
/// are flat (MenuList and MenuItem are both elements of the "menu" block,
/// but MenuItem's `<li>` goes inside MenuList's `<ul>`).
fn infer_dom_nesting(
tree: &mut CompositionTree,
edge_map: &mut HashMap<(String, String), usize>,
profiles: &HashMap<String, ComponentSourceProfile>,
family_exports: &[String],
) {
// Collect DOM nesting signals: (parent, child, slot_el, root_el)
let mut signals: Vec<(String, String, String, String)> = Vec::new();
for parent_name in family_exports {
let Some(parent_profile) = profiles.get(parent_name) else {
continue;
};
if !parent_profile.has_children_prop {
continue;
}
let slot_element = parent_profile
.children_slot_path
.iter()
.rev()
.find(|e| e.starts_with(|c: char| c.is_lowercase()));
let Some(slot_el) = slot_element else {
continue;
};
let expected_children = html_expected_children(slot_el);
if expected_children.is_empty() {
continue;
}
for child_name in family_exports {
if child_name == parent_name {
continue;
}
let Some(child_profile) = profiles.get(child_name) else {
continue;
};
let child_root = child_profile
.children_slot_path
.first()
.filter(|e| e.starts_with(|c: char| c.is_lowercase()))
.cloned()
.or_else(|| infer_root_element(child_profile));
if let Some(ref root_el) = child_root {
if expected_children.contains(&root_el.as_str()) {
signals.push((
parent_name.clone(),
child_name.clone(),
slot_el.clone(),
root_el.clone(),
));
}
}
}
}
// For pure containers with multiple matching children, use Structural
// instead of Required. The container needs SOME children, but no
// specific child is individually required (e.g., NavList has NavItem,
// NavItemSeparator, NavExpandable — all render <li>, but only NavItem
// is the "primary" child).
let mut parent_dom_child_count: HashMap<String, usize> = HashMap::new();
for (parent, _, _, _) in &signals {
*parent_dom_child_count.entry(parent.clone()).or_insert(0) += 1;
}
for (parent, child, slot_el, root_el) in signals {
let child_count = parent_dom_child_count.get(&parent).copied().unwrap_or(0);
let strength = if is_pure_container_tag(&slot_el)
&& (child_count == 1 || all_children_are_peers(&slot_el))
{
EdgeStrength::Required
} else {
EdgeStrength::Structural
};
record_signal(
tree,
edge_map,
parent.clone(),
child.clone(),
strength,
ChildRelationship::DirectChild,
format!(
"DOM nesting: {} wraps children in <{}>, {} renders <{}> as root",
parent, slot_el, child, root_el
),
None,
);
}
// ── Flow container fallback ─────────────────────────────────────
//
// Last-resort: for components still only connected to the root,
// check if a sibling renders a flow content container (<section>,
// <div>, etc.) wrapping {children}. If so, a <ul>/<ol>-rendering
// component likely goes inside it (e.g., MenuGroup <section> wraps
// MenuList <ul>).
//
// Only applies when:
// - The child is NOT the family root (prevents DataList root edge)
// - The child is still a direct child of root (no other parent)
// - The child renders <ul> or <ol> as its root element
// - The parent renders a flow container wrapping {children}
// - No existing edge from parent to child
let flow_containers = [
"section", "div", "article", "aside", "main", "nav", "header", "footer",
];
let list_tags = ["ul", "ol"];
let root_children: Vec<String> = tree
.edges
.iter()
.filter(|e| e.parent == tree.root)
.map(|e| e.child.clone())
.collect();
let mut flow_edges = Vec::new();
for child_name in &root_children {
if child_name == &tree.root {
continue;
}
// Only consider children that have no other parent (still root-level)
let has_other_parent = tree
.edges
.iter()
.any(|e| e.child == *child_name && e.parent != tree.root);
if has_other_parent {
continue;
}
let child_profile = match profiles.get(child_name) {
Some(p) => p,
None => continue,
};
// Check if this child renders <ul> or <ol>
let child_root = child_profile
.children_slot_path
.first()
.filter(|e| e.starts_with(|c: char| c.is_lowercase()))
.cloned()
.or_else(|| infer_root_element(child_profile));
let is_list = child_root
.as_ref()
.is_some_and(|r| list_tags.contains(&r.as_str()));
if !is_list {
continue;
}
// Find a flow container sibling that could wrap this child
for parent_name in family_exports {
if parent_name == child_name || parent_name == &tree.root {
continue;
}
let existing_edge = tree
.edges
.iter()
.any(|e| e.parent == *parent_name && e.child == *child_name);
if existing_edge {
continue;
}
let parent_profile = match profiles.get(parent_name) {
Some(p) => p,
None => continue,
};
if !parent_profile.has_children_prop {
continue;
}
let parent_slot = parent_profile
.children_slot_path
.iter()
.rev()
.find(|e| e.starts_with(|c: char| c.is_lowercase()));
let is_flow = parent_slot.is_some_and(|s| flow_containers.contains(&s.as_str()));
if !is_flow {
continue;
}
// Verify this parent is itself a root-level child (not deeply nested)
let parent_is_root_child = tree
.edges
.iter()
.any(|e| e.parent == tree.root && e.child == *parent_name);
if !parent_is_root_child {
continue;
}
flow_edges.push((parent_name.clone(), child_name.clone()));
break; // Only assign to first matching flow container
}
}
for (parent, child) in flow_edges {
tree.edges
.retain(|e| !(e.parent == tree.root && e.child == child));
tree.edges.push(CompositionEdge {
parent: parent.clone(),
child: child.clone(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: Some(format!(
"Flow container nesting: {} renders <ul>/<ol>, {} wraps {{children}} in flow container",
child, parent
)),
strength: EdgeStrength::Allowed, prop_name: None,
});
}
}
/// Infer parent→child edges from React Context provider→consumer relationships.
///
/// If a family member renders `<XContext.Provider>` (visible in its
/// `rendered_components`) and another family member calls `useContext(XContext)`
/// (visible in its `consumed_contexts`), the consumer must be nested somewhere
/// under the provider.
/// Check whether `child_name` is prop-passed to a family member OTHER than
/// `provider_name`. Uses the same name-matching heuristic as Step 8.7:
/// strip the parent's name prefix from the child, then compare the suffix
/// against the parent's ReactNode/ReactElement props (bidirectional
/// starts_with, case-insensitive).
///
/// This is used by Step 6 to skip context edges when the child's structural
/// home is a different component (via a named prop), and the context
/// dependency is merely ambient (the child sits inside the provider
/// transitively through the prop parent).
fn is_prop_passed_to_other_parent(
child_name: &str,
provider_name: &str,
profiles: &HashMap<String, ComponentSourceProfile>,
family_exports: &[String],
) -> bool {
let child_lower = child_name.to_lowercase();
for parent_name in family_exports {
if parent_name == child_name || parent_name == provider_name {
continue;
}
let Some(parent_prof) = profiles.get(parent_name) else {
continue;
};
let parent_lower = parent_name.to_lowercase();
// Strip parent name prefix from child to get suffix
let suffix = if child_lower.starts_with(&parent_lower) {
&child_lower[parent_lower.len()..]
} else {
continue;
};
if suffix.is_empty() {
continue;
}
// Check parent's prop_types for ReactNode/ReactElement props
for (prop_name, prop_type) in &parent_prof.prop_types {
if prop_name == "children" {
continue;
}
if !prop_type.contains("ReactNode")
&& !prop_type.contains("ReactElement")
&& !prop_type.contains("ComponentType")
{
continue;
}
let prop_lower = prop_name.to_lowercase();
if suffix.starts_with(&prop_lower) || prop_lower.starts_with(suffix) {
return true;
}
}
}
false
}
fn infer_context_nesting(
tree: &mut CompositionTree,
edge_map: &mut HashMap<(String, String), usize>,
profiles: &HashMap<String, ComponentSourceProfile>,
family_exports: &[String],
) {
// Build map: context_name → provider component
// Detect from rendered_components entries like "XContext.Provider"
let mut context_providers: HashMap<String, Vec<String>> = HashMap::new();
for name in family_exports {
let Some(profile) = profiles.get(name) else {
continue;
};
for rc in &profile.rendered_components {
// Direct member expression: <XContext.Provider>
if let Some(ctx_name) = rc.name.strip_suffix(".Provider") {
context_providers
.entry(ctx_name.to_string())
.or_default()
.push(name.clone());
}
// Aliased provider: <XContextProvider> or <XProvider>
// These are plain identifiers, not member expressions.
// Example: `export const TabsContextProvider = TabsContext.Provider`
// then `<TabsContextProvider>` in JSX.
else if rc.name.ends_with("Provider") && rc.name != "Provider" {
let base = rc.name.strip_suffix("Provider").unwrap();
// "TabsContextProvider" → base = "TabsContext"
// "TabsProvider" → base = "Tabs", candidate = "TabsContext"
let candidates = if base.ends_with("Context") {
vec![base.to_string()]
} else {
vec![format!("{}Context", base), base.to_string()]
};
for candidate in candidates {
if !context_providers.contains_key(&candidate) {
context_providers
.entry(candidate)
.or_default()
.push(name.clone());
}
}
}
}
}
if context_providers.is_empty() {
return;
}
// Collect signals to add (can't borrow tree mutably while iterating profiles)
let mut signals: Vec<(String, String, String)> = Vec::new();
for child_name in family_exports {
let Some(child_profile) = profiles.get(child_name) else {
continue;
};
for consumed_ctx in &child_profile.consumed_contexts {
if let Some(providers) = context_providers.get(consumed_ctx) {
for provider_name in providers {
if provider_name == child_name {
continue;
}
// Skip re-providers
let Some(provider_profile) = profiles.get(provider_name) else {
continue;
};
if provider_profile.consumed_contexts.contains(consumed_ctx) {
debug!(
provider = %provider_name,
consumer = %child_name,
context = %consumed_ctx,
"skipping re-provider context nesting"
);
continue;
}
// Avoid duplicate signals within this step
if signals
.iter()
.any(|(p, c, _)| p == provider_name && c == child_name)
{
continue;
}
// Skip context edges when the child is prop-passed to a
// different parent. The context dependency is ambient —
// the child lives inside the prop parent, which sits
// inside the provider transitively. Creating a direct
// context edge would produce a wrong-parent relationship
// after collapse_internal_nodes.
//
// Example: AlertActionCloseButton consumes AlertGroupContext
// (provided by AlertGroupInline), but its structural home is
// Alert (via actionClose prop). Without this filter, collapse
// creates AlertGroup→AlertActionCloseButton (wrong parent).
if is_prop_passed_to_other_parent(
child_name,
provider_name,
profiles,
family_exports,
) {
debug!(
provider = %provider_name,
consumer = %child_name,
context = %consumed_ctx,
"skipping context edge — child is prop-passed to another parent"
);
continue;
}
debug!(
provider = %provider_name,
consumer = %child_name,
context = %consumed_ctx,
"context nesting inferred"
);
signals.push((
provider_name.clone(),
child_name.clone(),
consumed_ctx.clone(),
));
}
}
}
}
for (provider, child, ctx) in signals {
record_signal(
tree,
edge_map,
provider.clone(),
child.clone(),
EdgeStrength::Structural,
ChildRelationship::DirectChild,
format!(
"Context nesting: {} provides {}, {} consumes it via useContext",
provider, ctx, child
),
None,
);
}
}
/// Get expected child element tags for a given HTML parent element.
fn html_expected_children(parent_tag: &str) -> Vec<&'static str> {
match parent_tag {
"ul" | "ol" => vec!["li"],
"table" => vec!["thead", "tbody", "tfoot", "tr", "caption", "colgroup"],
"thead" | "tbody" | "tfoot" => vec!["tr"],
"tr" => vec!["td", "th"],
"dl" => vec!["dt", "dd"],
"select" => vec!["option", "optgroup"],
"optgroup" => vec!["option"],
_ => vec![],
}
}
/// Whether an HTML tag is a "pure container" whose only purpose is to
/// hold specific children. Empty pure containers are semantically invalid.
///
/// Pure containers produce Required (both directions) DOM nesting edges.
/// Non-pure containers produce Structural (child→parent only) edges.
fn is_pure_container_tag(tag: &str) -> bool {
// Tags whose only purpose is to hold specific children.
// Excluded: select (empty is valid), optgroup (option can exist
// directly in select without optgroup).
matches!(tag, "ul" | "ol" | "tbody" | "thead" | "tfoot" | "tr" | "dl")
}
/// Whether ALL valid HTML children of this tag are interchangeable
/// structural peers. For these tags, every matching child component
/// gets Required even when child_count > 1.
///
/// Example: `<tr>` accepts both `<td>` and `<th>` — both are table
/// cells and the row needs at least one. Contrast with `<ul>` where
/// NavItem is primary but NavItemSeparator is auxiliary.
fn all_children_are_peers(tag: &str) -> bool {
matches!(tag, "tr")
}
/// Infer the root HTML element from a component's rendered_elements
/// or prop defaults.
///
/// Heuristic: if the component only renders one type of block-level
/// element, that's likely the root. For components like MenuItem that
/// render `<li>` as the wrapper, this picks up `li`.
///
/// Fallback: if the component has a `component` or `as` prop with an
/// HTML element string default (e.g., `component = 'th'`), use that.
/// This handles polymorphic components that render via a dynamic variable
/// (e.g., `<MergedComponent>` defaulting to `'th'`).
fn infer_root_element(profile: &ComponentSourceProfile) -> Option<String> {
// Check rendered_elements for common root candidates
let root_candidates = [
"li", "tr", "td", "th", "dt", "dd", "option", "section", "article", "div",
];
for candidate in &root_candidates {
if profile.rendered_elements.contains_key(*candidate) {
return Some(candidate.to_string());
}
}
// Fallback: check prop defaults for `component` or `as` with an HTML
// element value. This covers polymorphic components like PatternFly's
// Td/Th that render via `<MergedComponent>` with `component = 'td'`.
for prop_name in &["component", "as"] {
if let Some(default_val) = profile.prop_defaults.get(*prop_name) {
// Strip quotes: 'td' or "td" → td
let tag = default_val.trim_matches(|c| c == '\'' || c == '"');
if !tag.is_empty()
&& tag.starts_with(|c: char| c.is_lowercase())
&& tag.chars().all(|c| c.is_ascii_alphanumeric())
{
return Some(tag.to_string());
}
}
}
None
}
/// Suppress root→child BEM edges when a more specific intermediate→child
/// edge exists from DOM nesting, context, or delegation projection.
///
/// Downgrade the weaker edge in bidirectional CHP cycles to Allowed.
///
/// When A→B and B→A both have CHP=YES (Required or Structural), this
/// represents recursive nesting (e.g., WizardNavItem→WizardNav for
/// sub-navigation plus WizardNav→WizardNavItem for list items). The
/// weaker direction (lower EdgeStrength ordinal) is optional recursive
/// nesting and should be Allowed, not Structural.
///
/// If both edges have equal strength, the one where the child has more
/// incoming CHP edges is kept (it's the "hub" — the real parent).
fn downgrade_bidirectional_chp_cycles(tree: &mut CompositionTree) {
// Build a set of (parent, child) pairs with CHP=YES
let chp_edges: HashSet<(String, String)> = tree
.edges
.iter()
.filter(|e| e.strength.child_requires_parent())
.map(|e| (e.parent.clone(), e.child.clone()))
.collect();
// Find bidirectional pairs
let mut to_downgrade: HashSet<(String, String)> = HashSet::new();
for (parent, child) in &chp_edges {
let reverse = (child.clone(), parent.clone());
if chp_edges.contains(&reverse) && !to_downgrade.contains(&reverse) {
// Both directions exist with CHP=YES. Find the weaker one.
let forward_strength = tree
.edges
.iter()
.find(|e| e.parent == *parent && e.child == *child)
.map(|e| e.strength.clone())
.unwrap_or(EdgeStrength::Allowed);
let reverse_strength = tree
.edges
.iter()
.find(|e| e.parent == *child && e.child == *parent)
.map(|e| e.strength.clone())
.unwrap_or(EdgeStrength::Allowed);
if forward_strength < reverse_strength {
// Forward is weaker — downgrade it
to_downgrade.insert((parent.clone(), child.clone()));
} else if reverse_strength < forward_strength {
// Reverse is weaker — downgrade it
to_downgrade.insert((child.clone(), parent.clone()));
} else {
// Equal strength — downgrade the one where the child has
// more incoming CHP edges (the "hub" is the real parent)
let forward_child_incoming = tree
.edges
.iter()
.filter(|e| e.child == *child && e.strength.child_requires_parent())
.count();
let reverse_child_incoming = tree
.edges
.iter()
.filter(|e| e.child == *parent && e.strength.child_requires_parent())
.count();
if forward_child_incoming >= reverse_child_incoming {
// child has more/equal incoming → it's the hub → keep
// forward (parent→child), downgrade reverse
to_downgrade.insert((child.clone(), parent.clone()));
} else {
to_downgrade.insert((parent.clone(), child.clone()));
}
}
}
}
// Apply downgrades
for edge in &mut tree.edges {
if to_downgrade.contains(&(edge.parent.clone(), edge.child.clone())) {
tracing::debug!(
parent = %edge.parent,
child = %edge.child,
old_strength = ?edge.strength,
"downgrading bidirectional CHP cycle edge to Allowed"
);
edge.strength = EdgeStrength::Allowed;
}
}
}
/// BEM analysis creates edges from the block owner to every component
/// that uses its CSS tokens. But DOM/context/projection analysis discovers
/// the actual JSX nesting, which may have an intermediate wrapper between
/// the root and the child.
///
/// When both exist, the root edge is suppressed because the intermediate
/// is the correct JSX parent. This prevents conformance rules from
/// incorrectly requiring components to be direct children of the root.
fn suppress_root_edges_with_intermediate(tree: &mut CompositionTree) {
let root = &tree.root;
// Collect children with intermediate edges AND their max strength.
// Only suppress root edges when the intermediate is at least as
// strong — a Required root edge (e.g., CSS `>`) should not be
// suppressed by an Allowed intermediate (e.g., layout_children).
//
// Also track which non-root parents serve as intermediates for each
// child. This is needed to verify that at least one intermediate
// has a CHP edge from the root (is structurally required, not
// optional). If all intermediates are Allowed from the root, the
// child can bypass them and the root→child edge should be preserved.
let mut children_max_strength: HashMap<String, EdgeStrength> = HashMap::new();
let mut children_intermediate_parents: HashMap<String, Vec<String>> = HashMap::new();
for edge in &tree.edges {
if edge.parent != *root && matches!(edge.relationship, ChildRelationship::DirectChild) {
let entry = children_max_strength
.entry(edge.child.clone())
.or_insert(EdgeStrength::Allowed);
if edge.strength > *entry {
*entry = edge.strength.clone();
}
children_intermediate_parents
.entry(edge.child.clone())
.or_default()
.push(edge.parent.clone());
}
}
// Children where the root has a CHP edge (Required or Structural).
// These are children the root structurally contains — if an
// intermediate parent is among these, it is NOT optional.
let root_chp_children: HashSet<String> = tree
.edges
.iter()
.filter(|e| {
e.parent == *root
&& e.relationship != ChildRelationship::Internal
&& e.strength.child_requires_parent()
})
.map(|e| e.child.clone())
.collect();
// Children where the root has a PMC edge (Required or Wrapper).
// These are children the root REQUIRES — the intermediate is
// always present. Used to distinguish mandatory intermediates
// (Table→Tbody) from optional ones (FormSelect→FormSelectOptionGroup).
let root_pmc_children: HashSet<String> = tree
.edges
.iter()
.filter(|e| {
e.parent == *root
&& e.relationship != ChildRelationship::Internal
&& e.strength.parent_requires_child()
})
.map(|e| e.child.clone())
.collect();
// Collect Required children of the root. These are structural wrappers
// that are always present. If such a wrapper has ANY edge to a child
// that the root also has a direct edge to, the root's direct edge is
// a DOM shortcut that bypasses the API wrapper and should be suppressed.
//
// Example: DescriptionList→DescriptionListGroup [required] and
// DescriptionListGroup→DescriptionListTerm [allowed]. The root edge
// DescriptionList→DescriptionListTerm [required] is a DOM shortcut
// (<dl>→<dt>) that bypasses the DescriptionListGroup wrapper.
let mut required_wrapper_children: HashSet<String> = HashSet::new();
for edge in &tree.edges {
if edge.parent == *root && edge.strength.parent_requires_child() {
required_wrapper_children.insert(edge.child.clone());
}
}
// Build: for each Required wrapper, what children does it have?
let mut wrapper_grandchildren: HashSet<String> = HashSet::new();
for edge in &tree.edges {
if required_wrapper_children.contains(&edge.parent)
&& matches!(edge.relationship, ChildRelationship::DirectChild)
{
wrapper_grandchildren.insert(edge.child.clone());
}
}
if children_max_strength.is_empty() && wrapper_grandchildren.is_empty() {
return;
}
let before = tree.edges.len();
tree.edges.retain(|edge| {
// Keep all non-root edges
if edge.parent != *root {
return true;
}
// Only suppress DirectChild edges
if !matches!(edge.relationship, ChildRelationship::DirectChild) {
return true;
}
// Path 1: Suppress when intermediate is at least as strong as root edge
// AND at least one intermediate parent has a CHP edge from the root
// (meaning the intermediate is structurally required, not optional).
//
// If ALL intermediate parents have only Allowed edges from the root,
// the intermediates are optional wrappers — the child can bypass
// them and go directly into the root. Preserving the root→child
// edge ensures the child appears as a valid direct child.
//
// Example (suppress): Menu→MenuList [Structural], MenuList→MenuItem
// [Required]. MenuList has CHP from root → MenuItem must go through
// MenuList → suppress root→MenuItem.
//
// Example (preserve): SimpleList→SimpleListGroup [Allowed],
// SimpleListGroup→SimpleListItem [Required]. SimpleListGroup has NO
// CHP from root → SimpleListItem can bypass Group → preserve
// root→SimpleListItem.
if let Some(intermediate_strength) = children_max_strength.get(&edge.child) {
if *intermediate_strength >= edge.strength {
// Check if any intermediate parent is structurally required
// from the root (has a CHP edge from root).
let any_intermediate_required = children_intermediate_parents
.get(&edge.child)
.map(|parents| parents.iter().any(|p| root_chp_children.contains(p)))
.unwrap_or(false);
if !any_intermediate_required {
tracing::debug!(
root = %root,
child = %edge.child,
root_strength = ?edge.strength,
intermediate_strength = ?intermediate_strength,
"preserving root edge — all intermediate parents are optional (Allowed from root)"
);
return true;
}
// Preserve DOM nesting edges when the intermediate is NOT
// PMC=YES from the root. A DOM nesting edge means the root's
// HTML element directly accepts this child type (e.g.,
// <select> directly contains <option>). If the intermediate
// is optional (not PMC), the child can bypass it.
//
// Example (preserve): FormSelect wraps children in <select>,
// FormSelectOption renders <option>. FormSelectOptionGroup
// (<optgroup>) is Structural from root but NOT PMC — options
// can go directly in <select> without <optgroup>.
//
// Example (suppress): Table wraps children in <table>, Tr
// renders <tr>. Tbody IS PMC from root (Required) — <tr>
// must go through <tbody>.
//
// Non-DOM edges (context, CSS, BEM) are always suppressed
// when an intermediate with CHP exists, because those edges
// represent transitive dependencies, not direct containment.
let is_dom_nesting = edge
.bem_evidence
.as_ref()
.map(|ev| ev.contains("DOM nesting:"))
.unwrap_or(false);
if is_dom_nesting {
let any_intermediate_pmc = children_intermediate_parents
.get(&edge.child)
.map(|parents| parents.iter().any(|p| root_pmc_children.contains(p)))
.unwrap_or(false);
if !any_intermediate_pmc {
tracing::debug!(
root = %root,
child = %edge.child,
root_strength = ?edge.strength,
intermediate_strength = ?intermediate_strength,
"preserving root DOM nesting edge — intermediate is not PMC from root"
);
return true;
}
}
tracing::debug!(
root = %root,
child = %edge.child,
root_strength = ?edge.strength,
intermediate_strength = ?intermediate_strength,
"suppressing root edge — equally/more-strong intermediate parent exists"
);
return false;
}
}
// Path 2: Suppress when a Required wrapper of the root also has
// an edge to this child. The wrapper is always present (PMC=YES
// from root), so the child goes through it — the root's direct
// edge is a DOM shortcut bypassing the API wrapper.
//
// Example: DescriptionList has Required edges to both DLGroup
// (the wrapper) and DLTerm (via DOM <dl>→<dt> nesting). DLGroup
// also has an edge to DLTerm. The root→DLTerm edge is a DOM
// shortcut that bypasses the DLGroup wrapper and should be
// suppressed. DLGroup itself is never in wrapper_grandchildren
// (no other wrapper has an edge to it), so the root→DLGroup
// edge is preserved.
if wrapper_grandchildren.contains(&edge.child) {
tracing::debug!(
root = %root,
child = %edge.child,
root_strength = ?edge.strength,
"suppressing root edge — required wrapper provides path to child"
);
return false;
}
true
});
let suppressed = before - tree.edges.len();
if suppressed > 0 {
tracing::debug!(
root = %root,
suppressed,
"suppressed root→child edges with intermediate parents"
);
}
}
/// Remove redundant edges. If both parent→child (Internal) and
/// parent→child (DirectChild) exist, keep only the more specific one.
fn deduplicate_edges(tree: &mut CompositionTree) {
let mut seen = HashSet::new();
tree.edges.retain(|edge| {
let key = (edge.parent.clone(), edge.child.clone());
seen.insert(key)
});
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeSet;
fn make_profile(name: &str) -> ComponentSourceProfile {
ComponentSourceProfile {
name: name.to_string(),
..Default::default()
}
}
/// Helper to create a BTreeSet<String> from a slice of &str.
fn tokens(items: &[&str]) -> BTreeSet<String> {
items.iter().map(|s| s.to_string()).collect()
}
#[test]
fn test_context_nesting_provider_consumer() {
// Menu renders <MenuContext.Provider>, MenuList calls
// useContext(MenuContext). MenuList must be nested under Menu.
let mut menu = make_profile("Menu");
menu.has_children_prop = true;
menu.rendered_components = vec!["MenuContext.Provider".into()];
let mut menu_list = make_profile("MenuList");
menu_list.has_children_prop = true;
menu_list.consumed_contexts = vec!["MenuContext".into()];
let mut profiles = HashMap::new();
profiles.insert("Menu".into(), menu);
profiles.insert("MenuList".into(), menu_list);
let family = vec!["Menu".into(), "MenuList".into()];
let tree = build_composition_tree_v2(&profiles, &family, None, None, &[], None).unwrap();
let menu_to_list = tree
.edges
.iter()
.find(|e| e.parent == "Menu" && e.child == "MenuList");
assert!(
menu_to_list.is_some(),
"Expected Menu → MenuList from context nesting, got edges: {:?}",
tree.edges
);
assert!(menu_to_list
.unwrap()
.bem_evidence
.as_ref()
.unwrap()
.contains("Context nesting"));
}
/// AlertActionCloseButton scenario: the child consumes AlertGroupContext
/// (provided by AlertGroupInline), but is prop-passed to Alert via the
/// `actionClose` prop. The context edge AlertGroupInline→AlertActionCloseButton
/// should be skipped because the child's structural home is Alert.
#[test]
fn test_context_nesting_skipped_for_prop_passed_child() {
// AlertGroupInline provides AlertGroupContext
let mut alert_group_inline = make_profile("AlertGroupInline");
alert_group_inline.rendered_components = vec!["AlertGroupContext.Provider".into()];
// AlertActionCloseButton consumes AlertGroupContext
let mut close_btn = make_profile("AlertActionCloseButton");
close_btn.consumed_contexts = vec!["AlertGroupContext".into()];
// Alert has actionClose: ReactNode prop that matches AlertActionCloseButton
let mut alert = make_profile("Alert");
alert.has_children_prop = true;
alert
.prop_types
.insert("actionClose".into(), "React.ReactNode".into());
let mut profiles = HashMap::new();
profiles.insert("AlertGroupInline".into(), alert_group_inline);
profiles.insert("AlertActionCloseButton".into(), close_btn);
profiles.insert("Alert".into(), alert);
let family = vec![
"Alert".into(),
"AlertGroupInline".into(),
"AlertActionCloseButton".into(),
];
let mut tree = CompositionTree {
root: "Alert".into(),
family_members: family.clone(),
edges: vec![],
};
let mut edge_map = HashMap::new();
infer_context_nesting(&mut tree, &mut edge_map, &profiles, &family);
// AlertGroupInline → AlertActionCloseButton should NOT exist
// (child is prop-passed to Alert via actionClose)
assert!(
!tree
.edges
.iter()
.any(|e| e.parent == "AlertGroupInline" && e.child == "AlertActionCloseButton"),
"Context edge to prop-passed child should be skipped. Got edges: {:?}",
tree.edges
);
}
/// MenuItem → MenuItemAction scenario: MenuItemAction consumes MenuContext
/// (provided by Menu), but is prop-passed to MenuItem via the `actions` prop.
/// The context edge Menu→MenuItemAction should be skipped.
#[test]
fn test_context_nesting_skipped_for_menu_item_action() {
// Menu provides MenuContext
let mut menu = make_profile("Menu");
menu.has_children_prop = true;
menu.rendered_components = vec!["MenuContext.Provider".into()];
// MenuItemAction consumes MenuContext
let mut action = make_profile("MenuItemAction");
action.consumed_contexts = vec!["MenuContext".into()];
// MenuItem has actions: ReactNode prop AND consumes MenuContext
let mut menu_item = make_profile("MenuItem");
menu_item.has_children_prop = true;
menu_item.consumed_contexts = vec!["MenuContext".into()];
menu_item
.prop_types
.insert("actions".into(), "React.ReactNode".into());
let mut profiles = HashMap::new();
profiles.insert("Menu".into(), menu);
profiles.insert("MenuItemAction".into(), action);
profiles.insert("MenuItem".into(), menu_item);
let family = vec!["Menu".into(), "MenuItem".into(), "MenuItemAction".into()];
let mut tree = CompositionTree {
root: "Menu".into(),
family_members: family.clone(),
edges: vec![],
};
let mut edge_map = HashMap::new();
infer_context_nesting(&mut tree, &mut edge_map, &profiles, &family);
// Menu → MenuItemAction should NOT exist
assert!(
!tree
.edges
.iter()
.any(|e| e.parent == "Menu" && e.child == "MenuItemAction"),
"Context edge to prop-passed child should be skipped. Got edges: {:?}",
tree.edges
);
// Menu → MenuItem should still exist (MenuItem is NOT prop-passed,
// it's a regular context consumer)
assert!(
tree.edges
.iter()
.any(|e| e.parent == "Menu" && e.child == "MenuItem"),
"Menu → MenuItem context edge should still exist. Got edges: {:?}",
tree.edges
);
}
/// Ensure that a regular context consumer (not prop-passed) still gets
/// a context edge even when the prop-passed filter is active.
#[test]
fn test_context_nesting_preserved_for_non_prop_passed_child() {
// Tabs provides TabsContext
let mut tabs = make_profile("Tabs");
tabs.has_children_prop = true;
tabs.rendered_components = vec!["TabsContext.Provider".into()];
// Tab consumes TabsContext (NOT prop-passed — it's a direct child)
let mut tab = make_profile("Tab");
tab.has_children_prop = true;
tab.consumed_contexts = vec!["TabsContext".into()];
let mut profiles = HashMap::new();
profiles.insert("Tabs".into(), tabs);
profiles.insert("Tab".into(), tab);
let family = vec!["Tabs".into(), "Tab".into()];
let mut tree = CompositionTree {
root: "Tabs".into(),
family_members: family.clone(),
edges: vec![],
};
let mut edge_map = HashMap::new();
infer_context_nesting(&mut tree, &mut edge_map, &profiles, &family);
// Tabs → Tab should exist (Tab is not prop-passed to anything)
assert!(
tree.edges
.iter()
.any(|e| e.parent == "Tabs" && e.child == "Tab"),
"Tabs → Tab context edge should exist. Got edges: {:?}",
tree.edges
);
}
#[test]
fn test_dom_nesting_ul_li() {
// MenuList wraps children in <ul>, MenuItem renders <li> as root.
// DOM nesting inference should create MenuList → MenuItem edge.
let mut menu_list = make_profile("MenuList");
menu_list.has_children_prop = true;
menu_list.children_slot_path = vec!["ul".into()];
menu_list.rendered_elements.insert("ul".into(), 1);
let mut menu_item = make_profile("MenuItem");
menu_item.has_children_prop = true;
menu_item.children_slot_path = vec!["li".into(), "button".into(), "span".into()];
menu_item.rendered_elements.insert("li".into(), 1);
menu_item.rendered_elements.insert("button".into(), 1);
menu_item.rendered_elements.insert("span".into(), 3);
let mut profiles = HashMap::new();
profiles.insert("MenuList".into(), menu_list);
profiles.insert("MenuItem".into(), menu_item);
let family = vec!["MenuList".into(), "MenuItem".into()];
let tree = build_composition_tree_v2(&profiles, &family, None, None, &[], None).unwrap();
let list_to_item = tree
.edges
.iter()
.find(|e| e.parent == "MenuList" && e.child == "MenuItem");
assert!(
list_to_item.is_some(),
"Expected MenuList → MenuItem from DOM nesting (ul→li), got edges: {:?}",
tree.edges
);
assert!(list_to_item
.unwrap()
.bem_evidence
.as_ref()
.unwrap()
.contains("DOM nesting"));
}
#[test]
fn test_suppress_root_edges_with_intermediate() {
// Simulate Accordion: root has BEM edges to AccordionContent and
// AccordionToggle, but context nesting proves they go inside
// AccordionItem. AccordionItem has a Required edge from root
// (CHP=YES), making it a structurally required intermediate.
let mut tree = CompositionTree {
root: "Accordion".into(),
family_members: vec![
"Accordion".into(),
"AccordionItem".into(),
"AccordionContent".into(),
"AccordionToggle".into(),
],
edges: vec![
// BEM-derived root edges (should be suppressed)
CompositionEdge {
parent: "Accordion".into(),
child: "AccordionContent".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: Some("BEM element of accordion block".into()),
strength: EdgeStrength::Structural,
prop_name: None,
},
CompositionEdge {
parent: "Accordion".into(),
child: "AccordionToggle".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: Some("BEM element of accordion block".into()),
strength: EdgeStrength::Structural,
prop_name: None,
},
// Correct root edge — AccordionItem is CHP from root
CompositionEdge {
parent: "Accordion".into(),
child: "AccordionItem".into(),
relationship: ChildRelationship::DirectChild,
required: true,
bem_evidence: Some("DOM nesting + context".into()),
strength: EdgeStrength::Required,
prop_name: None,
},
// Context-derived intermediate edges (should be kept)
CompositionEdge {
parent: "AccordionItem".into(),
child: "AccordionContent".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: Some("Context nesting".into()),
strength: EdgeStrength::Structural,
prop_name: None,
},
CompositionEdge {
parent: "AccordionItem".into(),
child: "AccordionToggle".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: Some("Context nesting".into()),
strength: EdgeStrength::Structural,
prop_name: None,
},
],
};
suppress_root_edges_with_intermediate(&mut tree);
// Root → AccordionItem should be kept (no intermediate)
assert!(
tree.edges
.iter()
.any(|e| e.parent == "Accordion" && e.child == "AccordionItem"),
"Accordion → AccordionItem should be kept"
);
// Root → AccordionContent should be suppressed
assert!(
!tree
.edges
.iter()
.any(|e| e.parent == "Accordion" && e.child == "AccordionContent"),
"Accordion → AccordionContent should be suppressed (intermediate exists)"
);
// Root → AccordionToggle should be suppressed
assert!(
!tree
.edges
.iter()
.any(|e| e.parent == "Accordion" && e.child == "AccordionToggle"),
"Accordion → AccordionToggle should be suppressed (intermediate exists)"
);
// Intermediate edges should be kept
assert!(
tree.edges
.iter()
.any(|e| e.parent == "AccordionItem" && e.child == "AccordionContent"),
"AccordionItem → AccordionContent should be kept"
);
assert!(
tree.edges
.iter()
.any(|e| e.parent == "AccordionItem" && e.child == "AccordionToggle"),
"AccordionItem → AccordionToggle should be kept"
);
assert_eq!(tree.edges.len(), 3, "Should have 3 edges remaining");
}
#[test]
fn test_suppress_no_false_positives_masthead() {
// Masthead: all edges are root→child only, no intermediates.
// Nothing should be suppressed.
let mut tree = CompositionTree {
root: "Masthead".into(),
family_members: vec![
"Masthead".into(),
"MastheadBrand".into(),
"MastheadContent".into(),
"MastheadMain".into(),
],
edges: vec![
CompositionEdge {
parent: "Masthead".into(),
child: "MastheadBrand".into(),
relationship: ChildRelationship::DirectChild,
required: true,
bem_evidence: None,
strength: EdgeStrength::Allowed,
prop_name: None,
},
CompositionEdge {
parent: "Masthead".into(),
child: "MastheadContent".into(),
relationship: ChildRelationship::DirectChild,
required: true,
bem_evidence: None,
strength: EdgeStrength::Allowed,
prop_name: None,
},
CompositionEdge {
parent: "Masthead".into(),
child: "MastheadMain".into(),
relationship: ChildRelationship::DirectChild,
required: true,
bem_evidence: None,
strength: EdgeStrength::Allowed,
prop_name: None,
},
],
};
suppress_root_edges_with_intermediate(&mut tree);
assert_eq!(
tree.edges.len(),
3,
"No edges should be suppressed for Masthead"
);
}
#[test]
fn test_suppress_preserves_structural_root_edges_card() {
// Card scenario (matches real v6 tree): root→child edges are
// Structural (CHP=YES, PMC=NO) from CSS `>` selectors.
// CardHeader→CardBody/CardFooter are Allowed from CSS layout.
// Path 1 correctly preserves root edges (Structural > Allowed).
// Path 2 never fires because no root edge has PMC=YES.
let mut tree = CompositionTree {
root: "Card".into(),
family_members: vec![
"Card".into(),
"CardHeader".into(),
"CardBody".into(),
"CardFooter".into(),
],
edges: vec![
// Root → CardHeader (Structural, CSS > + context)
CompositionEdge {
parent: "Card".into(),
child: "CardHeader".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: Some("CSS direct child: > .header".into()),
strength: EdgeStrength::Structural,
prop_name: None,
},
// Root → CardBody (Structural, CSS >)
CompositionEdge {
parent: "Card".into(),
child: "CardBody".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: Some("CSS direct child: > .body".into()),
strength: EdgeStrength::Structural,
prop_name: None,
},
// Root → CardFooter (Structural, CSS >)
CompositionEdge {
parent: "Card".into(),
child: "CardFooter".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: Some("CSS direct child: > .footer".into()),
strength: EdgeStrength::Structural,
prop_name: None,
},
// Intermediate: CardHeader → CardBody (Allowed, layout_children)
CompositionEdge {
parent: "CardHeader".into(),
child: "CardBody".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: Some("CSS layout container".into()),
strength: EdgeStrength::Allowed,
prop_name: None,
},
// Intermediate: CardHeader → CardFooter (Allowed, layout_children)
CompositionEdge {
parent: "CardHeader".into(),
child: "CardFooter".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: Some("CSS layout container".into()),
strength: EdgeStrength::Allowed,
prop_name: None,
},
],
};
suppress_root_edges_with_intermediate(&mut tree);
// Card → CardHeader should be kept (no intermediate)
assert!(
tree.edges
.iter()
.any(|e| e.parent == "Card" && e.child == "CardHeader"),
"Card → CardHeader should be kept"
);
// Card → CardBody should be KEPT (Structural root > Allowed intermediate)
assert!(
tree.edges
.iter()
.any(|e| e.parent == "Card" && e.child == "CardBody"),
"Card → CardBody should be kept (Structural root edge is stronger than Allowed intermediate)"
);
// Card → CardFooter should be KEPT (Structural root > Allowed intermediate)
assert!(
tree.edges
.iter()
.any(|e| e.parent == "Card" && e.child == "CardFooter"),
"Card → CardFooter should be kept (Structural root edge is stronger than Allowed intermediate)"
);
// Intermediate edges should also be kept
assert!(
tree.edges
.iter()
.any(|e| e.parent == "CardHeader" && e.child == "CardBody"),
"CardHeader → CardBody intermediate should be kept"
);
assert!(
tree.edges
.iter()
.any(|e| e.parent == "CardHeader" && e.child == "CardFooter"),
"CardHeader → CardFooter intermediate should be kept"
);
assert_eq!(tree.edges.len(), 5, "All 5 edges should be preserved");
}
/// DescriptionList scenario: root has Required DOM nesting edges to
/// leaf children (<dl>→<dt>, <dl>→<dd>) that bypass the intermediate
/// DescriptionListGroup wrapper. The root also has a Required edge to
/// the Group wrapper, and the Group has edges to the same leaf children.
/// The DOM shortcut edges should be suppressed — the leaf children
/// are reachable through the Group wrapper.
#[test]
fn test_suppress_dom_shortcut_edges_description_list() {
let mut tree = CompositionTree {
root: "DescriptionList".into(),
family_members: vec![
"DescriptionList".into(),
"DescriptionListGroup".into(),
"DescriptionListTerm".into(),
"DescriptionListDescription".into(),
"DescriptionListTermHelpText".into(),
],
edges: vec![
// Root → Group (Required, CSS grid) — the API wrapper, must be kept
CompositionEdge {
parent: "DescriptionList".into(),
child: "DescriptionListGroup".into(),
relationship: ChildRelationship::DirectChild,
required: true,
bem_evidence: Some("CSS grid".into()),
strength: EdgeStrength::Required,
prop_name: None,
},
// Root → Term (Required, DOM nesting <dl>→<dt>) — DOM shortcut,
// should be suppressed because Group provides path
CompositionEdge {
parent: "DescriptionList".into(),
child: "DescriptionListTerm".into(),
relationship: ChildRelationship::DirectChild,
required: true,
bem_evidence: None,
strength: EdgeStrength::Required,
prop_name: None,
},
// Root → Description (Required, DOM nesting <dl>→<dd>) — same
CompositionEdge {
parent: "DescriptionList".into(),
child: "DescriptionListDescription".into(),
relationship: ChildRelationship::DirectChild,
required: true,
bem_evidence: None,
strength: EdgeStrength::Required,
prop_name: None,
},
// Root → TermHelpText (Required, DOM nesting <dl>→<dt>) — same
CompositionEdge {
parent: "DescriptionList".into(),
child: "DescriptionListTermHelpText".into(),
relationship: ChildRelationship::DirectChild,
required: true,
bem_evidence: None,
strength: EdgeStrength::Required,
prop_name: None,
},
// Group → Term (Allowed, CSS implicit grid child)
CompositionEdge {
parent: "DescriptionListGroup".into(),
child: "DescriptionListTerm".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: None,
strength: EdgeStrength::Allowed,
prop_name: None,
},
// Group → Description (Structural, CSS implicit grid child)
CompositionEdge {
parent: "DescriptionListGroup".into(),
child: "DescriptionListDescription".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: None,
strength: EdgeStrength::Structural,
prop_name: None,
},
// Group → TermHelpText (Allowed, CSS implicit grid child)
CompositionEdge {
parent: "DescriptionListGroup".into(),
child: "DescriptionListTermHelpText".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: None,
strength: EdgeStrength::Allowed,
prop_name: None,
},
],
};
suppress_root_edges_with_intermediate(&mut tree);
// Root → Group should be kept (it's the wrapper itself)
assert!(
tree.edges
.iter()
.any(|e| e.parent == "DescriptionList" && e.child == "DescriptionListGroup"),
"DescriptionList → DescriptionListGroup should be kept (API wrapper)"
);
// Root → Term should be suppressed (Group provides path)
assert!(
!tree
.edges
.iter()
.any(|e| e.parent == "DescriptionList" && e.child == "DescriptionListTerm"),
"DescriptionList → DescriptionListTerm should be suppressed (DOM shortcut)"
);
// Root → Description should be suppressed
assert!(
!tree
.edges
.iter()
.any(|e| e.parent == "DescriptionList" && e.child == "DescriptionListDescription"),
"DescriptionList → DescriptionListDescription should be suppressed (DOM shortcut)"
);
// Root → TermHelpText should be suppressed
assert!(
!tree
.edges
.iter()
.any(|e| e.parent == "DescriptionList" && e.child == "DescriptionListTermHelpText"),
"DescriptionList → DescriptionListTermHelpText should be suppressed (DOM shortcut)"
);
// Group's intermediate edges should all be kept
assert!(
tree.edges
.iter()
.any(|e| e.parent == "DescriptionListGroup" && e.child == "DescriptionListTerm"),
"Group → Term intermediate edge should be kept"
);
assert!(
tree.edges
.iter()
.any(|e| e.parent == "DescriptionListGroup"
&& e.child == "DescriptionListDescription"),
"Group → Description intermediate edge should be kept"
);
assert!(
tree.edges
.iter()
.any(|e| e.parent == "DescriptionListGroup"
&& e.child == "DescriptionListTermHelpText"),
"Group → TermHelpText intermediate edge should be kept"
);
// 4 edges remaining: 1 root→Group + 3 Group→children
assert_eq!(
tree.edges.len(),
4,
"Should have 4 edges remaining (1 root→Group + 3 Group→children)"
);
}
/// Wizard scenario: WizardNav ↔ WizardNavItem have a bidirectional
/// CHP cycle. WizardNav→WizardNavItem is Required (DOM nesting ol→li),
/// WizardNavItem→WizardNav is Structural (CSS > .nav-item > .nav-list).
/// Step 8.8 should downgrade the weaker direction (Structural) to Allowed.
#[test]
fn test_bidirectional_chp_cycle_downgrades_weaker_edge() {
let mut tree = CompositionTree {
root: "Wizard".into(),
family_members: vec!["Wizard".into(), "WizardNav".into(), "WizardNavItem".into()],
edges: vec![
// Forward: WizardNav → WizardNavItem (Required, DOM nesting)
CompositionEdge {
parent: "WizardNav".into(),
child: "WizardNavItem".into(),
relationship: ChildRelationship::DirectChild,
required: true,
bem_evidence: Some("DOM nesting: ol→li".into()),
strength: EdgeStrength::Required,
prop_name: None,
},
// Reverse: WizardNavItem → WizardNav (Structural, CSS >)
CompositionEdge {
parent: "WizardNavItem".into(),
child: "WizardNav".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: Some("CSS direct child: .nav-item > .nav-list".into()),
strength: EdgeStrength::Structural,
prop_name: None,
},
],
};
downgrade_bidirectional_chp_cycles(&mut tree);
// Required direction should be unchanged
let forward = tree
.edges
.iter()
.find(|e| e.parent == "WizardNav" && e.child == "WizardNavItem")
.unwrap();
assert_eq!(
forward.strength,
EdgeStrength::Required,
"WizardNav→WizardNavItem (Required) should be unchanged"
);
// Structural direction should be downgraded to Allowed
let reverse = tree
.edges
.iter()
.find(|e| e.parent == "WizardNavItem" && e.child == "WizardNav")
.unwrap();
assert_eq!(
reverse.strength,
EdgeStrength::Allowed,
"WizardNavItem→WizardNav (Structural) should be downgraded to Allowed"
);
}
/// When one direction is Allowed (CHP=NO) and the other is Required
/// (CHP=YES), there is no bidirectional CHP cycle — no downgrade needed.
#[test]
fn test_no_downgrade_when_one_direction_is_allowed() {
let mut tree = CompositionTree {
root: "Menu".into(),
family_members: vec!["Menu".into(), "MenuItem".into()],
edges: vec![
// Forward: Menu → MenuItem (Required, CHP=YES)
CompositionEdge {
parent: "Menu".into(),
child: "MenuItem".into(),
relationship: ChildRelationship::DirectChild,
required: true,
bem_evidence: None,
strength: EdgeStrength::Required,
prop_name: None,
},
// Reverse: MenuItem → Menu (Allowed, CHP=NO)
CompositionEdge {
parent: "MenuItem".into(),
child: "Menu".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: None,
strength: EdgeStrength::Allowed,
prop_name: None,
},
],
};
downgrade_bidirectional_chp_cycles(&mut tree);
// Both should be unchanged — no CHP cycle (Allowed has CHP=NO)
let forward = tree
.edges
.iter()
.find(|e| e.parent == "Menu" && e.child == "MenuItem")
.unwrap();
assert_eq!(forward.strength, EdgeStrength::Required);
let reverse = tree
.edges
.iter()
.find(|e| e.parent == "MenuItem" && e.child == "Menu")
.unwrap();
assert_eq!(reverse.strength, EdgeStrength::Allowed);
}
/// Verify that BEM edges are created with required=false.
/// Only internally rendered children should be required.
#[test]
fn test_bem_edges_are_not_required() {
let mut parent = make_profile("Dropdown");
parent.has_children_prop = true;
parent.bem_block = Some("dropdown".into());
parent.css_tokens_used.insert("styles.dropdown".into());
let mut list = make_profile("DropdownList");
list.has_children_prop = true;
list.bem_block = Some("dropdown".into());
list.css_tokens_used.insert("styles.dropdownList".into());
let mut group = make_profile("DropdownGroup");
group.has_children_prop = true;
group.bem_block = Some("dropdown".into());
group.css_tokens_used.insert("styles.dropdownGroup".into());
let mut profiles = HashMap::new();
profiles.insert("Dropdown".into(), parent);
profiles.insert("DropdownList".into(), list);
profiles.insert("DropdownGroup".into(), group);
let family = vec![
"Dropdown".into(),
"DropdownList".into(),
"DropdownGroup".into(),
];
let tree = build_composition_tree_v2(&profiles, &family, None, None, &[], None).unwrap();
// All BEM-derived edges should be required=false
for edge in &tree.edges {
if edge.relationship == ChildRelationship::DirectChild {
assert!(
!edge.required,
"BEM edge {} → {} should not be required (BEM proves membership, not requirement)",
edge.parent, edge.child
);
}
}
}
// ── Hyphen boundary tests for infer_ownership_by_name_prefix ─────
#[test]
fn test_label_labelgroup_no_ownership_edge() {
// LabelGroup has BEM block "labelGroup" (camelCase of "label-group")
// — a SEPARATE block from Label's "label" block. Label should NOT
// own LabelGroup via name-prefix inference.
let mut label = make_profile("Label");
label.has_children_prop = true;
label.bem_block = Some("label".into());
label.css_tokens_used = [
"styles.label".to_string(),
"styles.labelText".to_string(),
"styles.labelIcon".to_string(),
]
.into_iter()
.collect();
let mut label_group = make_profile("LabelGroup");
label_group.has_children_prop = true;
label_group.bem_block = Some("labelGroup".into()); // camelCase of "label-group"
label_group.css_tokens_used = [
"styles.labelGroup".to_string(),
"styles.labelGroupList".to_string(),
"styles.labelGroupMain".to_string(),
"styles.labelGroupClose".to_string(),
]
.into_iter()
.collect();
let mut profiles = HashMap::new();
profiles.insert("Label".into(), label);
profiles.insert("LabelGroup".into(), label_group);
let family = vec!["Label".into(), "LabelGroup".into()];
let tree = build_composition_tree_v2(&profiles, &family, None, None, &[], None).unwrap();
// There should be NO edge from Label -> LabelGroup
let bad_edge = tree
.edges
.iter()
.find(|e| e.parent == "Label" && e.child == "LabelGroup");
assert!(
bad_edge.is_none(),
"Label should NOT own LabelGroup — 'label-group' is a separate BEM block \
(hyphen boundary after 'label'). Found edge: {:?}",
bad_edge
);
}
#[test]
fn test_alert_alertgroup_no_ownership_edge() {
// AlertGroup has BEM block "alert-group" — separate from Alert's
// "alert" block.
let mut alert = make_profile("Alert");
alert.has_children_prop = true;
alert.bem_block = Some("alert".into());
alert.css_tokens_used = ["styles.alert".to_string(), "styles.alertTitle".to_string()]
.into_iter()
.collect();
let mut alert_group = make_profile("AlertGroup");
alert_group.has_children_prop = true;
alert_group.bem_block = Some("alertGroup".into()); // camelCase of "alert-group"
alert_group.css_tokens_used = [
"styles.alertGroup".to_string(),
"styles.alertGroupItem".to_string(),
]
.into_iter()
.collect();
let mut profiles = HashMap::new();
profiles.insert("Alert".into(), alert);
profiles.insert("AlertGroup".into(), alert_group);
let family = vec!["Alert".into(), "AlertGroup".into()];
let tree = build_composition_tree_v2(&profiles, &family, None, None, &[], None).unwrap();
let bad_edge = tree
.edges
.iter()
.find(|e| e.parent == "Alert" && e.child == "AlertGroup");
assert!(
bad_edge.is_none(),
"Alert should NOT own AlertGroup — 'alert-group' is a separate BEM block. \
Found edge: {:?}",
bad_edge
);
}
#[test]
fn test_modal_modalbox_no_false_ownership() {
// Modal's own BEM block is "backdrop", children use "modalBox".
// Even though "modal" is a prefix of "modalBox", the name-prefix
// inference should NOT create ownership edges because "modalBox"
// is a different block from "modal" (it's a separate CSS file
// pf-v6-c-modal-box, not an element of a "modal" block).
//
// In practice, Modal has zero composition tree edges because its
// children (ModalBody, ModalHeader, ModalFooter) are consumer-
// provided via {children}, not internally rendered.
let mut modal = make_profile("Modal");
modal.has_children_prop = true;
modal.bem_block = Some("backdrop".into());
modal.css_tokens_used = ["styles.backdrop".to_string()].into_iter().collect();
let mut modal_box = make_profile("ModalBox");
modal_box.has_children_prop = true;
modal_box.bem_block = Some("modalBox".into()); // camelCase of "modal-box"
modal_box.css_tokens_used = [
"styles.modalBox".to_string(),
"styles.modalBoxBody".to_string(),
"styles.modalBoxHeader".to_string(),
]
.into_iter()
.collect();
let mut modal_body = make_profile("ModalBoxBody");
modal_body.has_children_prop = true;
modal_body.bem_block = None; // Shares ModalBox's block
modal_body.css_tokens_used = ["styles.modalBoxBody".to_string()].into_iter().collect();
let mut profiles = HashMap::new();
profiles.insert("Modal".into(), modal);
profiles.insert("ModalBox".into(), modal_box);
profiles.insert("ModalBoxBody".into(), modal_body);
let family = vec!["Modal".into(), "ModalBox".into(), "ModalBoxBody".into()];
let tree = build_composition_tree_v2(&profiles, &family, None, None, &[], None).unwrap();
// Name-prefix inference should NOT create Modal → ModalBox because
// "modalBox" != "modal" (different block). ModalBox → ModalBoxBody
// edges may be created via BEM element matching (modalBoxBody is an
// element of the modalBox block).
let modal_owns_box = tree
.edges
.iter()
.any(|e| e.parent == "Modal" && e.child == "ModalBox");
assert!(
!modal_owns_box,
"Modal should NOT own ModalBox via name-prefix — 'modalBox' is a different \
BEM block from 'modal'. Modal's children are consumer-provided. \
Edges: {:?}",
tree.edges
);
}
/// Components with outgoing edges but no incoming edges should be
/// retained as secondary roots, not dropped by Step 10.
///
/// Models the JumpLinks pattern: JumpLinksList wraps <ul> and
/// JumpLinksItem renders <li>. JumpLinksList has no parent in the
/// tree, making it a secondary root alongside JumpLinks.
#[test]
fn test_secondary_root_retained_for_dom_nesting() {
// JumpLinks is the primary root (first export).
// JumpLinksList wraps <ul>, JumpLinksItem renders <li>.
// No signal creates an edge INTO JumpLinksList, but DOM nesting
// creates JumpLinksList → JumpLinksItem. JumpLinksList should
// survive as a secondary root.
let jump_links = make_profile("JumpLinks");
let mut jump_links_list = make_profile("JumpLinksList");
jump_links_list.has_children_prop = true;
jump_links_list.children_slot_path = vec!["ul".into()];
jump_links_list.rendered_elements.insert("ul".into(), 1);
let mut jump_links_item = make_profile("JumpLinksItem");
jump_links_item.has_children_prop = true;
jump_links_item.children_slot_path = vec!["li".into(), "a".into()];
jump_links_item.rendered_elements.insert("li".into(), 1);
let mut profiles = HashMap::new();
profiles.insert("JumpLinks".into(), jump_links);
profiles.insert("JumpLinksList".into(), jump_links_list);
profiles.insert("JumpLinksItem".into(), jump_links_item);
let family = vec![
"JumpLinks".into(),
"JumpLinksList".into(),
"JumpLinksItem".into(),
];
let tree = build_composition_tree_v2(&profiles, &family, None, None, &[], None).unwrap();
// JumpLinksList should be retained as a member (secondary root)
assert!(
tree.family_members.contains(&"JumpLinksList".into()),
"JumpLinksList should be retained as a secondary root. Members: {:?}",
tree.family_members
);
// The DOM nesting edge JumpLinksList → JumpLinksItem should exist
let list_to_item = tree
.edges
.iter()
.find(|e| e.parent == "JumpLinksList" && e.child == "JumpLinksItem");
assert!(
list_to_item.is_some(),
"Expected JumpLinksList → JumpLinksItem from DOM nesting. Edges: {:?}",
tree.edges
);
// JumpLinksItem should also be retained (it has an incoming edge)
assert!(
tree.family_members.contains(&"JumpLinksItem".into()),
"JumpLinksItem should be retained (has incoming edge). Members: {:?}",
tree.family_members
);
}
/// Components with no edges at all should still be dropped by Step 10.
#[test]
fn test_truly_unconnected_member_still_dropped() {
let mut root = make_profile("Root");
root.has_children_prop = true;
let mut child = make_profile("Child");
child.has_children_prop = true;
// Orphan has no edges at all — no structural evidence
let orphan = make_profile("Orphan");
// Create a context edge Root → Child to give them structure
root.rendered_components = vec!["RootContext.Provider".into()];
child.consumed_contexts = vec!["RootContext".into()];
let mut profiles = HashMap::new();
profiles.insert("Root".into(), root);
profiles.insert("Child".into(), child);
profiles.insert("Orphan".into(), orphan);
let family = vec!["Root".into(), "Child".into(), "Orphan".into()];
let tree = build_composition_tree_v2(&profiles, &family, None, None, &[], None).unwrap();
// Orphan has no edges, should be dropped
assert!(
!tree.family_members.contains(&"Orphan".into()),
"Orphan with no edges should be dropped. Members: {:?}",
tree.family_members
);
// Root and Child should still be present
assert!(tree.family_members.contains(&"Root".into()));
assert!(tree.family_members.contains(&"Child".into()));
}
#[test]
fn test_menu_menutoggle_no_ownership_edge() {
// MenuToggle has BEM block "menu-toggle" — separate from Menu.
let mut menu = make_profile("Menu");
menu.has_children_prop = true;
menu.bem_block = Some("menu".into());
menu.css_tokens_used = ["styles.menu".to_string()].into_iter().collect();
let mut menu_toggle = make_profile("MenuToggle");
menu_toggle.has_children_prop = true;
menu_toggle.bem_block = Some("menuToggle".into()); // camelCase of "menu-toggle"
menu_toggle.css_tokens_used = [
"styles.menuToggle".to_string(),
"styles.menuToggleIcon".to_string(),
]
.into_iter()
.collect();
let mut profiles = HashMap::new();
profiles.insert("Menu".into(), menu);
profiles.insert("MenuToggle".into(), menu_toggle);
let family = vec!["Menu".into(), "MenuToggle".into()];
let tree = build_composition_tree_v2(&profiles, &family, None, None, &[], None).unwrap();
let bad_edge = tree
.edges
.iter()
.find(|e| e.parent == "Menu" && e.child == "MenuToggle");
assert!(
bad_edge.is_none(),
"Menu should NOT own MenuToggle — 'menu-toggle' is a separate BEM block. \
Found edge: {:?}",
bad_edge
);
}
/// Bidirectional cloneElement edges (A→B and B→A) should be removed.
/// These indicate peers with shared prop vocabulary, not hierarchy.
#[test]
fn test_clone_element_bidirectional_pairs_removed() {
use crate::sd_types::CloneElementInjection;
// Three components: Root, SubA, SubB.
// SubA and SubB both inject the same props (height, width)
// and both declare the same props. This creates bidirectional
// cloneElement edges SubA→SubB and SubB→SubA.
let mut root = make_profile("Root");
root.has_children_prop = true;
// Root renders SubA and SubB via Step 1
root.rendered_components = vec!["SubA".into(), "SubB".into()];
let mut sub_a = make_profile("SubA");
sub_a.clone_element_injections = vec![CloneElementInjection {
injected_props: vec!["height".into(), "width".into(), "theme".into()],
}];
sub_a.all_props = vec!["height".into(), "width".into(), "theme".into()]
.into_iter()
.collect();
let mut sub_b = make_profile("SubB");
sub_b.clone_element_injections = vec![CloneElementInjection {
injected_props: vec!["height".into(), "width".into(), "theme".into()],
}];
sub_b.all_props = vec!["height".into(), "width".into(), "theme".into()]
.into_iter()
.collect();
let mut profiles = HashMap::new();
profiles.insert("Root".into(), root);
profiles.insert("SubA".into(), sub_a);
profiles.insert("SubB".into(), sub_b);
let family = vec!["Root".into(), "SubA".into(), "SubB".into()];
let tree = build_composition_tree_v2(&profiles, &family, None, None, &[], None).unwrap();
// Step 1 edges Root→SubA and Root→SubB should exist
assert!(
tree.edges
.iter()
.any(|e| e.parent == "Root" && e.child == "SubA"),
"Root → SubA should exist from internal rendering"
);
assert!(
tree.edges
.iter()
.any(|e| e.parent == "Root" && e.child == "SubB"),
"Root → SubB should exist from internal rendering"
);
// Bidirectional cloneElement edges SubA↔SubB should NOT exist
let sub_a_to_b = tree
.edges
.iter()
.find(|e| e.parent == "SubA" && e.child == "SubB");
assert!(
sub_a_to_b.is_none(),
"SubA → SubB should be removed as bidirectional cloneElement pair. Edges: {:?}",
tree.edges
);
let sub_b_to_a = tree
.edges
.iter()
.find(|e| e.parent == "SubB" && e.child == "SubA");
assert!(
sub_b_to_a.is_none(),
"SubB → SubA should be removed as bidirectional cloneElement pair. Edges: {:?}",
tree.edges
);
}
/// cloneElement edge A→B should be skipped when B→A already exists
/// from a prior step (e.g., Step 1 internal rendering).
#[test]
fn test_clone_element_skipped_when_reverse_exists() {
use crate::sd_types::CloneElementInjection;
// Root renders Sub (Step 1). Sub uses cloneElement to inject
// props that Root declares. Without the reverse-edge check,
// Sub→Root would be created (wrong). With it, Sub→Root is skipped.
let mut root = make_profile("Root");
root.has_children_prop = true;
root.rendered_components = vec!["Sub".into()];
root.all_props = vec!["height".into(), "width".into()].into_iter().collect();
let mut sub = make_profile("Sub");
sub.clone_element_injections = vec![CloneElementInjection {
injected_props: vec!["height".into(), "width".into()],
}];
let mut profiles = HashMap::new();
profiles.insert("Root".into(), root);
profiles.insert("Sub".into(), sub);
let family = vec!["Root".into(), "Sub".into()];
let tree = build_composition_tree_v2(&profiles, &family, None, None, &[], None).unwrap();
// Root→Sub should exist from Step 1
assert!(
tree.edges
.iter()
.any(|e| e.parent == "Root" && e.child == "Sub"),
"Root → Sub should exist from internal rendering"
);
// Sub→Root should NOT exist (reverse of Step 1 edge)
let bad_edge = tree
.edges
.iter()
.find(|e| e.parent == "Sub" && e.child == "Root");
assert!(
bad_edge.is_none(),
"Sub → Root should be skipped (reverse of prior Root → Sub). Edges: {:?}",
tree.edges
);
}
#[test]
fn test_clone_element_skipped_when_no_children_prop() {
use crate::sd_types::CloneElementInjection;
// ActionsColumn scenario: parent uses cloneElement on internally-
// created elements (dropdown items), NOT on consumer children.
// has_children_prop = false → no edge should be created.
let mut parent = make_profile("ActionsColumn");
parent.has_children_prop = false; // does not accept children
parent.clone_element_injections = vec![CloneElementInjection {
injected_props: vec!["onClick".into(), "isDisabled".into()],
}];
let mut child = make_profile("DraggableCell");
child.has_children_prop = true;
child.all_props = vec!["className".into(), "id".into(), "onClick".into()]
.into_iter()
.collect();
let mut profiles = HashMap::new();
profiles.insert("ActionsColumn".into(), parent);
profiles.insert("DraggableCell".into(), child);
let family = vec!["ActionsColumn".into(), "DraggableCell".into()];
let tree = build_composition_tree_v2(&profiles, &family, None, None, &[], None).unwrap();
// No edge should be created — parent doesn't accept children
let false_edge = tree
.edges
.iter()
.find(|e| e.parent == "ActionsColumn" && e.child == "DraggableCell");
assert!(
false_edge.is_none(),
"ActionsColumn → DraggableCell should not exist (has_children_prop=false). Edges: {:?}",
tree.edges
);
}
#[test]
fn test_clone_element_works_when_has_children_prop() {
use crate::sd_types::CloneElementInjection;
// ToggleGroup scenario: parent uses cloneElement on consumer-
// provided children. has_children_prop = true → edge is created.
let mut parent = make_profile("ToggleGroup");
parent.has_children_prop = true; // accepts children
parent.clone_element_injections = vec![CloneElementInjection {
injected_props: vec!["isDisabled".into()],
}];
let mut child = make_profile("ToggleGroupItem");
child.has_children_prop = true;
child.all_props = vec!["isDisabled".into(), "onChange".into()]
.into_iter()
.collect();
let mut profiles = HashMap::new();
profiles.insert("ToggleGroup".into(), parent);
profiles.insert("ToggleGroupItem".into(), child);
let family = vec!["ToggleGroup".into(), "ToggleGroupItem".into()];
let tree = build_composition_tree_v2(&profiles, &family, None, None, &[], None).unwrap();
// Edge should be created — parent accepts children and injects via cloneElement
let edge = tree
.edges
.iter()
.find(|e| e.parent == "ToggleGroup" && e.child == "ToggleGroupItem");
assert!(
edge.is_some(),
"ToggleGroup → ToggleGroupItem should exist (has_children_prop=true). Edges: {:?}",
tree.edges
);
assert_eq!(
edge.unwrap().strength,
EdgeStrength::Structural,
"cloneElement edge should be Structural (CHP=YES: child needs injected props)"
);
}
/// ChartDonutThreshold scenario: parent types `children` as
/// `React.ReactElement<any>` (singular, specific). This indicates a
/// purpose-built wrapper — the parent exists to wrap a specific child.
/// The cloneElement edge should use Wrapper strength (PMC=YES, CHP=NO)
/// instead of Structural (CHP=YES, PMC=NO).
#[test]
fn test_clone_element_react_element_children_uses_wrapper_strength() {
use crate::sd_types::CloneElementInjection;
let mut parent = make_profile("ChartDonutThreshold");
parent.has_children_prop = true;
parent
.prop_types
.insert("children".into(), "React.ReactElement<any>".into());
parent.clone_element_injections = vec![CloneElementInjection {
injected_props: vec!["isStatic".into(), "theme".into()],
}];
let mut child = make_profile("ChartDonutUtilization");
child.all_props = vec!["isStatic".into(), "theme".into(), "data".into()]
.into_iter()
.collect();
let mut profiles = HashMap::new();
profiles.insert("ChartDonutThreshold".into(), parent);
profiles.insert("ChartDonutUtilization".into(), child);
let family = vec!["ChartDonutUtilization".into(), "ChartDonutThreshold".into()];
let tree = build_composition_tree_v2(&profiles, &family, None, None, &[], None).unwrap();
let edge = tree
.edges
.iter()
.find(|e| e.parent == "ChartDonutThreshold" && e.child == "ChartDonutUtilization");
assert!(
edge.is_some(),
"ChartDonutThreshold → ChartDonutUtilization should exist. Edges: {:?}",
tree.edges
);
assert_eq!(
edge.unwrap().strength,
EdgeStrength::Wrapper,
"ReactElement<any> children type should produce Wrapper strength (PMC=YES, CHP=NO)"
);
}
/// ReactNode children type should still produce Structural strength.
/// This is the common case (AlertGroup, DataListItem, Breadcrumb, etc.).
#[test]
fn test_clone_element_react_node_children_uses_structural_strength() {
use crate::sd_types::CloneElementInjection;
let mut parent = make_profile("ToggleGroup");
parent.has_children_prop = true;
parent
.prop_types
.insert("children".into(), "React.ReactNode".into());
parent.clone_element_injections = vec![CloneElementInjection {
injected_props: vec!["isDisabled".into()],
}];
let mut child = make_profile("ToggleGroupItem");
child.has_children_prop = true;
child.all_props = vec!["isDisabled".into(), "onChange".into()]
.into_iter()
.collect();
let mut profiles = HashMap::new();
profiles.insert("ToggleGroup".into(), parent);
profiles.insert("ToggleGroupItem".into(), child);
let family = vec!["ToggleGroup".into(), "ToggleGroupItem".into()];
let tree = build_composition_tree_v2(&profiles, &family, None, None, &[], None).unwrap();
let edge = tree
.edges
.iter()
.find(|e| e.parent == "ToggleGroup" && e.child == "ToggleGroupItem");
assert!(
edge.is_some(),
"ToggleGroup → ToggleGroupItem should exist. Edges: {:?}",
tree.edges
);
assert_eq!(
edge.unwrap().strength,
EdgeStrength::Structural,
"ReactNode children type should produce Structural strength (CHP=YES, PMC=NO)"
);
}
// ── Signal A (Step 5.5): CSS layout_children tests ──────────────
#[test]
fn test_layout_children_creates_intermediate_edge() {
// EmptyState scenario: CSS shows footer wraps actions (shared
// rule with flex-wrap). Signal A should create
// EmptyStateFooter → EmptyStateActions.
let mut root_prof = make_profile("EmptyState");
root_prof.has_children_prop = true;
root_prof.bem_block = Some("emptyState".into());
root_prof.css_tokens_used = tokens(&["styles.emptyState"]);
let mut footer = make_profile("EmptyStateFooter");
footer.has_children_prop = true;
footer.bem_block = Some("emptyState".into());
footer.css_tokens_used = tokens(&["styles.emptyStateFooter"]);
let mut actions = make_profile("EmptyStateActions");
actions.bem_block = Some("emptyState".into());
actions.css_tokens_used = tokens(&["styles.emptyStateActions"]);
let mut profiles = HashMap::new();
profiles.insert("EmptyState".into(), root_prof);
profiles.insert("EmptyStateFooter".into(), footer);
profiles.insert("EmptyStateActions".into(), actions);
let family = vec![
"EmptyState".into(),
"EmptyStateFooter".into(),
"EmptyStateActions".into(),
];
// CSS profile with layout_children: footer wraps actions
let css_prof = CssBlockProfile {
block: "emptyState".into(),
layout_children: vec![("footer".into(), "actions".into())],
..Default::default()
};
let tree = {
let css_map = HashMap::from([(css_prof.block.clone(), css_prof)]);
let block_key = css_map.keys().next().unwrap().clone();
build_composition_tree_v2(
&profiles,
&family,
Some(&css_map),
Some(&block_key),
&[],
None,
)
}
.unwrap();
// Signal A: EmptyStateFooter → EmptyStateActions (from layout_children)
let footer_to_actions = tree
.edges
.iter()
.find(|e| e.parent == "EmptyStateFooter" && e.child == "EmptyStateActions");
assert!(
footer_to_actions.is_some(),
"Expected EmptyStateFooter → EmptyStateActions from layout_children. Edges: {:?}",
tree.edges
);
assert_eq!(footer_to_actions.unwrap().strength, EdgeStrength::Allowed);
assert!(footer_to_actions
.unwrap()
.bem_evidence
.as_ref()
.unwrap()
.contains("CSS layout container"));
// Signal B: EmptyStateFooter should get root edge (orphan with outgoing
// from Signal A but no incoming). EmptyStateActions should NOT get
// root edge (already has parent from Signal A).
let root_to_footer = tree
.edges
.iter()
.find(|e| e.parent == "EmptyState" && e.child == "EmptyStateFooter");
assert!(
root_to_footer.is_some(),
"Expected EmptyState → EmptyStateFooter from BEM orphan fallback. Edges: {:?}",
tree.edges
);
// EmptyStateActions should NOT be a direct child of root (has parent from Signal A)
let root_to_actions = tree
.edges
.iter()
.find(|e| e.parent == "EmptyState" && e.child == "EmptyStateActions");
assert!(
root_to_actions.is_none(),
"EmptyState → EmptyStateActions should NOT exist (has parent from Signal A). Edges: {:?}",
tree.edges
);
}
// ── Signal B (Step 8.5): BEM element orphan fallback tests ──────
#[test]
fn test_bem_orphan_fallback_connects_orphans_to_root() {
// Panel scenario: root has children, sub-components are BEM
// elements with no other signals connecting them.
let mut root_prof = make_profile("Panel");
root_prof.has_children_prop = true;
root_prof.bem_block = Some("panel".into());
root_prof.css_tokens_used = tokens(&["styles.panel"]);
let mut header = make_profile("PanelHeader");
header.bem_block = Some("panel".into());
header.css_tokens_used = tokens(&["styles.panelHeader"]);
let mut main = make_profile("PanelMain");
main.has_children_prop = true;
main.bem_block = Some("panel".into());
main.css_tokens_used = tokens(&["styles.panelMain"]);
let mut footer = make_profile("PanelFooter");
footer.bem_block = Some("panel".into());
footer.css_tokens_used = tokens(&["styles.panelFooter"]);
let mut profiles = HashMap::new();
profiles.insert("Panel".into(), root_prof);
profiles.insert("PanelHeader".into(), header);
profiles.insert("PanelMain".into(), main);
profiles.insert("PanelFooter".into(), footer);
let family = vec![
"Panel".into(),
"PanelHeader".into(),
"PanelMain".into(),
"PanelFooter".into(),
];
// Minimal CSS profile — just the block name, no nesting selectors
let css_prof = CssBlockProfile {
block: "panel".into(),
..Default::default()
};
let tree = {
let css_map = HashMap::from([(css_prof.block.clone(), css_prof)]);
let block_key = css_map.keys().next().unwrap().clone();
build_composition_tree_v2(
&profiles,
&family,
Some(&css_map),
Some(&block_key),
&[],
None,
)
}
.unwrap();
// All 3 sub-components should be connected to root
for child in &["PanelHeader", "PanelMain", "PanelFooter"] {
let edge = tree
.edges
.iter()
.find(|e| e.parent == "Panel" && e.child == *child);
assert!(
edge.is_some(),
"Expected Panel → {} from BEM orphan fallback. Edges: {:?}",
child,
tree.edges
);
assert_eq!(edge.unwrap().strength, EdgeStrength::Allowed);
assert!(edge
.unwrap()
.bem_evidence
.as_ref()
.unwrap()
.contains("BEM element fallback"));
}
// All 3 should be retained as family members
for member in &["PanelHeader", "PanelMain", "PanelFooter"] {
assert!(
tree.family_members.contains(&member.to_string()),
"{} should be in family_members. Members: {:?}",
member,
tree.family_members
);
}
}
#[test]
fn test_bem_orphan_fallback_skips_independent_block() {
// Label/LabelGroup scenario: LabelGroup has its own BEM block
// ("labelGroup") different from Label's ("label"). Signal B
// should NOT create Label → LabelGroup.
let mut label = make_profile("Label");
label.has_children_prop = true;
label.bem_block = Some("label".into());
label.css_tokens_used = tokens(&["styles.label"]);
let mut label_group = make_profile("LabelGroup");
label_group.has_children_prop = true;
label_group.bem_block = Some("labelGroup".into());
// This token would match the prefix strip: "labelGroup" starts
// with "label", producing false map entry "group" → "LabelGroup"
label_group.css_tokens_used = tokens(&["styles.labelGroup"]);
let mut profiles = HashMap::new();
profiles.insert("Label".into(), label);
profiles.insert("LabelGroup".into(), label_group);
let family = vec!["Label".into(), "LabelGroup".into()];
let css_prof = CssBlockProfile {
block: "label".into(),
..Default::default()
};
let tree = {
let css_map = HashMap::from([(css_prof.block.clone(), css_prof)]);
let block_key = css_map.keys().next().unwrap().clone();
build_composition_tree_v2(
&profiles,
&family,
Some(&css_map),
Some(&block_key),
&[],
None,
)
}
.unwrap();
// Label → LabelGroup should NOT exist (independent BEM block)
let bad_edge = tree
.edges
.iter()
.find(|e| e.parent == "Label" && e.child == "LabelGroup");
assert!(
bad_edge.is_none(),
"Label → LabelGroup should NOT exist (independent BEM block). Edges: {:?}",
tree.edges
);
}
#[test]
fn test_bem_orphan_fallback_skips_already_parented() {
// If a component already has a parent from another signal,
// Signal B should not create a duplicate root edge.
let mut root_prof = make_profile("Menu");
root_prof.has_children_prop = true;
root_prof.bem_block = Some("menu".into());
root_prof.css_tokens_used = tokens(&["styles.menu"]);
root_prof.rendered_components = vec!["MenuContext.Provider".into()];
let mut menu_list = make_profile("MenuList");
menu_list.has_children_prop = true;
menu_list.bem_block = Some("menu".into());
menu_list.css_tokens_used = tokens(&["styles.menuList"]);
menu_list.consumed_contexts = vec!["MenuContext".into()];
let mut profiles = HashMap::new();
profiles.insert("Menu".into(), root_prof);
profiles.insert("MenuList".into(), menu_list);
let family = vec!["Menu".into(), "MenuList".into()];
let css_prof = CssBlockProfile {
block: "menu".into(),
..Default::default()
};
let tree = {
let css_map = HashMap::from([(css_prof.block.clone(), css_prof)]);
let block_key = css_map.keys().next().unwrap().clone();
build_composition_tree_v2(
&profiles,
&family,
Some(&css_map),
Some(&block_key),
&[],
None,
)
}
.unwrap();
// MenuList should have a parent from context nesting (Step 6)
let context_edge = tree
.edges
.iter()
.find(|e| e.parent == "Menu" && e.child == "MenuList");
assert!(
context_edge.is_some(),
"Expected Menu → MenuList from context nesting. Edges: {:?}",
tree.edges
);
// Should be exactly ONE edge from Menu → MenuList (not duplicated
// by Signal B)
let edge_count = tree
.edges
.iter()
.filter(|e| e.parent == "Menu" && e.child == "MenuList")
.count();
assert_eq!(
edge_count, 1,
"Should have exactly 1 Menu → MenuList edge, not duplicated by Signal B. Edges: {:?}",
tree.edges
);
}
#[test]
fn test_bem_orphan_fallback_skips_when_root_has_no_children() {
// If root doesn't have has_children_prop, Signal B should not fire.
let mut root_prof = make_profile("Widget");
root_prof.has_children_prop = false; // no children!
root_prof.bem_block = Some("widget".into());
root_prof.css_tokens_used = tokens(&["styles.widget"]);
let mut sub = make_profile("WidgetBody");
sub.bem_block = Some("widget".into());
sub.css_tokens_used = tokens(&["styles.widgetBody"]);
let mut profiles = HashMap::new();
profiles.insert("Widget".into(), root_prof);
profiles.insert("WidgetBody".into(), sub);
let family = vec!["Widget".into(), "WidgetBody".into()];
let css_prof = CssBlockProfile {
block: "widget".into(),
..Default::default()
};
let tree = {
let css_map = HashMap::from([(css_prof.block.clone(), css_prof)]);
let block_key = css_map.keys().next().unwrap().clone();
build_composition_tree_v2(
&profiles,
&family,
Some(&css_map),
Some(&block_key),
&[],
None,
)
}
.unwrap();
// WidgetBody should NOT be connected (root has no children prop)
let edge = tree
.edges
.iter()
.find(|e| e.parent == "Widget" && e.child == "WidgetBody");
assert!(
edge.is_none(),
"Widget → WidgetBody should NOT exist (root has no children). Edges: {:?}",
tree.edges
);
}
#[test]
fn test_bem_orphan_fallback_promotes_secondary_root() {
// JumpLinks scenario: JumpLinksList is a secondary root (has
// outgoing edge to JumpLinksItem via DOM nesting but no incoming).
// Signal B should create JumpLinks → JumpLinksList.
let mut root_prof = make_profile("JumpLinks");
root_prof.has_children_prop = true;
root_prof.bem_block = Some("jumpLinks".into());
root_prof.css_tokens_used = tokens(&["styles.jumpLinks"]);
let mut list = make_profile("JumpLinksList");
list.has_children_prop = true;
list.bem_block = Some("jumpLinks".into());
list.css_tokens_used = tokens(&["styles.jumpLinksList"]);
list.children_slot_path = vec!["ul".into()];
list.rendered_elements.insert("ul".into(), 1);
let mut item = make_profile("JumpLinksItem");
item.bem_block = Some("jumpLinks".into());
item.css_tokens_used = tokens(&["styles.jumpLinksItem"]);
item.children_slot_path = vec!["li".into(), "button".into()];
item.rendered_elements.insert("li".into(), 1);
let mut profiles = HashMap::new();
profiles.insert("JumpLinks".into(), root_prof);
profiles.insert("JumpLinksList".into(), list);
profiles.insert("JumpLinksItem".into(), item);
let family = vec![
"JumpLinks".into(),
"JumpLinksList".into(),
"JumpLinksItem".into(),
];
let css_prof = CssBlockProfile {
block: "jumpLinks".into(),
..Default::default()
};
let tree = {
let css_map = HashMap::from([(css_prof.block.clone(), css_prof)]);
let block_key = css_map.keys().next().unwrap().clone();
build_composition_tree_v2(
&profiles,
&family,
Some(&css_map),
Some(&block_key),
&[],
None,
)
}
.unwrap();
// JumpLinksList → JumpLinksItem from DOM nesting (Step 7)
let list_to_item = tree
.edges
.iter()
.find(|e| e.parent == "JumpLinksList" && e.child == "JumpLinksItem");
assert!(
list_to_item.is_some(),
"Expected JumpLinksList → JumpLinksItem from DOM nesting. Edges: {:?}",
tree.edges
);
// JumpLinks → JumpLinksList from Signal B (secondary root promotion)
let root_to_list = tree
.edges
.iter()
.find(|e| e.parent == "JumpLinks" && e.child == "JumpLinksList");
assert!(
root_to_list.is_some(),
"Expected JumpLinks → JumpLinksList from BEM orphan fallback. Edges: {:?}",
tree.edges
);
// The full chain: JumpLinks → JumpLinksList → JumpLinksItem
// After Step 9 (suppress), root→JumpLinksItem should not exist
// (because JumpLinksList is an intermediate).
let root_to_item = tree
.edges
.iter()
.find(|e| e.parent == "JumpLinks" && e.child == "JumpLinksItem");
assert!(
root_to_item.is_none(),
"JumpLinks → JumpLinksItem should not exist (JumpLinksList is intermediate). Edges: {:?}",
tree.edges
);
// All 3 should be retained
assert_eq!(tree.family_members.len(), 3);
}
#[test]
fn test_layout_children_and_orphan_fallback_interaction() {
// Verifies Signal A runs before Signal B. With layout_children
// creating an intermediate edge, Signal B should not create a
// redundant root→leaf edge.
let mut root_prof = make_profile("EmptyState");
root_prof.has_children_prop = true;
root_prof.bem_block = Some("emptyState".into());
root_prof.css_tokens_used = tokens(&["styles.emptyState"]);
let mut body = make_profile("EmptyStateBody");
body.bem_block = Some("emptyState".into());
body.css_tokens_used = tokens(&["styles.emptyStateBody"]);
let mut footer = make_profile("EmptyStateFooter");
footer.has_children_prop = true;
footer.bem_block = Some("emptyState".into());
footer.css_tokens_used = tokens(&["styles.emptyStateFooter"]);
let mut actions = make_profile("EmptyStateActions");
actions.bem_block = Some("emptyState".into());
actions.css_tokens_used = tokens(&["styles.emptyStateActions"]);
let mut profiles = HashMap::new();
profiles.insert("EmptyState".into(), root_prof);
profiles.insert("EmptyStateBody".into(), body);
profiles.insert("EmptyStateFooter".into(), footer);
profiles.insert("EmptyStateActions".into(), actions);
let family = vec![
"EmptyState".into(),
"EmptyStateBody".into(),
"EmptyStateFooter".into(),
"EmptyStateActions".into(),
];
let css_prof = CssBlockProfile {
block: "emptyState".into(),
layout_children: vec![("footer".into(), "actions".into())],
..Default::default()
};
let tree = {
let css_map = HashMap::from([(css_prof.block.clone(), css_prof)]);
let block_key = css_map.keys().next().unwrap().clone();
build_composition_tree_v2(
&profiles,
&family,
Some(&css_map),
Some(&block_key),
&[],
None,
)
}
.unwrap();
// Expected tree:
// EmptyState
// ├── EmptyStateBody (Signal B: orphan)
// └── EmptyStateFooter (Signal B: orphan with outgoing from Signal A)
// └── EmptyStateActions (Signal A: layout_children)
// Signal A: Footer → Actions
assert!(tree
.edges
.iter()
.any(|e| e.parent == "EmptyStateFooter" && e.child == "EmptyStateActions"));
// Signal B: Root → Body (orphan)
assert!(tree
.edges
.iter()
.any(|e| e.parent == "EmptyState" && e.child == "EmptyStateBody"));
// Signal B: Root → Footer (orphan — no incoming, only outgoing from A)
assert!(tree
.edges
.iter()
.any(|e| e.parent == "EmptyState" && e.child == "EmptyStateFooter"));
// Signal B should NOT create Root → Actions (already parented by Footer)
assert!(
!tree
.edges
.iter()
.any(|e| e.parent == "EmptyState" && e.child == "EmptyStateActions"),
"EmptyState → EmptyStateActions should NOT exist. Edges: {:?}",
tree.edges
);
// All 4 members retained
assert_eq!(tree.family_members.len(), 4);
}
#[test]
fn test_bem_orphan_fallback_no_css_profile() {
// When no CSS profile is provided, Signal B should not fire
// (css_to_component is empty).
let mut root_prof = make_profile("Panel");
root_prof.has_children_prop = true;
root_prof.bem_block = Some("panel".into());
root_prof.css_tokens_used = tokens(&["styles.panel"]);
let mut header = make_profile("PanelHeader");
header.bem_block = Some("panel".into());
header.css_tokens_used = tokens(&["styles.panelHeader"]);
let mut profiles = HashMap::new();
profiles.insert("Panel".into(), root_prof);
profiles.insert("PanelHeader".into(), header);
let family = vec!["Panel".into(), "PanelHeader".into()];
// No CSS profile — Signal B should not fire
let tree = build_composition_tree_v2(&profiles, &family, None, None, &[], None).unwrap();
// PanelHeader should be dropped (no edges, no CSS profile)
assert!(
!tree.family_members.contains(&"PanelHeader".to_string()),
"PanelHeader should be dropped without CSS profile. Members: {:?}",
tree.family_members
);
}
// ── Step 1.5: Delegate tree projection tests ────────────────────
#[test]
fn test_delegate_projection_dropdown_menu() {
// Dropdown wraps Menu. Menu tree has Menu → MenuList → MenuItem.
// Projection should produce Dropdown → DropdownList → DropdownItem.
// Build the Menu "delegate" tree
let menu_tree = CompositionTree {
root: "Menu".into(),
family_members: vec![
"Menu".into(),
"MenuList".into(),
"MenuItem".into(),
"MenuGroup".into(),
],
edges: vec![
CompositionEdge {
parent: "Menu".into(),
child: "MenuList".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: Some("context nesting".into()),
strength: EdgeStrength::Required,
prop_name: None,
},
CompositionEdge {
parent: "MenuList".into(),
child: "MenuItem".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: Some("DOM nesting: ul → li".into()),
strength: EdgeStrength::Required,
prop_name: None,
},
CompositionEdge {
parent: "Menu".into(),
child: "MenuGroup".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: Some("CSS descendant".into()),
strength: EdgeStrength::Allowed,
prop_name: None,
},
],
};
// Dropdown family profiles (thin wrappers, no CSS)
let mut dropdown = make_profile("Dropdown");
dropdown.has_children_prop = true;
let mut dropdown_list = make_profile("DropdownList");
dropdown_list.has_children_prop = true;
let mut dropdown_item = make_profile("DropdownItem");
dropdown_item.has_children_prop = true;
let mut dropdown_group = make_profile("DropdownGroup");
dropdown_group.has_children_prop = true;
let mut profiles = HashMap::new();
profiles.insert("Dropdown".into(), dropdown);
profiles.insert("DropdownList".into(), dropdown_list);
profiles.insert("DropdownItem".into(), dropdown_item);
profiles.insert("DropdownGroup".into(), dropdown_group);
let family = vec![
"Dropdown".into(),
"DropdownList".into(),
"DropdownItem".into(),
"DropdownGroup".into(),
];
// Wrapper → delegate mapping
let mut wrapper_map = HashMap::new();
wrapper_map.insert("Dropdown".into(), "Menu".into());
wrapper_map.insert("DropdownList".into(), "MenuList".into());
wrapper_map.insert("DropdownItem".into(), "MenuItem".into());
wrapper_map.insert("DropdownGroup".into(), "MenuGroup".into());
let ctx = DelegateContext {
delegate_tree: &menu_tree,
wrapper_to_delegate: wrapper_map,
};
let tree = build_composition_tree_v2(&profiles, &family, None, None, &[ctx], None).unwrap();
// Dropdown → DropdownList (from Menu → MenuList)
assert!(
tree.edges
.iter()
.any(|e| e.parent == "Dropdown" && e.child == "DropdownList"),
"Expected Dropdown → DropdownList. Edges: {:?}",
tree.edges
);
// DropdownList → DropdownItem (from MenuList → MenuItem)
assert!(
tree.edges
.iter()
.any(|e| e.parent == "DropdownList" && e.child == "DropdownItem"),
"Expected DropdownList → DropdownItem. Edges: {:?}",
tree.edges
);
// Dropdown → DropdownGroup (from Menu → MenuGroup)
assert!(
tree.edges
.iter()
.any(|e| e.parent == "Dropdown" && e.child == "DropdownGroup"),
"Expected Dropdown → DropdownGroup. Edges: {:?}",
tree.edges
);
// Projected edges inherit the delegate edge's strength.
// Dropdown→DropdownList: delegate Menu→MenuList is Required → Required
// DropdownList→DropdownItem: delegate MenuList→MenuItem is Required → Required
// Dropdown→DropdownGroup: delegate Menu→MenuGroup is Allowed → Allowed
let dl_edge = tree
.edges
.iter()
.find(|e| e.parent == "Dropdown" && e.child == "DropdownList")
.unwrap();
assert_eq!(
dl_edge.strength,
EdgeStrength::Required,
"Dropdown → DropdownList should inherit Required from delegate"
);
let di_edge = tree
.edges
.iter()
.find(|e| e.parent == "DropdownList" && e.child == "DropdownItem")
.unwrap();
assert_eq!(
di_edge.strength,
EdgeStrength::Required,
"DropdownList → DropdownItem should inherit Required from delegate"
);
let dg_edge = tree
.edges
.iter()
.find(|e| e.parent == "Dropdown" && e.child == "DropdownGroup")
.unwrap();
assert_eq!(
dg_edge.strength,
EdgeStrength::Allowed,
"Dropdown → DropdownGroup should inherit Allowed from delegate"
);
// All 4 members retained (not dropped by Step 10)
assert_eq!(
tree.family_members.len(),
4,
"All 4 members should be retained. Members: {:?}",
tree.family_members
);
// Evidence should reference delegate projection
let dd_to_dl = tree
.edges
.iter()
.find(|e| e.parent == "Dropdown" && e.child == "DropdownList")
.unwrap();
assert!(
dd_to_dl
.bem_evidence
.as_ref()
.unwrap()
.contains("Delegate projection"),
"Evidence should reference delegate projection: {:?}",
dd_to_dl.bem_evidence
);
}
#[test]
fn test_delegate_projection_no_edge_for_unmapped_members() {
// If a delegate tree edge has one side unmapped, no edge is created.
let delegate_tree = CompositionTree {
root: "Menu".into(),
family_members: vec!["Menu".into(), "MenuList".into(), "MenuItem".into()],
edges: vec![
CompositionEdge {
parent: "Menu".into(),
child: "MenuList".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: None,
strength: EdgeStrength::Required,
prop_name: None,
},
CompositionEdge {
parent: "MenuList".into(),
child: "MenuItem".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: None,
strength: EdgeStrength::Required,
prop_name: None,
},
],
};
// Only map root and list, NOT item
let mut wrapper_map = HashMap::new();
wrapper_map.insert("Wrapper".into(), "Menu".into());
wrapper_map.insert("WrapperList".into(), "MenuList".into());
// WrapperItem is NOT mapped
let ctx = DelegateContext {
delegate_tree: &delegate_tree,
wrapper_to_delegate: wrapper_map,
};
let wrapper = make_profile("Wrapper");
let wrapper_list = make_profile("WrapperList");
let wrapper_item = make_profile("WrapperItem");
let mut profiles = HashMap::new();
profiles.insert("Wrapper".into(), wrapper);
profiles.insert("WrapperList".into(), wrapper_list);
profiles.insert("WrapperItem".into(), wrapper_item);
let family = vec!["Wrapper".into(), "WrapperList".into(), "WrapperItem".into()];
let tree = build_composition_tree_v2(&profiles, &family, None, None, &[ctx], None).unwrap();
// Wrapper → WrapperList should exist (both mapped)
assert!(tree
.edges
.iter()
.any(|e| e.parent == "Wrapper" && e.child == "WrapperList"));
// WrapperList → WrapperItem should NOT exist (WrapperItem not mapped)
assert!(
!tree
.edges
.iter()
.any(|e| e.parent == "WrapperList" && e.child == "WrapperItem"),
"WrapperList → WrapperItem should not exist (unmapped). Edges: {:?}",
tree.edges
);
// WrapperItem should be dropped (no edges)
assert!(
!tree.family_members.contains(&"WrapperItem".to_string()),
"WrapperItem should be dropped. Members: {:?}",
tree.family_members
);
}
#[test]
fn test_delegate_projection_empty_delegate_tree() {
// If the delegate tree has no edges (e.g., Button), nothing is projected.
let delegate_tree = CompositionTree {
root: "Button".into(),
family_members: vec!["Button".into()],
edges: vec![],
};
let mut wrapper_map = HashMap::new();
wrapper_map.insert("MyButton".into(), "Button".into());
let ctx = DelegateContext {
delegate_tree: &delegate_tree,
wrapper_to_delegate: wrapper_map,
};
let my_button = make_profile("MyButton");
let mut profiles = HashMap::new();
profiles.insert("MyButton".into(), my_button);
let family = vec!["MyButton".into()];
let tree = build_composition_tree_v2(&profiles, &family, None, None, &[ctx], None).unwrap();
// No edges projected (Button has no edges)
assert!(tree.edges.is_empty());
assert_eq!(tree.family_members.len(), 1);
}
/// Test Step 8.6: Secondary BEM block sub-root fallback.
///
/// Simulates the Modal family pattern where the root (Modal) uses BEM
/// block "backdrop" while sub-components (ModalBody, ModalFooter) use
/// BEM block "modalBox". An internal component (ModalBox) acts as the
/// sub-root for the "modalBox" block.
///
/// Step 8.6 should connect ModalBody and ModalFooter to ModalBox as
/// orphan BEM elements of the secondary block.
#[test]
fn test_secondary_block_subroot_fallback() {
let mut profiles = HashMap::new();
// Modal: root, uses "backdrop" block, renders ModalContent internally
let mut modal = make_profile("Modal");
modal.bem_block = Some("backdrop".into());
modal.rendered_components = vec!["ModalContent".into()];
profiles.insert("Modal".into(), modal);
// ModalContent: internal, renders ModalBox
let mut modal_content = make_profile("ModalContent");
modal_content.rendered_components = vec!["ModalBox".into()];
profiles.insert("ModalContent".into(), modal_content);
// ModalBox: internal, sub-root for "modalBox" block, has children
let mut modal_box = make_profile("ModalBox");
modal_box.bem_block = Some("modalBox".into());
modal_box.css_tokens_used = ["styles.modalBox".to_string()].into_iter().collect();
modal_box.has_children_prop = true;
profiles.insert("ModalBox".into(), modal_box);
// ModalBody: uses "modalBox" block, orphan (renders only HTML)
let mut modal_body = make_profile("ModalBody");
modal_body.bem_block = Some("modalBox".into());
modal_body.css_tokens_used = ["styles.modalBoxBody".to_string()].into_iter().collect();
profiles.insert("ModalBody".into(), modal_body);
// ModalFooter: uses "modalBox" block, orphan (renders only HTML)
let mut modal_footer = make_profile("ModalFooter");
modal_footer.bem_block = Some("modalBox".into());
modal_footer.css_tokens_used = ["styles.modalBoxFooter".to_string()].into_iter().collect();
profiles.insert("ModalFooter".into(), modal_footer);
let family = vec![
"Modal".into(),
"ModalContent".into(),
"ModalBox".into(),
"ModalBody".into(),
"ModalFooter".into(),
];
// CSS profiles: we need "modalBox" block to exist so Step 8.6 fires
let modal_box_css = CssBlockProfile {
block: "modalBox".into(),
..Default::default()
};
let css_map = HashMap::from([("modalBox".to_string(), modal_box_css)]);
// Primary block is "backdrop" (from root) but no CSS profile for it
let tree = build_composition_tree_v2(
&profiles,
&family,
Some(&css_map),
None, // no primary CSS profile for "backdrop"
&[],
None,
)
.unwrap();
// Step 1 should create: Modal → ModalContent, ModalContent → ModalBox
// Step 8.6 should create: ModalBox → ModalBody, ModalBox → ModalFooter
let box_to_body = tree
.edges
.iter()
.any(|e| e.parent == "ModalBox" && e.child == "ModalBody");
let box_to_footer = tree
.edges
.iter()
.any(|e| e.parent == "ModalBox" && e.child == "ModalFooter");
assert!(
box_to_body,
"Expected ModalBox → ModalBody edge from secondary block fallback. Edges: {:?}",
tree.edges
);
assert!(
box_to_footer,
"Expected ModalBox → ModalFooter edge from secondary block fallback. Edges: {:?}",
tree.edges
);
// All 5 members should be retained
assert_eq!(
tree.family_members.len(),
5,
"All members should be retained. Members: {:?}",
tree.family_members
);
}
/// When an intermediate parent has only an Allowed edge from the root
/// (meaning it is optional), the root→child edge should be preserved.
/// The child can bypass the optional intermediate and go directly into
/// the root.
///
/// Pattern: SimpleList→SimpleListGroup [Allowed], Group→Item [Required],
/// SimpleList→Item [Structural from context]. The Group is optional, so
/// the root→Item edge should survive suppress_root_edges_with_intermediate.
#[test]
fn test_suppress_preserves_root_edge_when_intermediate_is_optional() {
use crate::sd_types::{CompositionEdge, CompositionTree, EdgeStrength};
let mut tree = CompositionTree {
root: "SimpleList".into(),
family_members: vec![
"SimpleList".into(),
"SimpleListGroup".into(),
"SimpleListItem".into(),
],
edges: vec![
// Root → Group: Allowed (group is optional)
CompositionEdge {
parent: "SimpleList".into(),
child: "SimpleListGroup".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: None,
strength: EdgeStrength::Allowed,
prop_name: None,
},
// Group → Item: Required (DOM <ul>→<li>)
CompositionEdge {
parent: "SimpleListGroup".into(),
child: "SimpleListItem".into(),
relationship: ChildRelationship::DirectChild,
required: true,
bem_evidence: None,
strength: EdgeStrength::Required,
prop_name: None,
},
// Root → Item: Structural (from context dependency)
CompositionEdge {
parent: "SimpleList".into(),
child: "SimpleListItem".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: None,
strength: EdgeStrength::Structural,
prop_name: None,
},
],
};
suppress_root_edges_with_intermediate(&mut tree);
// Root → Item should be PRESERVED (Group is optional from root)
let root_to_item = tree
.edges
.iter()
.any(|e| e.parent == "SimpleList" && e.child == "SimpleListItem");
assert!(
root_to_item,
"Root→Item should be preserved when intermediate (Group) is optional. Edges: {:?}",
tree.edges
);
// Root → Group should still exist (not affected)
let root_to_group = tree
.edges
.iter()
.any(|e| e.parent == "SimpleList" && e.child == "SimpleListGroup");
assert!(
root_to_group,
"Root→Group should be preserved. Edges: {:?}",
tree.edges
);
}
/// When an intermediate parent has a CHP edge from the root (Structural
/// or Required), the root→child edge SHOULD be suppressed — the
/// intermediate is structurally required and the child must go through it.
///
/// Pattern: Menu→MenuList [Structural], MenuList→MenuItem [Required],
/// Menu→MenuItem [Structural from context]. MenuList has CHP from root,
/// so root→MenuItem should be suppressed.
#[test]
fn test_suppress_removes_root_edge_when_intermediate_is_required() {
use crate::sd_types::{CompositionEdge, CompositionTree, EdgeStrength};
let mut tree = CompositionTree {
root: "Menu".into(),
family_members: vec!["Menu".into(), "MenuList".into(), "MenuItem".into()],
edges: vec![
// Root → MenuList: Structural (CHP=YES, not optional)
CompositionEdge {
parent: "Menu".into(),
child: "MenuList".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: None,
strength: EdgeStrength::Structural,
prop_name: None,
},
// MenuList → MenuItem: Required (DOM <ul>→<li>)
CompositionEdge {
parent: "MenuList".into(),
child: "MenuItem".into(),
relationship: ChildRelationship::DirectChild,
required: true,
bem_evidence: None,
strength: EdgeStrength::Required,
prop_name: None,
},
// Root → MenuItem: Structural (from context)
CompositionEdge {
parent: "Menu".into(),
child: "MenuItem".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: None,
strength: EdgeStrength::Structural,
prop_name: None,
},
],
};
suppress_root_edges_with_intermediate(&mut tree);
// Root → MenuItem should be SUPPRESSED (MenuList is CHP from root)
let root_to_item = tree
.edges
.iter()
.any(|e| e.parent == "Menu" && e.child == "MenuItem");
assert!(
!root_to_item,
"Root→MenuItem should be suppressed when intermediate (MenuList) has CHP from root. Edges: {:?}",
tree.edges
);
// Root → MenuList should still exist
let root_to_list = tree
.edges
.iter()
.any(|e| e.parent == "Menu" && e.child == "MenuList");
assert!(
root_to_list,
"Root→MenuList should be preserved. Edges: {:?}",
tree.edges
);
}
/// DOM nesting edges from the root should be preserved when the
/// intermediate parent is NOT PMC=YES from the root (the intermediate
/// is an optional wrapper, not a mandatory one).
///
/// Pattern: FormSelect→FormSelectOptionGroup [Structural, CHP=YES but
/// PMC=NO], FormSelectOptionGroup→FormSelectOption [Required],
/// FormSelect→FormSelectOption [Structural, DOM nesting]. The optgroup
/// wrapper is optional — options can go directly in <select>.
#[test]
fn test_suppress_preserves_dom_nesting_edge_when_intermediate_not_pmc() {
use crate::sd_types::{CompositionEdge, CompositionTree, EdgeStrength};
let mut tree = CompositionTree {
root: "FormSelect".into(),
family_members: vec![
"FormSelect".into(),
"FormSelectOptionGroup".into(),
"FormSelectOption".into(),
],
edges: vec![
// Root → OptGroup: Structural (CHP=YES, PMC=NO — optional wrapper)
CompositionEdge {
parent: "FormSelect".into(),
child: "FormSelectOptionGroup".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: Some(
"DOM nesting: FormSelect wraps children in <select>, \
FormSelectOptionGroup renders <optgroup> as root"
.into(),
),
strength: EdgeStrength::Structural,
prop_name: None,
},
// OptGroup → Option: Required (PMC=YES — optgroup must contain options)
CompositionEdge {
parent: "FormSelectOptionGroup".into(),
child: "FormSelectOption".into(),
relationship: ChildRelationship::DirectChild,
required: true,
bem_evidence: Some(
"DOM nesting: FormSelectOptionGroup wraps children in <optgroup>, \
FormSelectOption renders <option> as root"
.into(),
),
strength: EdgeStrength::Required,
prop_name: None,
},
// Root → Option: Structural (DOM nesting — <select> directly accepts <option>)
CompositionEdge {
parent: "FormSelect".into(),
child: "FormSelectOption".into(),
relationship: ChildRelationship::DirectChild,
required: false,
bem_evidence: Some(
"DOM nesting: FormSelect wraps children in <select>, \
FormSelectOption renders <option> as root"
.into(),
),
strength: EdgeStrength::Structural,
prop_name: None,
},
],
};
suppress_root_edges_with_intermediate(&mut tree);
// Root → FormSelectOption should be PRESERVED (DOM nesting edge,
// intermediate FormSelectOptionGroup is NOT PMC from root)
let root_to_option = tree
.edges
.iter()
.any(|e| e.parent == "FormSelect" && e.child == "FormSelectOption");
assert!(
root_to_option,
"Root→FormSelectOption DOM nesting edge should be preserved when \
intermediate (FormSelectOptionGroup) is not PMC from root. Edges: {:?}",
tree.edges
);
// Root → FormSelectOptionGroup should still exist
let root_to_group = tree
.edges
.iter()
.any(|e| e.parent == "FormSelect" && e.child == "FormSelectOptionGroup");
assert!(
root_to_group,
"Root→FormSelectOptionGroup should be preserved. Edges: {:?}",
tree.edges
);
}
/// DOM nesting edges from the root SHOULD be suppressed when the
/// intermediate parent IS PMC=YES from the root (the intermediate is
/// mandatory — the child must go through it).
///
/// Pattern: Table→Tbody [Required, PMC=YES], Tbody→Tr [Required],
/// Table→Tr [Required, DOM nesting]. Tbody is always present, so
/// <tr> must go through <tbody>.
#[test]
fn test_suppress_removes_dom_nesting_edge_when_intermediate_is_pmc() {
use crate::sd_types::{CompositionEdge, CompositionTree, EdgeStrength};
let mut tree = CompositionTree {
root: "Table".into(),
family_members: vec!["Table".into(), "Tbody".into(), "Tr".into()],
edges: vec![
// Root → Tbody: Required (PMC=YES — table must contain tbody)
CompositionEdge {
parent: "Table".into(),
child: "Tbody".into(),
relationship: ChildRelationship::DirectChild,
required: true,
bem_evidence: Some(
"DOM nesting: Table wraps children in <table>, \
Tbody renders <tbody> as root"
.into(),
),
strength: EdgeStrength::Required,
prop_name: None,
},
// Tbody → Tr: Required
CompositionEdge {
parent: "Tbody".into(),
child: "Tr".into(),
relationship: ChildRelationship::DirectChild,
required: true,
bem_evidence: Some(
"DOM nesting: Tbody wraps children in <tbody>, \
Tr renders <tr> as root"
.into(),
),
strength: EdgeStrength::Required,
prop_name: None,
},
// Root → Tr: Required (DOM nesting — <table> accepts <tr>)
CompositionEdge {
parent: "Table".into(),
child: "Tr".into(),
relationship: ChildRelationship::DirectChild,
required: true,
bem_evidence: Some(
"DOM nesting: Table wraps children in <table>, \
Tr renders <tr> as root"
.into(),
),
strength: EdgeStrength::Required,
prop_name: None,
},
],
};
suppress_root_edges_with_intermediate(&mut tree);
// Root → Tr should be SUPPRESSED (Tbody is PMC from root)
let root_to_tr = tree
.edges
.iter()
.any(|e| e.parent == "Table" && e.child == "Tr");
assert!(
!root_to_tr,
"Root→Tr should be suppressed when intermediate (Tbody) is PMC from root. Edges: {:?}",
tree.edges
);
// Root → Tbody should still exist
let root_to_tbody = tree
.edges
.iter()
.any(|e| e.parent == "Table" && e.child == "Tbody");
assert!(
root_to_tbody,
"Root→Tbody should be preserved. Edges: {:?}",
tree.edges
);
}
}