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