1use crate::inline_config::{DisableSite, collect_disable_sites, normalize_rule_name};
27use crate::lint_context::LintContext;
28use crate::rule::{FixCapability, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity, SuppressionReport};
29
30#[derive(Debug, Clone, Default)]
31pub struct MD087UnusedDisableComment;
32
33impl MD087UnusedDisableComment {
34 pub fn new() -> Self {
35 Self
36 }
37
38 fn unused_rules(&self, site: &DisableSite, report: &SuppressionReport) -> Vec<String> {
43 let mut unused: Vec<String> = Vec::new();
44
45 for written in &site.rules {
46 let canonical = normalize_rule_name(written);
50 if canonical == self.name() || !report.judged_rules.contains(&canonical) {
53 continue;
54 }
55 let used = report
56 .suppressed
57 .iter()
58 .any(|warning| warning.rule_name == canonical && site.scope.carries(warning.layer, warning.line));
59 if !used && !unused.contains(written) {
60 unused.push(written.clone());
61 }
62 }
63
64 unused
65 }
66
67 fn warning(&self, ctx: &LintContext, site: &DisableSite, unused: &[String]) -> LintWarning {
68 let line_offset = ctx.line_info(site.line).map_or(0, |info| info.byte_offset);
69 let (line, column) = ctx.offset_to_line_col(line_offset + site.span.start);
70 let (_, end_column) = ctx.offset_to_line_col(line_offset + site.span.end);
71 let names = unused.join(", ");
72 let message = if site.kind == "configure-file" {
75 format!("Unused configure-file disable: {names}")
76 } else {
77 format!("Unused {} comment: {names}", site.kind)
78 };
79 LintWarning {
80 rule_name: Some(self.name().to_string()),
81 severity: Severity::Warning,
82 line,
83 column,
84 end_line: line,
85 end_column,
86 message,
87 fix: None,
88 }
89 }
90}
91
92impl Rule for MD087UnusedDisableComment {
93 fn name(&self) -> &'static str {
94 "MD087"
95 }
96
97 fn description(&self) -> &'static str {
98 "Inline disable comments should suppress something"
99 }
100
101 fn category(&self) -> RuleCategory {
102 RuleCategory::Other
103 }
104
105 fn should_skip(&self, ctx: &LintContext) -> bool {
106 !ctx.content.contains("<!--")
107 }
108
109 fn check(&self, _ctx: &LintContext) -> LintResult {
110 Ok(Vec::new())
114 }
115
116 fn observes_suppressions(&self) -> bool {
117 true
118 }
119
120 fn check_suppressions(&self, ctx: &LintContext, report: &SuppressionReport) -> LintResult {
121 let mut warnings = Vec::new();
122 for site in collect_disable_sites(ctx.content, &ctx.code_blocks) {
123 let unused = self.unused_rules(&site, report);
124 if unused.is_empty() {
125 continue;
126 }
127 warnings.push(self.warning(ctx, &site, &unused));
128 }
129 Ok(warnings)
130 }
131
132 fn fix_capability(&self) -> FixCapability {
133 FixCapability::Unfixable
134 }
135
136 fn fix(&self, ctx: &LintContext) -> Result<String, LintError> {
137 Ok(ctx.content.to_string())
138 }
139
140 fn as_any(&self) -> &dyn std::any::Any {
141 self
142 }
143
144 fn from_config(_config: &crate::config::Config) -> Box<dyn Rule>
145 where
146 Self: Sized,
147 {
148 Box::new(Self)
149 }
150}
151
152#[cfg(test)]
153mod tests {
154 use super::*;
155 use crate::config::MarkdownFlavor;
156 use crate::inline_config::DisableLayer;
157 use crate::rule::SuppressedWarning;
158 use std::collections::HashSet;
159
160 fn report(suppressed: &[(&str, DisableLayer, usize)], judged: &[&str]) -> SuppressionReport {
161 SuppressionReport {
162 suppressed: suppressed
163 .iter()
164 .map(|&(rule_name, layer, line)| SuppressedWarning {
165 rule_name: rule_name.to_string(),
166 line,
167 layer,
168 })
169 .collect(),
170 judged_rules: judged.iter().map(|name| (*name).to_string()).collect::<HashSet<_>>(),
171 }
172 }
173
174 fn check(content: &str, suppressed: &[(&str, DisableLayer, usize)], judged: &[&str]) -> Vec<LintWarning> {
175 check_in(MarkdownFlavor::Standard, content, suppressed, judged)
176 }
177
178 fn check_in(
179 flavor: MarkdownFlavor,
180 content: &str,
181 suppressed: &[(&str, DisableLayer, usize)],
182 judged: &[&str],
183 ) -> Vec<LintWarning> {
184 let ctx = LintContext::new(content, flavor, None);
185 MD087UnusedDisableComment::new()
186 .check_suppressions(&ctx, &report(suppressed, judged))
187 .unwrap()
188 }
189
190 #[test]
191 fn reports_a_disable_line_comment_that_suppressed_nothing() {
192 let content = "# Title\n\nA short line <!-- rumdl-disable-line MD013 -->\n";
193 let warnings = check(content, &[], &["MD013"]);
194 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
195 assert_eq!(warnings[0].message, "Unused disable-line comment: MD013");
196 assert_eq!((warnings[0].line, warnings[0].column), (3, 14));
197 assert_eq!(warnings[0].end_column, 47, "the warning spans the comment");
198 assert!(
199 warnings[0].fix.is_none(),
200 "removing an authored comment is not automatic"
201 );
202 }
203
204 #[test]
205 fn keeps_quiet_when_the_comment_suppressed_a_finding() {
206 let content = "# Title\n\nA short line <!-- rumdl-disable-line MD013 -->\n";
207 let warnings = check(content, &[("MD013", DisableLayer::Line, 3)], &["MD013"]);
208 assert!(warnings.is_empty(), "got: {warnings:?}");
209 }
210
211 #[test]
212 fn a_disable_line_comment_is_judged_on_its_own_line_only() {
213 let content = "<!-- rumdl-disable-line MD013 -->\nA long line\n";
214 let warnings = check(content, &[("MD013", DisableLayer::Line, 2)], &["MD013"]);
215 assert_eq!(warnings.len(), 1, "a finding on line 2 is not this comment's doing");
216 assert_eq!(warnings[0].line, 1);
217 }
218
219 #[test]
220 fn a_disable_next_line_comment_is_judged_on_the_following_line() {
221 let content = "<!-- rumdl-disable-next-line MD013 -->\nA long line\n";
222 assert!(
223 check(content, &[("MD013", DisableLayer::Line, 2)], &["MD013"]).is_empty(),
224 "the suppression on line 2 is what the comment is for"
225 );
226 let warnings = check(content, &[("MD013", DisableLayer::Line, 1)], &["MD013"]);
227 assert_eq!(warnings.len(), 1, "a finding on line 1 is not this comment's doing");
228 assert_eq!(warnings[0].message, "Unused disable-next-line comment: MD013");
229 }
230
231 #[test]
232 fn a_block_disable_reaches_the_end_of_the_document() {
233 let content = "<!-- rumdl-disable MD013 -->\n\ntext\n\n<!-- rumdl-enable MD013 -->\n\nmore\n";
234 assert!(
235 check(content, &[("MD013", DisableLayer::Block, 7)], &["MD013"]).is_empty(),
236 "a scope wider than the truth may only under-report"
237 );
238 let warnings = check(content, &[], &["MD013"]);
239 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
240 assert_eq!(warnings[0].message, "Unused disable comment: MD013");
241 }
242
243 #[test]
244 fn a_disable_file_comment_covers_a_finding_above_it() {
245 let content = "A long line\n\n<!-- rumdl-disable-file MD013 -->\n";
246 assert!(
247 check(content, &[("MD013", DisableLayer::File, 1)], &["MD013"]).is_empty(),
248 "disable-file applies to the whole document, including lines above it"
249 );
250 }
251
252 #[test]
253 fn a_comment_a_wider_one_already_covers_is_reported() {
254 let content = "<!-- rumdl-disable-file MD013 -->\n\nA long line <!-- rumdl-disable-line MD013 -->\n";
255 let warnings = check(content, &[("MD013", DisableLayer::File, 3)], &["MD013"]);
256 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
257 assert_eq!(warnings[0].message, "Unused disable-line comment: MD013");
258 assert_eq!(warnings[0].line, 3, "the file-wide comment is the one doing the work");
259 }
260
261 #[test]
262 fn the_wider_comment_is_the_one_reported_when_the_narrow_one_does_the_work() {
263 let content = "<!-- rumdl-disable MD013 -->\n<!-- rumdl-enable MD013 -->\nA long line <!-- rumdl-disable-line MD013 -->\n";
266 let warnings = check(content, &[("MD013", DisableLayer::Line, 3)], &["MD013"]);
267 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
268 assert_eq!(warnings[0].message, "Unused disable comment: MD013");
269 assert_eq!(warnings[0].line, 1);
270 }
271
272 #[test]
273 fn only_the_unused_names_of_a_multi_rule_comment_are_reported() {
274 let content = "text <!-- rumdl-disable-line MD013 MD033 MD009 -->\n";
275 let warnings = check(
276 content,
277 &[("MD033", DisableLayer::Line, 1)],
278 &["MD009", "MD013", "MD033"],
279 );
280 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
281 assert_eq!(warnings[0].message, "Unused disable-line comment: MD013, MD009");
282 }
283
284 #[test]
285 fn a_rule_the_run_does_not_carry_is_not_judged() {
286 let content = "text <!-- rumdl-disable-line MD013 -->\n";
287 assert!(
288 check(content, &[], &["MD009"]).is_empty(),
289 "MD013 produced nothing because it did not run"
290 );
291 }
292
293 #[test]
294 fn an_unknown_rule_name_is_not_judged() {
295 let content = "text <!-- rumdl-disable-line MD999 -->\n";
296 assert!(
297 check(content, &[], &["MD013"]).is_empty(),
298 "MD999 is not a rule the run carries"
299 );
300 }
301
302 #[test]
303 fn a_comment_naming_no_rule_is_never_reported() {
304 let content = "text <!-- rumdl-disable-line -->\n<!-- rumdl-disable -->\n";
305 assert!(
306 check(content, &[], &["MD013"]).is_empty(),
307 "a bare comment disables rules this run may not carry"
308 );
309 }
310
311 #[test]
312 fn prettier_ignore_belongs_to_another_formatter() {
313 let content = "<!-- prettier-ignore -->\n| a | b |\n";
314 assert!(
315 check(content, &[], &["MD013"]).is_empty(),
316 "not rumdl's comment to judge"
317 );
318 }
319
320 #[test]
321 fn a_comment_inside_a_code_block_configures_nothing() {
322 let content = "# Title\n\n```markdown\n<!-- rumdl-disable-line MD013 -->\n```\n";
323 assert!(
324 check(content, &[], &["MD013"]).is_empty(),
325 "a fenced example documents a comment rather than writing one"
326 );
327 }
328
329 #[test]
330 fn a_comment_in_an_indented_container_body_is_judged() {
331 let content = "# Title\n\n!!! example\n\n A short line <!-- rumdl-disable-line MD013 -->\n";
335 let warnings = check_in(MarkdownFlavor::MkDocs, content, &[], &["MD013"]);
336 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
337 assert_eq!(warnings[0].line, 5);
338 assert!(
339 check_in(MarkdownFlavor::Standard, content, &[], &["MD013"]).is_empty(),
340 "without admonitions the same lines are an indented code block"
341 );
342 }
343
344 #[test]
345 fn an_alias_is_reported_as_the_author_wrote_it() {
346 let content = "text <!-- rumdl-disable-line line-length -->\n";
347 let warnings = check(content, &[], &["MD013"]);
348 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
349 assert_eq!(warnings[0].message, "Unused disable-line comment: line-length");
350 }
351
352 #[test]
353 fn a_markdownlint_comment_is_judged_the_same_way() {
354 let content = "text <!-- markdownlint-disable-line MD013 -->\n";
355 let warnings = check(content, &[], &["MD013"]);
356 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
357 assert_eq!(warnings[0].message, "Unused disable-line comment: MD013");
358 }
359
360 #[test]
361 fn a_configure_file_entry_turning_a_rule_off_is_judged_like_a_disable() {
362 let content = "<!-- rumdl-configure-file { \"MD013\": false } -->\n\ntext\n";
363 let warnings = check(content, &[], &["MD013"]);
364 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
365 assert_eq!(warnings[0].message, "Unused configure-file disable: MD013");
366 assert!(
367 check(content, &[("MD013", DisableLayer::File, 3)], &["MD013"]).is_empty(),
368 "the entry turned the rule off for the whole file"
369 );
370 }
371
372 #[test]
373 fn a_configure_file_entry_carrying_options_is_not_a_disable() {
374 let content = "<!-- rumdl-configure-file { \"MD013\": { \"line_length\": 200 } } -->\n\ntext\n";
375 assert!(
376 check(content, &[], &["MD013"]).is_empty(),
377 "configuring a rule is not suppressing it"
378 );
379 }
380
381 #[test]
382 fn this_rule_never_judges_a_comment_silencing_itself() {
383 let content = "text <!-- rumdl-disable-line MD087 -->\n";
384 assert!(
385 check(content, &[], &["MD013", "MD087"]).is_empty(),
386 "MD087 findings are raised after the report is assembled"
387 );
388 }
389
390 #[test]
391 fn the_column_is_measured_in_characters() {
392 let content = "héllo wörld <!-- rumdl-disable-line MD013 -->\n";
393 let warnings = check(content, &[], &["MD013"]);
394 assert_eq!(warnings.len(), 1, "got: {warnings:?}");
395 assert_eq!(warnings[0].column, 13, "two multi-byte characters precede the comment");
396 }
397
398 #[test]
399 fn check_reports_nothing_on_its_own() {
400 let content = "text <!-- rumdl-disable-line MD013 -->\n";
401 let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
402 assert!(
403 MD087UnusedDisableComment::new().check(&ctx).unwrap().is_empty(),
404 "the verdict needs the run's suppressions"
405 );
406 }
407}