rumdl_lib/
lib.rs

1pub mod config;
2pub mod exit_codes;
3pub mod filtered_lines;
4pub mod fix_coordinator;
5pub mod inline_config;
6pub mod lint_context;
7pub mod lsp;
8pub mod markdownlint_config;
9pub mod output;
10pub mod parallel;
11pub mod performance;
12pub mod profiling;
13pub mod rule;
14pub mod vscode;
15#[macro_use]
16pub mod rule_config;
17#[macro_use]
18pub mod rule_config_serde;
19pub mod rules;
20pub mod utils;
21
22pub use rules::heading_utils::{Heading, HeadingStyle};
23pub use rules::*;
24
25pub use crate::lint_context::{LineInfo, LintContext, ListItemInfo};
26use crate::rule::{LintResult, Rule, RuleCategory};
27use std::time::Instant;
28
29/// Content characteristics for efficient rule filtering
30#[derive(Debug, Default)]
31struct ContentCharacteristics {
32    has_headings: bool,    // # or setext headings
33    has_lists: bool,       // *, -, +, 1. etc
34    has_links: bool,       // [text](url) or [text][ref]
35    has_code: bool,        // ``` or ~~~ or indented code
36    has_emphasis: bool,    // * or _ for emphasis
37    has_html: bool,        // < > tags
38    has_tables: bool,      // | pipes
39    has_blockquotes: bool, // > markers
40    has_images: bool,      // ![alt](url)
41}
42
43impl ContentCharacteristics {
44    fn analyze(content: &str) -> Self {
45        let mut chars = Self { ..Default::default() };
46
47        // Quick single-pass analysis
48        let mut has_atx_heading = false;
49        let mut has_setext_heading = false;
50
51        for line in content.lines() {
52            let trimmed = line.trim();
53
54            // Headings: ATX (#) or Setext (underlines)
55            if !has_atx_heading && trimmed.starts_with('#') {
56                has_atx_heading = true;
57            }
58            if !has_setext_heading && (trimmed.chars().all(|c| c == '=' || c == '-') && trimmed.len() > 1) {
59                has_setext_heading = true;
60            }
61
62            // Quick character-based detection (more efficient than regex)
63            if !chars.has_lists && (line.contains("* ") || line.contains("- ") || line.contains("+ ")) {
64                chars.has_lists = true;
65            }
66            if !chars.has_lists && line.chars().next().is_some_and(|c| c.is_ascii_digit()) && line.contains(". ") {
67                chars.has_lists = true;
68            }
69            if !chars.has_links
70                && (line.contains('[')
71                    || line.contains("http://")
72                    || line.contains("https://")
73                    || line.contains("ftp://"))
74            {
75                chars.has_links = true;
76            }
77            if !chars.has_images && line.contains("![") {
78                chars.has_images = true;
79            }
80            if !chars.has_code && (line.contains('`') || line.contains("~~~")) {
81                chars.has_code = true;
82            }
83            if !chars.has_emphasis && (line.contains('*') || line.contains('_')) {
84                chars.has_emphasis = true;
85            }
86            if !chars.has_html && line.contains('<') {
87                chars.has_html = true;
88            }
89            if !chars.has_tables && line.contains('|') {
90                chars.has_tables = true;
91            }
92            if !chars.has_blockquotes && line.starts_with('>') {
93                chars.has_blockquotes = true;
94            }
95        }
96
97        chars.has_headings = has_atx_heading || has_setext_heading;
98        chars
99    }
100
101    /// Check if a rule should be skipped based on content characteristics
102    fn should_skip_rule(&self, rule: &dyn Rule) -> bool {
103        match rule.category() {
104            RuleCategory::Heading => !self.has_headings,
105            RuleCategory::List => !self.has_lists,
106            RuleCategory::Link => !self.has_links && !self.has_images,
107            RuleCategory::Image => !self.has_images,
108            RuleCategory::CodeBlock => !self.has_code,
109            RuleCategory::Html => !self.has_html,
110            RuleCategory::Emphasis => !self.has_emphasis,
111            RuleCategory::Blockquote => !self.has_blockquotes,
112            RuleCategory::Table => !self.has_tables,
113            // Always check these categories as they apply to all content
114            RuleCategory::Whitespace | RuleCategory::FrontMatter | RuleCategory::Other => false,
115        }
116    }
117}
118
119/// Lint a file against the given rules with intelligent rule filtering
120/// Assumes the provided `rules` vector contains the final,
121/// configured, and filtered set of rules to be executed.
122pub fn lint(
123    content: &str,
124    rules: &[Box<dyn Rule>],
125    _verbose: bool,
126    flavor: crate::config::MarkdownFlavor,
127) -> LintResult {
128    let mut warnings = Vec::new();
129    let _overall_start = Instant::now();
130
131    // Early return for empty content
132    if content.is_empty() {
133        return Ok(warnings);
134    }
135
136    // Parse inline configuration comments once
137    let inline_config = crate::inline_config::InlineConfig::from_content(content);
138
139    // Analyze content characteristics for rule filtering
140    let characteristics = ContentCharacteristics::analyze(content);
141
142    // Filter rules based on content characteristics
143    let applicable_rules: Vec<_> = rules
144        .iter()
145        .filter(|rule| !characteristics.should_skip_rule(rule.as_ref()))
146        .collect();
147
148    // Calculate skipped rules count before consuming applicable_rules
149    let _total_rules = rules.len();
150    let _applicable_count = applicable_rules.len();
151
152    // Parse LintContext once with the provided flavor
153    let lint_ctx = crate::lint_context::LintContext::new(content, flavor);
154
155    let profile_rules = std::env::var("RUMDL_PROFILE_RULES").is_ok();
156
157    for rule in applicable_rules {
158        let _rule_start = Instant::now();
159
160        let result = rule.check(&lint_ctx);
161
162        match result {
163            Ok(rule_warnings) => {
164                // Filter out warnings for rules disabled via inline comments
165                let filtered_warnings: Vec<_> = rule_warnings
166                    .into_iter()
167                    .filter(|warning| {
168                        // Use the warning's rule_name if available, otherwise use the rule's name
169                        let rule_name_to_check = warning.rule_name.as_deref().unwrap_or(rule.name());
170
171                        // Extract the base rule name for sub-rules like "MD029-style" -> "MD029"
172                        let base_rule_name = if let Some(dash_pos) = rule_name_to_check.find('-') {
173                            &rule_name_to_check[..dash_pos]
174                        } else {
175                            rule_name_to_check
176                        };
177
178                        !inline_config.is_rule_disabled(
179                            base_rule_name,
180                            warning.line, // Already 1-indexed
181                        )
182                    })
183                    .collect();
184                warnings.extend(filtered_warnings);
185            }
186            Err(e) => {
187                log::error!("Error checking rule {}: {}", rule.name(), e);
188                return Err(e);
189            }
190        }
191
192        let rule_duration = _rule_start.elapsed();
193        if profile_rules {
194            eprintln!("[RULE] {:6} {:?}", rule.name(), rule_duration);
195        }
196
197        #[cfg(not(test))]
198        if _verbose && rule_duration.as_millis() > 500 {
199            log::debug!("Rule {} took {:?}", rule.name(), rule_duration);
200        }
201    }
202
203    #[cfg(not(test))]
204    if _verbose {
205        let skipped_rules = _total_rules - _applicable_count;
206        if skipped_rules > 0 {
207            log::debug!("Skipped {skipped_rules} of {_total_rules} rules based on content analysis");
208        }
209    }
210
211    Ok(warnings)
212}
213
214/// Get the profiling report
215pub fn get_profiling_report() -> String {
216    profiling::get_report()
217}
218
219/// Reset the profiling data
220pub fn reset_profiling() {
221    profiling::reset()
222}
223
224/// Get regex cache statistics for performance monitoring
225pub fn get_regex_cache_stats() -> std::collections::HashMap<String, u64> {
226    crate::utils::regex_cache::get_cache_stats()
227}
228
229/// Get AST cache statistics for performance monitoring
230pub fn get_ast_cache_stats() -> std::collections::HashMap<u64, u64> {
231    crate::utils::ast_utils::get_ast_cache_stats()
232}
233
234/// Clear all caches (useful for testing and memory management)
235pub fn clear_all_caches() {
236    crate::utils::ast_utils::clear_ast_cache();
237    // Note: Regex cache is intentionally not cleared as it's global and shared
238}
239
240/// Get comprehensive cache performance report
241pub fn get_cache_performance_report() -> String {
242    let regex_stats = get_regex_cache_stats();
243    let ast_stats = get_ast_cache_stats();
244
245    let mut report = String::new();
246
247    report.push_str("=== Cache Performance Report ===\n\n");
248
249    // Regex cache statistics
250    report.push_str("Regex Cache:\n");
251    if regex_stats.is_empty() {
252        report.push_str("  No regex patterns cached\n");
253    } else {
254        let total_usage: u64 = regex_stats.values().sum();
255        report.push_str(&format!("  Total patterns: {}\n", regex_stats.len()));
256        report.push_str(&format!("  Total usage: {total_usage}\n"));
257
258        // Show top 5 most used patterns
259        let mut sorted_patterns: Vec<_> = regex_stats.iter().collect();
260        sorted_patterns.sort_by(|a, b| b.1.cmp(a.1));
261
262        report.push_str("  Top patterns by usage:\n");
263        for (pattern, count) in sorted_patterns.iter().take(5) {
264            let truncated_pattern = if pattern.len() > 50 {
265                format!("{}...", &pattern[..47])
266            } else {
267                pattern.to_string()
268            };
269            report.push_str(&format!(
270                "    {} ({}x): {}\n",
271                count,
272                pattern.len().min(50),
273                truncated_pattern
274            ));
275        }
276    }
277
278    report.push('\n');
279
280    // AST cache statistics
281    report.push_str("AST Cache:\n");
282    if ast_stats.is_empty() {
283        report.push_str("  No ASTs cached\n");
284    } else {
285        let total_usage: u64 = ast_stats.values().sum();
286        report.push_str(&format!("  Total ASTs: {}\n", ast_stats.len()));
287        report.push_str(&format!("  Total usage: {total_usage}\n"));
288
289        if total_usage > ast_stats.len() as u64 {
290            let cache_hit_rate = ((total_usage - ast_stats.len() as u64) as f64 / total_usage as f64) * 100.0;
291            report.push_str(&format!("  Cache hit rate: {cache_hit_rate:.1}%\n"));
292        }
293    }
294
295    report
296}
297
298#[cfg(test)]
299mod tests {
300    use super::*;
301    use crate::rule::Rule;
302    use crate::rules::{MD001HeadingIncrement, MD009TrailingSpaces, MD012NoMultipleBlanks};
303
304    #[test]
305    fn test_content_characteristics_analyze() {
306        // Test empty content
307        let chars = ContentCharacteristics::analyze("");
308        assert!(!chars.has_headings);
309        assert!(!chars.has_lists);
310        assert!(!chars.has_links);
311        assert!(!chars.has_code);
312        assert!(!chars.has_emphasis);
313        assert!(!chars.has_html);
314        assert!(!chars.has_tables);
315        assert!(!chars.has_blockquotes);
316        assert!(!chars.has_images);
317
318        // Test content with headings
319        let chars = ContentCharacteristics::analyze("# Heading");
320        assert!(chars.has_headings);
321
322        // Test setext headings
323        let chars = ContentCharacteristics::analyze("Heading\n=======");
324        assert!(chars.has_headings);
325
326        // Test lists
327        let chars = ContentCharacteristics::analyze("* Item\n- Item 2\n+ Item 3");
328        assert!(chars.has_lists);
329
330        // Test ordered lists
331        let chars = ContentCharacteristics::analyze("1. First\n2. Second");
332        assert!(chars.has_lists);
333
334        // Test links
335        let chars = ContentCharacteristics::analyze("[link](url)");
336        assert!(chars.has_links);
337
338        // Test URLs
339        let chars = ContentCharacteristics::analyze("Visit https://example.com");
340        assert!(chars.has_links);
341
342        // Test images
343        let chars = ContentCharacteristics::analyze("![alt text](image.png)");
344        assert!(chars.has_images);
345
346        // Test code
347        let chars = ContentCharacteristics::analyze("`inline code`");
348        assert!(chars.has_code);
349
350        let chars = ContentCharacteristics::analyze("~~~\ncode block\n~~~");
351        assert!(chars.has_code);
352
353        // Test emphasis
354        let chars = ContentCharacteristics::analyze("*emphasis* and _more_");
355        assert!(chars.has_emphasis);
356
357        // Test HTML
358        let chars = ContentCharacteristics::analyze("<div>HTML content</div>");
359        assert!(chars.has_html);
360
361        // Test tables
362        let chars = ContentCharacteristics::analyze("| Header | Header |\n|--------|--------|");
363        assert!(chars.has_tables);
364
365        // Test blockquotes
366        let chars = ContentCharacteristics::analyze("> Quote");
367        assert!(chars.has_blockquotes);
368
369        // Test mixed content
370        let content = "# Heading\n* List item\n[link](url)\n`code`\n*emphasis*\n<p>html</p>\n| table |\n> quote\n![image](img.png)";
371        let chars = ContentCharacteristics::analyze(content);
372        assert!(chars.has_headings);
373        assert!(chars.has_lists);
374        assert!(chars.has_links);
375        assert!(chars.has_code);
376        assert!(chars.has_emphasis);
377        assert!(chars.has_html);
378        assert!(chars.has_tables);
379        assert!(chars.has_blockquotes);
380        assert!(chars.has_images);
381    }
382
383    #[test]
384    fn test_content_characteristics_should_skip_rule() {
385        let chars = ContentCharacteristics {
386            has_headings: true,
387            has_lists: false,
388            has_links: true,
389            has_code: false,
390            has_emphasis: true,
391            has_html: false,
392            has_tables: true,
393            has_blockquotes: false,
394            has_images: false,
395        };
396
397        // Create test rules for different categories
398        let heading_rule = MD001HeadingIncrement;
399        assert!(!chars.should_skip_rule(&heading_rule));
400
401        let trailing_spaces_rule = MD009TrailingSpaces::new(2, false);
402        assert!(!chars.should_skip_rule(&trailing_spaces_rule)); // Whitespace rules always run
403
404        // Test skipping based on content
405        let chars_no_headings = ContentCharacteristics {
406            has_headings: false,
407            ..Default::default()
408        };
409        assert!(chars_no_headings.should_skip_rule(&heading_rule));
410    }
411
412    #[test]
413    fn test_lint_empty_content() {
414        let rules: Vec<Box<dyn Rule>> = vec![Box::new(MD001HeadingIncrement)];
415
416        let result = lint("", &rules, false, crate::config::MarkdownFlavor::Standard);
417        assert!(result.is_ok());
418        assert!(result.unwrap().is_empty());
419    }
420
421    #[test]
422    fn test_lint_with_violations() {
423        let content = "## Level 2\n#### Level 4"; // Skips level 3
424        let rules: Vec<Box<dyn Rule>> = vec![Box::new(MD001HeadingIncrement)];
425
426        let result = lint(content, &rules, false, crate::config::MarkdownFlavor::Standard);
427        assert!(result.is_ok());
428        let warnings = result.unwrap();
429        assert!(!warnings.is_empty());
430        // Check the rule field of LintWarning struct
431        assert_eq!(warnings[0].rule_name.as_deref(), Some("MD001"));
432    }
433
434    #[test]
435    fn test_lint_with_inline_disable() {
436        let content = "<!-- rumdl-disable MD001 -->\n## Level 2\n#### Level 4";
437        let rules: Vec<Box<dyn Rule>> = vec![Box::new(MD001HeadingIncrement)];
438
439        let result = lint(content, &rules, false, crate::config::MarkdownFlavor::Standard);
440        assert!(result.is_ok());
441        let warnings = result.unwrap();
442        assert!(warnings.is_empty()); // Should be disabled by inline comment
443    }
444
445    #[test]
446    fn test_lint_rule_filtering() {
447        // Content with no lists
448        let content = "# Heading\nJust text";
449        let rules: Vec<Box<dyn Rule>> = vec![
450            Box::new(MD001HeadingIncrement),
451            // A list-related rule would be skipped
452        ];
453
454        let result = lint(content, &rules, false, crate::config::MarkdownFlavor::Standard);
455        assert!(result.is_ok());
456    }
457
458    #[test]
459    fn test_get_profiling_report() {
460        // Just test that it returns a string without panicking
461        let report = get_profiling_report();
462        assert!(!report.is_empty());
463        assert!(report.contains("Profiling"));
464    }
465
466    #[test]
467    fn test_reset_profiling() {
468        // Test that reset_profiling doesn't panic
469        reset_profiling();
470
471        // After reset, report should indicate no measurements or profiling disabled
472        let report = get_profiling_report();
473        assert!(report.contains("disabled") || report.contains("no measurements"));
474    }
475
476    #[test]
477    fn test_get_regex_cache_stats() {
478        let stats = get_regex_cache_stats();
479        // Stats should be a valid HashMap (might be empty)
480        assert!(stats.is_empty() || !stats.is_empty());
481
482        // If not empty, all values should be positive
483        for count in stats.values() {
484            assert!(*count > 0);
485        }
486    }
487
488    #[test]
489    fn test_get_ast_cache_stats() {
490        let stats = get_ast_cache_stats();
491        // Stats should be a valid HashMap (might be empty)
492        assert!(stats.is_empty() || !stats.is_empty());
493
494        // If not empty, all values should be positive
495        for count in stats.values() {
496            assert!(*count > 0);
497        }
498    }
499
500    #[test]
501    fn test_clear_all_caches() {
502        // Test that clear_all_caches doesn't panic
503        clear_all_caches();
504
505        // Function completes successfully - cache state is process-global and may
506        // be modified by other tests, so we don't assert on specific state
507    }
508
509    #[test]
510    fn test_get_cache_performance_report() {
511        // Test that the report generation works and has the correct structure
512        let report = get_cache_performance_report();
513
514        // Report should always contain expected section headers
515        assert!(report.contains("Cache Performance Report"));
516        assert!(report.contains("Regex Cache:"));
517        assert!(report.contains("AST Cache:"));
518
519        // Report should contain either usage stats or "no cache" messages
520        // (depends on whether other tests have populated the cache)
521        assert!(report.contains("Total patterns:") || report.contains("No regex patterns cached"));
522        assert!(report.contains("Total ASTs:") || report.contains("No ASTs cached"));
523    }
524
525    #[test]
526    fn test_lint_with_ast_rules() {
527        // Create content that would benefit from AST parsing
528        let content = "# Heading\n\nParagraph with **bold** text.";
529        let rules: Vec<Box<dyn Rule>> = vec![Box::new(MD012NoMultipleBlanks::new(1))];
530
531        let result = lint(content, &rules, false, crate::config::MarkdownFlavor::Standard);
532        assert!(result.is_ok());
533    }
534
535    #[test]
536    fn test_content_characteristics_edge_cases() {
537        // Test setext heading edge case
538        let chars = ContentCharacteristics::analyze("-"); // Single dash, not a heading
539        assert!(!chars.has_headings);
540
541        let chars = ContentCharacteristics::analyze("--"); // Two dashes, valid setext
542        assert!(chars.has_headings);
543
544        // Test list detection edge cases
545        let chars = ContentCharacteristics::analyze("*emphasis*"); // Not a list
546        assert!(!chars.has_lists);
547
548        let chars = ContentCharacteristics::analyze("1.Item"); // No space after period
549        assert!(!chars.has_lists);
550
551        // Test blockquote must be at start of line
552        let chars = ContentCharacteristics::analyze("text > not a quote");
553        assert!(!chars.has_blockquotes);
554    }
555
556    #[test]
557    fn test_cache_performance_report_formatting() {
558        // Add some data to caches to test formatting
559        // (Would require actual usage of the caches, which happens during linting)
560
561        let report = get_cache_performance_report();
562
563        // Test truncation of long patterns
564        // Since we can't easily add a long pattern to the cache in this test,
565        // we'll just verify the report structure is correct
566        assert!(!report.is_empty());
567        assert!(report.lines().count() > 3); // Should have multiple lines
568    }
569}