Skip to main content

rumdl_lib/rules/
mod.rs

1pub mod code_fence_utils;
2pub mod emphasis_style;
3pub mod front_matter_utils;
4pub mod heading_utils;
5pub mod strong_style;
6
7mod md001_heading_increment;
8mod md003_heading_style;
9pub mod md004_unordered_list_style;
10mod md005_list_indent;
11pub mod md007_ul_indent;
12mod md009_trailing_spaces;
13mod md010_no_hard_tabs;
14mod md011_no_reversed_links;
15pub mod md013_line_length;
16mod md014_commands_show_output;
17mod md024_no_duplicate_heading;
18mod md025_single_title;
19mod md026_no_trailing_punctuation;
20mod md027_multiple_spaces_blockquote;
21mod md028_no_blanks_blockquote;
22mod md029_ordered_list_prefix;
23pub mod md030_list_marker_space;
24mod md031_blanks_around_fences;
25mod md032_blanks_around_lists;
26mod md033_no_inline_html;
27mod md034_no_bare_urls;
28mod md035_hr_style;
29pub mod md036_no_emphasis_only_first;
30mod md037_spaces_around_emphasis;
31mod md038_no_space_in_code;
32mod md039_no_space_in_links;
33pub mod md040_fenced_code_language;
34mod md041_first_line_heading;
35mod md042_no_empty_links;
36mod md043_required_headings;
37mod md044_proper_names;
38mod md045_no_alt_text;
39mod md046_code_block_style;
40mod md047_single_trailing_newline;
41mod md048_code_fence_style;
42mod md049_emphasis_style;
43mod md050_strong_style;
44pub mod md051_link_fragments;
45mod md052_reference_links_images;
46mod md053_link_image_reference_definitions;
47mod md054_link_image_style;
48mod md055_table_pipe_style;
49mod md056_table_column_count;
50mod md058_blanks_around_tables;
51mod md059_link_text;
52mod md060_table_format;
53mod md061_forbidden_terms;
54mod md062_link_destination_whitespace;
55mod md063_heading_capitalization;
56mod md064_no_multiple_consecutive_spaces;
57mod md065_blanks_around_horizontal_rules;
58mod md066_footnote_validation;
59mod md067_footnote_definition_order;
60mod md068_empty_footnote_definition;
61mod md069_no_duplicate_list_markers;
62mod md070_nested_code_fence;
63mod md071_blank_line_after_frontmatter;
64mod md072_frontmatter_key_sort;
65mod md073_toc_validation;
66mod md074_mkdocs_nav;
67mod md075_orphaned_table_rows;
68mod md076_list_item_spacing;
69mod md077_list_continuation_indent;
70
71pub use code_fence_utils::CodeFenceStyle;
72pub use md001_heading_increment::MD001HeadingIncrement;
73pub use md003_heading_style::MD003HeadingStyle;
74pub use md004_unordered_list_style::MD004UnorderedListStyle;
75pub use md004_unordered_list_style::UnorderedListStyle;
76pub use md005_list_indent::MD005ListIndent;
77pub use md007_ul_indent::MD007ULIndent;
78pub use md009_trailing_spaces::MD009TrailingSpaces;
79pub use md010_no_hard_tabs::MD010NoHardTabs;
80pub use md011_no_reversed_links::MD011NoReversedLinks;
81pub use md013_line_length::MD013Config;
82pub use md013_line_length::MD013LineLength;
83pub use md014_commands_show_output::MD014CommandsShowOutput;
84pub use md024_no_duplicate_heading::MD024NoDuplicateHeading;
85pub use md025_single_title::MD025SingleTitle;
86pub use md026_no_trailing_punctuation::MD026NoTrailingPunctuation;
87pub use md027_multiple_spaces_blockquote::MD027MultipleSpacesBlockquote;
88pub use md028_no_blanks_blockquote::MD028NoBlanksBlockquote;
89pub use md029_ordered_list_prefix::{ListStyle, MD029OrderedListPrefix};
90pub use md030_list_marker_space::MD030ListMarkerSpace;
91pub use md031_blanks_around_fences::MD031BlanksAroundFences;
92pub use md032_blanks_around_lists::MD032BlanksAroundLists;
93pub use md033_no_inline_html::MD033NoInlineHtml;
94pub use md034_no_bare_urls::MD034NoBareUrls;
95pub use md035_hr_style::MD035HRStyle;
96pub use md036_no_emphasis_only_first::MD036NoEmphasisAsHeading;
97pub use md037_spaces_around_emphasis::MD037NoSpaceInEmphasis;
98pub use md038_no_space_in_code::MD038NoSpaceInCode;
99pub use md039_no_space_in_links::MD039NoSpaceInLinks;
100pub use md040_fenced_code_language::MD040FencedCodeLanguage;
101pub use md041_first_line_heading::MD041FirstLineHeading;
102pub use md042_no_empty_links::MD042NoEmptyLinks;
103pub use md043_required_headings::MD043RequiredHeadings;
104pub use md044_proper_names::MD044ProperNames;
105pub use md045_no_alt_text::MD045NoAltText;
106pub use md046_code_block_style::{CodeBlockStyle, MD046CodeBlockStyle};
107pub use md047_single_trailing_newline::MD047SingleTrailingNewline;
108pub use md048_code_fence_style::MD048CodeFenceStyle;
109pub use md049_emphasis_style::MD049EmphasisStyle;
110pub use md050_strong_style::MD050StrongStyle;
111pub use md051_link_fragments::MD051LinkFragments;
112pub use md052_reference_links_images::MD052ReferenceLinkImages;
113pub use md053_link_image_reference_definitions::MD053LinkImageReferenceDefinitions;
114pub use md054_link_image_style::MD054LinkImageStyle;
115pub use md055_table_pipe_style::MD055TablePipeStyle;
116pub use md056_table_column_count::MD056TableColumnCount;
117pub use md058_blanks_around_tables::MD058BlanksAroundTables;
118pub use md059_link_text::MD059LinkText;
119pub use md060_table_format::ColumnAlign;
120pub use md060_table_format::MD060Config;
121pub use md060_table_format::MD060TableFormat;
122pub use md061_forbidden_terms::MD061ForbiddenTerms;
123pub use md062_link_destination_whitespace::MD062LinkDestinationWhitespace;
124pub use md063_heading_capitalization::MD063HeadingCapitalization;
125pub use md064_no_multiple_consecutive_spaces::MD064NoMultipleConsecutiveSpaces;
126pub use md065_blanks_around_horizontal_rules::MD065BlanksAroundHorizontalRules;
127pub use md066_footnote_validation::MD066FootnoteValidation;
128pub use md067_footnote_definition_order::MD067FootnoteDefinitionOrder;
129pub use md068_empty_footnote_definition::MD068EmptyFootnoteDefinition;
130pub use md069_no_duplicate_list_markers::MD069NoDuplicateListMarkers;
131pub use md070_nested_code_fence::MD070NestedCodeFence;
132pub use md071_blank_line_after_frontmatter::MD071BlankLineAfterFrontmatter;
133pub use md072_frontmatter_key_sort::MD072FrontmatterKeySort;
134pub use md073_toc_validation::MD073TocValidation;
135pub use md074_mkdocs_nav::MD074MkDocsNav;
136pub use md075_orphaned_table_rows::MD075OrphanedTableRows;
137pub use md076_list_item_spacing::{ListItemSpacingStyle, MD076ListItemSpacing};
138pub use md077_list_continuation_indent::MD077ListContinuationIndent;
139
140mod md012_no_multiple_blanks;
141pub use md012_no_multiple_blanks::MD012NoMultipleBlanks;
142
143mod md018_no_missing_space_atx;
144pub use md018_no_missing_space_atx::MD018NoMissingSpaceAtx;
145
146mod md019_no_multiple_space_atx;
147pub use md019_no_multiple_space_atx::MD019NoMultipleSpaceAtx;
148
149mod md020_no_missing_space_closed_atx;
150mod md021_no_multiple_space_closed_atx;
151pub use md020_no_missing_space_closed_atx::MD020NoMissingSpaceClosedAtx;
152pub use md021_no_multiple_space_closed_atx::MD021NoMultipleSpaceClosedAtx;
153
154pub(crate) mod md022_blanks_around_headings;
155pub use md022_blanks_around_headings::MD022BlanksAroundHeadings;
156
157mod md023_heading_start_left;
158pub use md023_heading_start_left::MD023HeadingStartLeft;
159
160mod md057_existing_relative_links;
161
162pub use md057_existing_relative_links::{AbsoluteLinksOption, MD057Config, MD057ExistingRelativeLinks};
163
164use crate::rule::Rule;
165
166/// Type alias for rule constructor functions
167type RuleCtor = fn(&crate::config::Config) -> Box<dyn Rule>;
168
169/// Entry in the rule registry, with metadata about the rule
170struct RuleEntry {
171    name: &'static str,
172    ctor: RuleCtor,
173    /// Whether this rule requires explicit opt-in via extend-enable or enable=["ALL"]
174    opt_in: bool,
175}
176
177/// Registry of all available rules with their constructor functions
178/// This enables automatic inline config support - the engine can recreate
179/// any rule with a merged config without per-rule changes.
180///
181/// Rules marked `opt_in: true` are excluded from the default rule set and must
182/// be explicitly enabled via `extend-enable` or `enable = ["ALL"]`.
183const RULES: &[RuleEntry] = &[
184    RuleEntry {
185        name: "MD001",
186        ctor: MD001HeadingIncrement::from_config,
187        opt_in: false,
188    },
189    RuleEntry {
190        name: "MD003",
191        ctor: MD003HeadingStyle::from_config,
192        opt_in: false,
193    },
194    RuleEntry {
195        name: "MD004",
196        ctor: MD004UnorderedListStyle::from_config,
197        opt_in: false,
198    },
199    RuleEntry {
200        name: "MD005",
201        ctor: MD005ListIndent::from_config,
202        opt_in: false,
203    },
204    RuleEntry {
205        name: "MD007",
206        ctor: MD007ULIndent::from_config,
207        opt_in: false,
208    },
209    RuleEntry {
210        name: "MD009",
211        ctor: MD009TrailingSpaces::from_config,
212        opt_in: false,
213    },
214    RuleEntry {
215        name: "MD010",
216        ctor: MD010NoHardTabs::from_config,
217        opt_in: false,
218    },
219    RuleEntry {
220        name: "MD011",
221        ctor: MD011NoReversedLinks::from_config,
222        opt_in: false,
223    },
224    RuleEntry {
225        name: "MD012",
226        ctor: MD012NoMultipleBlanks::from_config,
227        opt_in: false,
228    },
229    RuleEntry {
230        name: "MD013",
231        ctor: MD013LineLength::from_config,
232        opt_in: false,
233    },
234    RuleEntry {
235        name: "MD014",
236        ctor: MD014CommandsShowOutput::from_config,
237        opt_in: false,
238    },
239    RuleEntry {
240        name: "MD018",
241        ctor: MD018NoMissingSpaceAtx::from_config,
242        opt_in: false,
243    },
244    RuleEntry {
245        name: "MD019",
246        ctor: MD019NoMultipleSpaceAtx::from_config,
247        opt_in: false,
248    },
249    RuleEntry {
250        name: "MD020",
251        ctor: MD020NoMissingSpaceClosedAtx::from_config,
252        opt_in: false,
253    },
254    RuleEntry {
255        name: "MD021",
256        ctor: MD021NoMultipleSpaceClosedAtx::from_config,
257        opt_in: false,
258    },
259    RuleEntry {
260        name: "MD022",
261        ctor: MD022BlanksAroundHeadings::from_config,
262        opt_in: false,
263    },
264    RuleEntry {
265        name: "MD023",
266        ctor: MD023HeadingStartLeft::from_config,
267        opt_in: false,
268    },
269    RuleEntry {
270        name: "MD024",
271        ctor: MD024NoDuplicateHeading::from_config,
272        opt_in: false,
273    },
274    RuleEntry {
275        name: "MD025",
276        ctor: MD025SingleTitle::from_config,
277        opt_in: false,
278    },
279    RuleEntry {
280        name: "MD026",
281        ctor: MD026NoTrailingPunctuation::from_config,
282        opt_in: false,
283    },
284    RuleEntry {
285        name: "MD027",
286        ctor: MD027MultipleSpacesBlockquote::from_config,
287        opt_in: false,
288    },
289    RuleEntry {
290        name: "MD028",
291        ctor: MD028NoBlanksBlockquote::from_config,
292        opt_in: false,
293    },
294    RuleEntry {
295        name: "MD029",
296        ctor: MD029OrderedListPrefix::from_config,
297        opt_in: false,
298    },
299    RuleEntry {
300        name: "MD030",
301        ctor: MD030ListMarkerSpace::from_config,
302        opt_in: false,
303    },
304    RuleEntry {
305        name: "MD031",
306        ctor: MD031BlanksAroundFences::from_config,
307        opt_in: false,
308    },
309    RuleEntry {
310        name: "MD032",
311        ctor: MD032BlanksAroundLists::from_config,
312        opt_in: false,
313    },
314    RuleEntry {
315        name: "MD033",
316        ctor: MD033NoInlineHtml::from_config,
317        opt_in: false,
318    },
319    RuleEntry {
320        name: "MD034",
321        ctor: MD034NoBareUrls::from_config,
322        opt_in: false,
323    },
324    RuleEntry {
325        name: "MD035",
326        ctor: MD035HRStyle::from_config,
327        opt_in: false,
328    },
329    RuleEntry {
330        name: "MD036",
331        ctor: MD036NoEmphasisAsHeading::from_config,
332        opt_in: false,
333    },
334    RuleEntry {
335        name: "MD037",
336        ctor: MD037NoSpaceInEmphasis::from_config,
337        opt_in: false,
338    },
339    RuleEntry {
340        name: "MD038",
341        ctor: MD038NoSpaceInCode::from_config,
342        opt_in: false,
343    },
344    RuleEntry {
345        name: "MD039",
346        ctor: MD039NoSpaceInLinks::from_config,
347        opt_in: false,
348    },
349    RuleEntry {
350        name: "MD040",
351        ctor: MD040FencedCodeLanguage::from_config,
352        opt_in: false,
353    },
354    RuleEntry {
355        name: "MD041",
356        ctor: MD041FirstLineHeading::from_config,
357        opt_in: false,
358    },
359    RuleEntry {
360        name: "MD042",
361        ctor: MD042NoEmptyLinks::from_config,
362        opt_in: false,
363    },
364    RuleEntry {
365        name: "MD043",
366        ctor: MD043RequiredHeadings::from_config,
367        opt_in: false,
368    },
369    RuleEntry {
370        name: "MD044",
371        ctor: MD044ProperNames::from_config,
372        opt_in: false,
373    },
374    RuleEntry {
375        name: "MD045",
376        ctor: MD045NoAltText::from_config,
377        opt_in: false,
378    },
379    RuleEntry {
380        name: "MD046",
381        ctor: MD046CodeBlockStyle::from_config,
382        opt_in: false,
383    },
384    RuleEntry {
385        name: "MD047",
386        ctor: MD047SingleTrailingNewline::from_config,
387        opt_in: false,
388    },
389    RuleEntry {
390        name: "MD048",
391        ctor: MD048CodeFenceStyle::from_config,
392        opt_in: false,
393    },
394    RuleEntry {
395        name: "MD049",
396        ctor: MD049EmphasisStyle::from_config,
397        opt_in: false,
398    },
399    RuleEntry {
400        name: "MD050",
401        ctor: MD050StrongStyle::from_config,
402        opt_in: false,
403    },
404    RuleEntry {
405        name: "MD051",
406        ctor: MD051LinkFragments::from_config,
407        opt_in: false,
408    },
409    RuleEntry {
410        name: "MD052",
411        ctor: MD052ReferenceLinkImages::from_config,
412        opt_in: false,
413    },
414    RuleEntry {
415        name: "MD053",
416        ctor: MD053LinkImageReferenceDefinitions::from_config,
417        opt_in: false,
418    },
419    RuleEntry {
420        name: "MD054",
421        ctor: MD054LinkImageStyle::from_config,
422        opt_in: false,
423    },
424    RuleEntry {
425        name: "MD055",
426        ctor: MD055TablePipeStyle::from_config,
427        opt_in: false,
428    },
429    RuleEntry {
430        name: "MD056",
431        ctor: MD056TableColumnCount::from_config,
432        opt_in: false,
433    },
434    RuleEntry {
435        name: "MD057",
436        ctor: MD057ExistingRelativeLinks::from_config,
437        opt_in: false,
438    },
439    RuleEntry {
440        name: "MD058",
441        ctor: MD058BlanksAroundTables::from_config,
442        opt_in: false,
443    },
444    RuleEntry {
445        name: "MD059",
446        ctor: MD059LinkText::from_config,
447        opt_in: false,
448    },
449    RuleEntry {
450        name: "MD060",
451        ctor: MD060TableFormat::from_config,
452        opt_in: true,
453    },
454    RuleEntry {
455        name: "MD061",
456        ctor: MD061ForbiddenTerms::from_config,
457        opt_in: false,
458    },
459    RuleEntry {
460        name: "MD062",
461        ctor: MD062LinkDestinationWhitespace::from_config,
462        opt_in: false,
463    },
464    RuleEntry {
465        name: "MD063",
466        ctor: MD063HeadingCapitalization::from_config,
467        opt_in: true,
468    },
469    RuleEntry {
470        name: "MD064",
471        ctor: MD064NoMultipleConsecutiveSpaces::from_config,
472        opt_in: false,
473    },
474    RuleEntry {
475        name: "MD065",
476        ctor: MD065BlanksAroundHorizontalRules::from_config,
477        opt_in: false,
478    },
479    RuleEntry {
480        name: "MD066",
481        ctor: MD066FootnoteValidation::from_config,
482        opt_in: false,
483    },
484    RuleEntry {
485        name: "MD067",
486        ctor: MD067FootnoteDefinitionOrder::from_config,
487        opt_in: false,
488    },
489    RuleEntry {
490        name: "MD068",
491        ctor: MD068EmptyFootnoteDefinition::from_config,
492        opt_in: false,
493    },
494    RuleEntry {
495        name: "MD069",
496        ctor: MD069NoDuplicateListMarkers::from_config,
497        opt_in: false,
498    },
499    RuleEntry {
500        name: "MD070",
501        ctor: MD070NestedCodeFence::from_config,
502        opt_in: true,
503    },
504    RuleEntry {
505        name: "MD071",
506        ctor: MD071BlankLineAfterFrontmatter::from_config,
507        opt_in: false,
508    },
509    RuleEntry {
510        name: "MD072",
511        ctor: MD072FrontmatterKeySort::from_config,
512        opt_in: true,
513    },
514    RuleEntry {
515        name: "MD073",
516        ctor: MD073TocValidation::from_config,
517        opt_in: true,
518    },
519    RuleEntry {
520        name: "MD074",
521        ctor: MD074MkDocsNav::from_config,
522        opt_in: true,
523    },
524    RuleEntry {
525        name: "MD075",
526        ctor: MD075OrphanedTableRows::from_config,
527        opt_in: false,
528    },
529    RuleEntry {
530        name: "MD076",
531        ctor: MD076ListItemSpacing::from_config,
532        opt_in: false,
533    },
534    RuleEntry {
535        name: "MD077",
536        ctor: MD077ListContinuationIndent::from_config,
537        opt_in: false,
538    },
539];
540
541/// Returns all rule instances (including opt-in) for config validation and CLI
542pub fn all_rules(config: &crate::config::Config) -> Vec<Box<dyn Rule>> {
543    RULES.iter().map(|entry| (entry.ctor)(config)).collect()
544}
545
546/// Returns the set of rule names that require explicit opt-in
547pub fn opt_in_rules() -> HashSet<&'static str> {
548    RULES
549        .iter()
550        .filter(|entry| entry.opt_in)
551        .map(|entry| entry.name)
552        .collect()
553}
554
555/// Creates a single rule by name with the given config
556///
557/// This enables automatic inline config support - the engine can recreate
558/// any rule with a merged config without per-rule changes.
559///
560/// Returns None if the rule name is not found.
561pub fn create_rule_by_name(name: &str, config: &crate::config::Config) -> Option<Box<dyn Rule>> {
562    RULES
563        .iter()
564        .find(|entry| entry.name == name)
565        .map(|entry| (entry.ctor)(config))
566}
567
568// Filter rules based on config (moved from main.rs)
569// Note: This needs access to GlobalConfig from the config module.
570use crate::config::GlobalConfig;
571use std::collections::HashSet;
572
573/// Check whether the enable list contains the "all" keyword (case-insensitive).
574fn contains_all_keyword(list: &[String]) -> bool {
575    list.iter().any(|s| s.eq_ignore_ascii_case("all"))
576}
577
578/// Build a canonical-form `HashSet` from a rule-name list.
579///
580/// Rewrites every entry through `resolve_rule_name` so aliases
581/// (`"no-inline-html"`) match the same set as canonical IDs (`"MD033"`).
582/// The `"all"` keyword is preserved (case-folded to lowercase) so the
583/// special-case branches in `filter_rules` continue to work.
584fn canonical_rule_set(list: &[String]) -> HashSet<String> {
585    list.iter()
586        .map(|s| {
587            if s.eq_ignore_ascii_case("all") {
588                "all".to_string()
589            } else {
590                crate::config::resolve_rule_name(s)
591            }
592        })
593        .collect()
594}
595
596/// Filter `rules` according to `global_config.{enable,disable,extend_enable,extend_disable}`.
597///
598/// Rule-name entries may be either canonical IDs (`"MD033"`) or aliases
599/// (`"no-inline-html"`); both forms match identically. Canonical IDs are
600/// the norm — every `Config` produced through a mutation boundary
601/// (`From<SourcedConfig> for Config`, LSP `apply_lsp_settings_*`, WASM
602/// `to_config_with_warnings`) is canonicalised by
603/// `Config::canonicalize_rule_lists`. The defensive canonicalisation here
604/// keeps `filter_rules` correct for programmatic callers that build a
605/// `GlobalConfig` without going through those boundaries.
606pub fn filter_rules(rules: &[Box<dyn Rule>], global_config: &GlobalConfig) -> Vec<Box<dyn Rule>> {
607    let mut enabled_rules: Vec<Box<dyn Rule>> = Vec::new();
608    let disabled_rules: HashSet<String> = canonical_rule_set(&global_config.disable);
609    let opt_in_set = opt_in_rules();
610    let extend_enable_set: HashSet<String> = canonical_rule_set(&global_config.extend_enable);
611    let extend_disable_set: HashSet<String> = canonical_rule_set(&global_config.extend_disable);
612
613    let extend_enable_all = contains_all_keyword(&global_config.extend_enable);
614    let extend_disable_all = contains_all_keyword(&global_config.extend_disable);
615
616    // Helper: should this rule be removed by any disable source?
617    let is_disabled = |name: &str| -> bool {
618        disabled_rules.contains(name) || extend_disable_all || extend_disable_set.contains(name)
619    };
620
621    // Handle 'disable: ["all"]'
622    if disabled_rules.contains("all") {
623        // If 'enable' is also provided, only those rules are enabled, overriding "disable all"
624        if !global_config.enable.is_empty() {
625            if contains_all_keyword(&global_config.enable) {
626                // enable: ["ALL"] + disable: ["all"] cancel out → all rules enabled
627                for rule in rules {
628                    enabled_rules.push(dyn_clone::clone_box(&**rule));
629                }
630            } else {
631                let enabled_set: HashSet<String> = canonical_rule_set(&global_config.enable);
632                for rule in rules {
633                    if enabled_set.contains(rule.name()) {
634                        enabled_rules.push(dyn_clone::clone_box(&**rule));
635                    }
636                }
637            }
638        }
639        // If 'enable' is empty and 'disable: ["all"]', return empty vector.
640        return enabled_rules;
641    }
642
643    // If 'enable' is specified, only use those rules
644    if !global_config.enable.is_empty() || global_config.enable_is_explicit {
645        if contains_all_keyword(&global_config.enable) || extend_enable_all {
646            // enable: ["ALL"] or extend-enable: ["ALL"] → all rules including opt-in
647            for rule in rules {
648                if !is_disabled(rule.name()) {
649                    enabled_rules.push(dyn_clone::clone_box(&**rule));
650                }
651            }
652        } else {
653            // Merge enable set with extend-enable
654            let mut enabled_set: HashSet<String> = canonical_rule_set(&global_config.enable);
655            for name in &extend_enable_set {
656                enabled_set.insert(name.clone());
657            }
658            for rule in rules {
659                if enabled_set.contains(rule.name()) && !is_disabled(rule.name()) {
660                    enabled_rules.push(dyn_clone::clone_box(&**rule));
661                }
662            }
663        }
664    } else if extend_enable_all {
665        // No explicit enable, but extend-enable: ["ALL"] → all rules including opt-in
666        for rule in rules {
667            if !is_disabled(rule.name()) {
668                enabled_rules.push(dyn_clone::clone_box(&**rule));
669            }
670        }
671    } else {
672        // No explicit enable: use all non-opt-in rules + extend-enable, minus disable
673        for rule in rules {
674            let is_opt_in = opt_in_set.contains(rule.name());
675            let explicitly_extended = extend_enable_set.contains(rule.name());
676            if (!is_opt_in || explicitly_extended) && !is_disabled(rule.name()) {
677                enabled_rules.push(dyn_clone::clone_box(&**rule));
678            }
679        }
680    }
681
682    enabled_rules
683}
684
685#[cfg(test)]
686mod filter_rules_alias_tests {
687    use super::*;
688    use crate::config::Config;
689
690    /// `filter_rules` must accept aliases in `disable` even when the caller
691    /// builds a `GlobalConfig` directly (bypassing the canonicalisation
692    /// boundary in `From<SourcedConfig> for Config`). This is the public-API
693    /// guarantee that programmatic library callers depend on.
694    #[test]
695    fn alias_in_disable_filters_canonical_rule() {
696        let config = Config::default();
697        let rules = all_rules(&config);
698        let global = GlobalConfig {
699            disable: vec!["no-inline-html".to_string()],
700            ..Default::default()
701        };
702
703        let filtered = filter_rules(&rules, &global);
704        assert!(
705            !filtered.iter().any(|r| r.name() == "MD033"),
706            "filter_rules must drop MD033 when its alias `no-inline-html` is in disable, \
707             even on a hand-built GlobalConfig. Got: {:?}",
708            filtered.iter().map(|r| r.name()).collect::<Vec<_>>(),
709        );
710    }
711
712    /// Same guarantee for `extend_disable`.
713    #[test]
714    fn alias_in_extend_disable_filters_canonical_rule() {
715        let config = Config::default();
716        let rules = all_rules(&config);
717        let global = GlobalConfig {
718            extend_disable: vec!["line-length".to_string()],
719            ..Default::default()
720        };
721
722        let filtered = filter_rules(&rules, &global);
723        assert!(
724            !filtered.iter().any(|r| r.name() == "MD013"),
725            "filter_rules must drop MD013 when alias `line-length` is in extend_disable. Got: {:?}",
726            filtered.iter().map(|r| r.name()).collect::<Vec<_>>(),
727        );
728    }
729
730    /// `enable` aliases must select the canonical rule.
731    #[test]
732    fn alias_in_enable_selects_canonical_rule() {
733        let config = Config::default();
734        let rules = all_rules(&config);
735        let global = GlobalConfig {
736            enable: vec!["no-inline-html".to_string()],
737            ..Default::default()
738        };
739
740        let filtered = filter_rules(&rules, &global);
741        let names: Vec<&str> = filtered.iter().map(|r| r.name()).collect();
742        assert_eq!(
743            names,
744            vec!["MD033"],
745            "filter_rules must select only MD033 when alias `no-inline-html` is the sole enable. \
746             Got: {names:?}",
747        );
748    }
749
750    /// The canonical-IDs invariant is preserved: a config that is already
751    /// canonical produces the same filter result as one that uses aliases.
752    #[test]
753    fn alias_and_canonical_produce_identical_filter_result() {
754        let config = Config::default();
755        let rules = all_rules(&config);
756
757        let alias_global = GlobalConfig {
758            disable: vec!["no-inline-html".to_string(), "line-length".to_string()],
759            ..Default::default()
760        };
761        let canonical_global = GlobalConfig {
762            disable: vec!["MD033".to_string(), "MD013".to_string()],
763            ..Default::default()
764        };
765
766        let alias_names: Vec<&str> = filter_rules(&rules, &alias_global).iter().map(|r| r.name()).collect();
767        let canonical_names: Vec<&str> = filter_rules(&rules, &canonical_global)
768            .iter()
769            .map(|r| r.name())
770            .collect();
771        assert_eq!(alias_names, canonical_names);
772    }
773}