Skip to content

Commit 20c5727

Browse files
committed
feat(MD012): report one warning per blank-line run with its exact range
MD012 emitted one zero-width warning per excess blank line, so a run of five blank lines produced four diagnostics, four quick fixes, and editor markers that did not show what the fix would delete. Each run now yields a single warning whose half-open range equals the byte range its fix removes, line terminators included. The end-of-file warning starts at the first trailing blank line and ends at end of file. Messages state the counts, e.g. "(4 found, 1 allowed)", including the higher limit derived from MD022 next to headings. Inline suppression now uses LintWarning::covered_lines(), which treats a range ending at column 1 as not covering that line. A disable-line comment on the content after a run therefore does not suppress the run, and disable-next-line directly above trailing blank lines suppresses the whole end-of-file run rather than only a single trailing blank.
1 parent 7d09be6 commit 20c5727

11 files changed

Lines changed: 394 additions & 131 deletions

‎docs/md012.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ The two blank lines before `## Section` are allowed by MD012 because MD022 requi
102102

103103
If MD022 is disabled, MD012 enforces its own maximum everywhere without any special heading treatment. If MD022 uses `lines-above = -1` (unlimited), MD012 will not flag any blank lines above headings.
104104

105+
## How violations are reported
106+
107+
MD012 reports one warning per run of consecutive blank lines, not one per excess line. The message states how many blank lines the run holds and how many are allowed there, including any higher limit derived from MD022 next to a heading:
108+
109+
```text
110+
doc.md:4:1: [MD012] Multiple consecutive blank lines between content (4 found, 1 allowed) [*]
111+
doc.md:9:1: [MD012] Blank line at end of file (1 found, 0 allowed) [*]
112+
```
113+
114+
The warning's range covers exactly the lines its fix removes. It starts at the first excess line and ends at the start of the line after the last one, so editors highlight every line that will be deleted. At the end of a file the range starts at the first trailing blank line and runs to the end of the file. A single fix removes the whole run.
115+
116+
Because the range ends at the start of the next content line, a `rumdl-disable-line MD012` comment on that line does not suppress the blank lines before it. To suppress a run, wrap it in `<!-- rumdl-disable MD012 -->` and `<!-- rumdl-enable MD012 -->`, or put `<!-- rumdl-disable-next-line MD012 -->` directly above its first excess line.
117+
105118
## Automatic fixes
106119

107120
This rule automatically removes excess blank lines. Near headings, blanks are capped at the limit derived from MD022's configuration. Between non-heading content, blanks are capped at MD012's own maximum (default: 1).

‎src/lib.rs‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -452,17 +452,10 @@ pub(crate) fn retain_reportable_warnings(
452452
rule_name_to_check
453453
};
454454

455-
// Check if the rule is disabled at any line in the warning's range.
455+
// Check if the rule is disabled at any line the warning covers.
456456
// Multi-line warnings (e.g., reflow) report on the first line,
457457
// but inline disable comments may appear later in the range.
458-
// Guard: if end_line < line (e.g., end_line=0), fall back to
459-
// checking only the warning's line to match original behavior.
460-
let end = if warning.end_line >= warning.line {
461-
warning.end_line
462-
} else {
463-
warning.line
464-
};
465-
let disabled_at = (warning.line..=end).find_map(|line| {
458+
let disabled_at = warning.covered_lines().find_map(|line| {
466459
inline_config
467460
.disabling_layer(base_rule_name, line)
468461
.map(|layer| (line, layer))

‎src/rule.rs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@ pub struct LintWarning {
5353
pub rule_name: Option<String>,
5454
}
5555

56+
impl LintWarning {
57+
/// The 1-indexed lines whose content this warning covers, used to decide
58+
/// whether an inline disable comment suppresses it.
59+
///
60+
/// `end_column` is exclusive, so a range ending at column 1 of a later line
61+
/// stops at that line's start and covers none of it: a warning spanning
62+
/// whole lines with their terminators ends there. That line is excluded,
63+
/// so a `disable-line` comment on the content after a flagged run does not
64+
/// suppress the run. An `end_line` before `line` falls back to `line` alone.
65+
pub fn covered_lines(&self) -> std::ops::RangeInclusive<usize> {
66+
let end = if self.end_line > self.line && self.end_column <= 1 {
67+
self.end_line - 1
68+
} else {
69+
self.end_line.max(self.line)
70+
};
71+
self.line..=end
72+
}
73+
}
74+
5675
/// One atomic fix attached to a `LintWarning`.
5776
///
5877
/// `range`/`replacement` describe the primary edit. `additional_edits`

0 commit comments

Comments
 (0)
⌂ 换网址