Skip to content

Commit ef926b7

Browse files
shuvamkclaudervben
authored
fix(md022): use the same list test below a heading in check and fix (#790)
On `text\n# Heading\n - deeper\n`, `rumdl check -e MD022` reports exactly one warning, "Expected 1 blank line above heading", and offers one fix. `rumdl check --fix -e MD022` then prints "Fixed 1/1 issues" but writes `text\n\n# Heading\n\n - deeper\n` - a blank line above the heading *and* one below it. The formatter applies an edit that check never advertised. The LSP quick-fix path applies the fix attached to the warning rather than calling `rule.fix()`, so it inserts only the blank above, and the CLI and the editor produce different documents from the same warning. The default rule set does not mask this: MD041 and MD046 also fire on the input, but neither is fixable here, so `rumdl fmt` makes both insertions. ` - deeper` is an indented code block, so the parser leaves `list_item` as None. check() (:544) therefore also tests the line syntactically with `starts_with_list_marker`, to keep the blank-below guard parse-stable - the comment above it says "so the guard - and therefore the fix - stays idempotent". fix_content() (:296) tested only `list_item`, saw a plain line, and inserted the blank. The same term is added to fix_content() so both sides ask the same question. `rumdl fmt` output for that input changes from text\n\n# Heading\n\n - deeper\n to text\n\n# Heading\n - deeper\n Both parse identically - MD046 reports the indented code block either way - so nothing about the rendered document changes. Only the unadvertised edit goes away. Measured over 69904 synthetic documents (every sequence of 1-4 lines over a 16-line alphabet, Standard flavour), comparing `rule.fix()` against `apply_warning_fixes` over `rule.check()`: 5753 divergences before, 5166 after - 587 removed, 0 introduced. Documents needing a second `fix()` pass: 272 before, 272 after. MD032's divergence set over the same corpus is bit-identical. The 5166 that remain are three pre-existing classes this does not touch: 4368 end with a blank line, 538 contain an HTML comment, 260 contain a blockquote. Tests: two MD022 cases in tests/rules/md022_test.rs, one case in the CLI/LSP consistency harness, and the MD022 entry in `get_test_content_for_rule` switched to content that reaches the branch. Co-authored-by: Claude Opus 5 <[email protected]> Co-authored-by: Ruben J. Jongejan <[email protected]>
1 parent 6099a6c commit ef926b7

3 files changed

Lines changed: 42 additions & 6 deletions

File tree

‎src/rules/md022_blanks_around_headings.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,16 +295,16 @@ impl MD022BlanksAroundHeadings {
295295
// Check if the next non-blank line is special (code fence or list item)
296296
let next_is_special = if let Some(idx) = next_content_line_idx {
297297
let next_line = &ctx.lines[idx];
298-
next_line.list_item.is_some() || {
299-
let trimmed = next_line.content(ctx.content).trim();
300-
(trimmed.starts_with("```") || trimmed.starts_with("~~~"))
298+
let trimmed = next_line.content(ctx.content).trim();
299+
next_line.list_item.is_some()
300+
|| starts_with_list_marker(trimmed)
301+
|| ((trimmed.starts_with("```") || trimmed.starts_with("~~~"))
301302
&& (trimmed.len() == 3
302303
|| (trimmed.len() > 3
303304
&& trimmed
304305
.chars()
305306
.nth(3)
306-
.is_some_and(|c| c.is_whitespace() || c.is_alphabetic())))
307-
}
307+
.is_some_and(|c| c.is_whitespace() || c.is_alphabetic()))))
308308
} else {
309309
false
310310
};

‎tests/cli/cli_lsp_fix_consistency.rs‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,25 @@ fn test_md065_blanks_around_horizontal_rules_consistency() {
263263
}
264264
}
265265

266+
#[test]
267+
fn test_md022_blanks_around_headings_consistency() {
268+
let rule = MD022BlanksAroundHeadings::default();
269+
270+
let test_cases = vec![
271+
("Text\n# Heading\nMore text", "Heading surrounded by paragraphs"),
272+
(
273+
"text\n# Heading\n - deeper\n",
274+
"Heading followed by an indented list marker",
275+
),
276+
("text\n# Heading\n- item\n", "Heading followed by a list item"),
277+
("text\n# Heading\n```\ncode\n```\n", "Heading followed by a code fence"),
278+
];
279+
280+
for (content, description) in test_cases {
281+
test_cli_lsp_consistency(&rule, content, &format!("MD022: {description}"));
282+
}
283+
}
284+
266285
/// Create appropriate test content for each rule based on what it checks
267286
fn get_test_content_for_rule(rule_name: &str) -> Option<&'static str> {
268287
match rule_name {
@@ -283,7 +302,7 @@ fn get_test_content_for_rule(rule_name: &str) -> Option<&'static str> {
283302
"MD019" => Some("## Multiple spaces"),
284303
"MD020" => Some("##No space in closed##"),
285304
"MD021" => Some("## Multiple spaces ##"),
286-
"MD022" => Some("Text\n# Heading\nMore text"),
305+
"MD022" => Some("text\n# Heading\n - deeper\n"),
287306
"MD023" => Some(" # Indented heading"),
288307
"MD024" => Some("# Duplicate\n# Duplicate"),
289308
"MD025" => Some("# First\n# Second H1"),

‎tests/rules/md022_test.rs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,3 +622,20 @@ fn test_roundtrip_code_block_heading() {
622622
&rule,
623623
);
624624
}
625+
626+
#[test]
627+
fn test_roundtrip_heading_followed_by_indented_list_marker() {
628+
let rule = MD022BlanksAroundHeadings::default();
629+
assert_check_fix_roundtrip("text\n# Heading\n - deeper\n", &rule);
630+
}
631+
632+
#[test]
633+
fn test_no_blank_below_heading_followed_by_indented_list_marker() {
634+
let rule = MD022BlanksAroundHeadings::default();
635+
let content = "text\n# Heading\n - deeper\n";
636+
let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
637+
let result = rule.check(&ctx).unwrap();
638+
assert_eq!(result.len(), 1);
639+
assert!(result[0].message.contains("above"), "got: {:?}", result[0].message);
640+
assert_eq!(rule.fix(&ctx).unwrap(), "text\n\n# Heading\n - deeper\n");
641+
}

0 commit comments

Comments
 (0)