quickmark-core 1.1.0

Lightning-fast Markdown/CommonMark linter core library with tree-sitter based parsing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
use std::rc::Rc;

use once_cell::sync::Lazy;
use regex::Regex;
use tree_sitter::Node;

use crate::{
    linter::{range_from_tree_sitter, Context, RuleViolation},
    rules::{Rule, RuleLinter, RuleType},
};

// Regex patterns to find emphasis markers with spaces
static ASTERISK_EMPHASIS_REGEX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"(\*{1,3})(\s*)([^*\n]*?)(\s*)(\*{1,3})").expect("Invalid asterisk emphasis regex")
});

static UNDERSCORE_EMPHASIS_REGEX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"(\_{1,3})(\s*)([^_\n]*?)(\s*)(\_{1,3})")
        .expect("Invalid underscore emphasis regex")
});

// Regex to find code spans
static CODE_SPAN_REGEX: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"`[^`\n]*`").expect("Invalid code span regex"));

pub(crate) struct MD037Linter {
    context: Rc<Context>,
    violations: Vec<RuleViolation>,
}

impl MD037Linter {
    pub fn new(context: Rc<Context>) -> Self {
        Self {
            context,
            violations: Vec::new(),
        }
    }

    fn is_in_code_context(&self, node: &Node) -> bool {
        // Check if this node is inside a code span or code block
        let mut current = Some(*node);
        while let Some(node_to_check) = current {
            match node_to_check.kind() {
                "code_span" | "fenced_code_block" | "indented_code_block" => {
                    return true;
                }
                _ => {
                    current = node_to_check.parent();
                }
            }
        }
        false
    }

    fn find_emphasis_violations_in_text(&mut self, node: &Node) {
        if self.is_in_code_context(node) {
            return;
        }

        let start_byte = node.start_byte();
        let text = {
            let source = self.context.get_document_content();
            source[start_byte..node.end_byte()].to_string()
        };

        // Find code span ranges to exclude
        let code_span_ranges: Vec<(usize, usize)> = CODE_SPAN_REGEX
            .find_iter(&text)
            .map(|m| (m.start(), m.end()))
            .collect();

        // Check for asterisk emphasis violations
        self.check_emphasis_pattern(
            &text,
            start_byte,
            &ASTERISK_EMPHASIS_REGEX,
            &code_span_ranges,
        );

        // Check for underscore emphasis violations
        self.check_emphasis_pattern(
            &text,
            start_byte,
            &UNDERSCORE_EMPHASIS_REGEX,
            &code_span_ranges,
        );
    }

    fn check_emphasis_pattern(
        &mut self,
        text: &str,
        text_start_byte: usize,
        regex: &Regex,
        code_span_ranges: &[(usize, usize)],
    ) {
        for capture in regex.captures_iter(text) {
            if let (
                Some(opening_marker),
                Some(opening_space),
                Some(_content),
                Some(closing_space),
                Some(closing_marker),
            ) = (
                capture.get(1),
                capture.get(2),
                capture.get(3),
                capture.get(4),
                capture.get(5),
            ) {
                // Check if this match overlaps with any code span
                let match_start = capture.get(0).unwrap().start();
                let match_end = capture.get(0).unwrap().end();

                let in_code_span = code_span_ranges.iter().any(|(code_start, code_end)| {
                    // Check if the match overlaps with a code span
                    match_start < *code_end && match_end > *code_start
                });

                if in_code_span {
                    continue; // Skip this match as it's inside a code span
                }

                let opening_text = opening_marker.as_str();
                let closing_text = closing_marker.as_str();

                // Only process if markers match (same type and count)
                if opening_text == closing_text {
                    // Check for space after opening marker
                    if !opening_space.as_str().is_empty() {
                        self.create_opening_space_violation(
                            opening_marker,
                            opening_space,
                            text_start_byte,
                        );
                    }

                    // Check for space before closing marker
                    if !closing_space.as_str().is_empty() {
                        self.create_closing_space_violation(
                            closing_marker,
                            closing_space,
                            text_start_byte,
                        );
                    }
                }
            }
        }
    }

    fn create_opening_space_violation(
        &mut self,
        opening_marker: regex::Match,
        opening_space: regex::Match,
        text_start_byte: usize,
    ) {
        let marker = opening_marker.as_str();
        let space = opening_space.as_str();
        let violation_start = text_start_byte + opening_marker.end();
        let violation_end = text_start_byte + opening_space.end();

        let range = tree_sitter::Range {
            start_byte: violation_start,
            end_byte: violation_end,
            start_point: self.byte_to_point(violation_start),
            end_point: self.byte_to_point(violation_end),
        };

        self.violations.push(RuleViolation::new(
            &MD037,
            format!("{} [Context: \"{}{}\"]", MD037.description, marker, space),
            self.context.file_path.clone(),
            range_from_tree_sitter(&range),
        ));
    }

    fn create_closing_space_violation(
        &mut self,
        closing_marker: regex::Match,
        closing_space: regex::Match,
        text_start_byte: usize,
    ) {
        let marker = closing_marker.as_str();
        let space = closing_space.as_str();
        let violation_start = text_start_byte + closing_space.start();
        let violation_end = text_start_byte + closing_marker.end();

        let range = tree_sitter::Range {
            start_byte: violation_start,
            end_byte: violation_end,
            start_point: self.byte_to_point(violation_start),
            end_point: self.byte_to_point(violation_end),
        };

        self.violations.push(RuleViolation::new(
            &MD037,
            format!("{} [Context: \"{}{}\"]", MD037.description, space, marker),
            self.context.file_path.clone(),
            range_from_tree_sitter(&range),
        ));
    }

    fn byte_to_point(&self, byte_pos: usize) -> tree_sitter::Point {
        let source = self.context.get_document_content();
        let mut line = 0;
        let mut column = 0;

        for (i, ch) in source.char_indices() {
            if i >= byte_pos {
                break;
            }
            if ch == '\n' {
                line += 1;
                column = 0;
            } else {
                column += 1;
            }
        }

        tree_sitter::Point { row: line, column }
    }
}

impl RuleLinter for MD037Linter {
    fn feed(&mut self, node: &Node) {
        match node.kind() {
            // Look for text content that might contain emphasis markers with spaces
            "text" | "inline" => {
                self.find_emphasis_violations_in_text(node);
            }
            _ => {}
        }
    }

    fn finalize(&mut self) -> Vec<RuleViolation> {
        std::mem::take(&mut self.violations)
    }
}

pub const MD037: Rule = Rule {
    id: "MD037",
    alias: "no-space-in-emphasis",
    tags: &["whitespace", "emphasis"],
    description: "Spaces inside emphasis markers",
    rule_type: RuleType::Token,
    required_nodes: &["emphasis", "strong_emphasis"],
    new_linter: |context| Box::new(MD037Linter::new(context)),
};

#[cfg(test)]
mod test {
    use std::path::PathBuf;

    use crate::config::RuleSeverity;
    use crate::linter::MultiRuleLinter;
    use crate::test_utils::test_helpers::test_config_with_rules;

    fn test_config() -> crate::config::QuickmarkConfig {
        test_config_with_rules(vec![("no-space-in-emphasis", RuleSeverity::Error)])
    }

    #[test]
    fn test_no_violations_valid_emphasis() {
        let config = test_config();
        let input = "This has *valid emphasis* and **valid strong** text.
Also _valid emphasis_ and __valid strong__ text.
And ***valid strong emphasis*** and ___valid strong emphasis___ text.";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md037_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD037")
            .collect();
        assert_eq!(md037_violations.len(), 0);
    }

    #[test]
    fn test_violations_spaces_inside_single_asterisk() {
        let config = test_config();
        let input = "This has * invalid emphasis * with spaces inside.";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md037_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD037")
            .collect();

        // Should find 2 violations: one for opening space, one for closing space
        assert_eq!(md037_violations.len(), 2);
    }

    #[test]
    fn test_violations_spaces_inside_double_asterisk() {
        let config = test_config();
        let input = "This has ** invalid strong ** with spaces inside.";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md037_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD037")
            .collect();

        // Should find 2 violations: one for opening space, one for closing space
        assert_eq!(md037_violations.len(), 2);
    }

    #[test]
    fn test_violations_spaces_inside_triple_asterisk() {
        let config = test_config();
        let input = "This has *** invalid strong emphasis *** with spaces inside.";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md037_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD037")
            .collect();

        // Should find 2 violations: one for opening space, one for closing space
        assert_eq!(md037_violations.len(), 2);
    }

    #[test]
    fn test_violations_spaces_inside_single_underscore() {
        let config = test_config();
        let input = "This has _ invalid emphasis _ with spaces inside.";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md037_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD037")
            .collect();

        // Should find 2 violations: one for opening space, one for closing space
        assert_eq!(md037_violations.len(), 2);
    }

    #[test]
    fn test_violations_spaces_inside_double_underscore() {
        let config = test_config();
        let input = "This has __ invalid strong __ with spaces inside.";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md037_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD037")
            .collect();

        // Should find 2 violations: one for opening space, one for closing space
        assert_eq!(md037_violations.len(), 2);
    }

    #[test]
    fn test_violations_spaces_inside_triple_underscore() {
        let config = test_config();
        let input = "This has ___ invalid strong emphasis ___ with spaces inside.";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md037_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD037")
            .collect();

        // Should find 2 violations: one for opening space, one for closing space
        assert_eq!(md037_violations.len(), 2);
    }

    #[test]
    fn test_violations_mixed_valid_and_invalid() {
        let config = test_config();
        let input = "Mix of *valid* and * invalid * emphasis.
Also **valid** and ** invalid ** strong.";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md037_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD037")
            .collect();

        // Should find 4 violations: 2 from each invalid emphasis (opening and closing spaces)
        assert_eq!(md037_violations.len(), 4);
    }

    #[test]
    fn test_violations_one_sided_spaces() {
        let config = test_config();
        let input = "One sided *invalid * and * invalid* emphasis.";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md037_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD037")
            .collect();

        // Should find 2 violations: one for each one-sided space
        assert_eq!(md037_violations.len(), 2);
    }

    #[test]
    fn test_no_violations_in_code_blocks() {
        let config = test_config();
        let input = "Regular text with *valid* emphasis.

```markdown
This should not trigger * invalid * emphasis in code blocks.
```

More text with _valid_ emphasis.";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md037_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD037")
            .collect();
        assert_eq!(md037_violations.len(), 0);
    }

    #[test]
    fn test_no_violations_in_code_spans() {
        let config = test_config();
        let input = "Regular text with `* invalid * code spans` should not trigger violations.";

        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        let md037_violations: Vec<_> = violations
            .iter()
            .filter(|v| v.rule().id == "MD037")
            .collect();
        assert_eq!(md037_violations.len(), 0);
    }
}