Skip to main content

rumdl_lib/rules/
md038_no_space_in_code.rs

1use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
2use crate::utils::mkdocs_extensions::is_inline_hilite_content;
3
4/// Rule MD038: No space inside code span markers
5///
6/// See [docs/md038.md](../../docs/md038.md) for full documentation, configuration, and examples.
7///
8/// MD038: Spaces inside code span elements
9///
10/// This rule is triggered when there are spaces inside code span elements.
11///
12/// For example:
13///
14/// ``` markdown
15/// ` some text`
16/// `some text `
17/// ` some text `
18/// ```
19///
20/// To fix this issue, remove the leading and trailing spaces within the code span markers:
21///
22/// ``` markdown
23/// `some text`
24/// ```
25///
26/// Note: Code spans containing backticks (e.g., `` `backticks` inside ``) are not flagged
27/// to avoid breaking nested backtick structures used to display backticks in documentation.
28#[derive(Debug, Clone, Default)]
29pub struct MD038NoSpaceInCode {
30    pub enabled: bool,
31}
32
33impl MD038NoSpaceInCode {
34    pub fn new() -> Self {
35        Self { enabled: true }
36    }
37
38    /// Check if a code span is part of Hugo template syntax (e.g., {{raw `...`}})
39    ///
40    /// Hugo static site generator uses backticks as part of template delimiters,
41    /// not markdown code spans. This function detects common Hugo shortcode patterns:
42    /// - {{raw `...`}} - Raw HTML shortcode
43    /// - {{< `...` >}} - Partial shortcode
44    /// - {{% `...` %}} - Shortcode with percent delimiters
45    /// - {{ `...` }} - Generic shortcode
46    ///
47    /// The detection is conservative to avoid false positives:
48    /// - Requires opening {{ pattern before the backtick
49    /// - Requires closing }} after the code span
50    /// - Handles multi-line templates correctly
51    ///
52    /// Returns true if the code span is part of Hugo template syntax and should be skipped.
53    fn is_hugo_template_syntax(
54        &self,
55        ctx: &crate::lint_context::LintContext,
56        code_span: &crate::lint_context::CodeSpan,
57    ) -> bool {
58        let start_line_idx = code_span.line.saturating_sub(1);
59        if start_line_idx >= ctx.lines.len() {
60            return false;
61        }
62
63        let start_line_content = ctx.lines[start_line_idx].content(ctx.content);
64
65        // start_col is 0-indexed character position
66        let span_start_col = code_span.start_col;
67
68        // Check if there's Hugo template syntax before the code span on the same line
69        // Pattern: {{raw ` or {{< ` or similar Hugo template patterns
70        // The code span starts at the backtick, so we need to check what's before it
71        // span_start_col is the position of the backtick (0-indexed character position)
72        // Minimum pattern is "{{ `" which has 3 characters before the backtick
73        if span_start_col >= 3 {
74            // Look backwards for Hugo template patterns
75            // Get the content up to (but not including) the backtick
76            let before_span: String = start_line_content.chars().take(span_start_col).collect();
77
78            // Check for Hugo template patterns: {{raw `, {{< `, {{% `, etc.
79            // The backtick is at span_start_col, so we check if the content before it
80            // ends with the Hugo pattern (without the backtick), and verify the next char is a backtick
81            let char_at_span_start = start_line_content.chars().nth(span_start_col).unwrap_or(' ');
82
83            // Match Hugo shortcode patterns:
84            // - {{raw ` - Raw HTML shortcode
85            // - {{< ` - Partial shortcode (may have parameters before backtick)
86            // - {{% ` - Shortcode with percent delimiters
87            // - {{ ` - Generic shortcode
88            // Also handle cases with parameters: {{< highlight go ` or {{< code ` etc.
89            // We check if the pattern starts with {{ and contains the shortcode type before the backtick
90            let is_hugo_start =
91                // Exact match: {{raw `
92                (before_span.ends_with("{{raw ") && char_at_span_start == '`')
93                // Partial shortcode: {{< ` or {{< name ` or {{< name param ` etc.
94                || (before_span.starts_with("{{<") && before_span.ends_with(' ') && char_at_span_start == '`')
95                // Percent shortcode: {{% `
96                || (before_span.ends_with("{{% ") && char_at_span_start == '`')
97                // Generic shortcode: {{ `
98                || (before_span.ends_with("{{ ") && char_at_span_start == '`');
99
100            if is_hugo_start {
101                // Check if there's a closing }} after the code span
102                // First check the end line of the code span
103                let end_line_idx = code_span.end_line.saturating_sub(1);
104                if end_line_idx < ctx.lines.len() {
105                    let end_line_content = ctx.lines[end_line_idx].content(ctx.content);
106                    let end_line_char_count = end_line_content.chars().count();
107                    let span_end_col = code_span.end_col.min(end_line_char_count);
108
109                    // Check for closing }} on the same line as the end of the code span
110                    if span_end_col < end_line_char_count {
111                        let after_span: String = end_line_content.chars().skip(span_end_col).collect();
112                        if after_span.trim_start().starts_with("}}") {
113                            return true;
114                        }
115                    }
116
117                    // Also check the next line for closing }}
118                    let next_line_idx = code_span.end_line;
119                    if next_line_idx < ctx.lines.len() {
120                        let next_line = ctx.lines[next_line_idx].content(ctx.content);
121                        if next_line.trim_start().starts_with("}}") {
122                            return true;
123                        }
124                    }
125                }
126            }
127        }
128
129        false
130    }
131
132    /// Check if content is an Obsidian Dataview inline query
133    ///
134    /// Dataview plugin uses two inline query syntaxes:
135    /// - Inline DQL: `= expression` - Starts with "= "
136    /// - Inline DataviewJS: `$= expression` - Starts with "$= "
137    ///
138    /// Examples:
139    /// - `= this.file.name` - Get current file name
140    /// - `= date(today)` - Get today's date
141    /// - `= [[Page]].field` - Access field from another page
142    /// - `$= dv.current().file.mtime` - DataviewJS expression
143    /// - `$= dv.pages().length` - Count pages
144    ///
145    /// These patterns legitimately start with a space after = or $=,
146    /// so they should not trigger MD038.
147    fn is_dataview_expression(content: &str) -> bool {
148        // Inline DQL: starts with "= " (equals followed by space)
149        // Inline DataviewJS: starts with "$= " (dollar-equals followed by space)
150        content.starts_with("= ") || content.starts_with("$= ")
151    }
152
153    /// Check if a code span is likely part of a nested backtick structure
154    fn is_likely_nested_backticks(&self, ctx: &crate::lint_context::LintContext, span_index: usize) -> bool {
155        // If there are multiple code spans on the same line, and there's text
156        // between them that contains "code" or other indicators, it's likely nested
157        let code_spans = ctx.code_spans();
158        let current_span = &code_spans[span_index];
159        let current_line = current_span.line;
160
161        // Look for other code spans on the same line
162        let same_line_spans: Vec<_> = code_spans
163            .iter()
164            .enumerate()
165            .filter(|(i, s)| s.line == current_line && *i != span_index)
166            .collect();
167
168        if same_line_spans.is_empty() {
169            return false;
170        }
171
172        // Check if there's content between spans that might indicate nesting
173        // Get the line content
174        let line_idx = current_line - 1; // Convert to 0-based
175        if line_idx >= ctx.lines.len() {
176            return false;
177        }
178
179        let line_content = &ctx.lines[line_idx].content(ctx.content);
180
181        // For each pair of adjacent code spans, check what's between them
182        for (_, other_span) in &same_line_spans {
183            let start_char = current_span.end_col.min(other_span.end_col);
184            let end_char = current_span.start_col.max(other_span.start_col);
185
186            if start_char < end_char {
187                // Convert character positions to byte offsets for string slicing
188                let char_indices: Vec<(usize, char)> = line_content.char_indices().collect();
189                let start_byte = char_indices.get(start_char).map(|(i, _)| *i);
190                let end_byte = char_indices.get(end_char).map_or(line_content.len(), |(i, _)| *i);
191
192                if let Some(start_byte) = start_byte
193                    && start_byte < end_byte
194                    && end_byte <= line_content.len()
195                {
196                    let between = &line_content[start_byte..end_byte];
197                    // If there's text containing "code" or similar patterns between spans,
198                    // it's likely they're showing nested backticks
199                    if between.contains("code") || between.contains("backtick") {
200                        return true;
201                    }
202                }
203            }
204        }
205
206        false
207    }
208
209    /// Check for a CommonMark parse shape produced by nested single backticks.
210    ///
211    /// In text like `` `{ outer `inner` outer }` ``, CommonMark sees two adjacent
212    /// code spans rather than one nested span. Removing the apparent leading or
213    /// trailing space from those parsed spans moves prose across the inner
214    /// backticks and changes the rendered text.
215    fn has_attached_nested_backtick_boundary(
216        &self,
217        ctx: &crate::lint_context::LintContext,
218        code_span: &crate::lint_context::CodeSpan,
219    ) -> bool {
220        let content = code_span.content.as_str();
221
222        let next_char = ctx.content[code_span.byte_end..].chars().next();
223        let prev_char = ctx.content[..code_span.byte_offset].chars().next_back();
224
225        (content.ends_with(char::is_whitespace) && next_char.is_some_and(|c| !c.is_whitespace()))
226            || (content.starts_with(char::is_whitespace) && prev_char.is_some_and(|c| !c.is_whitespace()))
227    }
228}
229
230impl Rule for MD038NoSpaceInCode {
231    fn name(&self) -> &'static str {
232        "MD038"
233    }
234
235    fn description(&self) -> &'static str {
236        "Spaces inside code span elements"
237    }
238
239    fn category(&self) -> RuleCategory {
240        RuleCategory::Other
241    }
242
243    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
244        if !self.enabled {
245            return Ok(vec![]);
246        }
247
248        let mut warnings = Vec::new();
249
250        // Use centralized code spans from LintContext
251        let code_spans = ctx.code_spans();
252        for (i, code_span) in code_spans.iter().enumerate() {
253            // Skip code spans that are inside fenced/indented code blocks
254            if let Some(line_info) = ctx.lines.get(code_span.line - 1) {
255                if line_info.in_code_block {
256                    continue;
257                }
258                // Skip multi-line code spans inside MkDocs containers where pulldown-cmark
259                // misinterprets indented fenced code block markers as code spans.
260                // Covers admonitions, tabs, HTML markdown blocks, and PyMdown blocks.
261                if (line_info.in_mkdocs_container() || line_info.in_pymdown_block) && code_span.content.contains('\n') {
262                    continue;
263                }
264            }
265
266            let code_content = &code_span.content;
267
268            // Skip empty code spans
269            if code_content.is_empty() {
270                continue;
271            }
272
273            // Early check: if no leading/trailing whitespace, skip
274            let has_leading_space = code_content.chars().next().is_some_and(char::is_whitespace);
275            let has_trailing_space = code_content.chars().last().is_some_and(char::is_whitespace);
276
277            if !has_leading_space && !has_trailing_space {
278                continue;
279            }
280
281            let trimmed = code_content.trim();
282
283            // Check if there are leading or trailing spaces
284            if code_content != trimmed {
285                // CommonMark behavior: if there is exactly ONE space at start AND ONE at end,
286                // and the content after trimming is non-empty, those spaces are stripped.
287                // We should NOT flag this case since the spaces are intentionally stripped.
288                // See: https://spec.commonmark.org/0.31.2/#code-spans
289                //
290                // Examples:
291                // ` text ` → "text" (spaces stripped, NOT flagged)
292                // `  text ` → " text" (extra leading space remains, FLAGGED)
293                // ` text  ` → "text " (extra trailing space remains, FLAGGED)
294                // ` text` → " text" (no trailing space to balance, FLAGGED)
295                // `text ` → "text " (no leading space to balance, FLAGGED)
296                if has_leading_space && has_trailing_space && !trimmed.is_empty() {
297                    let leading_spaces = code_content.len() - code_content.trim_start().len();
298                    let trailing_spaces = code_content.len() - code_content.trim_end().len();
299
300                    // Exactly one space on each side - CommonMark strips them
301                    if leading_spaces == 1 && trailing_spaces == 1 {
302                        continue;
303                    }
304                }
305                // Check if the content itself contains backticks - if so, skip to avoid
306                // breaking nested backtick structures
307                if trimmed.contains('`') {
308                    continue;
309                }
310
311                // Skip inline R code in Quarto/RMarkdown: `r expression`
312                // This is a legitimate pattern where space is required after 'r'
313                if ctx.flavor == crate::config::MarkdownFlavor::Quarto
314                    && trimmed.starts_with('r')
315                    && trimmed.len() > 1
316                    && trimmed.chars().nth(1).is_some_and(char::is_whitespace)
317                {
318                    continue;
319                }
320
321                // Skip InlineHilite syntax in MkDocs: `#!python code`
322                // The space after the language specifier is legitimate
323                if ctx.flavor == crate::config::MarkdownFlavor::MkDocs && is_inline_hilite_content(trimmed) {
324                    continue;
325                }
326
327                // Skip Dataview inline queries in Obsidian: `= expression` or `$= expression`
328                // Dataview plugin uses these patterns for inline DQL and DataviewJS queries.
329                // The space after = or $= is part of the syntax, not a spacing error.
330                if ctx.flavor == crate::config::MarkdownFlavor::Obsidian && Self::is_dataview_expression(code_content) {
331                    continue;
332                }
333
334                // Check if this is part of Hugo template syntax (e.g., {{raw `...`}})
335                // Hugo uses backticks as part of template delimiters, not markdown code spans
336                if self.is_hugo_template_syntax(ctx, code_span) {
337                    continue;
338                }
339
340                // Check if this might be part of a nested backtick structure
341                // by looking for other code spans nearby that might indicate nesting
342                if self.is_likely_nested_backticks(ctx, i) {
343                    continue;
344                }
345
346                if self.has_attached_nested_backtick_boundary(ctx, code_span) {
347                    continue;
348                }
349
350                warnings.push(LintWarning {
351                    rule_name: Some(self.name().to_string()),
352                    line: code_span.line,
353                    column: code_span.start_col + 1, // Convert to 1-indexed
354                    end_line: code_span.line,
355                    end_column: code_span.end_col, // Don't add 1 to match test expectation
356                    message: "Spaces inside code span elements".to_string(),
357                    severity: Severity::Warning,
358                    fix: Some(Fix::new(
359                        code_span.byte_offset..code_span.byte_end,
360                        format!(
361                            "{}{}{}",
362                            "`".repeat(code_span.backtick_count),
363                            trimmed,
364                            "`".repeat(code_span.backtick_count)
365                        ),
366                    )),
367                });
368            }
369        }
370
371        Ok(warnings)
372    }
373
374    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
375        let content = ctx.content;
376        if !self.enabled {
377            return Ok(content.to_string());
378        }
379
380        // Early return if no backticks in content
381        if !content.contains('`') {
382            return Ok(content.to_string());
383        }
384
385        // Get warnings to identify what needs to be fixed
386        let warnings = self.check(ctx)?;
387        let warnings =
388            crate::utils::fix_utils::filter_warnings_by_inline_config(warnings, ctx.inline_config(), self.name());
389        if warnings.is_empty() {
390            return Ok(content.to_string());
391        }
392
393        // Collect all fixes and sort by position (reverse order to avoid position shifts)
394        let mut fixes: Vec<(std::ops::Range<usize>, String)> = warnings
395            .into_iter()
396            .filter_map(|w| w.fix.map(|f| (f.range, f.replacement)))
397            .collect();
398
399        fixes.sort_by_key(|(range, _)| std::cmp::Reverse(range.start));
400
401        // Apply fixes - only allocate string when we have fixes to apply
402        let mut result = content.to_string();
403        for (range, replacement) in fixes {
404            result.replace_range(range, &replacement);
405        }
406
407        Ok(result)
408    }
409
410    /// Check if content is likely to have code spans
411    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
412        !ctx.likely_has_code()
413    }
414
415    fn as_any(&self) -> &dyn std::any::Any {
416        self
417    }
418
419    fn from_config(_config: &crate::config::Config) -> Box<dyn Rule>
420    where
421        Self: Sized,
422    {
423        Box::new(MD038NoSpaceInCode { enabled: true })
424    }
425}
426
427#[cfg(test)]
428mod tests {
429    use super::*;
430
431    #[test]
432    fn test_md038_readme_false_positives() {
433        // These are the exact cases from README.md that are incorrectly flagged
434        let rule = MD038NoSpaceInCode::new();
435        let valid_cases = vec![
436            "3. `pyproject.toml` (must contain `[tool.rumdl]` section)",
437            "#### Effective Configuration (`rumdl config`)",
438            "- Blue: `.rumdl.toml`",
439            "### Defaults Only (`rumdl config --defaults`)",
440        ];
441
442        for case in valid_cases {
443            let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
444            let result = rule.check(&ctx).unwrap();
445            assert!(
446                result.is_empty(),
447                "Should not flag code spans without leading/trailing spaces: '{}'. Got {} warnings",
448                case,
449                result.len()
450            );
451        }
452    }
453
454    #[test]
455    fn test_md038_valid() {
456        let rule = MD038NoSpaceInCode::new();
457        let valid_cases = vec![
458            "This is `code` in a sentence.",
459            "This is a `longer code span` in a sentence.",
460            "This is `code with internal spaces` which is fine.",
461            "Code span at `end of line`",
462            "`Start of line` code span",
463            "Multiple `code spans` in `one line` are fine",
464            "Code span with `symbols: !@#$%^&*()`",
465            "Empty code span `` is technically valid",
466        ];
467        for case in valid_cases {
468            let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
469            let result = rule.check(&ctx).unwrap();
470            assert!(result.is_empty(), "Valid case should not have warnings: {case}");
471        }
472    }
473
474    #[test]
475    fn test_md038_invalid() {
476        let rule = MD038NoSpaceInCode::new();
477        // Flag cases that violate CommonMark:
478        // - Space only at start (no matching end space)
479        // - Space only at end (no matching start space)
480        // - Multiple spaces at start or end (extra space will remain after CommonMark stripping)
481        let invalid_cases = vec![
482            // Unbalanced: only leading space
483            "This is ` code` with leading space.",
484            // Unbalanced: only trailing space
485            "This is `code ` with trailing space.",
486            // Multiple leading spaces (one will remain after CommonMark strips one)
487            "This is `  code ` with double leading space.",
488            // Multiple trailing spaces (one will remain after CommonMark strips one)
489            "This is ` code  ` with double trailing space.",
490            // Multiple spaces both sides
491            "This is `  code  ` with double spaces both sides.",
492        ];
493        for case in invalid_cases {
494            let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
495            let result = rule.check(&ctx).unwrap();
496            assert!(!result.is_empty(), "Invalid case should have warnings: {case}");
497        }
498    }
499
500    #[test]
501    fn test_md038_valid_commonmark_stripping() {
502        let rule = MD038NoSpaceInCode::new();
503        // These cases have exactly ONE space at start AND ONE at end.
504        // CommonMark strips both, so these should NOT be flagged.
505        // See: https://spec.commonmark.org/0.31.2/#code-spans
506        let valid_cases = vec![
507            "Type ` y ` to confirm.",
508            "Use ` git commit -m \"message\" ` to commit.",
509            "The variable ` $HOME ` contains home path.",
510            "The pattern ` *.txt ` matches text files.",
511            "This is ` random word ` with unnecessary spaces.",
512            "Text with ` plain text ` is valid.",
513            "Code with ` just code ` here.",
514            "Multiple ` word ` spans with ` text ` in one line.",
515            "This is ` code ` with both leading and trailing single space.",
516            "Use ` - ` as separator.",
517        ];
518        for case in valid_cases {
519            let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
520            let result = rule.check(&ctx).unwrap();
521            assert!(
522                result.is_empty(),
523                "Single space on each side should not be flagged (CommonMark strips them): {case}"
524            );
525        }
526    }
527
528    #[test]
529    fn test_md038_fix() {
530        let rule = MD038NoSpaceInCode::new();
531        // Only cases that violate CommonMark should be fixed
532        let test_cases = vec![
533            // Unbalanced: only leading space - should be fixed
534            (
535                "This is ` code` with leading space.",
536                "This is `code` with leading space.",
537            ),
538            // Unbalanced: only trailing space - should be fixed
539            (
540                "This is `code ` with trailing space.",
541                "This is `code` with trailing space.",
542            ),
543            // Single space on both sides - NOT fixed (valid per CommonMark)
544            (
545                "This is ` code ` with both spaces.",
546                "This is ` code ` with both spaces.", // unchanged
547            ),
548            // Double leading space - should be fixed
549            (
550                "This is `  code ` with double leading space.",
551                "This is `code` with double leading space.",
552            ),
553            // Mixed: one valid (single space both), one invalid (trailing only)
554            (
555                "Multiple ` code ` and `spans ` to fix.",
556                "Multiple ` code ` and `spans` to fix.", // only spans is fixed
557            ),
558        ];
559        for (input, expected) in test_cases {
560            let ctx = crate::lint_context::LintContext::new(input, crate::config::MarkdownFlavor::Standard, None);
561            let result = rule.fix(&ctx).unwrap();
562            assert_eq!(result, expected, "Fix did not produce expected output for: {input}");
563        }
564    }
565
566    #[test]
567    fn test_check_invalid_leading_space() {
568        let rule = MD038NoSpaceInCode::new();
569        let input = "This has a ` leading space` in code";
570        let ctx = crate::lint_context::LintContext::new(input, crate::config::MarkdownFlavor::Standard, None);
571        let result = rule.check(&ctx).unwrap();
572        assert_eq!(result.len(), 1);
573        assert_eq!(result[0].line, 1);
574        assert!(result[0].fix.is_some());
575    }
576
577    #[test]
578    fn test_code_span_parsing_nested_backticks() {
579        let content = "Code with ` nested `code` example ` should preserve backticks";
580        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
581
582        println!("Content: {content}");
583        println!("Code spans found:");
584        let code_spans = ctx.code_spans();
585        for (i, span) in code_spans.iter().enumerate() {
586            println!(
587                "  Span {}: line={}, col={}-{}, backticks={}, content='{}'",
588                i, span.line, span.start_col, span.end_col, span.backtick_count, span.content
589            );
590        }
591
592        // This test reveals the issue - we're getting multiple separate code spans instead of one
593        assert_eq!(code_spans.len(), 2, "Should parse as 2 code spans");
594    }
595
596    #[test]
597    fn test_nested_backtick_detection() {
598        let rule = MD038NoSpaceInCode::new();
599
600        // Test that code spans with backticks are skipped
601        let content = "Code with `` `backticks` inside `` should not be flagged";
602        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
603        let result = rule.check(&ctx).unwrap();
604        assert!(result.is_empty(), "Code spans with backticks should be skipped");
605    }
606
607    #[test]
608    fn test_quarto_inline_r_code() {
609        // Test that Quarto-specific R code exception works
610        let rule = MD038NoSpaceInCode::new();
611
612        // Test inline R code - should NOT trigger warning in Quarto flavor
613        // The key pattern is "r " followed by code
614        let content = r#"The result is `r nchar("test")` which equals 4."#;
615
616        // Quarto flavor should allow R code
617        let ctx_quarto = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Quarto, None);
618        let result_quarto = rule.check(&ctx_quarto).unwrap();
619        assert!(
620            result_quarto.is_empty(),
621            "Quarto inline R code should not trigger warnings. Got {} warnings",
622            result_quarto.len()
623        );
624
625        // Test that invalid code spans (not matching CommonMark stripping) still get flagged in Quarto
626        // Use only trailing space - this violates CommonMark (no balanced stripping)
627        let content_other = "This has `plain text ` with trailing space.";
628        let ctx_other =
629            crate::lint_context::LintContext::new(content_other, crate::config::MarkdownFlavor::Quarto, None);
630        let result_other = rule.check(&ctx_other).unwrap();
631        assert_eq!(
632            result_other.len(),
633            1,
634            "Quarto should still flag non-R code spans with improper spaces"
635        );
636    }
637
638    /// Comprehensive tests for Hugo template syntax detection
639    ///
640    /// These tests ensure MD038 correctly handles Hugo template syntax patterns
641    /// without false positives, while maintaining correct detection of actual
642    /// code span spacing issues.
643    #[test]
644    fn test_hugo_template_syntax_comprehensive() {
645        let rule = MD038NoSpaceInCode::new();
646
647        // ===== VALID HUGO TEMPLATE SYNTAX (Should NOT trigger warnings) =====
648
649        // Basic Hugo shortcode patterns
650        let valid_hugo_cases = vec![
651            // Raw HTML shortcode
652            (
653                "{{raw `\n\tgo list -f '{{.DefaultGODEBUG}}' my/main/package\n`}}",
654                "Multi-line raw shortcode",
655            ),
656            (
657                "Some text {{raw ` code `}} more text",
658                "Inline raw shortcode with spaces",
659            ),
660            ("{{raw `code`}}", "Raw shortcode without spaces"),
661            // Partial shortcode
662            ("{{< ` code ` >}}", "Partial shortcode with spaces"),
663            ("{{< `code` >}}", "Partial shortcode without spaces"),
664            // Shortcode with percent
665            ("{{% ` code ` %}}", "Percent shortcode with spaces"),
666            ("{{% `code` %}}", "Percent shortcode without spaces"),
667            // Generic shortcode
668            ("{{ ` code ` }}", "Generic shortcode with spaces"),
669            ("{{ `code` }}", "Generic shortcode without spaces"),
670            // Shortcodes with parameters (common Hugo pattern)
671            ("{{< highlight go `code` >}}", "Shortcode with highlight parameter"),
672            ("{{< code `go list` >}}", "Shortcode with code parameter"),
673            // Multi-line Hugo templates
674            ("{{raw `\n\tcommand here\n\tmore code\n`}}", "Multi-line raw template"),
675            ("{{< highlight `\ncode here\n` >}}", "Multi-line highlight template"),
676            // Hugo templates with nested Go template syntax
677            (
678                "{{raw `\n\t{{.Variable}}\n\t{{range .Items}}\n`}}",
679                "Nested Go template syntax",
680            ),
681            // Edge case: Hugo template at start of line
682            ("{{raw `code`}}", "Hugo template at line start"),
683            // Edge case: Hugo template at end of line
684            ("Text {{raw `code`}}", "Hugo template at end of line"),
685            // Edge case: Multiple Hugo templates
686            ("{{raw `code1`}} and {{raw `code2`}}", "Multiple Hugo templates"),
687        ];
688
689        for (case, description) in valid_hugo_cases {
690            let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
691            let result = rule.check(&ctx).unwrap();
692            assert!(
693                result.is_empty(),
694                "Hugo template syntax should not trigger MD038 warnings: {description} - {case}"
695            );
696        }
697
698        // ===== FALSE POSITIVE PREVENTION (Non-Hugo asymmetric spaces should be flagged) =====
699
700        // These have asymmetric spaces (leading-only or trailing-only) and should be flagged
701        // Per CommonMark spec: symmetric single-space pairs are stripped and NOT flagged
702        let should_be_flagged = vec![
703            ("This is ` code` with leading space.", "Leading space only"),
704            ("This is `code ` with trailing space.", "Trailing space only"),
705            ("Text `  code ` here", "Extra leading space (asymmetric)"),
706            ("Text ` code  ` here", "Extra trailing space (asymmetric)"),
707            ("Text `  code` here", "Double leading, no trailing"),
708            ("Text `code  ` here", "No leading, double trailing"),
709        ];
710
711        for (case, description) in should_be_flagged {
712            let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
713            let result = rule.check(&ctx).unwrap();
714            assert!(
715                !result.is_empty(),
716                "Should flag asymmetric space code spans: {description} - {case}"
717            );
718        }
719
720        // ===== COMMONMARK SYMMETRIC SPACE BEHAVIOR (Should NOT be flagged) =====
721
722        // Per CommonMark 0.31.2: When a code span has exactly one space at start AND end,
723        // those spaces are stripped from the output. This is intentional, not an error.
724        // These cases should NOT trigger MD038.
725        let symmetric_single_space = vec![
726            ("Text ` code ` here", "Symmetric single space - CommonMark strips"),
727            ("{raw ` code `}", "Looks like Hugo but missing opening {{"),
728            ("raw ` code `}}", "Missing opening {{ - but symmetric spaces"),
729        ];
730
731        for (case, description) in symmetric_single_space {
732            let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
733            let result = rule.check(&ctx).unwrap();
734            assert!(
735                result.is_empty(),
736                "CommonMark symmetric spaces should NOT be flagged: {description} - {case}"
737            );
738        }
739
740        // ===== EDGE CASES: Unicode and Special Characters =====
741
742        let unicode_cases = vec![
743            ("{{raw `\n\t你好世界\n`}}", "Unicode in Hugo template"),
744            ("{{raw `\n\t🎉 emoji\n`}}", "Emoji in Hugo template"),
745            ("{{raw `\n\tcode with \"quotes\"\n`}}", "Quotes in Hugo template"),
746            (
747                "{{raw `\n\tcode with 'single quotes'\n`}}",
748                "Single quotes in Hugo template",
749            ),
750        ];
751
752        for (case, description) in unicode_cases {
753            let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
754            let result = rule.check(&ctx).unwrap();
755            assert!(
756                result.is_empty(),
757                "Hugo templates with special characters should not trigger warnings: {description} - {case}"
758            );
759        }
760
761        // ===== BOUNDARY CONDITIONS =====
762
763        // Minimum valid Hugo pattern
764        assert!(
765            rule.check(&crate::lint_context::LintContext::new(
766                "{{ ` ` }}",
767                crate::config::MarkdownFlavor::Standard,
768                None
769            ))
770            .unwrap()
771            .is_empty(),
772            "Minimum Hugo pattern should be valid"
773        );
774
775        // Hugo template with only whitespace
776        assert!(
777            rule.check(&crate::lint_context::LintContext::new(
778                "{{raw `\n\t\n`}}",
779                crate::config::MarkdownFlavor::Standard,
780                None
781            ))
782            .unwrap()
783            .is_empty(),
784            "Hugo template with only whitespace should be valid"
785        );
786    }
787
788    /// Test interaction with other markdown elements
789    #[test]
790    fn test_hugo_template_with_other_markdown() {
791        let rule = MD038NoSpaceInCode::new();
792
793        // Hugo template inside a list
794        let content = r#"1. First item
7952. Second item with {{raw `code`}} template
7963. Third item"#;
797        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
798        let result = rule.check(&ctx).unwrap();
799        assert!(result.is_empty(), "Hugo template in list should not trigger warnings");
800
801        // Hugo template in blockquote
802        let content = r#"> Quote with {{raw `code`}} template"#;
803        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
804        let result = rule.check(&ctx).unwrap();
805        assert!(
806            result.is_empty(),
807            "Hugo template in blockquote should not trigger warnings"
808        );
809
810        // Hugo template near regular code span (should flag the regular one)
811        let content = r#"{{raw `code`}} and ` bad code` here"#;
812        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
813        let result = rule.check(&ctx).unwrap();
814        assert_eq!(result.len(), 1, "Should flag regular code span but not Hugo template");
815    }
816
817    /// Performance test: Many Hugo templates
818    #[test]
819    fn test_hugo_template_performance() {
820        let rule = MD038NoSpaceInCode::new();
821
822        // Create content with many Hugo templates
823        let mut content = String::new();
824        for i in 0..100 {
825            content.push_str(&format!("{{{{raw `code{i}\n`}}}}\n"));
826        }
827
828        let ctx = crate::lint_context::LintContext::new(&content, crate::config::MarkdownFlavor::Standard, None);
829        let start = std::time::Instant::now();
830        let result = rule.check(&ctx).unwrap();
831        let duration = start.elapsed();
832
833        assert!(result.is_empty(), "Many Hugo templates should not trigger warnings");
834        assert!(
835            duration.as_millis() < 1000,
836            "Performance test: Should process 100 Hugo templates in <1s, took {duration:?}"
837        );
838    }
839
840    #[test]
841    fn test_mkdocs_inline_hilite_not_flagged() {
842        // InlineHilite syntax: `#!language code` should NOT be flagged
843        // The space after the language specifier is legitimate
844        let rule = MD038NoSpaceInCode::new();
845
846        let valid_cases = vec![
847            "`#!python print('hello')`",
848            "`#!js alert('hi')`",
849            "`#!c++ cout << x;`",
850            "Use `#!python import os` to import modules",
851            "`#!bash echo $HOME`",
852        ];
853
854        for case in valid_cases {
855            let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::MkDocs, None);
856            let result = rule.check(&ctx).unwrap();
857            assert!(
858                result.is_empty(),
859                "InlineHilite syntax should not be flagged in MkDocs: {case}"
860            );
861        }
862
863        // Test that InlineHilite IS flagged in Standard flavor (not MkDocs-aware)
864        let content = "`#!python print('hello')`";
865        let ctx_standard =
866            crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
867        let result_standard = rule.check(&ctx_standard).unwrap();
868        // In standard flavor, the content " print('hello')" has no special meaning
869        // But since "#!python print('hello')" doesn't have leading/trailing spaces, it's valid!
870        assert!(
871            result_standard.is_empty(),
872            "InlineHilite with no extra spaces should not be flagged even in Standard flavor"
873        );
874    }
875
876    #[test]
877    fn test_multibyte_utf8_no_panic() {
878        // Regression test: ensure multi-byte UTF-8 characters don't cause panics
879        // when checking for nested backticks between code spans.
880        // These are real examples from the-art-of-command-line translations.
881        let rule = MD038NoSpaceInCode::new();
882
883        // Greek text with code spans
884        let greek = "- Χρήσιμα εργαλεία της γραμμής εντολών είναι τα `ping`,` ipconfig`, `traceroute` και `netstat`.";
885        let ctx = crate::lint_context::LintContext::new(greek, crate::config::MarkdownFlavor::Standard, None);
886        let result = rule.check(&ctx);
887        assert!(result.is_ok(), "Greek text should not panic");
888
889        // Chinese text with code spans
890        let chinese = "- 當你需要對文字檔案做集合交、並、差運算時,`sort`/`uniq` 很有幫助。";
891        let ctx = crate::lint_context::LintContext::new(chinese, crate::config::MarkdownFlavor::Standard, None);
892        let result = rule.check(&ctx);
893        assert!(result.is_ok(), "Chinese text should not panic");
894
895        // Cyrillic/Ukrainian text with code spans
896        let cyrillic = "- Основи роботи з файлами: `ls` і `ls -l`, `less`, `head`,` tail` і `tail -f`.";
897        let ctx = crate::lint_context::LintContext::new(cyrillic, crate::config::MarkdownFlavor::Standard, None);
898        let result = rule.check(&ctx);
899        assert!(result.is_ok(), "Cyrillic text should not panic");
900
901        // Mixed multi-byte with multiple code spans on same line
902        let mixed = "使用 `git` 命令和 `npm` 工具来管理项目,可以用 `docker` 容器化。";
903        let ctx = crate::lint_context::LintContext::new(mixed, crate::config::MarkdownFlavor::Standard, None);
904        let result = rule.check(&ctx);
905        assert!(
906            result.is_ok(),
907            "Mixed Chinese text with multiple code spans should not panic"
908        );
909    }
910
911    // ==================== Obsidian Dataview Plugin Tests ====================
912
913    /// Test that Dataview inline DQL expressions are not flagged in Obsidian flavor
914    #[test]
915    fn test_obsidian_dataview_inline_dql_not_flagged() {
916        let rule = MD038NoSpaceInCode::new();
917
918        // Basic inline DQL expressions - should NOT be flagged in Obsidian
919        let valid_dql_cases = vec![
920            "`= this.file.name`",
921            "`= date(today)`",
922            "`= [[Page]].field`",
923            "`= choice(condition, \"yes\", \"no\")`",
924            "`= this.file.mtime`",
925            "`= this.file.ctime`",
926            "`= this.file.path`",
927            "`= this.file.folder`",
928            "`= this.file.size`",
929            "`= this.file.ext`",
930            "`= this.file.link`",
931            "`= this.file.outlinks`",
932            "`= this.file.inlinks`",
933            "`= this.file.tags`",
934        ];
935
936        for case in valid_dql_cases {
937            let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
938            let result = rule.check(&ctx).unwrap();
939            assert!(
940                result.is_empty(),
941                "Dataview DQL expression should not be flagged in Obsidian: {case}"
942            );
943        }
944    }
945
946    /// Test that Dataview inline DataviewJS expressions are not flagged in Obsidian flavor
947    #[test]
948    fn test_obsidian_dataview_inline_dvjs_not_flagged() {
949        let rule = MD038NoSpaceInCode::new();
950
951        // Inline DataviewJS expressions - should NOT be flagged in Obsidian
952        let valid_dvjs_cases = vec![
953            "`$= dv.current().file.mtime`",
954            "`$= dv.pages().length`",
955            "`$= dv.current()`",
956            "`$= dv.pages('#tag').length`",
957            "`$= dv.pages('\"folder\"').length`",
958            "`$= dv.current().file.name`",
959            "`$= dv.current().file.path`",
960            "`$= dv.current().file.folder`",
961            "`$= dv.current().file.link`",
962        ];
963
964        for case in valid_dvjs_cases {
965            let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
966            let result = rule.check(&ctx).unwrap();
967            assert!(
968                result.is_empty(),
969                "Dataview JS expression should not be flagged in Obsidian: {case}"
970            );
971        }
972    }
973
974    /// Test complex Dataview expressions with nested parentheses
975    #[test]
976    fn test_obsidian_dataview_complex_expressions() {
977        let rule = MD038NoSpaceInCode::new();
978
979        let complex_cases = vec![
980            // Nested function calls
981            "`= sum(filter(pages, (p) => p.done))`",
982            "`= length(filter(file.tags, (t) => startswith(t, \"project\")))`",
983            // choice() function
984            "`= choice(x > 5, \"big\", \"small\")`",
985            "`= choice(this.status = \"done\", \"✅\", \"⏳\")`",
986            // date functions
987            "`= date(today) - dur(7 days)`",
988            "`= dateformat(this.file.mtime, \"yyyy-MM-dd\")`",
989            // Math expressions
990            "`= sum(rows.amount)`",
991            "`= round(average(rows.score), 2)`",
992            "`= min(rows.priority)`",
993            "`= max(rows.priority)`",
994            // String operations
995            "`= join(this.file.tags, \", \")`",
996            "`= replace(this.title, \"-\", \" \")`",
997            "`= lower(this.file.name)`",
998            "`= upper(this.file.name)`",
999            // List operations
1000            "`= length(this.file.outlinks)`",
1001            "`= contains(this.file.tags, \"important\")`",
1002            // Link references
1003            "`= [[Page Name]].field`",
1004            "`= [[Folder/Subfolder/Page]].nested.field`",
1005            // Conditional expressions
1006            "`= default(this.status, \"unknown\")`",
1007            "`= coalesce(this.priority, this.importance, 0)`",
1008        ];
1009
1010        for case in complex_cases {
1011            let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
1012            let result = rule.check(&ctx).unwrap();
1013            assert!(
1014                result.is_empty(),
1015                "Complex Dataview expression should not be flagged in Obsidian: {case}"
1016            );
1017        }
1018    }
1019
1020    /// Test that complex DataviewJS expressions with method chains are not flagged
1021    #[test]
1022    fn test_obsidian_dataviewjs_method_chains() {
1023        let rule = MD038NoSpaceInCode::new();
1024
1025        let method_chain_cases = vec![
1026            "`$= dv.pages().where(p => p.status).length`",
1027            "`$= dv.pages('#project').where(p => !p.done).length`",
1028            "`$= dv.pages().filter(p => p.file.day).sort(p => p.file.mtime, 'desc').limit(5)`",
1029            "`$= dv.pages('\"folder\"').map(p => p.file.link).join(', ')`",
1030            "`$= dv.current().file.tasks.where(t => !t.completed).length`",
1031            "`$= dv.pages().flatMap(p => p.file.tags).distinct().sort()`",
1032            "`$= dv.page('Index').children.map(p => p.title)`",
1033            "`$= dv.pages().groupBy(p => p.status).map(g => [g.key, g.rows.length])`",
1034        ];
1035
1036        for case in method_chain_cases {
1037            let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
1038            let result = rule.check(&ctx).unwrap();
1039            assert!(
1040                result.is_empty(),
1041                "DataviewJS method chain should not be flagged in Obsidian: {case}"
1042            );
1043        }
1044    }
1045
1046    /// Test Dataview-like patterns in Standard flavor
1047    ///
1048    /// Note: The actual content `= this.file.name` starts with `=`, not whitespace,
1049    /// so it doesn't have a leading space issue. Dataview expressions only become
1050    /// relevant when their content would otherwise be flagged.
1051    ///
1052    /// To properly test the difference, we need patterns that have leading whitespace
1053    /// issues that would be skipped in Obsidian but flagged in Standard.
1054    #[test]
1055    fn test_standard_flavor_vs_obsidian_dataview() {
1056        let rule = MD038NoSpaceInCode::new();
1057
1058        // These Dataview expressions don't have leading whitespace (they start with "=")
1059        // so they wouldn't be flagged in ANY flavor
1060        let no_issue_cases = vec!["`= this.file.name`", "`$= dv.current()`"];
1061
1062        for case in no_issue_cases {
1063            // Standard flavor - no issue because content doesn't start with whitespace
1064            let ctx_std = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
1065            let result_std = rule.check(&ctx_std).unwrap();
1066            assert!(
1067                result_std.is_empty(),
1068                "Dataview expression without leading space shouldn't be flagged in Standard: {case}"
1069            );
1070
1071            // Obsidian flavor - also no issue
1072            let ctx_obs = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
1073            let result_obs = rule.check(&ctx_obs).unwrap();
1074            assert!(
1075                result_obs.is_empty(),
1076                "Dataview expression shouldn't be flagged in Obsidian: {case}"
1077            );
1078        }
1079
1080        // Test that regular code with leading/trailing spaces is still flagged in both flavors
1081        // (when not matching Dataview pattern)
1082        let space_issues = vec![
1083            "` code`", // Leading space, no trailing
1084            "`code `", // Trailing space, no leading
1085        ];
1086
1087        for case in space_issues {
1088            // Standard flavor - should be flagged
1089            let ctx_std = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
1090            let result_std = rule.check(&ctx_std).unwrap();
1091            assert!(
1092                !result_std.is_empty(),
1093                "Code with spacing issue should be flagged in Standard: {case}"
1094            );
1095
1096            // Obsidian flavor - should also be flagged (not a Dataview pattern)
1097            let ctx_obs = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
1098            let result_obs = rule.check(&ctx_obs).unwrap();
1099            assert!(
1100                !result_obs.is_empty(),
1101                "Code with spacing issue should be flagged in Obsidian (not Dataview): {case}"
1102            );
1103        }
1104    }
1105
1106    /// Test that regular code spans with leading space are still flagged in Obsidian
1107    #[test]
1108    fn test_obsidian_still_flags_regular_code_spans_with_space() {
1109        let rule = MD038NoSpaceInCode::new();
1110
1111        // These are NOT Dataview expressions, just regular code spans with leading space
1112        // They should still be flagged even in Obsidian flavor
1113        let invalid_cases = [
1114            "` regular code`", // Space at start, not Dataview
1115            "`code `",         // Space at end
1116            "` code `",        // This is valid per CommonMark (symmetric single space)
1117            "`  code`",        // Double space at start (not Dataview pattern)
1118        ];
1119
1120        // Only the asymmetric cases should be flagged
1121        let expected_flags = [
1122            true,  // ` regular code` - leading space, no trailing
1123            true,  // `code ` - trailing space, no leading
1124            false, // ` code ` - symmetric single space (CommonMark valid)
1125            true,  // `  code` - double leading space
1126        ];
1127
1128        for (case, should_flag) in invalid_cases.iter().zip(expected_flags.iter()) {
1129            let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
1130            let result = rule.check(&ctx).unwrap();
1131            if *should_flag {
1132                assert!(
1133                    !result.is_empty(),
1134                    "Non-Dataview code span with spacing issue should be flagged in Obsidian: {case}"
1135                );
1136            } else {
1137                assert!(
1138                    result.is_empty(),
1139                    "CommonMark-valid symmetric spacing should not be flagged: {case}"
1140                );
1141            }
1142        }
1143    }
1144
1145    /// Test edge cases for Dataview pattern detection
1146    #[test]
1147    fn test_obsidian_dataview_edge_cases() {
1148        let rule = MD038NoSpaceInCode::new();
1149
1150        // Valid Dataview patterns
1151        let valid_cases = vec![
1152            ("`= x`", true),                         // Minimal DQL
1153            ("`$= x`", true),                        // Minimal DVJS
1154            ("`= `", true),                          // Just equals-space (empty expression)
1155            ("`$= `", true),                         // Just dollar-equals-space (empty expression)
1156            ("`=x`", false),                         // No space after = (not Dataview, and no leading whitespace issue)
1157            ("`$=x`", false),       // No space after $= (not Dataview, and no leading whitespace issue)
1158            ("`= [[Link]]`", true), // Link in expression
1159            ("`= this`", true),     // Simple this reference
1160            ("`$= dv`", true),      // Just dv object reference
1161            ("`= 1 + 2`", true),    // Math expression
1162            ("`$= 1 + 2`", true),   // Math in DVJS
1163            ("`= \"string\"`", true), // String literal
1164            ("`$= 'string'`", true), // Single-quoted string
1165            ("`= this.field ?? \"default\"`", true), // Null coalescing
1166            ("`$= dv?.pages()`", true), // Optional chaining
1167        ];
1168
1169        for (case, should_be_valid) in valid_cases {
1170            let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
1171            let result = rule.check(&ctx).unwrap();
1172            if should_be_valid {
1173                assert!(
1174                    result.is_empty(),
1175                    "Valid Dataview expression should not be flagged: {case}"
1176                );
1177            } else {
1178                // These might or might not be flagged depending on other MD038 rules
1179                // We just verify they don't crash
1180                let _ = result;
1181            }
1182        }
1183    }
1184
1185    /// Test Dataview expressions in context (mixed with regular markdown)
1186    #[test]
1187    fn test_obsidian_dataview_in_context() {
1188        let rule = MD038NoSpaceInCode::new();
1189
1190        // Document with mixed Dataview and regular code spans
1191        let content = r#"# My Note
1192
1193The file name is `= this.file.name` and it was created on `= this.file.ctime`.
1194
1195Regular code: `println!("hello")` and `let x = 5;`
1196
1197DataviewJS count: `$= dv.pages('#project').length` projects found.
1198
1199More regular code with issue: ` bad code` should be flagged.
1200"#;
1201
1202        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
1203        let result = rule.check(&ctx).unwrap();
1204
1205        // Should only flag ` bad code` (line 9)
1206        assert_eq!(
1207            result.len(),
1208            1,
1209            "Should only flag the regular code span with leading space, not Dataview expressions"
1210        );
1211        assert_eq!(result[0].line, 9, "Warning should be on line 9");
1212    }
1213
1214    /// Test that Dataview expressions in code blocks are properly handled
1215    #[test]
1216    fn test_obsidian_dataview_in_code_blocks() {
1217        let rule = MD038NoSpaceInCode::new();
1218
1219        // Dataview expressions inside fenced code blocks should be ignored
1220        // (because they're inside code blocks, not because of Dataview logic)
1221        let content = r#"# Example
1222
1223```
1224`= this.file.name`
1225`$= dv.current()`
1226```
1227
1228Regular paragraph with `= this.file.name` Dataview.
1229"#;
1230
1231        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
1232        let result = rule.check(&ctx).unwrap();
1233
1234        // Should not flag anything - code blocks are skipped, and inline Dataview is valid
1235        assert!(
1236            result.is_empty(),
1237            "Dataview in code blocks should be ignored, inline Dataview should be valid"
1238        );
1239    }
1240
1241    /// Test Dataview with Unicode content
1242    #[test]
1243    fn test_obsidian_dataview_unicode() {
1244        let rule = MD038NoSpaceInCode::new();
1245
1246        let unicode_cases = vec![
1247            "`= this.日本語`",                  // Japanese field name
1248            "`= this.中文字段`",                // Chinese field name
1249            "`= \"Привет мир\"`",               // Russian string
1250            "`$= dv.pages('#日本語タグ')`",     // Japanese tag
1251            "`= choice(true, \"✅\", \"❌\")`", // Emoji in strings
1252            "`= this.file.name + \" 📝\"`",     // Emoji concatenation
1253        ];
1254
1255        for case in unicode_cases {
1256            let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
1257            let result = rule.check(&ctx).unwrap();
1258            assert!(
1259                result.is_empty(),
1260                "Unicode Dataview expression should not be flagged: {case}"
1261            );
1262        }
1263    }
1264
1265    /// Test that Dataview detection doesn't break regular equals patterns
1266    #[test]
1267    fn test_obsidian_regular_equals_still_works() {
1268        let rule = MD038NoSpaceInCode::new();
1269
1270        // Regular code with equals signs should still work normally
1271        let valid_regular_cases = vec![
1272            "`x = 5`",       // Assignment (no leading space)
1273            "`a == b`",      // Equality check
1274            "`x >= 10`",     // Comparison
1275            "`let x = 10`",  // Variable declaration
1276            "`const y = 5`", // Const declaration
1277        ];
1278
1279        for case in valid_regular_cases {
1280            let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
1281            let result = rule.check(&ctx).unwrap();
1282            assert!(
1283                result.is_empty(),
1284                "Regular code with equals should not be flagged: {case}"
1285            );
1286        }
1287    }
1288
1289    /// Test fix behavior doesn't break Dataview expressions
1290    #[test]
1291    fn test_obsidian_dataview_fix_preserves_expressions() {
1292        let rule = MD038NoSpaceInCode::new();
1293
1294        // Content with Dataview expressions and one fixable issue
1295        let content = "Dataview: `= this.file.name` and bad: ` fixme`";
1296        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
1297        let fixed = rule.fix(&ctx).unwrap();
1298
1299        // Should fix ` fixme` but preserve `= this.file.name`
1300        assert!(
1301            fixed.contains("`= this.file.name`"),
1302            "Dataview expression should be preserved after fix"
1303        );
1304        assert!(
1305            fixed.contains("`fixme`"),
1306            "Regular code span should be fixed (space removed)"
1307        );
1308        assert!(!fixed.contains("` fixme`"), "Bad code span should have been fixed");
1309    }
1310
1311    /// Test multiple Dataview expressions on same line
1312    #[test]
1313    fn test_obsidian_multiple_dataview_same_line() {
1314        let rule = MD038NoSpaceInCode::new();
1315
1316        let content = "Created: `= this.file.ctime` | Modified: `= this.file.mtime` | Count: `$= dv.pages().length`";
1317        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
1318        let result = rule.check(&ctx).unwrap();
1319
1320        assert!(
1321            result.is_empty(),
1322            "Multiple Dataview expressions on same line should all be valid"
1323        );
1324    }
1325
1326    /// Performance test: Many Dataview expressions
1327    #[test]
1328    fn test_obsidian_dataview_performance() {
1329        let rule = MD038NoSpaceInCode::new();
1330
1331        // Create content with many Dataview expressions
1332        let mut content = String::new();
1333        for i in 0..100 {
1334            content.push_str(&format!("Field {i}: `= this.field{i}` | JS: `$= dv.current().f{i}`\n"));
1335        }
1336
1337        let ctx = crate::lint_context::LintContext::new(&content, crate::config::MarkdownFlavor::Obsidian, None);
1338        let start = std::time::Instant::now();
1339        let result = rule.check(&ctx).unwrap();
1340        let duration = start.elapsed();
1341
1342        assert!(result.is_empty(), "All Dataview expressions should be valid");
1343        assert!(
1344            duration.as_millis() < 1000,
1345            "Performance test: Should process 200 Dataview expressions in <1s, took {duration:?}"
1346        );
1347    }
1348
1349    /// Test is_dataview_expression helper function directly
1350    #[test]
1351    fn test_is_dataview_expression_helper() {
1352        // Valid Dataview patterns
1353        assert!(MD038NoSpaceInCode::is_dataview_expression("= this.file.name"));
1354        assert!(MD038NoSpaceInCode::is_dataview_expression("= "));
1355        assert!(MD038NoSpaceInCode::is_dataview_expression("$= dv.current()"));
1356        assert!(MD038NoSpaceInCode::is_dataview_expression("$= "));
1357        assert!(MD038NoSpaceInCode::is_dataview_expression("= x"));
1358        assert!(MD038NoSpaceInCode::is_dataview_expression("$= x"));
1359
1360        // Invalid Dataview patterns
1361        assert!(!MD038NoSpaceInCode::is_dataview_expression("=")); // No space after =
1362        assert!(!MD038NoSpaceInCode::is_dataview_expression("$=")); // No space after $=
1363        assert!(!MD038NoSpaceInCode::is_dataview_expression("=x")); // No space
1364        assert!(!MD038NoSpaceInCode::is_dataview_expression("$=x")); // No space
1365        assert!(!MD038NoSpaceInCode::is_dataview_expression(" = x")); // Leading space before =
1366        assert!(!MD038NoSpaceInCode::is_dataview_expression("x = 5")); // Assignment, not Dataview
1367        assert!(!MD038NoSpaceInCode::is_dataview_expression("== x")); // Double equals
1368        assert!(!MD038NoSpaceInCode::is_dataview_expression("")); // Empty
1369        assert!(!MD038NoSpaceInCode::is_dataview_expression("regular")); // Regular text
1370    }
1371
1372    /// Test Dataview expressions work alongside other Obsidian features (tags)
1373    #[test]
1374    fn test_obsidian_dataview_with_tags() {
1375        let rule = MD038NoSpaceInCode::new();
1376
1377        // Document using both Dataview and Obsidian tags
1378        let content = r#"# Project Status
1379
1380Tags: #project #active
1381
1382Status: `= this.status`
1383Count: `$= dv.pages('#project').length`
1384
1385Regular code: `function test() {}`
1386"#;
1387
1388        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
1389        let result = rule.check(&ctx).unwrap();
1390
1391        // Nothing should be flagged
1392        assert!(
1393            result.is_empty(),
1394            "Dataview expressions and regular code should work together"
1395        );
1396    }
1397
1398    #[test]
1399    fn test_unicode_between_code_spans_no_panic() {
1400        // Verify that multi-byte characters between code spans do not cause panics
1401        // or incorrect slicing in the nested-backtick detection logic.
1402        let rule = MD038NoSpaceInCode::new();
1403
1404        // Multi-byte character (U-umlaut = 2 bytes) between two code spans
1405        let content = "Use `one` \u{00DC}nited `two` for backtick examples.";
1406        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1407        let result = rule.check(&ctx);
1408        // Should not panic; any warnings or lack thereof are acceptable
1409        assert!(result.is_ok(), "Should not panic with Unicode between code spans");
1410
1411        // CJK characters (3 bytes each) between code spans
1412        let content_cjk = "Use `one` \u{4E16}\u{754C} `two` for examples.";
1413        let ctx_cjk = crate::lint_context::LintContext::new(content_cjk, crate::config::MarkdownFlavor::Standard, None);
1414        let result_cjk = rule.check(&ctx_cjk);
1415        assert!(result_cjk.is_ok(), "Should not panic with CJK between code spans");
1416    }
1417}