rumdl 0.1.88

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

use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
use pulldown_cmark::LinkType;
use std::collections::HashMap;

mod label;
mod md054_config;
mod transform;

use md054_config::{MD054Config, PreferredStyles};

/// Rule MD054: Link and image style should be consistent
///
/// This rule is triggered when different link or image styles are used in the same document.
/// Markdown supports various styles for links and images, and this rule enforces consistency.
///
/// ## Supported Link Styles
///
/// - **Autolink**: `<https://example.com>`
/// - **Inline**: `[link text](https://example.com)`
/// - **URL Inline**: Special case of inline links where the URL itself is also the link text: `[https://example.com](https://example.com)`
/// - **Shortcut**: `[link text]` (requires a reference definition elsewhere in the document)
/// - **Collapsed**: `[link text][]` (requires a reference definition with the same name)
/// - **Full**: `[link text][reference]` (requires a reference definition for the reference)
///
/// ## Configuration Options
///
/// You can configure which link styles are allowed. By default, all styles are allowed:
///
/// ```yaml
/// MD054:
///   autolink: true    # Allow autolink style
///   inline: true      # Allow inline style
///   url_inline: true  # Allow URL inline style
///   shortcut: true    # Allow shortcut style
///   collapsed: true   # Allow collapsed style
///   full: true        # Allow full style
/// ```
///
/// To enforce a specific style, set only that style to `true` and all others to `false`.
///
/// ## Unicode Support
///
/// This rule fully supports Unicode characters in link text and URLs, including:
/// - Combining characters (e.g., cafe)
/// - Zero-width joiners (e.g., family emojis)
/// - Right-to-left text (e.g., Arabic, Hebrew)
/// - Emojis and other special characters
///
/// ## Rationale
///
/// Consistent link styles improve document readability and maintainability. Different link
/// styles have different advantages (e.g., inline links are self-contained, reference links
/// keep the content cleaner), but mixing styles can create confusion.
///
#[derive(Debug, Default, Clone)]
pub struct MD054LinkImageStyle {
    config: MD054Config,
}

impl MD054LinkImageStyle {
    pub fn new(autolink: bool, collapsed: bool, full: bool, inline: bool, shortcut: bool, url_inline: bool) -> Self {
        Self {
            config: MD054Config {
                autolink,
                collapsed,
                full,
                inline,
                shortcut,
                url_inline,
                preferred_style: PreferredStyles::default(),
            },
        }
    }

    pub fn from_config_struct(config: MD054Config) -> Self {
        Self { config }
    }

    /// Convert a byte offset to a 1-indexed character column within its line.
    /// Only called for disallowed links (cold path), so O(line_length) is fine.
    fn byte_to_char_col(content: &str, byte_offset: usize) -> usize {
        let before = &content[..byte_offset];
        let last_newline = before.rfind('\n').map_or(0, |i| i + 1);
        before[last_newline..].chars().count() + 1
    }

    /// Check if a style is allowed based on configuration
    fn is_style_allowed(&self, style: &str) -> bool {
        match style {
            "autolink" => self.config.autolink,
            "collapsed" => self.config.collapsed,
            "full" => self.config.full,
            "inline" => self.config.inline,
            "shortcut" => self.config.shortcut,
            "url-inline" => self.config.url_inline,
            _ => false,
        }
    }
}

impl Rule for MD054LinkImageStyle {
    fn name(&self) -> &'static str {
        "MD054"
    }

    fn description(&self) -> &'static str {
        "Link and image style should be consistent"
    }

    fn category(&self) -> RuleCategory {
        RuleCategory::Link
    }

    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
        let content = ctx.content;
        let mut warnings = Vec::new();

        // Compute the fix plan once and index its planned entries by source
        // byte offset. Each warning whose link/image has an entry gets a Fix
        // attached so the fix coordinator (which gates on `fix.is_some()`) and
        // LSP code actions both see this rule as fixable.
        //
        // Conversions that require a new reference definition (inline → full,
        // etc.) carry the EOF-appended ref-def as an `additional_edit` on the
        // per-warning Fix, so quick-fix paths that apply a single warning
        // produce a complete result (link rewrite + ref def) atomically.
        let plan = if self.should_skip(ctx) {
            transform::FixPlan::default()
        } else {
            transform::plan(ctx, &self.config)
        };
        let entries_by_offset: HashMap<usize, &transform::PlannedEdit> =
            plan.entries.iter().map(|e| (e.edit.range.start, e)).collect();
        let build_fix = |offset: usize| -> Option<Fix> {
            let entry = entries_by_offset.get(&offset)?;
            let primary_range = entry.edit.range.clone();
            let primary_replacement = entry.edit.replacement.clone();
            match &entry.new_ref {
                None => Some(Fix::new(primary_range, primary_replacement)),
                Some(def) => {
                    let appended = transform::render_ref_def_append(content, def)?;
                    let eof_range = content.len()..content.len();
                    Some(Fix::with_additional_edits(
                        primary_range,
                        primary_replacement,
                        vec![Fix::new(eof_range, appended)],
                    ))
                }
            }
        };

        // Process links from pre-parsed data
        for link in &ctx.links {
            // Skip broken references (empty URL means unresolved reference)
            if matches!(
                link.link_type,
                LinkType::Reference | LinkType::Collapsed | LinkType::Shortcut
            ) && link.url.is_empty()
            {
                continue;
            }

            let style = match link.link_type {
                LinkType::Autolink | LinkType::Email => "autolink",
                LinkType::Inline => {
                    if link.text == link.url {
                        "url-inline"
                    } else {
                        "inline"
                    }
                }
                LinkType::Reference => "full",
                LinkType::Collapsed => "collapsed",
                LinkType::Shortcut => "shortcut",
                _ => continue,
            };

            // Filter out links in frontmatter or code blocks
            if ctx
                .line_info(link.line)
                .is_some_and(|info| info.in_front_matter || info.in_code_block)
            {
                continue;
            }

            if !self.is_style_allowed(style) {
                let start_col = Self::byte_to_char_col(content, link.byte_offset);
                let (end_line, _) = ctx.offset_to_line_col(link.byte_end);
                let end_col = Self::byte_to_char_col(content, link.byte_end);

                warnings.push(LintWarning {
                    rule_name: Some(self.name().to_string()),
                    line: link.line,
                    column: start_col,
                    end_line,
                    end_column: end_col,
                    message: format!("Link/image style '{style}' is not allowed"),
                    severity: Severity::Warning,
                    fix: build_fix(link.byte_offset),
                });
            }
        }

        // Process images from pre-parsed data
        for image in &ctx.images {
            // Skip broken references (empty URL means unresolved reference)
            if matches!(
                image.link_type,
                LinkType::Reference | LinkType::Collapsed | LinkType::Shortcut
            ) && image.url.is_empty()
            {
                continue;
            }

            let style = match image.link_type {
                LinkType::Autolink | LinkType::Email => "autolink",
                LinkType::Inline => {
                    if image.alt_text == image.url {
                        "url-inline"
                    } else {
                        "inline"
                    }
                }
                LinkType::Reference => "full",
                LinkType::Collapsed => "collapsed",
                LinkType::Shortcut => "shortcut",
                _ => continue,
            };

            // Filter out images in frontmatter or code blocks
            if ctx
                .line_info(image.line)
                .is_some_and(|info| info.in_front_matter || info.in_code_block)
            {
                continue;
            }

            if !self.is_style_allowed(style) {
                let start_col = Self::byte_to_char_col(content, image.byte_offset);
                let (end_line, _) = ctx.offset_to_line_col(image.byte_end);
                let end_col = Self::byte_to_char_col(content, image.byte_end);

                warnings.push(LintWarning {
                    rule_name: Some(self.name().to_string()),
                    line: image.line,
                    column: start_col,
                    end_line,
                    end_column: end_col,
                    message: format!("Link/image style '{style}' is not allowed"),
                    severity: Severity::Warning,
                    fix: build_fix(image.byte_offset),
                });
            }
        }

        Ok(warnings)
    }

    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
        if self.should_skip(ctx) {
            return Ok(ctx.content.to_string());
        }
        let plan = transform::plan(ctx, &self.config);
        Ok(transform::apply(ctx.content, plan))
    }

    fn fix_capability(&self) -> crate::rule::FixCapability {
        // Some (source, target) pairs are intentionally not auto-fixed —
        // see `transform::reachable`.
        crate::rule::FixCapability::ConditionallyFixable
    }

    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
        ctx.content.is_empty() || (!ctx.likely_has_links_or_images() && !ctx.likely_has_html())
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn default_config_section(&self) -> Option<(String, toml::Value)> {
        let json_value = serde_json::to_value(&self.config).ok()?;
        let toml_value = crate::rule_config_serde::json_to_toml_value(&json_value)?;
        Some((self.name().to_string(), toml_value))
    }

    fn polymorphic_config_keys(&self) -> &'static [&'static str] {
        // `preferred-style` accepts either a scalar string or a list of strings.
        // The serialized default can only encode one variant, so the registry
        // replaces this entry with a polymorphic sentinel for validation while
        // the user-facing default config (`rumdl config --defaults`) keeps the
        // serialized scalar form.
        &["preferred-style"]
    }

    fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
    where
        Self: Sized,
    {
        let rule_config = crate::rule_config_serde::load_rule_config::<MD054Config>(config);
        Box::new(Self::from_config_struct(rule_config))
    }
}

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

    #[test]
    fn test_all_styles_allowed_by_default() {
        let rule = MD054LinkImageStyle::new(true, true, true, true, true, true);
        let content = "[inline](url) [ref][] [ref] <https://autolink.com> [full][ref] [url](url)\n\n[ref]: url";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_only_inline_allowed() {
        let rule = MD054LinkImageStyle::new(false, false, false, true, false, false);
        // [bad][] has no definition for "bad", so pulldown-cmark doesn't emit it as a link
        let content = "[allowed](url) [not][ref] <https://bad.com> [collapsed][] [shortcut]\n\n[ref]: url\n[shortcut]: url\n[collapsed]: url";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 4, "Expected 4 warnings, got: {result:?}");
        assert!(result[0].message.contains("'full'"));
        assert!(result[1].message.contains("'autolink'"));
        assert!(result[2].message.contains("'collapsed'"));
        assert!(result[3].message.contains("'shortcut'"));
    }

    #[test]
    fn test_only_autolink_allowed() {
        let rule = MD054LinkImageStyle::new(true, false, false, false, false, false);
        let content = "<https://good.com> [bad](url) [bad][ref]\n\n[ref]: url";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 2, "Expected 2 warnings, got: {result:?}");
        assert!(result[0].message.contains("'inline'"));
        assert!(result[1].message.contains("'full'"));
    }

    #[test]
    fn test_url_inline_detection() {
        let rule = MD054LinkImageStyle::new(false, false, false, true, false, true);
        let content = "[https://example.com](https://example.com) [text](https://example.com)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        // First is url_inline (allowed), second is inline (allowed)
        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_url_inline_not_allowed() {
        let rule = MD054LinkImageStyle::new(false, false, false, true, false, false);
        let content = "[https://example.com](https://example.com)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 1);
        assert!(result[0].message.contains("'url-inline'"));
    }

    #[test]
    fn test_shortcut_vs_full_detection() {
        let rule = MD054LinkImageStyle::new(false, false, true, false, false, false);
        let content = "[shortcut] [full][ref]\n\n[shortcut]: url\n[ref]: url2";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        // Only shortcut should be flagged
        assert_eq!(result.len(), 1, "Expected 1 warning, got: {result:?}");
        assert!(result[0].message.contains("'shortcut'"));
    }

    #[test]
    fn test_collapsed_reference() {
        let rule = MD054LinkImageStyle::new(false, true, false, false, false, false);
        let content = "[collapsed][] [bad][ref]\n\n[collapsed]: url\n[ref]: url2";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 1, "Expected 1 warning, got: {result:?}");
        assert!(result[0].message.contains("'full'"));
    }

    #[test]
    fn test_code_blocks_ignored() {
        let rule = MD054LinkImageStyle::new(false, false, false, true, false, false);
        let content = "```\n[ignored](url) <https://ignored.com>\n```\n\n[checked](url)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        // Only the link outside code block should be checked
        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_code_spans_ignored() {
        let rule = MD054LinkImageStyle::new(false, false, false, true, false, false);
        let content = "`[ignored](url)` and `<https://ignored.com>` but [checked](url)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        // Only the link outside code spans should be checked
        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_reference_definitions_ignored() {
        let rule = MD054LinkImageStyle::new(false, false, false, true, false, false);
        let content = "[ref]: https://example.com\n[ref2]: <https://example2.com>";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        // Reference definitions should be ignored
        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_html_comments_ignored() {
        let rule = MD054LinkImageStyle::new(false, false, false, true, false, false);
        let content = "<!-- [ignored](url) -->\n  <!-- <https://ignored.com> -->";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_unicode_support() {
        let rule = MD054LinkImageStyle::new(false, false, false, true, false, false);
        let content = "[cafe](https://cafe.com) [emoji](url) [korean](url) [hebrew](url)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        // All should be detected as inline (allowed)
        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_line_positions() {
        let rule = MD054LinkImageStyle::new(false, false, false, true, false, false);
        let content = "Line 1\n\nLine 3 with <https://bad.com> here";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].line, 3);
        assert_eq!(result[0].column, 13); // Position of '<'
    }

    #[test]
    fn test_multiple_links_same_line() {
        let rule = MD054LinkImageStyle::new(false, false, false, true, false, false);
        let content = "[ok](url) but <https://good.com> and [also][bad]\n\n[bad]: url";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 2, "Expected 2 warnings, got: {result:?}");
        assert!(result[0].message.contains("'autolink'"));
        assert!(result[1].message.contains("'full'"));
    }

    #[test]
    fn test_empty_content() {
        let rule = MD054LinkImageStyle::new(false, false, false, true, false, false);
        let content = "";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_no_links() {
        let rule = MD054LinkImageStyle::new(false, false, false, true, false, false);
        let content = "Just plain text without any links";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_fix_unreachable_target_is_noop() {
        // inline disallowed but no reachable reference style allowed (only autolink),
        // and the link's text doesn't match its url so autolink is unreachable too.
        // The fix should leave the content unchanged rather than error.
        let rule = MD054LinkImageStyle::new(true, false, false, false, false, false);
        let content = "[link](url)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, content);
    }

    #[test]
    fn test_priority_order() {
        let rule = MD054LinkImageStyle::new(false, false, false, true, false, false);
        // Test that [text][ref] is detected as full, not shortcut
        let content = "[text][ref] not detected as [shortcut]\n\n[ref]: url\n[shortcut]: url2";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 2, "Expected 2 warnings, got: {result:?}");
        assert!(result[0].message.contains("'full'"));
        assert!(result[1].message.contains("'shortcut'"));
    }

    #[test]
    fn test_not_shortcut_when_followed_by_bracket() {
        let rule = MD054LinkImageStyle::new(false, false, false, true, true, false);
        // [text][ should not be detected as shortcut
        let content = "[text][ more text\n[text](url) is inline";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        // Only second line should have inline link
        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_cjk_correct_column_positions() {
        let rule = MD054LinkImageStyle::new(false, false, false, true, false, false);
        let content = "日本語テスト <https://example.com>";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 1);
        assert!(result[0].message.contains("'autolink'"));
        // The '<' starts at byte position 19 (after 6 CJK chars * 3 bytes + 1 space)
        // which is character position 8 (1-indexed)
        assert_eq!(
            result[0].column, 8,
            "Column should be 1-indexed character position of '<'"
        );
    }

    #[test]
    fn test_code_span_detection_with_cjk_prefix() {
        let rule = MD054LinkImageStyle::new(false, false, false, true, false, false);
        // Link inside code span after CJK characters
        let content = "日本語 `[link](url)` text";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        // The link is inside a code span, so it should not be flagged
        assert_eq!(result.len(), 0, "Link inside code span should not be flagged");
    }

    #[test]
    fn test_complex_unicode_with_zwj() {
        let rule = MD054LinkImageStyle::new(false, false, false, true, false, false);
        let content = "[family](url) [cafe](https://cafe.com)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        // Both should be detected as inline (allowed)
        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_gfm_alert_not_flagged_as_shortcut() {
        let rule = MD054LinkImageStyle::new(true, true, true, true, false, true);
        let content = "> [!NOTE]\n> This is a note.\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "GFM alert should not be flagged as shortcut link, got: {result:?}"
        );
    }

    #[test]
    fn test_various_alert_types_not_flagged() {
        let rule = MD054LinkImageStyle::new(true, true, true, true, false, true);
        for alert_type in ["NOTE", "TIP", "IMPORTANT", "WARNING", "CAUTION", "note", "info"] {
            let content = format!("> [!{alert_type}]\n> Content.\n");
            let ctx = LintContext::new(&content, crate::config::MarkdownFlavor::Standard, None);
            let result = rule.check(&ctx).unwrap();
            assert!(
                result.is_empty(),
                "Alert type {alert_type} should not be flagged, got: {result:?}"
            );
        }
    }

    #[test]
    fn test_shortcut_link_still_flagged_when_disallowed() {
        let rule = MD054LinkImageStyle::new(true, true, true, true, false, true);
        let content = "See [reference] for details.\n\n[reference]: https://example.com\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(!result.is_empty(), "Regular shortcut links should still be flagged");
    }

    #[test]
    fn test_alert_with_frontmatter_not_flagged() {
        let rule = MD054LinkImageStyle::new(true, true, true, true, false, true);
        let content = "---\ntitle: heading\n---\n\n> [!note]\n> Content for the note.\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Alert in blockquote with frontmatter should not be flagged, got: {result:?}"
        );
    }

    #[test]
    fn test_alert_without_blockquote_prefix_not_flagged() {
        // Even without the `> ` prefix, [!TYPE] is alert syntax and should not be
        // treated as a shortcut reference
        let rule = MD054LinkImageStyle::new(true, true, true, true, false, true);
        let content = "[!NOTE]\nSome content\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "[!NOTE] without blockquote prefix should not be flagged, got: {result:?}"
        );
    }

    #[test]
    fn test_alert_custom_types_not_flagged() {
        // Obsidian and other flavors support custom callout types
        let rule = MD054LinkImageStyle::new(true, true, true, true, false, true);
        for alert_type in ["bug", "example", "quote", "abstract", "todo", "faq"] {
            let content = format!("> [!{alert_type}]\n> Content.\n");
            let ctx = LintContext::new(&content, crate::config::MarkdownFlavor::Standard, None);
            let result = rule.check(&ctx).unwrap();
            assert!(
                result.is_empty(),
                "Custom alert type {alert_type} should not be flagged, got: {result:?}"
            );
        }
    }

    // Tests for issue #488: code spans with brackets in inline link text

    #[test]
    fn test_code_span_with_brackets_in_inline_link() {
        let rule = MD054LinkImageStyle::new(true, true, true, true, false, true);
        let content = "Link to [`[myArray]`](#info).";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // The inline link should be detected correctly, [myArray] should NOT be flagged as shortcut
        assert!(
            result.is_empty(),
            "Code span with brackets in inline link should not be flagged, got: {result:?}"
        );
    }

    #[test]
    fn test_code_span_with_array_index_in_inline_link() {
        let rule = MD054LinkImageStyle::new(true, true, true, true, false, true);
        let content = "See [`item[0]`](#info) for details.";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Array index in code span should not be flagged, got: {result:?}"
        );
    }

    #[test]
    fn test_code_span_with_hash_brackets_in_inline_link() {
        let rule = MD054LinkImageStyle::new(true, true, true, true, false, true);
        let content = r#"See [`hash["key"]`](#info) for details."#;
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Hash access in code span should not be flagged, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_488_full_reproduction() {
        // Exact reproduction case from issue #488
        let rule = MD054LinkImageStyle::new(true, true, true, true, false, true);
        let content = "---\ntitle: heading\n---\n\nLink to information about [`[myArray]`](#information-on-myarray).\n\n## Information on `[myArray]`\n\nSome section content.\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Issue #488 reproduction case should produce no warnings, got: {result:?}"
        );
    }

    #[test]
    fn test_bracket_text_without_definition_not_flagged() {
        // [text] without a matching [text]: url definition is NOT a link.
        // It should never be flagged regardless of config.
        let rule = MD054LinkImageStyle::new(true, true, true, true, false, true);
        let content = "Some [noref] text without a definition.";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Bracket text without definition should not be flagged as a link, got: {result:?}"
        );
    }

    #[test]
    fn test_array_index_notation_not_flagged() {
        // Common bracket patterns that are not links should never be flagged
        let rule = MD054LinkImageStyle::new(true, true, true, true, false, true);
        let content = "Access `arr[0]` and use [1] or [optional] in your code.";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Array indices and bracket text should not be flagged, got: {result:?}"
        );
    }

    #[test]
    fn test_real_shortcut_reference_still_flagged() {
        // [text] WITH a matching definition IS a shortcut link and should be flagged
        let rule = MD054LinkImageStyle::new(true, true, true, true, false, true);
        let content = "See [example] for details.\n\n[example]: https://example.com\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(
            result.len(),
            1,
            "Real shortcut reference with definition should be flagged, got: {result:?}"
        );
        assert!(result[0].message.contains("'shortcut'"));
    }

    #[test]
    fn test_footnote_syntax_not_flagged_as_shortcut() {
        // [^ref] should not be flagged as a shortcut reference
        let rule = MD054LinkImageStyle::new(true, true, true, true, false, true);
        let content = "See [^1] for details.";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Footnote syntax should not be flagged as shortcut, got: {result:?}"
        );
    }

    #[test]
    fn test_inline_link_with_code_span_detected_as_inline() {
        // When inline is disallowed, code-span-with-brackets inline link should be flagged as inline
        let rule = MD054LinkImageStyle::new(true, true, true, false, true, true);
        let content = "See [`[myArray]`](#info) for details.";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(
            result.len(),
            1,
            "Inline link with code span should be flagged when inline is disallowed"
        );
        assert!(
            result[0].message.contains("'inline'"),
            "Should be flagged as 'inline' style, got: {}",
            result[0].message
        );
    }

    #[test]
    fn test_autolink_only_document_not_skipped() {
        // Document with only autolinks (no brackets) must still be checked
        let rule = MD054LinkImageStyle::new(false, false, false, true, false, false);
        let content = "Visit <https://example.com> for more info.";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            !rule.should_skip(&ctx),
            "should_skip must return false for autolink-only documents"
        );
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 1, "Autolink should be flagged when disallowed");
        assert!(result[0].message.contains("'autolink'"));
    }

    #[test]
    fn test_nested_image_in_link() {
        // [![alt](img.png)](https://example.com) — image nested inside a link
        let rule = MD054LinkImageStyle::new(false, false, false, false, false, false);
        let content = "[![alt text](img.png)](https://example.com)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // Both the inner image (inline) and outer link (inline) should be detected
        assert!(
            result.len() >= 2,
            "Nested image-in-link should detect both elements, got: {result:?}"
        );
    }

    #[test]
    fn test_multi_line_link() {
        let rule = MD054LinkImageStyle::new(false, false, false, false, false, false);
        let content = "[long link\ntext](url)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 1, "Multi-line inline link should be detected");
        assert!(result[0].message.contains("'inline'"));
    }

    #[test]
    fn test_link_with_title() {
        let rule = MD054LinkImageStyle::new(false, false, false, false, false, false);
        let content = r#"[text](url "title")"#;
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 1, "Link with title should be detected as inline");
        assert!(result[0].message.contains("'inline'"));
    }

    #[test]
    fn test_empty_link_text() {
        let rule = MD054LinkImageStyle::new(false, false, false, false, false, false);
        let content = "[](url)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 1, "Empty link text should be detected");
        assert!(result[0].message.contains("'inline'"));
    }

    #[test]
    fn test_escaped_brackets_not_detected() {
        let rule = MD054LinkImageStyle::new(true, true, true, true, false, true);
        let content = r"\[not a link\] and also \[not this either\]";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Escaped brackets should not be flagged, got: {result:?}"
        );
    }

    #[test]
    fn test_links_in_blockquotes() {
        let rule = MD054LinkImageStyle::new(false, false, false, false, false, false);
        let content = "> [link](url) in a blockquote";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 1, "Links in blockquotes should be detected");
        assert!(result[0].message.contains("'inline'"));
    }

    #[test]
    fn test_image_detection() {
        let rule = MD054LinkImageStyle::new(false, false, false, false, false, false);
        let content = "![alt](img.png)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 1, "Inline image should be detected");
        assert!(result[0].message.contains("'inline'"));
    }
}

#[cfg(test)]
mod fix_tests {
    use super::*;
    use crate::config::MarkdownFlavor;
    use crate::lint_context::LintContext;
    use md054_config::PreferredStyle;
    use pulldown_cmark::LinkType;

    /// Resolve a link's destination to its canonical form. Pulldown-cmark stores
    /// email autolinks (`<me@x>`) with the bare email in `url`, but per
    /// CommonMark §6.5 the resolved destination is `mailto:<email>`. Round-trip
    /// equivalence checks compare *destinations*, not raw `url` strings — so we
    /// canonicalize before comparing.
    fn canonical_link_url(link_type: LinkType, url: &str) -> String {
        match link_type {
            LinkType::Email => format!("mailto:{url}"),
            _ => url.to_string(),
        }
    }

    /// Helper: build a rule that disallows `inline` and leaves the remaining
    /// reference styles allowed (mirrors the reporter's MD054 config in #587).
    fn rule_inline_disallowed() -> MD054LinkImageStyle {
        // autolink, collapsed, full, inline, shortcut, url_inline
        MD054LinkImageStyle::new(true, true, true, false, true, true)
    }

    /// Helper: build a rule that disallows reference styles, leaving inline allowed.
    fn rule_only_inline() -> MD054LinkImageStyle {
        MD054LinkImageStyle::new(false, false, false, true, false, false)
    }

    /// Helper: assert four invariants together —
    ///
    /// 1. After `fix()`, MD054 emits zero warnings on the result (round-trip clean).
    /// 2. `fix()` is idempotent (running it twice yields the same content).
    /// 3. The multiset of resolved link URLs is preserved (no silent retargeting).
    /// 4. The multiset of resolved image URLs is preserved.
    fn assert_round_trip_clean(rule: &MD054LinkImageStyle, content: &str) -> String {
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let before_link_urls: Vec<String> = ctx
            .links
            .iter()
            .map(|l| canonical_link_url(l.link_type, &l.url))
            .filter(|u| !u.is_empty())
            .collect();
        let before_image_urls: Vec<String> = ctx
            .images
            .iter()
            .map(|i| canonical_link_url(i.link_type, &i.url))
            .filter(|u| !u.is_empty())
            .collect();

        let fixed = rule.fix(&ctx).unwrap();

        let ctx2 = LintContext::new(&fixed, MarkdownFlavor::Standard, None);
        let warnings = rule.check(&ctx2).unwrap();
        assert!(
            warnings.is_empty(),
            "fix() left disallowed-style warnings: {warnings:?} in:\n{fixed}"
        );

        let mut after_link_urls: Vec<String> = ctx2
            .links
            .iter()
            .map(|l| canonical_link_url(l.link_type, &l.url))
            .filter(|u| !u.is_empty())
            .collect();
        let mut after_image_urls: Vec<String> = ctx2
            .images
            .iter()
            .map(|i| canonical_link_url(i.link_type, &i.url))
            .filter(|u| !u.is_empty())
            .collect();
        let mut before_link_urls_sorted = before_link_urls;
        let mut before_image_urls_sorted = before_image_urls;
        before_link_urls_sorted.sort();
        before_image_urls_sorted.sort();
        after_link_urls.sort();
        after_image_urls.sort();
        assert_eq!(
            before_link_urls_sorted, after_link_urls,
            "fix() changed the set of link URLs.\nbefore: {before_link_urls_sorted:?}\nafter: {after_link_urls:?}\nfixed:\n{fixed}"
        );
        assert_eq!(
            before_image_urls_sorted, after_image_urls,
            "fix() changed the set of image URLs.\nbefore: {before_image_urls_sorted:?}\nafter: {after_image_urls:?}\nfixed:\n{fixed}"
        );

        let fixed2 = rule.fix(&ctx2).unwrap();
        assert_eq!(fixed, fixed2, "fix() is not idempotent");
        fixed
    }

    // -------------------------------------------------------------------
    // inline → full
    // -------------------------------------------------------------------

    #[test]
    fn fix_inline_to_full_single_link() {
        let rule = rule_inline_disallowed();
        let content = "See the [documentation](https://example.com/docs) for details.\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert_eq!(
            fixed,
            "See the [documentation][documentation] for details.\n\n\
             [documentation]: https://example.com/docs\n"
        );
    }

    #[test]
    fn fix_inline_to_full_multiple_links_dedup_by_url() {
        let rule = rule_inline_disallowed();
        let content = "First [docs](https://example.com/x).\nAgain [docs](https://example.com/x).\n";
        let fixed = assert_round_trip_clean(&rule, content);
        // Both inline links collapse onto the same generated label.
        assert_eq!(
            fixed,
            "First [docs][docs].\nAgain [docs][docs].\n\n\
             [docs]: https://example.com/x\n"
        );
    }

    #[test]
    fn fix_inline_to_full_same_url_different_titles_keeps_both_titles() {
        // Two inline links share a URL but carry distinct titles. A single
        // shared reference definition could only encode one title — silently
        // dropping the other. The fix must produce two distinct ref defs.
        let rule = rule_inline_disallowed();
        let content = "First [a](https://example.com \"Title A\").\nLater [b](https://example.com \"Title B\").\n";
        let fixed = assert_round_trip_clean(&rule, content);
        // Both titles must survive the round-trip.
        assert!(fixed.contains(r#""Title A""#), "Title A lost in conversion: {fixed}");
        assert!(fixed.contains(r#""Title B""#), "Title B lost in conversion: {fixed}");
        // Two distinct definitions, not one.
        let def_count = fixed.matches("]: https://example.com").count();
        assert_eq!(def_count, 2, "expected two ref defs (one per title), got:\n{fixed}");
    }

    #[test]
    fn fix_inline_to_full_collision_disambiguates_with_suffix() {
        let rule = rule_inline_disallowed();
        let content = "[docs](https://a.com) and [docs](https://b.com).\n";
        let fixed = assert_round_trip_clean(&rule, content);
        // Same slug, different URL → second link gets `-2` suffix.
        assert!(fixed.contains("[docs][docs]"));
        assert!(fixed.contains("[docs][docs-2]"));
        assert!(fixed.contains("[docs]: https://a.com"));
        assert!(fixed.contains("[docs-2]: https://b.com"));
    }

    #[test]
    fn fix_inline_to_full_preserves_title() {
        let rule = rule_inline_disallowed();
        let content = "See [link](https://example.com \"My Title\").\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(fixed.contains("[link][link]"));
        assert!(fixed.contains(r#"[link]: https://example.com "My Title""#));
    }

    #[test]
    fn fix_inline_to_full_title_with_double_quotes_uses_single_quotes() {
        let rule = rule_inline_disallowed();
        let content = "See [link](https://example.com 'has \"double\" quotes').\n";
        let fixed = assert_round_trip_clean(&rule, content);
        // Output must use a delimiter that doesn't conflict; single quotes here.
        assert!(
            fixed.contains(r#"[link]: https://example.com 'has "double" quotes'"#),
            "got:\n{fixed}"
        );
    }

    #[test]
    fn fix_inline_to_full_title_with_escaped_quote_unescapes_through_parser() {
        // CommonMark allows backslash-escaping the title delimiter so the same
        // delimiter can appear inside the title: `"has \"escaped\" quotes"`.
        // pulldown-cmark unescapes those characters before handing us the title;
        // when MD054 emits the new ref def, it must pick a delimiter that doesn't
        // conflict — here, single quotes — and not blindly reuse the literal
        // backslashes from the source span.
        let rule = rule_inline_disallowed();
        let content = "See [link](https://example.com \"has \\\"escaped\\\" quotes\").\n";
        let fixed = assert_round_trip_clean(&rule, content);
        // The unescaped title contains real " characters, so output must wrap in 'single' quotes.
        assert!(
            fixed.contains(r#"[link]: https://example.com 'has "escaped" quotes'"#),
            "expected unescaped title with single-quote delimiter, got:\n{fixed}"
        );
        // And no stray backslash-quote in the emitted definition.
        assert!(
            !fixed.contains(r#"\""#),
            "title should be unescaped, not pass through literal `\\\"`:\n{fixed}"
        );
    }

    #[test]
    fn fix_inline_to_full_image() {
        let rule = rule_inline_disallowed();
        let content = "Logo: ![Company logo](https://example.com/logo.png).\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(fixed.contains("![Company logo][company-logo]"));
        assert!(fixed.contains("[company-logo]: https://example.com/logo.png"));
    }

    #[test]
    fn fix_inline_to_full_unicode_text() {
        let rule = rule_inline_disallowed();
        let content = "Voir [café résumé](https://cafe.example.com).\n";
        let fixed = assert_round_trip_clean(&rule, content);
        // Slug preserves the Unicode letters (they're alphanumeric) and lowercases them.
        assert!(fixed.contains("[café résumé][café-résumé]"));
        assert!(fixed.contains("[café-résumé]: https://cafe.example.com"));
    }

    #[test]
    fn fix_inline_to_full_reuses_existing_ref_def_for_same_url() {
        let rule = rule_inline_disallowed();
        let content = "Old: [other][site]\n\
                       New: [docs](https://example.com)\n\
                       \n\
                       [site]: https://example.com\n";
        let fixed = assert_round_trip_clean(&rule, content);
        // The new conversion should reuse the existing `site` label.
        assert!(
            fixed.contains("[docs][site]"),
            "expected reuse of existing label, got:\n{fixed}"
        );
        // No duplicate definition added.
        assert_eq!(fixed.matches("https://example.com").count(), 1);
    }

    #[test]
    fn fix_inline_to_full_avoids_existing_label_collision() {
        let rule = rule_inline_disallowed();
        let content = "Old: [a][docs]\n\
                       New: [docs](https://other.com)\n\
                       \n\
                       [docs]: https://existing.com\n";
        let fixed = assert_round_trip_clean(&rule, content);
        // New link must NOT reuse [docs] (different URL); should suffix.
        assert!(fixed.contains("[docs][docs-2]"));
        assert!(fixed.contains("[docs-2]: https://other.com"));
        // Original definition unchanged.
        assert!(fixed.contains("[docs]: https://existing.com"));
    }

    #[test]
    fn fix_inline_to_full_no_trailing_newline() {
        let rule = rule_inline_disallowed();
        let content = "[docs](https://example.com)";
        let fixed = assert_round_trip_clean(&rule, content);
        assert_eq!(fixed, "[docs][docs]\n\n[docs]: https://example.com\n");
    }

    #[test]
    fn fix_inline_to_full_skips_code_blocks() {
        let rule = rule_inline_disallowed();
        let content = "Outside [a](https://x.com).\n\n```\n[fenced](https://y.com)\n```\n";
        let fixed = assert_round_trip_clean(&rule, content);
        // The fenced block content stays untouched.
        assert!(fixed.contains("[fenced](https://y.com)"));
        // Outside link converted.
        assert!(fixed.contains("[a][a]"));
    }

    #[test]
    fn fix_inline_to_full_skips_frontmatter() {
        let rule = rule_inline_disallowed();
        let content = "---\nlink: [foo](https://x.com)\n---\n\n[doc](https://y.com)\n";
        let fixed = assert_round_trip_clean(&rule, content);
        // Frontmatter content untouched.
        assert!(fixed.contains("link: [foo](https://x.com)"));
        // Link in body converted.
        assert!(fixed.contains("[doc][doc]"));
    }

    // -------------------------------------------------------------------
    // reference → inline
    // -------------------------------------------------------------------

    #[test]
    fn fix_full_to_inline() {
        let rule = rule_only_inline();
        let content = "See [docs][site].\n\n[site]: https://example.com\n";
        let fixed = assert_round_trip_clean(&rule, content);
        // Inline splice; ref def remains (MD053 cleans up unused defs separately).
        assert!(fixed.contains("[docs](https://example.com)"));
    }

    #[test]
    fn fix_collapsed_to_inline() {
        let rule = rule_only_inline();
        let content = "See [docs][].\n\n[docs]: https://example.com\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(fixed.contains("[docs](https://example.com)"));
    }

    #[test]
    fn fix_shortcut_to_inline() {
        let rule = rule_only_inline();
        let content = "See [docs].\n\n[docs]: https://example.com\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(fixed.contains("[docs](https://example.com)"));
    }

    #[test]
    fn fix_full_to_inline_preserves_title() {
        let rule = rule_only_inline();
        let content = "See [docs][site].\n\n[site]: https://example.com \"Site Title\"\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(
            fixed.contains(r#"[docs](https://example.com "Site Title")"#),
            "title not preserved, got:\n{fixed}"
        );
    }

    #[test]
    fn fix_inline_to_full_text_with_code_span_containing_brackets() {
        // Text containing a code span with brackets used to confuse a hand-rolled
        // bracket-counter. We rely on pulldown-cmark for the parse, so the
        // conversion must round-trip cleanly.
        let rule = rule_inline_disallowed();
        let content = "See [`a[0]` index](https://example.com).\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(
            fixed.contains("[`a[0]` index]["),
            "code-span text not preserved, got:\n{fixed}"
        );
        assert!(
            fixed.contains("]: https://example.com"),
            "missing emitted ref def, got:\n{fixed}"
        );
    }

    #[test]
    fn fix_full_to_inline_image() {
        let rule = rule_only_inline();
        let content = "Logo: ![alt][logo].\n\n[logo]: https://x.com/img.png\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(fixed.contains("![alt](https://x.com/img.png)"));
    }

    // -------------------------------------------------------------------
    // Trivial reference inter-conversions
    // -------------------------------------------------------------------

    #[test]
    fn fix_collapsed_to_full() {
        // Allow only full of the reference styles.
        let rule = MD054LinkImageStyle::new(false, false, true, false, false, false);
        let content = "[docs][].\n\n[docs]: https://example.com\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert_eq!(fixed, "[docs][docs].\n\n[docs]: https://example.com\n");
    }

    #[test]
    fn fix_collapsed_to_full_with_trailing_content() {
        // Regression: pulldown-cmark's offset_iter range for a `Collapsed` link
        // covers only the `[text]` portion, not the trailing `[]`. If the span
        // end isn't extended, the auto-fix replaces just `[text]` and leaves
        // the `[]` behind, producing malformed `[docs][docs][]` output.
        let rule = MD054LinkImageStyle::new(false, false, true, false, false, false);
        let content = "See [docs][] for details.\n\n[docs]: https://example.com\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert_eq!(fixed, "See [docs][docs] for details.\n\n[docs]: https://example.com\n");
    }

    #[test]
    fn fix_shortcut_to_full() {
        let rule = MD054LinkImageStyle::new(false, false, true, false, false, false);
        let content = "See [docs].\n\n[docs]: https://example.com\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(fixed.contains("See [docs][docs]"));
    }

    #[test]
    fn fix_shortcut_to_collapsed() {
        let rule = MD054LinkImageStyle::new(false, true, false, false, false, false);
        let content = "See [docs].\n\n[docs]: https://example.com\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(fixed.contains("See [docs][]"));
    }

    // -------------------------------------------------------------------
    // autolink ↔ inline
    // -------------------------------------------------------------------

    #[test]
    fn fix_autolink_to_inline_form() {
        // Disallow autolink only; allow inline + url_inline (the default for the
        // remaining styles). An autolink's visible text is the URL, so the only
        // inline-shaped conversion is `[url](url)` — which classifies as
        // `url-inline`. Both must be allowed for a clean round-trip.
        let rule = MD054LinkImageStyle::new(false, true, true, true, true, true);
        let content = "Visit <https://example.com> today.\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(
            fixed.contains("[https://example.com](https://example.com)"),
            "got: {fixed:?}"
        );
    }

    #[test]
    fn fix_autolink_to_full_when_inline_styles_disallowed() {
        // Disallow autolink + inline + url-inline; the only reachable target is
        // a reference style. The rule should fall through to `full` and emit a
        // generated ref def.
        let rule = MD054LinkImageStyle::new(false, true, true, false, true, false);
        let content = "Visit <https://example.com> today.\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(
            fixed.contains("[https://example.com][https-example-com]"),
            "got: {fixed:?}"
        );
        assert!(fixed.contains("[https-example-com]: https://example.com"));
    }

    #[test]
    fn fix_url_inline_to_autolink() {
        // Disallow url-inline; autolink is the natural target when text==url.
        let rule = MD054LinkImageStyle::new(true, false, false, false, false, false);
        let content = "Visit [https://example.com](https://example.com).\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(fixed.contains("<https://example.com>"));
    }

    // -------------------------------------------------------------------
    // No-op / unreachable-target cases
    // -------------------------------------------------------------------

    #[test]
    fn fix_no_op_when_target_unreachable() {
        // Disallow inline, allow ONLY autolink. The inline link's text doesn't
        // match its URL, so autolink is unreachable. The fix is a no-op.
        let rule = MD054LinkImageStyle::new(true, false, false, false, false, false);
        let content = "See [docs](https://example.com).\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, content);
        // The warning is still produced.
        let warnings = rule.check(&ctx).unwrap();
        assert_eq!(warnings.len(), 1);
    }

    #[test]
    fn fix_preserves_allowed_links() {
        let rule = rule_inline_disallowed();
        let content = "Already [ref][r] is fine.\n\n[r]: https://example.com\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert_eq!(fixed, content);
    }

    // -------------------------------------------------------------------
    // preferred_style override
    // -------------------------------------------------------------------

    #[test]
    fn fix_preferred_style_explicit_full() {
        let config = md054_config::MD054Config {
            inline: false,
            preferred_style: PreferredStyles::single(PreferredStyle::Full),
            ..Default::default()
        };
        let rule = MD054LinkImageStyle::from_config_struct(config);
        let content = "[docs](https://example.com)\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(fixed.contains("[docs][docs]"));
    }

    #[test]
    fn fix_inline_to_collapsed_emits_matching_ref_def() {
        // Disallow inline, prefer collapsed. Inline → collapsed must produce
        // `[anchor][]` AND emit a `[anchor]: url` definition so the resulting
        // link still resolves to the original URL.
        let config = md054_config::MD054Config {
            inline: false,
            preferred_style: PreferredStyles::single(PreferredStyle::Collapsed),
            ..Default::default()
        };
        let rule = MD054LinkImageStyle::from_config_struct(config);
        let content = "[anchor](https://example.com)\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(fixed.contains("[anchor][]"), "got:\n{fixed}");
        assert!(fixed.contains("[anchor]: https://example.com"), "got:\n{fixed}");
    }

    #[test]
    fn fix_inline_to_shortcut_emits_matching_ref_def() {
        let config = md054_config::MD054Config {
            inline: false,
            preferred_style: PreferredStyles::single(PreferredStyle::Shortcut),
            ..Default::default()
        };
        let rule = MD054LinkImageStyle::from_config_struct(config);
        // Trailing period guarantees the shortcut isn't followed by `[` or `(`.
        let content = "See [anchor](https://example.com).\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(fixed.contains("[anchor]"), "got:\n{fixed}");
        assert!(!fixed.contains("[anchor]("), "shortcut form, not inline: {fixed}");
        assert!(fixed.contains("[anchor]: https://example.com"), "got:\n{fixed}");
    }

    #[test]
    fn fix_inline_to_collapsed_skips_empty_text() {
        // `[](url)` has no text — collapsed/shortcut emission would produce
        // `[][]` / `[]`, which CommonMark cannot parse as a link. The fix must
        // back off and leave the inline form intact.
        let config = md054_config::MD054Config {
            inline: false,
            preferred_style: PreferredStyles::single(PreferredStyle::Collapsed),
            ..Default::default()
        };
        let rule = MD054LinkImageStyle::from_config_struct(config);
        let content = "[](https://example.com)\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, content, "empty text must not collapse: {fixed}");
    }

    #[test]
    fn fix_inline_to_shortcut_skips_empty_text() {
        let config = md054_config::MD054Config {
            inline: false,
            preferred_style: PreferredStyles::single(PreferredStyle::Shortcut),
            ..Default::default()
        };
        let rule = MD054LinkImageStyle::from_config_struct(config);
        let content = "See [](https://example.com).\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, content);
    }

    #[test]
    fn fix_inline_to_collapsed_skips_text_with_brackets() {
        // Text containing literal `[` / `]` cannot be spliced into a label
        // without escaping; emit nothing rather than produce a broken link.
        let config = md054_config::MD054Config {
            inline: false,
            preferred_style: PreferredStyles::single(PreferredStyle::Collapsed),
            ..Default::default()
        };
        let rule = MD054LinkImageStyle::from_config_struct(config);
        let content = "See [`a[0]` index](https://example.com).\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, content, "text containing `[` / `]` must not collapse: {fixed}");
    }

    #[test]
    fn fix_inline_to_full_url_with_space_uses_angle_brackets_in_def() {
        // A URL containing a space must be carried in the angle-bracket
        // destination form on both sides of the conversion. The source already
        // uses `<...>`; the appended ref def must do the same so the resulting
        // `[label]: <url with space>` line round-trips through CommonMark.
        let rule = rule_inline_disallowed();
        let content = "See [docs](<./has space.md>).\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(
            fixed.contains("[docs]: <./has space.md>"),
            "ref def must wrap URL in angle brackets: {fixed}"
        );
    }

    #[test]
    fn fix_inline_to_full_url_with_unbalanced_paren_uses_angle_brackets_in_def() {
        // Unbalanced parens can't appear in the bare ref-def destination
        // either — the appended `[label]: url` must use angle-bracket form.
        let rule = rule_inline_disallowed();
        let content = "See [docs](<https://example.com/a)b>).\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(
            fixed.contains("[docs]: <https://example.com/a)b>"),
            "ref def must wrap unbalanced-paren URL in angle brackets: {fixed}"
        );
    }

    #[test]
    fn fix_full_to_inline_preserves_backslash_unescaped_title() {
        // pulldown-cmark unescapes `\"` → `"` inside titles. The fix must use
        // pulldown-cmark's resolved title (not the regex-captured raw form),
        // and re-quote it appropriately when emitting the inline destination.
        let rule = rule_only_inline();
        let content = "See [docs][d].\n\n[d]: https://example.com \"He said \\\"hi\\\"\"\n";
        let fixed = assert_round_trip_clean(&rule, content);
        // Pulldown-cmark gives us the title `He said "hi"` (unescaped).
        // The serializer chooses parentheses since `\"` appears, but either way
        // the round-trip must reproduce the same logical title without losing
        // backslashes or quotes.
        assert!(fixed.contains("https://example.com"), "URL must round-trip: {fixed}");
        assert!(
            fixed.contains(r#"\"hi\""#) || fixed.contains(r#"He said "hi""#),
            "title must round-trip with quotes preserved: {fixed}"
        );
    }

    #[test]
    fn fix_full_to_inline_url_with_close_paren_uses_angle_brackets() {
        // Existing ref def points to a URL containing `)`. Splicing it into
        // an inline `[t](url)` destination would terminate the destination
        // early. The fix must use the angle-bracket form `<url)>`.
        let rule = rule_only_inline();
        let content = "See [t][r].\n\n[r]: <https://example.com/a)b>\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(
            fixed.contains("[t](<https://example.com/a)b>)"),
            "inline form must use angle brackets for `)` URLs: {fixed}"
        );
    }

    #[test]
    fn fix_inline_to_collapsed_skips_when_label_collides_with_different_url() {
        // Existing ref def for `anchor` points to a DIFFERENT URL. Converting
        // `[anchor](other.com)` to collapsed would produce a broken/wrong link
        // (CommonMark would resolve `[anchor][]` to the existing def). The fix
        // must back off rather than silently change the link target.
        let config = md054_config::MD054Config {
            inline: false,
            preferred_style: PreferredStyles::single(PreferredStyle::Collapsed),
            ..Default::default()
        };
        let rule = MD054LinkImageStyle::from_config_struct(config);
        let content = "[other][anchor]\n[anchor](https://other.com)\n\n[anchor]: https://existing.com\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        // Inline link is left alone (no safe conversion); the warning persists.
        assert!(fixed.contains("[anchor](https://other.com)"), "got:\n{fixed}");
        // The original `[anchor]` definition is unchanged.
        assert!(fixed.contains("[anchor]: https://existing.com"));
    }

    #[test]
    fn fix_preferred_style_list_picks_first_reachable() {
        // List `[autolink, full]`: an autolinkable URL must convert to autolink
        // because it appears first AND is reachable.
        let config = md054_config::MD054Config {
            url_inline: false,
            preferred_style: PreferredStyles::from_iter([PreferredStyle::Autolink, PreferredStyle::Full]),
            ..Default::default()
        };
        let rule = MD054LinkImageStyle::from_config_struct(config);
        let content = "[https://example.com](https://example.com)\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(
            fixed.contains("<https://example.com>"),
            "expected autolink form, got:\n{fixed}"
        );
    }

    #[test]
    fn fix_preferred_style_list_falls_back_to_next_when_first_unreachable() {
        // List `[autolink, full]`: URL is not autolinkable (relative), so the
        // first entry isn't reachable. Must fall back to the second (`full`).
        let config = md054_config::MD054Config {
            inline: false,
            preferred_style: PreferredStyles::from_iter([PreferredStyle::Autolink, PreferredStyle::Full]),
            ..Default::default()
        };
        let rule = MD054LinkImageStyle::from_config_struct(config);
        let content = "[docs](./guide.md)\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(
            fixed.contains("[docs][docs]"),
            "expected fallback to full, got:\n{fixed}"
        );
        assert!(
            fixed.contains("[docs]: ./guide.md"),
            "expected matching ref def, got:\n{fixed}"
        );
    }

    #[test]
    fn fix_preferred_style_auto_in_list_acts_as_wildcard_fallback() {
        // `[autolink, auto]` for a non-autolinkable URL must fall through to
        // the source-aware Auto candidates (which for an inline source defaults
        // to `full`).
        let config = md054_config::MD054Config {
            inline: false,
            preferred_style: PreferredStyles::from_iter([PreferredStyle::Autolink, PreferredStyle::Auto]),
            ..Default::default()
        };
        let rule = MD054LinkImageStyle::from_config_struct(config);
        let content = "[docs](./guide.md)\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(
            fixed.contains("[docs][docs]"),
            "Auto fallback should pick full for inline-disallowed config, got:\n{fixed}"
        );
    }

    #[test]
    fn fix_default_auto_prefers_autolink_for_url_inline_source() {
        // Source: `[url](url)` (url-inline). Disallow url-inline; default Auto.
        // Autolink must win over Full because `<url>` is the tightest form when
        // text equals the URL and the URL is autolinkable.
        let rule = MD054LinkImageStyle::new(true, true, true, true, true, false);
        let content = "[https://example.com](https://example.com)\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(
            fixed.contains("<https://example.com>"),
            "expected autolink, got:\n{fixed}"
        );
        assert!(
            !fixed.contains("[https://example.com]["),
            "should not produce reference form when autolink is reachable, got:\n{fixed}"
        );
    }

    #[test]
    fn fix_default_auto_falls_back_when_autolink_disallowed() {
        // Same shape as above but with autolink disallowed: must skip autolink
        // and pick the next Auto candidate (`full`).
        let rule = MD054LinkImageStyle::new(false, true, true, true, true, false);
        let content = "[https://example.com](https://example.com)\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(
            fixed.contains("[https://example.com][https-example-com]"),
            "expected full form, got:\n{fixed}"
        );
        assert!(
            fixed.contains("[https-example-com]: https://example.com"),
            "missing ref def, got:\n{fixed}"
        );
    }

    #[test]
    fn fix_preferred_style_explicit_no_match_skips_fix() {
        // Single-entry list pinning a target that's neither allowed nor reachable
        // must produce no fix (warning persists, content unchanged).
        let config = md054_config::MD054Config {
            inline: false,
            // Pinning `Inline` for an Inline source — same style; not reachable.
            preferred_style: PreferredStyles::single(PreferredStyle::Inline),
            ..Default::default()
        };
        let rule = MD054LinkImageStyle::from_config_struct(config);
        let content = "[docs](./guide.md)\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, content, "expected no-op fix, got:\n{fixed}");
    }

    // -------------------------------------------------------------------
    // Mixed / interaction scenarios
    // -------------------------------------------------------------------

    #[test]
    fn fix_mixes_inline_and_image_in_same_doc() {
        let rule = rule_inline_disallowed();
        let content = "Text [link](https://example.com) and ![pic](https://example.com/p.png).\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(fixed.contains("[link][link]"));
        assert!(fixed.contains("![pic][pic]"));
        assert!(fixed.contains("[link]: https://example.com"));
        assert!(fixed.contains("[pic]: https://example.com/p.png"));
    }

    #[test]
    fn fix_appends_one_blank_line_separator() {
        let rule = rule_inline_disallowed();
        let content = "Plain prose.\n\n[link](https://x.com)\n";
        let fixed = assert_round_trip_clean(&rule, content);
        // Exactly one blank line between body and ref-def block.
        assert!(fixed.ends_with("\n[link]: https://x.com\n"));
        assert!(!fixed.contains("\n\n\n[link]"));
    }

    // -------------------------------------------------------------------
    // Overlapping edits / nested constructs
    // -------------------------------------------------------------------

    #[test]
    fn fix_nested_image_in_link_does_not_panic_or_corrupt() {
        // `[![alt](img.png)](https://x)` is an inline link whose text is itself
        // an inline image. The two spans overlap (the image lives entirely
        // inside the link span). With `inline = false`, both are flagged and
        // both produce candidate edits. Applying both edits to the same byte
        // range would corrupt the document — the planner must drop overlapping
        // edits.
        let rule = rule_inline_disallowed();
        let content = "See [![alt](img.png)](https://x.com).\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        // Must not panic, even though both candidate edits overlap.
        let fixed = rule.fix(&ctx).unwrap();
        // Both candidate edits got dropped, so the doc is unchanged and the
        // warnings persist for the user to resolve manually.
        assert_eq!(fixed, content);
        let warnings = rule.check(&ctx).unwrap();
        assert_eq!(warnings.len(), 2, "both nested constructs should still warn");
    }

    // -------------------------------------------------------------------
    // Email autolinks (CommonMark §6.5: bare email resolves to mailto:URL)
    // -------------------------------------------------------------------

    #[test]
    fn fix_email_autolink_to_inline_preserves_mailto_prefix() {
        // `<me@example.com>` is an email autolink. Per CommonMark §6.5 it
        // resolves to destination `mailto:me@example.com` while displaying the
        // bare email. Converting to any non-autolink form must preserve that
        // resolved destination — losing the `mailto:` prefix would silently
        // retarget the link to a relative path.
        let rule = MD054LinkImageStyle::new(false, true, true, true, true, true);
        let content = "Reach <me@example.com> for support.\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(
            fixed.contains("[me@example.com](mailto:me@example.com)"),
            "expected mailto: prefix on resolved destination, got:\n{fixed}"
        );
    }

    #[test]
    fn fix_email_autolink_to_full_preserves_mailto_in_ref_def() {
        // Same invariant as the inline conversion, but routed through `full`
        // when inline is disallowed: the generated ref-def URL must be
        // `mailto:me@example.com`, not the bare email.
        let rule = MD054LinkImageStyle::new(false, true, true, false, true, false);
        let content = "Reach <me@example.com> for support.\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(
            fixed.contains("]: mailto:me@example.com"),
            "ref def should carry the mailto: prefix, got:\n{fixed}"
        );
    }

    #[test]
    fn fix_rejects_bare_email_as_autolink_target() {
        // A `url-inline` link whose URL is a bare email must NOT be rewritten
        // to `<bare-email>`: that wraps the bare email in autolink syntax,
        // which the parser then resolves to `mailto:bare-email` — silently
        // changing the destination. The fix must fall through to a non-autolink
        // target instead.
        let config = md054_config::MD054Config {
            url_inline: false,
            preferred_style: PreferredStyles::from_iter([PreferredStyle::Autolink, PreferredStyle::Auto]),
            ..Default::default()
        };
        let rule = MD054LinkImageStyle::from_config_struct(config);
        let content = "[me@example.com](me@example.com)\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(
            !fixed.contains("<me@example.com>"),
            "bare-email autolink target would silently retarget to mailto:, got:\n{fixed}"
        );
    }

    // -------------------------------------------------------------------
    // Autolink target rejected when title would be lost
    // -------------------------------------------------------------------

    // -------------------------------------------------------------------
    // Generated ref defs round-trip through rumdl's own ref-def parser
    // -------------------------------------------------------------------

    #[test]
    fn fix_generated_ref_def_with_both_quote_types_round_trips_to_ctx() {
        // A title containing both `"` and `'` forces format_title onto the
        // paren-form CommonMark §4.7 delimiter. The generated ref def must
        // re-parse through rumdl's `parse_reference_defs` so downstream rules
        // (MD053 unused refs, MD057 link validity) still see the definition
        // — otherwise the URL silently disappears from `ctx.reference_defs`.
        let rule = rule_inline_disallowed();
        let content = "See [docs](https://example.com/x \"and 'both' quotes\") today.\n";
        let fixed = assert_round_trip_clean(&rule, content);
        // Confirm the fixer chose the paren form (the only valid CommonMark
        // delimiter when both quote types appear in the title).
        assert!(
            fixed.contains("(and 'both' quotes)") || fixed.contains("\"and 'both' quotes\""),
            "title should round-trip through some valid delimiter, got:\n{fixed}"
        );
        // The crucial invariant: the generated ref def is visible to rumdl's
        // own parser. `assert_round_trip_clean` already checks the URL set
        // round-trips, but let's also pin the title down explicitly.
        let ctx = LintContext::new(&fixed, MarkdownFlavor::Standard, None);
        let def = ctx
            .reference_defs
            .iter()
            .find(|d| d.url == "https://example.com/x")
            .expect("generated ref def must round-trip through parse_reference_defs");
        assert_eq!(
            def.title.as_deref(),
            Some("and 'both' quotes"),
            "title content must survive the round-trip"
        );
    }

    // -------------------------------------------------------------------
    // Generated ref-def block matches the document's line-ending style
    // -------------------------------------------------------------------

    #[test]
    fn fix_appends_generated_refs_with_crlf_when_source_is_crlf() {
        // A CRLF document must come back with CRLF endings — including the
        // separator and the per-ref lines we append. Otherwise `--fix` would
        // produce sequences like `\r\n\n[ref]: ...`, which `git diff` (and
        // any line-ending-strict tooling) flags as whole-file churn even
        // when only one link was rewritten.
        let rule = rule_inline_disallowed();
        let content = "See [docs](https://example.com/x).\r\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let warnings = rule.check(&ctx).expect("check must succeed");
        assert!(!warnings.is_empty(), "expected at least one warning");
        let fixed = rule.fix(&ctx).expect("fix must succeed");
        assert!(
            fixed.contains("\r\n"),
            "fixed output must preserve CRLF, got:\n{fixed:?}"
        );
        assert!(
            !fixed.lines().any(|l| l.ends_with('\r')) || !fixed.contains("\n\n"),
            "no line should end with stray \\r and there should be no naked LF blanks; got:\n{fixed:?}"
        );
        // The fixed buffer must not contain a naked LF (i.e. `\n` not preceded
        // by `\r`) anywhere — that would be the mixed-ending bug.
        let bytes = fixed.as_bytes();
        for (i, &b) in bytes.iter().enumerate() {
            if b == b'\n' {
                assert!(
                    i > 0 && bytes[i - 1] == b'\r',
                    "found naked LF at byte {i} in CRLF document, full output:\n{fixed:?}"
                );
            }
        }
    }

    #[test]
    fn fix_appends_generated_refs_with_lf_when_source_is_lf() {
        // Mirror of the CRLF test: an LF-only document must stay LF-only.
        let rule = rule_inline_disallowed();
        let content = "See [docs](https://example.com/x).\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).expect("fix must succeed");
        assert!(
            !fixed.contains('\r'),
            "LF document must not gain any CR characters, got:\n{fixed:?}"
        );
    }

    // -------------------------------------------------------------------
    // Shortcut target rejected when follower would reparse the link
    // -------------------------------------------------------------------

    #[test]
    fn fix_rejects_shortcut_target_when_followed_by_paren() {
        // `[docs](url)(suffix)` is a disallowed inline link followed by literal
        // `(suffix)`. Naively rewriting the link to shortcut form yields
        // `[docs](suffix)`, which CommonMark reparses as an inline link with
        // destination `suffix` — silently retargeting to the wrong URL.
        // The planner must reject Shortcut for this source.
        let config = md054_config::MD054Config {
            inline: false,
            preferred_style: PreferredStyles::single(PreferredStyle::Shortcut),
            ..Default::default()
        };
        let rule = MD054LinkImageStyle::from_config_struct(config);
        let content = "[docs](https://example.com/x)(suffix)\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        // Shortcut target is unreachable; with no other allowed style chosen
        // explicitly, the fix is a no-op.
        assert_eq!(fixed, content, "shortcut target was unsafe; fix should be a no-op");
    }

    #[test]
    fn fix_rejects_shortcut_target_when_followed_by_bracket() {
        // `[docs](url)[next]` rewritten to `[docs][next]` would parse as a
        // full reference link with label `next` — completely different
        // semantics. Reject Shortcut for this case.
        let config = md054_config::MD054Config {
            inline: false,
            preferred_style: PreferredStyles::single(PreferredStyle::Shortcut),
            ..Default::default()
        };
        let rule = MD054LinkImageStyle::from_config_struct(config);
        let content = "[docs](https://example.com/x)[next]\n\n[next]: https://example.com/n\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, content, "shortcut target was unsafe; fix should be a no-op");
    }

    #[test]
    fn fix_allows_shortcut_target_when_follower_is_safe() {
        // Sanity: when the follower is plain text (period, space, EOL), the
        // shortcut conversion is safe and proceeds normally. This guards
        // against an over-eager rejection that would block all shortcut fixes.
        let config = md054_config::MD054Config {
            inline: false,
            preferred_style: PreferredStyles::single(PreferredStyle::Shortcut),
            ..Default::default()
        };
        let rule = MD054LinkImageStyle::from_config_struct(config);
        let content = "See [docs](https://example.com/x). Also nice.\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(fixed.contains("[docs]"), "expected shortcut form, got:\n{fixed}");
        assert!(fixed.contains("[docs]: https://example.com/x"));
    }

    // -------------------------------------------------------------------
    // Fix metadata is reachable through FixCoordinator
    // -------------------------------------------------------------------

    #[test]
    fn check_attaches_fix_for_self_contained_rewrites() {
        // For rewrites where the per-warning Fix carries the entire change
        // (no paired ref-def needed), check() must attach the Fix so editor
        // quick-fix paths (which apply only `warning.fix.range/replacement`)
        // produce a correct result. autolink → url-inline is fully encoded
        // in a single span replacement.
        let rule = MD054LinkImageStyle::new(false, true, true, true, true, true);
        let content = "See <https://example.com>.\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let warnings = rule.check(&ctx).unwrap();
        assert_eq!(warnings.len(), 1, "should warn about the autolink");
        let fix = warnings[0]
            .fix
            .as_ref()
            .expect("self-contained rewrite must carry a Fix so quick-fix paths can apply it");
        assert_eq!(&content[fix.range.clone()], "<https://example.com>");
        assert_eq!(fix.replacement, "[https://example.com](https://example.com)");
    }

    #[test]
    fn check_carries_atomic_fix_when_rewrite_requires_new_ref_def() {
        // inline → collapsed/full/shortcut requires appending `[label]: url`
        // at end-of-file. The per-warning Fix carries the in-place rewrite
        // as its primary edit and the EOF ref-def insertion as an
        // additional_edit, so quick-fix paths that apply a single warning
        // produce a complete, parseable result without relying on a follow-up
        // fix-all pass to materialize the definition.
        let rule = rule_inline_disallowed();
        let content = "See [docs](https://example.com).\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let warnings = rule.check(&ctx).unwrap();
        assert_eq!(warnings.len(), 1, "should warn about the inline link");
        let fix = warnings[0]
            .fix
            .as_ref()
            .expect("ref-emitting rewrite must carry an atomic per-warning Fix");
        assert_eq!(&content[fix.range.clone()], "[docs](https://example.com)");
        assert!(
            fix.replacement.starts_with("[docs]"),
            "primary replacement should rewrite the link to a reference form, got: {:?}",
            fix.replacement
        );
        assert_eq!(
            fix.additional_edits.len(),
            1,
            "ref-emitting fix should carry one additional_edit for the ref-def"
        );
        let extra = &fix.additional_edits[0];
        assert_eq!(
            extra.range,
            content.len()..content.len(),
            "ref-def insertion should be a zero-width edit at EOF"
        );
        assert!(
            extra.replacement.contains("[docs]: https://example.com"),
            "additional_edit should append the ref-def, got: {:?}",
            extra.replacement
        );
        // Applying the per-warning fix in isolation must yield the same shape
        // the whole-document fix() path produces: link rewritten to a
        // reference form AND the ref def appended at EOF.
        let applied = crate::utils::fix_utils::apply_warning_fixes(content, &warnings).unwrap();
        let from_fix_all = rule.fix(&ctx).unwrap();
        assert!(
            applied.contains("[docs]: https://example.com"),
            "single-warning application must include ref-def, got:\n{applied}"
        );
        assert!(
            !applied.contains("[docs](https://example.com)"),
            "single-warning application must rewrite the inline link, got:\n{applied}"
        );
        // Both paths must still drive the document into a stable, fixed shape
        // — but exact equality isn't required because per-warning EOF
        // insertions don't deduplicate trailing newlines the way the
        // whole-document apply() does.
        assert!(
            from_fix_all.contains("[docs]: https://example.com"),
            "fix-all path must also produce the ref-def, got:\n{from_fix_all}"
        );
    }

    #[test]
    fn check_attaches_no_fix_when_target_unreachable() {
        // When no allowed style is reachable, no edit is produced — so the
        // warning carries no Fix and the coordinator skips fix-all for it.
        // This avoids advertising an "automatic fix" the user can't actually
        // accept.
        let rule = MD054LinkImageStyle::new(true, false, false, false, false, false);
        let content = "See [docs](https://example.com).\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let warnings = rule.check(&ctx).unwrap();
        assert_eq!(warnings.len(), 1);
        assert!(warnings[0].fix.is_none(), "unreachable target should leave fix empty");
    }

    #[test]
    fn fix_skips_autolink_target_when_title_present() {
        // Autolink syntax has no slot for a title. Rewriting
        // `[url](url "title")` to `<url>` would silently drop the title text,
        // so the planner must reject Autolink as a target and fall through to
        // a reference style.
        let config = md054_config::MD054Config {
            url_inline: false,
            preferred_style: PreferredStyles::from_iter([PreferredStyle::Autolink, PreferredStyle::Auto]),
            ..Default::default()
        };
        let rule = MD054LinkImageStyle::from_config_struct(config);
        let content = "[https://example.com](https://example.com \"Homepage\")\n";
        let fixed = assert_round_trip_clean(&rule, content);
        assert!(
            !fixed.contains("<https://example.com>"),
            "autolink target would drop the title, got:\n{fixed}"
        );
        assert!(
            fixed.contains("\"Homepage\""),
            "title text must survive the conversion, got:\n{fixed}"
        );
    }

    #[test]
    fn default_config_section_emits_clean_user_facing_defaults() {
        // `rumdl config --defaults` reads `default_config_section()` and prints the
        // values verbatim. The polymorphic sentinel is a schema-only marker — it
        // must never appear in user-facing output, otherwise the documented default
        // table contains a placeholder string the user can't actually paste back.
        let rule = MD054LinkImageStyle::default();
        let (_, value) = rule.default_config_section().expect("md054 has defaults");
        let table = value.as_table().expect("config section is a table");
        let preferred = table
            .get("preferred-style")
            .expect("preferred-style key must be present in defaults");
        assert!(
            !crate::rule_config_serde::is_polymorphic_sentinel(preferred),
            "preferred-style in user-facing defaults must be the serialized scalar, not the sentinel; got {preferred:?}"
        );
        // The serialized default of a single-element PreferredStyles collapses to a
        // scalar string. Verify the actual shape so a future serde change is caught.
        assert!(
            preferred.is_str(),
            "preferred-style default should serialize as a scalar string; got {preferred:?}"
        );
    }

    #[test]
    fn registry_marks_preferred_style_polymorphic_for_validation() {
        // The schema view (consumed by the validator) must carry the sentinel so
        // the alternative list form of `preferred-style` is accepted alongside the
        // serialized scalar default. This is the counterpart to
        // `default_config_section_emits_clean_user_facing_defaults`: the same key
        // looks different in the two views, by design.
        let registry = crate::config::registry::default_registry();
        let expected = registry
            .expected_value_for("MD054", "preferred-style")
            .or_else(|| registry.expected_value_for("MD054", "preferred_style"));
        // `expected_value_for` returns None precisely when the entry was filtered
        // as a sentinel — that's the contract the validator uses to skip type
        // checking. Any other return value would reintroduce the original bug
        // where the list form is rejected.
        assert!(
            expected.is_none(),
            "preferred-style must be sentinel-marked in the schema so type checking is skipped; got {expected:?}"
        );
        // Sanity check: the key is still recognized as valid (only the type check
        // is skipped, not the key-name check).
        let keys = registry.config_keys_for("MD054").expect("md054 must be registered");
        assert!(keys.contains("preferred-style"));
    }
}