Skip to main content

rumdl_lib/rules/
md056_table_column_count.rs

1use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
2use crate::utils::range_utils::calculate_line_range;
3use crate::utils::table_utils::TableUtils;
4
5/// Rule MD056: Table column count
6///
7/// See [docs/md056.md](../../docs/md056.md) for full documentation, configuration, and examples.
8/// Ensures all rows in a table have the same number of cells
9#[derive(Debug, Clone)]
10pub struct MD056TableColumnCount;
11
12impl Default for MD056TableColumnCount {
13    fn default() -> Self {
14        MD056TableColumnCount
15    }
16}
17
18impl MD056TableColumnCount {
19    /// Try to fix a table row content (with list context awareness)
20    fn fix_table_row_content(
21        &self,
22        row_content: &str,
23        expected_count: usize,
24        flavor: crate::config::MarkdownFlavor,
25        table_block: &crate::utils::table_utils::TableBlock,
26        line_index: usize,
27        original_line: &str,
28    ) -> Option<String> {
29        let current_count = TableUtils::count_cells_with_flavor(row_content, flavor);
30
31        if current_count == expected_count || current_count == 0 {
32            return None;
33        }
34
35        let fixed = self.fix_row_by_truncation(row_content, expected_count, flavor)?;
36        Some(self.restore_prefixes(&fixed, table_block, line_index, original_line))
37    }
38
39    /// Restore list/blockquote prefixes to a fixed row
40    fn restore_prefixes(
41        &self,
42        fixed_content: &str,
43        table_block: &crate::utils::table_utils::TableBlock,
44        line_index: usize,
45        original_line: &str,
46    ) -> String {
47        // Extract blockquote prefix from original
48        let (blockquote_prefix, _) = TableUtils::extract_blockquote_prefix(original_line);
49
50        // Handle list context
51        if let Some(ref list_ctx) = table_block.list_context {
52            if line_index == 0 {
53                // Header line: use list prefix
54                format!("{blockquote_prefix}{}{fixed_content}", list_ctx.list_prefix)
55            } else {
56                // Continuation lines: use indentation
57                let indent = " ".repeat(list_ctx.content_indent);
58                format!("{blockquote_prefix}{indent}{fixed_content}")
59            }
60        } else {
61            // No list context, just blockquote
62            if blockquote_prefix.is_empty() {
63                fixed_content.to_string()
64            } else {
65                format!("{blockquote_prefix}{fixed_content}")
66            }
67        }
68    }
69
70    /// Fix a table row by truncating or adding cells
71    fn fix_row_by_truncation(
72        &self,
73        row: &str,
74        expected_count: usize,
75        flavor: crate::config::MarkdownFlavor,
76    ) -> Option<String> {
77        let current_count = TableUtils::count_cells_with_flavor(row, flavor);
78
79        if current_count == expected_count || current_count == 0 {
80            return None;
81        }
82
83        let trimmed = row.trim();
84        let has_leading_pipe = trimmed.starts_with('|');
85        let has_trailing_pipe = trimmed.ends_with('|');
86
87        // Delegate to shared cell splitting (returns only cell contents, no empty leading/trailing parts)
88        let cells = TableUtils::split_table_row_with_flavor(trimmed, flavor);
89        let mut cell_contents: Vec<&str> = cells.iter().map(|c| c.trim()).collect();
90
91        // Adjust cell count to match expected count
92        match current_count.cmp(&expected_count) {
93            std::cmp::Ordering::Greater => {
94                // Too many cells, remove excess
95                cell_contents.truncate(expected_count);
96            }
97            std::cmp::Ordering::Less => {
98                // Too few cells, add empty ones
99                while cell_contents.len() < expected_count {
100                    cell_contents.push("");
101                }
102            }
103            std::cmp::Ordering::Equal => {
104                // Perfect number of cells, no adjustment needed
105            }
106        }
107
108        // Reconstruct row
109        let mut result = String::new();
110        if has_leading_pipe {
111            result.push('|');
112        }
113
114        for (i, cell) in cell_contents.iter().enumerate() {
115            result.push_str(&format!(" {cell} "));
116            if i < cell_contents.len() - 1 || has_trailing_pipe {
117                result.push('|');
118            }
119        }
120
121        Some(result)
122    }
123}
124
125impl Rule for MD056TableColumnCount {
126    fn name(&self) -> &'static str {
127        "MD056"
128    }
129
130    fn description(&self) -> &'static str {
131        "Table column count should be consistent"
132    }
133
134    fn category(&self) -> RuleCategory {
135        RuleCategory::Table
136    }
137
138    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
139        // Skip if no tables present
140        !ctx.likely_has_tables()
141    }
142
143    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
144        let content = ctx.content;
145        let flavor = ctx.flavor;
146        let mut warnings = Vec::new();
147
148        // Early return for empty content or content without tables
149        if content.is_empty() || !content.contains('|') {
150            return Ok(Vec::new());
151        }
152
153        let lines = ctx.raw_lines();
154
155        // Use pre-computed table blocks from context
156        let table_blocks = &ctx.table_blocks;
157
158        for table_block in table_blocks {
159            // Collect all table lines for building the whole-table fix
160            let all_line_indices: Vec<usize> = std::iter::once(table_block.header_line)
161                .chain(std::iter::once(table_block.delimiter_line))
162                .chain(table_block.content_lines.iter().copied())
163                .collect();
164
165            // Determine expected column count from header row (strip list/blockquote prefix first)
166            let header_content = TableUtils::extract_table_row_content(lines[table_block.header_line], table_block, 0);
167            let expected_count = TableUtils::count_cells_with_flavor(header_content, flavor);
168
169            if expected_count == 0 {
170                continue; // Skip invalid tables
171            }
172
173            // Check each row and emit a per-row fix. Per-row fixes ensure that
174            // inline-disabling one row does not cause the fix on another row to
175            // overwrite the disabled row's content.
176            for (i, &line_idx) in all_line_indices.iter().enumerate() {
177                let line = lines[line_idx];
178                let row_content = TableUtils::extract_table_row_content(line, table_block, i);
179                let count = TableUtils::count_cells_with_flavor(row_content, flavor);
180
181                if count > 0 && count != expected_count {
182                    let (start_line, start_col, end_line, end_col) = calculate_line_range(line_idx + 1, line);
183
184                    // Build a per-row fix so inline-disabled rows are not
185                    // overwritten by fixes on other rows in the same table.
186                    let fixed_line = self
187                        .fix_table_row_content(row_content, expected_count, flavor, table_block, i, line)
188                        .unwrap_or_else(|| line.to_string());
189                    let row_range =
190                        ctx.line_index
191                            .line_col_to_byte_range_with_length(line_idx + 1, 1, line.chars().count());
192
193                    warnings.push(LintWarning {
194                        rule_name: Some(self.name().to_string()),
195                        message: format!("Table row has {count} cells, but expected {expected_count}"),
196                        line: start_line,
197                        column: start_col,
198                        end_line,
199                        end_column: end_col,
200                        severity: Severity::Warning,
201                        fix: Some(Fix::new(row_range, fixed_line)),
202                    });
203                }
204            }
205        }
206
207        Ok(warnings)
208    }
209
210    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
211        if self.should_skip(ctx) {
212            return Ok(ctx.content.to_string());
213        }
214        let warnings = self.check(ctx)?;
215        if warnings.is_empty() {
216            return Ok(ctx.content.to_string());
217        }
218        let warnings =
219            crate::utils::fix_utils::filter_warnings_by_inline_config(warnings, ctx.inline_config(), self.name());
220        crate::utils::fix_utils::apply_warning_fixes(ctx.content, &warnings).map_err(LintError::InvalidInput)
221    }
222
223    fn as_any(&self) -> &dyn std::any::Any {
224        self
225    }
226
227    fn from_config(_config: &crate::config::Config) -> Box<dyn Rule>
228    where
229        Self: Sized,
230    {
231        Box::new(MD056TableColumnCount)
232    }
233}
234
235#[cfg(test)]
236mod tests {
237    use super::*;
238    use crate::lint_context::LintContext;
239
240    #[test]
241    fn test_valid_table() {
242        let rule = MD056TableColumnCount;
243        let content = "| Header 1 | Header 2 | Header 3 |
244|----------|----------|----------|
245| Cell 1   | Cell 2   | Cell 3   |
246| Cell 4   | Cell 5   | Cell 6   |";
247        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
248        let result = rule.check(&ctx).unwrap();
249
250        assert_eq!(result.len(), 0);
251    }
252
253    #[test]
254    fn test_too_few_columns() {
255        let rule = MD056TableColumnCount;
256        let content = "| Header 1 | Header 2 | Header 3 |
257|----------|----------|----------|
258| Cell 1   | Cell 2   |
259| Cell 4   | Cell 5   | Cell 6   |";
260        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
261        let result = rule.check(&ctx).unwrap();
262
263        assert_eq!(result.len(), 1);
264        assert_eq!(result[0].line, 3);
265        assert!(result[0].message.contains("has 2 cells, but expected 3"));
266    }
267
268    #[test]
269    fn test_too_many_columns() {
270        let rule = MD056TableColumnCount;
271        let content = "| Header 1 | Header 2 |
272|----------|----------|
273| Cell 1   | Cell 2   | Cell 3   | Cell 4   |
274| Cell 5   | Cell 6   |";
275        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
276        let result = rule.check(&ctx).unwrap();
277
278        assert_eq!(result.len(), 1);
279        assert_eq!(result[0].line, 3);
280        assert!(result[0].message.contains("has 4 cells, but expected 2"));
281    }
282
283    #[test]
284    fn test_delimiter_row_mismatch() {
285        let rule = MD056TableColumnCount;
286        let content = "| Header 1 | Header 2 | Header 3 |
287|----------|----------|
288| Cell 1   | Cell 2   | Cell 3   |";
289        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
290        let result = rule.check(&ctx).unwrap();
291
292        assert_eq!(result.len(), 1);
293        assert_eq!(result[0].line, 2);
294        assert!(result[0].message.contains("has 2 cells, but expected 3"));
295    }
296
297    #[test]
298    fn test_fix_too_few_columns() {
299        let rule = MD056TableColumnCount;
300        let content = "| Header 1 | Header 2 | Header 3 |
301|----------|----------|----------|
302| Cell 1   | Cell 2   |
303| Cell 4   | Cell 5   | Cell 6   |";
304        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
305        let fixed = rule.fix(&ctx).unwrap();
306
307        assert!(fixed.contains("| Cell 1 | Cell 2 |  |"));
308    }
309
310    #[test]
311    fn test_fix_too_many_columns() {
312        let rule = MD056TableColumnCount;
313        let content = "| Header 1 | Header 2 |
314|----------|----------|
315| Cell 1   | Cell 2   | Cell 3   | Cell 4   |
316| Cell 5   | Cell 6   |";
317        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
318        let fixed = rule.fix(&ctx).unwrap();
319
320        assert!(fixed.contains("| Cell 1 | Cell 2 |"));
321        assert!(!fixed.contains("Cell 3"));
322        assert!(!fixed.contains("Cell 4"));
323    }
324
325    #[test]
326    fn test_no_leading_pipe() {
327        let rule = MD056TableColumnCount;
328        let content = "Header 1 | Header 2 | Header 3 |
329---------|----------|----------|
330Cell 1   | Cell 2   |
331Cell 4   | Cell 5   | Cell 6   |";
332        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
333        let result = rule.check(&ctx).unwrap();
334
335        assert_eq!(result.len(), 1);
336        assert_eq!(result[0].line, 3);
337    }
338
339    #[test]
340    fn test_no_trailing_pipe() {
341        let rule = MD056TableColumnCount;
342        let content = "| Header 1 | Header 2 | Header 3
343|----------|----------|----------
344| Cell 1   | Cell 2
345| Cell 4   | Cell 5   | Cell 6";
346        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
347        let result = rule.check(&ctx).unwrap();
348
349        assert_eq!(result.len(), 1);
350        assert_eq!(result[0].line, 3);
351    }
352
353    #[test]
354    fn test_no_pipes_at_all() {
355        let rule = MD056TableColumnCount;
356        let content = "This is not a table
357Just regular text
358No pipes here";
359        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
360        let result = rule.check(&ctx).unwrap();
361
362        assert_eq!(result.len(), 0);
363    }
364
365    #[test]
366    fn test_empty_cells() {
367        let rule = MD056TableColumnCount;
368        let content = "| Header 1 | Header 2 | Header 3 |
369|----------|----------|----------|
370|          |          |          |
371| Cell 1   |          | Cell 3   |";
372        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
373        let result = rule.check(&ctx).unwrap();
374
375        assert_eq!(result.len(), 0);
376    }
377
378    #[test]
379    fn test_multiple_tables() {
380        let rule = MD056TableColumnCount;
381        let content = "| Table 1 Col 1 | Table 1 Col 2 |
382|----------------|----------------|
383| Data 1         | Data 2         |
384
385Some text in between.
386
387| Table 2 Col 1 | Table 2 Col 2 | Table 2 Col 3 |
388|----------------|----------------|----------------|
389| Data 3         | Data 4         |
390| Data 5         | Data 6         | Data 7         |";
391        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
392        let result = rule.check(&ctx).unwrap();
393
394        assert_eq!(result.len(), 1);
395        assert_eq!(result[0].line, 9);
396        assert!(result[0].message.contains("has 2 cells, but expected 3"));
397    }
398
399    #[test]
400    fn test_table_with_escaped_pipes() {
401        let rule = MD056TableColumnCount;
402
403        // Single backslash escapes the pipe: \| keeps pipe as content (2 columns)
404        let content = "| Command | Description |
405|---------|-------------|
406| `echo \\| grep` | Pipe example |
407| `ls` | List files |";
408        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
409        let result = rule.check(&ctx).unwrap();
410        assert_eq!(result.len(), 0, "escaped pipe \\| should not split cells");
411
412        // Double backslash + pipe inside code span: pipe is still masked by code span
413        let content_double = "| Command | Description |
414|---------|-------------|
415| `echo \\\\| grep` | Pipe example |
416| `ls` | List files |";
417        let ctx2 = LintContext::new(content_double, crate::config::MarkdownFlavor::Standard, None);
418        let result2 = rule.check(&ctx2).unwrap();
419        // The \\| is inside backticks, so the pipe is content, not a delimiter
420        assert_eq!(result2.len(), 0, "pipes inside code spans should not split cells");
421    }
422
423    #[test]
424    fn test_empty_content() {
425        let rule = MD056TableColumnCount;
426        let content = "";
427        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
428        let result = rule.check(&ctx).unwrap();
429
430        assert_eq!(result.len(), 0);
431    }
432
433    #[test]
434    fn test_code_block_with_table() {
435        let rule = MD056TableColumnCount;
436        let content = "```
437| This | Is | Code |
438|------|----|----|
439| Not  | A  | Table |
440```
441
442| Real | Table |
443|------|-------|
444| Data | Here  |";
445        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
446        let result = rule.check(&ctx).unwrap();
447
448        // Should not check tables inside code blocks
449        assert_eq!(result.len(), 0);
450    }
451
452    #[test]
453    fn test_fix_preserves_pipe_style() {
454        let rule = MD056TableColumnCount;
455        // Test with no trailing pipes
456        let content = "| Header 1 | Header 2 | Header 3
457|----------|----------|----------
458| Cell 1   | Cell 2";
459        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
460        let fixed = rule.fix(&ctx).unwrap();
461
462        let lines: Vec<&str> = fixed.lines().collect();
463        assert!(!lines[2].ends_with('|'));
464        assert!(lines[2].contains("Cell 1"));
465        assert!(lines[2].contains("Cell 2"));
466    }
467
468    #[test]
469    fn test_single_column_table() {
470        let rule = MD056TableColumnCount;
471        let content = "| Header |
472|---------|
473| Cell 1  |
474| Cell 2  |";
475        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
476        let result = rule.check(&ctx).unwrap();
477
478        assert_eq!(result.len(), 0);
479    }
480
481    #[test]
482    fn test_complex_delimiter_row() {
483        let rule = MD056TableColumnCount;
484        let content = "| Left | Center | Right |
485|:-----|:------:|------:|
486| L    | C      | R     |
487| Left | Center |";
488        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
489        let result = rule.check(&ctx).unwrap();
490
491        assert_eq!(result.len(), 1);
492        assert_eq!(result[0].line, 4);
493    }
494
495    #[test]
496    fn test_unicode_content() {
497        let rule = MD056TableColumnCount;
498        let content = "| 名前 | 年齢 | 都市 |
499|------|------|------|
500| 田中 | 25   | 東京 |
501| 佐藤 | 30   |";
502        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
503        let result = rule.check(&ctx).unwrap();
504
505        assert_eq!(result.len(), 1);
506        assert_eq!(result[0].line, 4);
507    }
508
509    #[test]
510    fn test_very_long_cells() {
511        let rule = MD056TableColumnCount;
512        let content = "| Short | Very very very very very very very very very very long header | Another |
513|-------|--------------------------------------------------------------|---------|
514| Data  | This is an extremely long cell content that goes on and on   |";
515        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
516        let result = rule.check(&ctx).unwrap();
517
518        assert_eq!(result.len(), 1);
519        assert!(result[0].message.contains("has 2 cells, but expected 3"));
520    }
521
522    #[test]
523    fn test_fix_with_newline_ending() {
524        let rule = MD056TableColumnCount;
525        let content = "| A | B | C |
526|---|---|---|
527| 1 | 2 |
528";
529        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
530        let fixed = rule.fix(&ctx).unwrap();
531
532        assert!(fixed.ends_with('\n'));
533        assert!(fixed.contains("| 1 | 2 |  |"));
534    }
535
536    #[test]
537    fn test_fix_without_newline_ending() {
538        let rule = MD056TableColumnCount;
539        let content = "| A | B | C |
540|---|---|---|
541| 1 | 2 |";
542        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
543        let fixed = rule.fix(&ctx).unwrap();
544
545        assert!(!fixed.ends_with('\n'));
546        assert!(fixed.contains("| 1 | 2 |  |"));
547    }
548
549    #[test]
550    fn test_blockquote_table_column_mismatch() {
551        let rule = MD056TableColumnCount;
552        let content = "> | Header 1 | Header 2 | Header 3 |
553> |----------|----------|----------|
554> | Cell 1   | Cell 2   |";
555        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
556        let result = rule.check(&ctx).unwrap();
557
558        assert_eq!(result.len(), 1);
559        assert_eq!(result[0].line, 3);
560        assert!(result[0].message.contains("has 2 cells, but expected 3"));
561    }
562
563    #[test]
564    fn test_fix_blockquote_table_preserves_prefix() {
565        let rule = MD056TableColumnCount;
566        let content = "> | Header 1 | Header 2 | Header 3 |
567> |----------|----------|----------|
568> | Cell 1   | Cell 2   |
569> | Cell 4   | Cell 5   | Cell 6   |";
570        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
571        let fixed = rule.fix(&ctx).unwrap();
572
573        // Each line should still start with "> "
574        for line in fixed.lines() {
575            assert!(line.starts_with("> "), "Line should preserve blockquote prefix: {line}");
576        }
577        // The fixed row should have 3 cells
578        assert!(fixed.contains("> | Cell 1 | Cell 2 |  |"));
579    }
580
581    #[test]
582    fn test_fix_nested_blockquote_table() {
583        let rule = MD056TableColumnCount;
584        let content = ">> | A | B | C |
585>> |---|---|---|
586>> | 1 | 2 |";
587        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
588        let fixed = rule.fix(&ctx).unwrap();
589
590        // Each line should preserve the nested blockquote prefix
591        for line in fixed.lines() {
592            assert!(
593                line.starts_with(">> "),
594                "Line should preserve nested blockquote prefix: {line}"
595            );
596        }
597        assert!(fixed.contains(">> | 1 | 2 |  |"));
598    }
599
600    #[test]
601    fn test_blockquote_table_too_many_columns() {
602        let rule = MD056TableColumnCount;
603        let content = "> | A | B |
604> |---|---|
605> | 1 | 2 | 3 | 4 |";
606        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
607        let fixed = rule.fix(&ctx).unwrap();
608
609        // Should preserve blockquote prefix while truncating columns
610        assert!(fixed.lines().nth(2).unwrap().starts_with("> "));
611        assert!(fixed.contains("> | 1 | 2 |"));
612        assert!(!fixed.contains("| 3 |"));
613    }
614
615    // === Roundtrip safety tests ===
616
617    fn assert_fix_roundtrip(content: &str) {
618        let rule = MD056TableColumnCount;
619        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
620        let fixed = rule.fix(&ctx).unwrap();
621        let ctx2 = LintContext::new(&fixed, crate::config::MarkdownFlavor::Standard, None);
622        let remaining = rule.check(&ctx2).unwrap();
623        assert!(
624            remaining.is_empty(),
625            "After fix(), check() should find 0 violations.\nOriginal: {content:?}\nFixed: {fixed:?}\nRemaining: {remaining:?}"
626        );
627    }
628
629    #[test]
630    fn test_roundtrip_too_few_columns() {
631        assert_fix_roundtrip("| A | B | C |\n|---|---|---|\n| 1 | 2 |");
632    }
633
634    #[test]
635    fn test_roundtrip_too_many_columns() {
636        assert_fix_roundtrip("| A | B |\n|---|---|\n| 1 | 2 | 3 | 4 |");
637    }
638
639    #[test]
640    fn test_roundtrip_with_trailing_newline() {
641        assert_fix_roundtrip("| A | B | C |\n|---|---|---|\n| 1 | 2 |\n");
642    }
643
644    #[test]
645    fn test_roundtrip_blockquote_table() {
646        assert_fix_roundtrip("> | A | B | C |\n> |---|---|---|\n> | 1 | 2 |");
647    }
648
649    #[test]
650    fn test_roundtrip_clean_table() {
651        assert_fix_roundtrip("| A | B |\n|---|---|\n| 1 | 2 |");
652    }
653
654    #[test]
655    fn test_roundtrip_multiple_tables() {
656        assert_fix_roundtrip("| A | B |\n|---|---|\n| 1 | 2 |\n\nText\n\n| C | D | E |\n|---|---|---|\n| 3 | 4 |");
657    }
658}