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