Skip to content

Commit 60bae56

Browse files
committed
fix(MD032): preserve line endings in document fixes
1 parent 56903ee commit 60bae56

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

‎src/rules/md032_blanks_around_lists.rs‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,10 @@ impl MD032BlanksAroundLists {
991991
}
992992
}
993993

994+
if insertions.is_empty() && lazy_fixes.is_empty() {
995+
return ctx.content.to_string();
996+
}
997+
994998
// Phase 2: Reconstruct with insertions and lazy fixes
995999
let mut result_lines: Vec<String> = Vec::with_capacity(num_lines + insertions.len());
9961000
for (i, line) in lines.iter().enumerate() {
@@ -1013,9 +1017,10 @@ impl MD032BlanksAroundLists {
10131017
}
10141018

10151019
// Preserve the final newline if the original content had one
1016-
let mut result = result_lines.join("\n");
1020+
let line_ending = crate::utils::detect_line_ending(ctx.content);
1021+
let mut result = result_lines.join(line_ending);
10171022
if ctx.content.ends_with('\n') {
1018-
result.push('\n');
1023+
result.push_str(line_ending);
10191024
}
10201025
result
10211026
}
@@ -1787,6 +1792,29 @@ mod tests {
17871792
assert_eq!(warnings_after_fix.len(), 0, "No warnings should remain after fix");
17881793
}
17891794

1795+
#[test]
1796+
fn test_fix_preserves_crlf_and_matches_diagnostic_edits() {
1797+
let rule = MD032BlanksAroundLists::default();
1798+
for (content, expected) in [
1799+
("Text\r\n- item\r\n", "Text\r\n\r\n- item\r\n"),
1800+
(
1801+
"> > - item\r\n> > ~~~\r\n> > code\r\n> > ~~~",
1802+
"> > - item\r\n> >\r\n> > ~~~\r\n> > code\r\n> > ~~~",
1803+
),
1804+
("Text\r\n\r\n- item\r\n", "Text\r\n\r\n- item\r\n"),
1805+
("Text\r\n\n- item\r\n", "Text\r\n\n- item\r\n"),
1806+
] {
1807+
let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1808+
let warnings = rule.check(&ctx).unwrap();
1809+
let edited = crate::utils::fix_utils::apply_warning_fixes(content, &warnings).unwrap();
1810+
assert_eq!(edited, expected);
1811+
assert_eq!(rule.fix(&ctx).unwrap(), expected);
1812+
let fixed_ctx = LintContext::new(expected, crate::config::MarkdownFlavor::Standard, None);
1813+
assert!(rule.check(&fixed_ctx).unwrap().is_empty());
1814+
assert_eq!(rule.fix(&fixed_ctx).unwrap(), expected);
1815+
}
1816+
}
1817+
17901818
#[test]
17911819
fn test_fix_with_normalized_line_endings() {
17921820
// In production, content is normalized to LF at I/O boundary

0 commit comments

Comments
 (0)