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