Skip to main content

rumdl_lib/rules/
md048_code_fence_style.rs

1use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, Severity};
2use crate::rules::code_fence_utils::CodeFenceStyle;
3use crate::utils::range_utils::calculate_match_range;
4use toml;
5
6mod md048_config;
7use md048_config::MD048Config;
8
9/// Parsed fence marker candidate on a single line.
10#[derive(Debug, Clone, Copy)]
11struct FenceMarker<'a> {
12    /// Fence character (` or ~).
13    fence_char: char,
14    /// Length of the contiguous fence run.
15    fence_len: usize,
16    /// Byte index where the fence run starts.
17    fence_start: usize,
18    /// Remaining text after the fence run.
19    rest: &'a str,
20}
21
22/// Parse a candidate fence marker line.
23///
24/// CommonMark only recognizes fenced code block markers when indented by at most
25/// three spaces (outside container contexts). This parser enforces that bound and
26/// returns the marker run and trailing text for further opening/closing checks.
27#[inline]
28fn parse_fence_marker(line: &str) -> Option<FenceMarker<'_>> {
29    let bytes = line.as_bytes();
30    let mut pos = 0usize;
31    while pos < bytes.len() && bytes[pos] == b' ' {
32        pos += 1;
33    }
34    if pos > 3 {
35        return None;
36    }
37
38    let fence_char = match bytes.get(pos).copied() {
39        Some(b'`') => '`',
40        Some(b'~') => '~',
41        _ => return None,
42    };
43
44    let marker = if fence_char == '`' { b'`' } else { b'~' };
45    let mut end = pos;
46    while end < bytes.len() && bytes[end] == marker {
47        end += 1;
48    }
49    let fence_len = end - pos;
50    if fence_len < 3 {
51        return None;
52    }
53
54    Some(FenceMarker {
55        fence_char,
56        fence_len,
57        fence_start: pos,
58        rest: &line[end..],
59    })
60}
61
62#[inline]
63fn is_closing_fence(marker: FenceMarker<'_>, opening_fence_char: char, opening_fence_len: usize) -> bool {
64    marker.fence_char == opening_fence_char && marker.fence_len >= opening_fence_len && marker.rest.trim().is_empty()
65}
66
67/// Rule MD048: Code fence style
68///
69/// See [docs/md048.md](../../docs/md048.md) for full documentation, configuration, and examples.
70#[derive(Clone)]
71pub struct MD048CodeFenceStyle {
72    config: MD048Config,
73}
74
75impl MD048CodeFenceStyle {
76    pub fn new(style: CodeFenceStyle) -> Self {
77        Self {
78            config: MD048Config { style },
79        }
80    }
81
82    pub fn from_config_struct(config: MD048Config) -> Self {
83        Self { config }
84    }
85
86    fn detect_style(&self, ctx: &crate::lint_context::LintContext) -> Option<CodeFenceStyle> {
87        // Count occurrences of each fence style (prevalence-based approach)
88        let mut backtick_count = 0;
89        let mut tilde_count = 0;
90        let mut in_code_block = false;
91        let mut opening_fence_char = '`';
92        let mut opening_fence_len = 0usize;
93
94        for line in ctx.content.lines() {
95            let Some(marker) = parse_fence_marker(line) else {
96                continue;
97            };
98
99            if !in_code_block {
100                // Opening fence - count it
101                if marker.fence_char == '`' {
102                    backtick_count += 1;
103                } else {
104                    tilde_count += 1;
105                }
106                in_code_block = true;
107                opening_fence_char = marker.fence_char;
108                opening_fence_len = marker.fence_len;
109            } else if is_closing_fence(marker, opening_fence_char, opening_fence_len) {
110                in_code_block = false;
111            }
112        }
113
114        // Use the most prevalent style
115        // In case of a tie, prefer backticks (more common, widely supported)
116        if backtick_count >= tilde_count && backtick_count > 0 {
117            Some(CodeFenceStyle::Backtick)
118        } else if tilde_count > 0 {
119            Some(CodeFenceStyle::Tilde)
120        } else {
121            None
122        }
123    }
124}
125
126/// Find the maximum fence length using `target_char` within the body of a fenced block.
127///
128/// Scans from the line after `opening_line` until the matching closing fence
129/// (same `opening_char`, length >= `opening_fence_len`, no trailing content).
130/// Returns the maximum number of consecutive `target_char` characters found at
131/// the start of any interior bare fence line (after stripping leading whitespace).
132///
133/// This is used to compute the minimum fence length needed when converting a
134/// fence from one style to another so that nesting remains unambiguous.
135/// For example, converting a `~~~` outer fence that contains ```` ``` ```` inner
136/// fences to backtick style requires using ```` ```` ```` (4 backticks) so that
137/// the inner 3-backtick bare fences cannot inadvertently close the outer block.
138///
139/// Only bare interior sequences (no trailing content) are counted. Per CommonMark
140/// spec section 4.5, a closing fence must be followed only by optional whitespace —
141/// lines with info strings (e.g. `` ```rust ``) can never be closing fences, so
142/// they never create ambiguity regardless of the outer fence's style.
143fn max_inner_fence_length_of_char(
144    lines: &[&str],
145    opening_line: usize,
146    opening_fence_len: usize,
147    opening_char: char,
148    target_char: char,
149) -> usize {
150    let mut max_len = 0usize;
151
152    for line in lines.iter().skip(opening_line + 1) {
153        let Some(marker) = parse_fence_marker(line) else {
154            continue;
155        };
156
157        // Stop at the closing fence of the outer block.
158        if is_closing_fence(marker, opening_char, opening_fence_len) {
159            break;
160        }
161
162        // Count only bare sequences (no info string). Lines with info strings
163        // can never be closing fences per CommonMark and pose no ambiguity risk.
164        if marker.fence_char == target_char && marker.rest.trim().is_empty() {
165            max_len = max_len.max(marker.fence_len);
166        }
167    }
168
169    max_len
170}
171
172impl Rule for MD048CodeFenceStyle {
173    fn name(&self) -> &'static str {
174        "MD048"
175    }
176
177    fn description(&self) -> &'static str {
178        "Code fence style should be consistent"
179    }
180
181    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
182        let content = ctx.content;
183        let line_index = &ctx.line_index;
184
185        let mut warnings = Vec::new();
186
187        let target_style = match self.config.style {
188            CodeFenceStyle::Consistent => self.detect_style(ctx).unwrap_or(CodeFenceStyle::Backtick),
189            _ => self.config.style,
190        };
191
192        let lines: Vec<&str> = content.lines().collect();
193        let mut in_code_block = false;
194        let mut code_block_fence_char = '`';
195        let mut code_block_fence_len = 0usize;
196        // The fence length to use when writing the converted/lengthened closing fence.
197        // May be longer than the original when inner fences require disambiguation by length.
198        let mut converted_fence_len = 0usize;
199        // True when the opening fence was already the correct style but its length is
200        // ambiguous (interior has same-style fences of equal or greater length).
201        let mut needs_lengthening = false;
202
203        for (line_num, &line) in lines.iter().enumerate() {
204            let Some(marker) = parse_fence_marker(line) else {
205                continue;
206            };
207            let fence_char = marker.fence_char;
208            let fence_len = marker.fence_len;
209
210            if !in_code_block {
211                in_code_block = true;
212                code_block_fence_char = fence_char;
213                code_block_fence_len = fence_len;
214
215                let needs_conversion = (fence_char == '`' && target_style == CodeFenceStyle::Tilde)
216                    || (fence_char == '~' && target_style == CodeFenceStyle::Backtick);
217
218                if needs_conversion {
219                    let target_char = if target_style == CodeFenceStyle::Backtick {
220                        '`'
221                    } else {
222                        '~'
223                    };
224
225                    // Compute how many target_char characters the converted fence needs.
226                    // Must be strictly greater than any inner bare fence of the target style.
227                    let prefix = &line[..marker.fence_start];
228                    let info = marker.rest;
229                    let max_inner =
230                        max_inner_fence_length_of_char(&lines, line_num, fence_len, fence_char, target_char);
231                    converted_fence_len = fence_len.max(max_inner + 1);
232                    needs_lengthening = false;
233
234                    let replacement = format!("{prefix}{}{info}", target_char.to_string().repeat(converted_fence_len));
235
236                    let fence_start = marker.fence_start;
237                    let fence_end = fence_start + fence_len;
238                    let (start_line, start_col, end_line, end_col) =
239                        calculate_match_range(line_num + 1, line, fence_start, fence_end - fence_start);
240
241                    warnings.push(LintWarning {
242                        rule_name: Some(self.name().to_string()),
243                        message: format!(
244                            "Code fence style: use {} instead of {}",
245                            if target_style == CodeFenceStyle::Backtick {
246                                "```"
247                            } else {
248                                "~~~"
249                            },
250                            if fence_char == '`' { "```" } else { "~~~" }
251                        ),
252                        line: start_line,
253                        column: start_col,
254                        end_line,
255                        end_column: end_col,
256                        severity: Severity::Warning,
257                        fix: Some(Fix {
258                            range: line_index.line_col_to_byte_range_with_length(line_num + 1, 1, line.len()),
259                            replacement,
260                        }),
261                    });
262                } else {
263                    // Already the correct style. Check for fence-length ambiguity:
264                    // if the interior contains same-style bare fences of equal or greater
265                    // length, the outer fence cannot be distinguished from an inner
266                    // closing fence and must be made longer.
267                    let prefix = &line[..marker.fence_start];
268                    let info = marker.rest;
269                    let max_inner = max_inner_fence_length_of_char(&lines, line_num, fence_len, fence_char, fence_char);
270                    if max_inner >= fence_len {
271                        converted_fence_len = max_inner + 1;
272                        needs_lengthening = true;
273
274                        let replacement =
275                            format!("{prefix}{}{info}", fence_char.to_string().repeat(converted_fence_len));
276
277                        let fence_start = marker.fence_start;
278                        let fence_end = fence_start + fence_len;
279                        let (start_line, start_col, end_line, end_col) =
280                            calculate_match_range(line_num + 1, line, fence_start, fence_end - fence_start);
281
282                        warnings.push(LintWarning {
283                            rule_name: Some(self.name().to_string()),
284                            message: format!(
285                                "Code fence length is ambiguous: outer fence ({fence_len} {}) \
286                                 contains interior fence sequences of equal length; \
287                                 use {converted_fence_len}",
288                                if fence_char == '`' { "backticks" } else { "tildes" },
289                            ),
290                            line: start_line,
291                            column: start_col,
292                            end_line,
293                            end_column: end_col,
294                            severity: Severity::Warning,
295                            fix: Some(Fix {
296                                range: line_index.line_col_to_byte_range_with_length(line_num + 1, 1, line.len()),
297                                replacement,
298                            }),
299                        });
300                    } else {
301                        converted_fence_len = fence_len;
302                        needs_lengthening = false;
303                    }
304                }
305            } else {
306                // Inside a code block — check if this is the closing fence.
307                let is_closing = is_closing_fence(marker, code_block_fence_char, code_block_fence_len);
308
309                if is_closing {
310                    let needs_conversion = (fence_char == '`' && target_style == CodeFenceStyle::Tilde)
311                        || (fence_char == '~' && target_style == CodeFenceStyle::Backtick);
312
313                    if needs_conversion || needs_lengthening {
314                        let target_char = if needs_conversion {
315                            if target_style == CodeFenceStyle::Backtick {
316                                '`'
317                            } else {
318                                '~'
319                            }
320                        } else {
321                            fence_char
322                        };
323
324                        let prefix = &line[..marker.fence_start];
325                        let replacement = format!(
326                            "{prefix}{}{}",
327                            target_char.to_string().repeat(converted_fence_len),
328                            marker.rest
329                        );
330
331                        let fence_start = marker.fence_start;
332                        let fence_end = fence_start + fence_len;
333                        let (start_line, start_col, end_line, end_col) =
334                            calculate_match_range(line_num + 1, line, fence_start, fence_end - fence_start);
335
336                        let message = if needs_conversion {
337                            format!(
338                                "Code fence style: use {} instead of {}",
339                                if target_style == CodeFenceStyle::Backtick {
340                                    "```"
341                                } else {
342                                    "~~~"
343                                },
344                                if fence_char == '`' { "```" } else { "~~~" }
345                            )
346                        } else {
347                            format!(
348                                "Code fence length is ambiguous: closing fence ({fence_len} {}) \
349                                 must match the lengthened outer fence; use {converted_fence_len}",
350                                if fence_char == '`' { "backticks" } else { "tildes" },
351                            )
352                        };
353
354                        warnings.push(LintWarning {
355                            rule_name: Some(self.name().to_string()),
356                            message,
357                            line: start_line,
358                            column: start_col,
359                            end_line,
360                            end_column: end_col,
361                            severity: Severity::Warning,
362                            fix: Some(Fix {
363                                range: line_index.line_col_to_byte_range_with_length(line_num + 1, 1, line.len()),
364                                replacement,
365                            }),
366                        });
367                    }
368
369                    in_code_block = false;
370                    code_block_fence_len = 0;
371                    converted_fence_len = 0;
372                    needs_lengthening = false;
373                }
374                // Lines inside the block that are not the closing fence are left alone.
375            }
376        }
377
378        Ok(warnings)
379    }
380
381    /// Check if this rule should be skipped for performance
382    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
383        // Skip if content is empty or has no code fence markers
384        ctx.content.is_empty() || (!ctx.likely_has_code() && !ctx.has_char('~'))
385    }
386
387    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
388        let content = ctx.content;
389
390        let target_style = match self.config.style {
391            CodeFenceStyle::Consistent => self.detect_style(ctx).unwrap_or(CodeFenceStyle::Backtick),
392            _ => self.config.style,
393        };
394
395        let lines: Vec<&str> = content.lines().collect();
396        let mut result = String::new();
397        let mut in_code_block = false;
398        let mut code_block_fence_char = '`';
399        let mut code_block_fence_len = 0usize;
400        let mut converted_fence_len = 0usize;
401        let mut needs_lengthening = false;
402
403        for (line_idx, &line) in lines.iter().enumerate() {
404            if let Some(marker) = parse_fence_marker(line) {
405                let fence_char = marker.fence_char;
406                let fence_len = marker.fence_len;
407
408                if !in_code_block {
409                    in_code_block = true;
410                    code_block_fence_char = fence_char;
411                    code_block_fence_len = fence_len;
412
413                    let needs_conversion = (fence_char == '`' && target_style == CodeFenceStyle::Tilde)
414                        || (fence_char == '~' && target_style == CodeFenceStyle::Backtick);
415
416                    let prefix = &line[..marker.fence_start];
417                    let info = marker.rest;
418
419                    if needs_conversion {
420                        let target_char = if target_style == CodeFenceStyle::Backtick {
421                            '`'
422                        } else {
423                            '~'
424                        };
425
426                        let max_inner =
427                            max_inner_fence_length_of_char(&lines, line_idx, fence_len, fence_char, target_char);
428                        converted_fence_len = fence_len.max(max_inner + 1);
429                        needs_lengthening = false;
430
431                        result.push_str(prefix);
432                        result.push_str(&target_char.to_string().repeat(converted_fence_len));
433                        result.push_str(info);
434                    } else {
435                        // Already the correct style. Check for fence-length ambiguity.
436                        let max_inner =
437                            max_inner_fence_length_of_char(&lines, line_idx, fence_len, fence_char, fence_char);
438                        if max_inner >= fence_len {
439                            converted_fence_len = max_inner + 1;
440                            needs_lengthening = true;
441
442                            result.push_str(prefix);
443                            result.push_str(&fence_char.to_string().repeat(converted_fence_len));
444                            result.push_str(info);
445                        } else {
446                            converted_fence_len = fence_len;
447                            needs_lengthening = false;
448                            result.push_str(line);
449                        }
450                    }
451                } else {
452                    // Inside a code block — check if this is the closing fence.
453                    let is_closing = is_closing_fence(marker, code_block_fence_char, code_block_fence_len);
454
455                    if is_closing {
456                        let needs_conversion = (fence_char == '`' && target_style == CodeFenceStyle::Tilde)
457                            || (fence_char == '~' && target_style == CodeFenceStyle::Backtick);
458
459                        if needs_conversion || needs_lengthening {
460                            let target_char = if needs_conversion {
461                                if target_style == CodeFenceStyle::Backtick {
462                                    '`'
463                                } else {
464                                    '~'
465                                }
466                            } else {
467                                fence_char
468                            };
469                            let prefix = &line[..marker.fence_start];
470                            result.push_str(prefix);
471                            result.push_str(&target_char.to_string().repeat(converted_fence_len));
472                            result.push_str(marker.rest);
473                        } else {
474                            result.push_str(line);
475                        }
476
477                        in_code_block = false;
478                        code_block_fence_len = 0;
479                        converted_fence_len = 0;
480                        needs_lengthening = false;
481                    } else {
482                        // Inside block, not the closing fence — preserve as-is.
483                        result.push_str(line);
484                    }
485                }
486            } else {
487                result.push_str(line);
488            }
489            result.push('\n');
490        }
491
492        // Remove the last newline if the original content didn't end with one
493        if !content.ends_with('\n') && result.ends_with('\n') {
494            result.pop();
495        }
496
497        Ok(result)
498    }
499
500    fn as_any(&self) -> &dyn std::any::Any {
501        self
502    }
503
504    fn default_config_section(&self) -> Option<(String, toml::Value)> {
505        let json_value = serde_json::to_value(&self.config).ok()?;
506        Some((
507            self.name().to_string(),
508            crate::rule_config_serde::json_to_toml_value(&json_value)?,
509        ))
510    }
511
512    fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
513    where
514        Self: Sized,
515    {
516        let rule_config = crate::rule_config_serde::load_rule_config::<MD048Config>(config);
517        Box::new(Self::from_config_struct(rule_config))
518    }
519}
520
521#[cfg(test)]
522mod tests {
523    use super::*;
524    use crate::lint_context::LintContext;
525
526    #[test]
527    fn test_backtick_style_with_backticks() {
528        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
529        let content = "```\ncode\n```";
530        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
531        let result = rule.check(&ctx).unwrap();
532
533        assert_eq!(result.len(), 0);
534    }
535
536    #[test]
537    fn test_backtick_style_with_tildes() {
538        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
539        let content = "~~~\ncode\n~~~";
540        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
541        let result = rule.check(&ctx).unwrap();
542
543        assert_eq!(result.len(), 2); // Opening and closing fence
544        assert!(result[0].message.contains("use ``` instead of ~~~"));
545        assert_eq!(result[0].line, 1);
546        assert_eq!(result[1].line, 3);
547    }
548
549    #[test]
550    fn test_tilde_style_with_tildes() {
551        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Tilde);
552        let content = "~~~\ncode\n~~~";
553        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
554        let result = rule.check(&ctx).unwrap();
555
556        assert_eq!(result.len(), 0);
557    }
558
559    #[test]
560    fn test_tilde_style_with_backticks() {
561        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Tilde);
562        let content = "```\ncode\n```";
563        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
564        let result = rule.check(&ctx).unwrap();
565
566        assert_eq!(result.len(), 2); // Opening and closing fence
567        assert!(result[0].message.contains("use ~~~ instead of ```"));
568    }
569
570    #[test]
571    fn test_consistent_style_tie_prefers_backtick() {
572        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Consistent);
573        // One backtick fence and one tilde fence - tie should prefer backticks
574        let content = "```\ncode\n```\n\n~~~\nmore code\n~~~";
575        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
576        let result = rule.check(&ctx).unwrap();
577
578        // Backticks win due to tie-breaker, so tildes should be flagged
579        assert_eq!(result.len(), 2);
580        assert_eq!(result[0].line, 5);
581        assert_eq!(result[1].line, 7);
582    }
583
584    #[test]
585    fn test_consistent_style_tilde_most_prevalent() {
586        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Consistent);
587        // Two tilde fences and one backtick fence - tildes are most prevalent
588        let content = "~~~\ncode\n~~~\n\n```\nmore code\n```\n\n~~~\neven more\n~~~";
589        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
590        let result = rule.check(&ctx).unwrap();
591
592        // Tildes are most prevalent, so backticks should be flagged
593        assert_eq!(result.len(), 2);
594        assert_eq!(result[0].line, 5);
595        assert_eq!(result[1].line, 7);
596    }
597
598    #[test]
599    fn test_detect_style_backtick() {
600        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Consistent);
601        let ctx = LintContext::new("```\ncode\n```", crate::config::MarkdownFlavor::Standard, None);
602        let style = rule.detect_style(&ctx);
603
604        assert_eq!(style, Some(CodeFenceStyle::Backtick));
605    }
606
607    #[test]
608    fn test_detect_style_tilde() {
609        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Consistent);
610        let ctx = LintContext::new("~~~\ncode\n~~~", crate::config::MarkdownFlavor::Standard, None);
611        let style = rule.detect_style(&ctx);
612
613        assert_eq!(style, Some(CodeFenceStyle::Tilde));
614    }
615
616    #[test]
617    fn test_detect_style_none() {
618        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Consistent);
619        let ctx = LintContext::new("No code fences here", crate::config::MarkdownFlavor::Standard, None);
620        let style = rule.detect_style(&ctx);
621
622        assert_eq!(style, None);
623    }
624
625    #[test]
626    fn test_fix_backticks_to_tildes() {
627        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Tilde);
628        let content = "```\ncode\n```";
629        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
630        let fixed = rule.fix(&ctx).unwrap();
631
632        assert_eq!(fixed, "~~~\ncode\n~~~");
633    }
634
635    #[test]
636    fn test_fix_tildes_to_backticks() {
637        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
638        let content = "~~~\ncode\n~~~";
639        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
640        let fixed = rule.fix(&ctx).unwrap();
641
642        assert_eq!(fixed, "```\ncode\n```");
643    }
644
645    #[test]
646    fn test_fix_preserves_fence_length() {
647        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Tilde);
648        let content = "````\ncode with backtick\n```\ncode\n````";
649        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
650        let fixed = rule.fix(&ctx).unwrap();
651
652        assert_eq!(fixed, "~~~~\ncode with backtick\n```\ncode\n~~~~");
653    }
654
655    #[test]
656    fn test_fix_preserves_language_info() {
657        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
658        let content = "~~~rust\nfn main() {}\n~~~";
659        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
660        let fixed = rule.fix(&ctx).unwrap();
661
662        assert_eq!(fixed, "```rust\nfn main() {}\n```");
663    }
664
665    #[test]
666    fn test_indented_code_fences() {
667        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Tilde);
668        let content = "  ```\n  code\n  ```";
669        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
670        let result = rule.check(&ctx).unwrap();
671
672        assert_eq!(result.len(), 2);
673    }
674
675    #[test]
676    fn test_fix_indented_fences() {
677        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Tilde);
678        let content = "  ```\n  code\n  ```";
679        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
680        let fixed = rule.fix(&ctx).unwrap();
681
682        assert_eq!(fixed, "  ~~~\n  code\n  ~~~");
683    }
684
685    #[test]
686    fn test_nested_fences_not_changed() {
687        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Tilde);
688        let content = "```\ncode with ``` inside\n```";
689        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
690        let fixed = rule.fix(&ctx).unwrap();
691
692        assert_eq!(fixed, "~~~\ncode with ``` inside\n~~~");
693    }
694
695    #[test]
696    fn test_multiple_code_blocks() {
697        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
698        let content = "~~~\ncode1\n~~~\n\nText\n\n~~~python\ncode2\n~~~";
699        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
700        let result = rule.check(&ctx).unwrap();
701
702        assert_eq!(result.len(), 4); // 2 opening + 2 closing fences
703    }
704
705    #[test]
706    fn test_empty_content() {
707        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
708        let content = "";
709        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
710        let result = rule.check(&ctx).unwrap();
711
712        assert_eq!(result.len(), 0);
713    }
714
715    #[test]
716    fn test_preserve_trailing_newline() {
717        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
718        let content = "~~~\ncode\n~~~\n";
719        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
720        let fixed = rule.fix(&ctx).unwrap();
721
722        assert_eq!(fixed, "```\ncode\n```\n");
723    }
724
725    #[test]
726    fn test_no_trailing_newline() {
727        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
728        let content = "~~~\ncode\n~~~";
729        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
730        let fixed = rule.fix(&ctx).unwrap();
731
732        assert_eq!(fixed, "```\ncode\n```");
733    }
734
735    #[test]
736    fn test_default_config() {
737        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Consistent);
738        let (name, _config) = rule.default_config_section().unwrap();
739        assert_eq!(name, "MD048");
740    }
741
742    /// Tilde outer fence containing backtick inner fence: converting to backtick
743    /// style must use a longer fence (4 backticks) to preserve valid nesting.
744    #[test]
745    fn test_tilde_outer_with_backtick_inner_uses_longer_fence() {
746        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
747        let content = "~~~text\n```rust\ncode\n```\n~~~";
748        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
749        let fixed = rule.fix(&ctx).unwrap();
750
751        // The outer fence must be 4 backticks to disambiguate from the inner 3-backtick fences.
752        assert_eq!(fixed, "````text\n```rust\ncode\n```\n````");
753    }
754
755    /// check() warns about the outer tilde fences and the fix replacements use the
756    /// correct (longer) fence length.
757    #[test]
758    fn test_check_tilde_outer_with_backtick_inner_warns_with_correct_replacement() {
759        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
760        let content = "~~~text\n```rust\ncode\n```\n~~~";
761        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
762        let warnings = rule.check(&ctx).unwrap();
763
764        // Only the outer tilde fences are warned about; inner backtick fences are untouched.
765        assert_eq!(warnings.len(), 2);
766        let open_fix = warnings[0].fix.as_ref().unwrap();
767        let close_fix = warnings[1].fix.as_ref().unwrap();
768        assert_eq!(open_fix.replacement, "````text");
769        assert_eq!(close_fix.replacement, "````");
770    }
771
772    /// When the inner backtick fences use 4 backticks, the outer converted fence
773    /// must use at least 5.
774    #[test]
775    fn test_tilde_outer_with_longer_backtick_inner() {
776        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
777        let content = "~~~text\n````rust\ncode\n````\n~~~";
778        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
779        let fixed = rule.fix(&ctx).unwrap();
780
781        assert_eq!(fixed, "`````text\n````rust\ncode\n````\n`````");
782    }
783
784    /// Backtick outer fence containing tilde inner fence: converting to tilde
785    /// style must use a longer tilde fence.
786    #[test]
787    fn test_backtick_outer_with_tilde_inner_uses_longer_fence() {
788        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Tilde);
789        let content = "```text\n~~~rust\ncode\n~~~\n```";
790        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
791        let fixed = rule.fix(&ctx).unwrap();
792
793        assert_eq!(fixed, "~~~~text\n~~~rust\ncode\n~~~\n~~~~");
794    }
795
796    // -----------------------------------------------------------------------
797    // Fence-length ambiguity detection
798    // -----------------------------------------------------------------------
799
800    /// A backtick block containing only an info-string interior sequence (not bare)
801    /// is NOT ambiguous: info-string sequences cannot be closing fences per CommonMark,
802    /// so the bare ``` at line 3 is simply the closing fence — no lengthening needed.
803    #[test]
804    fn test_info_string_interior_not_ambiguous() {
805        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
806        // line 0: ```text   ← opens block (len=3, info="text")
807        // line 1: ```rust   ← interior content, has info "rust" → cannot close outer
808        // line 2: code
809        // line 3: ```       ← bare, len=3 >= 3 → closes block 1 (per CommonMark)
810        // line 4: ```       ← orphaned second block
811        let content = "```text\n```rust\ncode\n```\n```";
812        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
813        let warnings = rule.check(&ctx).unwrap();
814
815        // No ambiguity: ```rust cannot close the outer, and the bare ``` IS the
816        // unambiguous closing fence. No lengthening needed.
817        assert_eq!(warnings.len(), 0, "expected 0 warnings, got {warnings:?}");
818    }
819
820    /// fix() leaves a block with only info-string interior sequences unchanged.
821    #[test]
822    fn test_info_string_interior_fix_unchanged() {
823        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
824        let content = "```text\n```rust\ncode\n```\n```";
825        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
826        let fixed = rule.fix(&ctx).unwrap();
827
828        // No conversion needed (already backtick), no lengthening needed → unchanged.
829        assert_eq!(fixed, content);
830    }
831
832    /// Same for tilde style: an info-string tilde interior is not ambiguous.
833    #[test]
834    fn test_tilde_info_string_interior_not_ambiguous() {
835        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Tilde);
836        let content = "~~~text\n~~~rust\ncode\n~~~\n~~~";
837        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
838        let fixed = rule.fix(&ctx).unwrap();
839
840        // ~~~rust cannot close outer (has info); ~~~ IS the closing fence → unchanged.
841        assert_eq!(fixed, content);
842    }
843
844    /// No warning when the outer fence is already longer than any interior fence.
845    #[test]
846    fn test_no_ambiguity_when_outer_is_longer() {
847        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
848        let content = "````text\n```rust\ncode\n```\n````";
849        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
850        let warnings = rule.check(&ctx).unwrap();
851
852        assert_eq!(
853            warnings.len(),
854            0,
855            "should have no warnings when outer is already longer"
856        );
857    }
858
859    /// An outer block containing a longer info-string sequence and a bare closing
860    /// fence is not ambiguous: the bare closing fence closes the outer normally,
861    /// and the info-string sequence is just content.
862    #[test]
863    fn test_longer_info_string_interior_not_ambiguous() {
864        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
865        // line 0: ```text    ← opens block (len=3, info="text")
866        // line 1: `````rust  ← interior, 5 backticks with info → cannot close outer
867        // line 2: code
868        // line 3: `````      ← bare, len=5 >= 3, no info → closes block 1
869        // line 4: ```        ← orphaned second block
870        let content = "```text\n`````rust\ncode\n`````\n```";
871        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
872        let fixed = rule.fix(&ctx).unwrap();
873
874        // `````rust cannot close the outer. ````` IS the closing fence. No lengthening.
875        assert_eq!(fixed, content);
876    }
877
878    /// Consistent style: info-string interior sequences are not ambiguous.
879    #[test]
880    fn test_info_string_interior_consistent_style_no_warning() {
881        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Consistent);
882        let content = "```text\n```rust\ncode\n```\n```";
883        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
884        let warnings = rule.check(&ctx).unwrap();
885
886        assert_eq!(warnings.len(), 0);
887    }
888
889    // -----------------------------------------------------------------------
890    // Cross-style conversion: bare-only inner sequence counting
891    // -----------------------------------------------------------------------
892
893    /// Cross-style conversion where outer has NO info string: interior info-string
894    /// sequences are not counted, only bare sequences are.
895    #[test]
896    fn test_cross_style_bare_inner_requires_lengthening() {
897        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
898        // Outer tilde fence (no info). Interior has a 5-backtick info-string sequence
899        // AND a 3-backtick bare sequence. Only the bare sequence (len=3) is counted
900        // → outer becomes 4, not 6.
901        let content = "~~~\n`````rust\ncode\n```\n~~~";
902        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
903        let fixed = rule.fix(&ctx).unwrap();
904
905        // 4 backticks (bare seq len=3 → 3+1=4). The 5-backtick info-string seq is
906        // not counted since it cannot be a closing fence.
907        assert_eq!(fixed, "````\n`````rust\ncode\n```\n````");
908    }
909
910    /// Cross-style conversion where outer HAS an info string but interior has only
911    /// info-string sequences: no bare inner sequences means no lengthening needed.
912    /// The outer converts at its natural length.
913    #[test]
914    fn test_cross_style_info_only_interior_no_lengthening() {
915        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
916        // Outer tilde fence (info "text"). Interior has only info-string backtick
917        // sequences — no bare closing sequence. Info-string sequences cannot be
918        // closing fences, so no lengthening is needed → outer converts at len=3.
919        let content = "~~~text\n```rust\nexample\n```rust\n~~~";
920        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
921        let fixed = rule.fix(&ctx).unwrap();
922
923        assert_eq!(fixed, "```text\n```rust\nexample\n```rust\n```");
924    }
925
926    /// Same-style block where outer has an info string but interior contains only
927    /// bare sequences SHORTER than the outer fence: no ambiguity, no warning.
928    #[test]
929    fn test_same_style_info_outer_shorter_bare_interior_no_warning() {
930        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
931        // Outer is 4 backticks with info "text". Interior shows raw fence syntax
932        // (3-backtick bare lines). These are shorter than outer (3 < 4) so they
933        // cannot close the outer block → no ambiguity.
934        let content = "````text\n```\nshowing raw fence\n```\n````";
935        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
936        let warnings = rule.check(&ctx).unwrap();
937
938        assert_eq!(
939            warnings.len(),
940            0,
941            "shorter bare interior sequences cannot close a 4-backtick outer"
942        );
943    }
944
945    /// Same-style block where outer has NO info string and interior has shorter
946    /// bare sequences: no ambiguity, no warning.
947    #[test]
948    fn test_same_style_no_info_outer_shorter_bare_interior_no_warning() {
949        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
950        // Outer is 4 backticks (no info). Interior has 3-backtick bare sequences.
951        // 3 < 4 → they cannot close the outer block → no ambiguity.
952        let content = "````\n```\nsome code\n```\n````";
953        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
954        let warnings = rule.check(&ctx).unwrap();
955
956        assert_eq!(
957            warnings.len(),
958            0,
959            "shorter bare interior sequences cannot close a 4-backtick outer (no info)"
960        );
961    }
962
963    /// Regression: over-indented inner same-style sequence (4 spaces) is content,
964    /// not a closing fence, and must not trigger ambiguity warnings.
965    #[test]
966    fn test_overindented_inner_sequence_not_ambiguous() {
967        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
968        let content = "```text\n    ```\ncode\n```";
969        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
970        let warnings = rule.check(&ctx).unwrap();
971        let fixed = rule.fix(&ctx).unwrap();
972
973        assert_eq!(warnings.len(), 0, "over-indented inner fence should not warn");
974        assert_eq!(fixed, content, "over-indented inner fence should remain unchanged");
975    }
976
977    /// Regression: when converting outer style, over-indented same-style content
978    /// lines must not be mistaken for an outer closing fence.
979    #[test]
980    fn test_conversion_ignores_overindented_inner_sequence_for_closing_detection() {
981        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Backtick);
982        let content = "~~~text\n    ~~~\n```rust\ncode\n```\n~~~";
983        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
984        let fixed = rule.fix(&ctx).unwrap();
985
986        assert_eq!(fixed, "````text\n    ~~~\n```rust\ncode\n```\n````");
987    }
988
989    /// CommonMark: a top-level fence marker indented 4 spaces is an indented code
990    /// block line, not a fenced code block marker, so MD048 should ignore it.
991    #[test]
992    fn test_top_level_four_space_fence_marker_is_ignored() {
993        let rule = MD048CodeFenceStyle::new(CodeFenceStyle::Tilde);
994        let content = "    ```\n    code\n    ```";
995        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
996        let warnings = rule.check(&ctx).unwrap();
997        let fixed = rule.fix(&ctx).unwrap();
998
999        assert_eq!(warnings.len(), 0);
1000        assert_eq!(fixed, content);
1001    }
1002
1003    /// The combined MD013+MD048 fix must be idempotent: applying the fix twice
1004    /// must produce the same result as applying it once, and must not introduce
1005    /// double blank lines (MD012).
1006    #[test]
1007    fn test_fix_idempotent_no_double_blanks_with_nested_fences() {
1008        use crate::fix_coordinator::FixCoordinator;
1009        use crate::rules::Rule;
1010        use crate::rules::md013_line_length::MD013LineLength;
1011
1012        // This is the exact pattern that caused double blank lines when MD048 and
1013        // MD013 were applied together: a tilde outer fence with an inner backtick
1014        // fence inside a list item that is too long.
1015        let content = "\
1016- **edition**: Rust edition to use by default for the code snippets. Default is `\"2015\"`. \
1017Individual code blocks can be controlled with the `edition2015`, `edition2018`, `edition2021` \
1018or `edition2024` annotations, such as:
1019
1020  ~~~text
1021  ```rust,edition2015
1022  // This only works in 2015.
1023  let try = true;
1024  ```
1025  ~~~
1026
1027### Build options
1028";
1029        let rules: Vec<Box<dyn Rule>> = vec![
1030            Box::new(MD013LineLength::new(80, false, false, false, true)),
1031            Box::new(MD048CodeFenceStyle::new(CodeFenceStyle::Backtick)),
1032        ];
1033
1034        let mut first_pass = content.to_string();
1035        let coordinator = FixCoordinator::new();
1036        coordinator
1037            .apply_fixes_iterative(&rules, &[], &mut first_pass, &Default::default(), 10, None)
1038            .expect("fix should not fail");
1039
1040        // No double blank lines after first pass.
1041        let lines: Vec<&str> = first_pass.lines().collect();
1042        for i in 0..lines.len().saturating_sub(1) {
1043            assert!(
1044                !(lines[i].is_empty() && lines[i + 1].is_empty()),
1045                "Double blank at lines {},{} after first pass:\n{first_pass}",
1046                i + 1,
1047                i + 2
1048            );
1049        }
1050
1051        // Second pass must produce identical output (idempotent).
1052        let mut second_pass = first_pass.clone();
1053        let rules2: Vec<Box<dyn Rule>> = vec![
1054            Box::new(MD013LineLength::new(80, false, false, false, true)),
1055            Box::new(MD048CodeFenceStyle::new(CodeFenceStyle::Backtick)),
1056        ];
1057        let coordinator2 = FixCoordinator::new();
1058        coordinator2
1059            .apply_fixes_iterative(&rules2, &[], &mut second_pass, &Default::default(), 10, None)
1060            .expect("fix should not fail");
1061
1062        assert_eq!(
1063            first_pass, second_pass,
1064            "Fix is not idempotent:\nFirst pass:\n{first_pass}\nSecond pass:\n{second_pass}"
1065        );
1066    }
1067}