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
use std::rc::Rc;

use tree_sitter::Node;

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

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

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

impl RuleLinter for MD005Linter {
    fn feed(&mut self, node: &Node) {
        if node.kind() == "list" {
            self.check_list_indentation(node);
        }
    }

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

impl MD005Linter {
    fn check_list_indentation(&mut self, list_node: &Node) {
        let list_items = Self::get_direct_list_items_static(list_node);
        if list_items.len() < 2 {
            // Need at least 2 items to compare indentation
            return;
        }

        let is_ordered = Self::is_ordered_list_static(
            list_node,
            self.context.document_content.borrow().as_bytes(),
        );

        if is_ordered {
            self.check_ordered_list_indentation(list_node, &list_items);
        } else {
            self.check_unordered_list_indentation(list_node, &list_items);
        }
    }

    fn get_direct_list_items_static<'a>(list_node: &Node<'a>) -> Vec<Node<'a>> {
        let mut cursor = list_node.walk();
        list_node
            .children(&mut cursor)
            .filter(|c| c.kind() == "list_item")
            .collect()
    }

    fn is_ordered_list_static(list_node: &Node, content: &[u8]) -> bool {
        let mut list_cursor = list_node.walk();
        if let Some(first_item) = list_node
            .children(&mut list_cursor)
            .find(|c| c.kind() == "list_item")
        {
            let mut item_cursor = first_item.walk();
            // Use a for loop to make lifetimes explicit and avoid borrow checker issues.
            for child in first_item.children(&mut item_cursor) {
                if child.kind().starts_with("list_marker") {
                    if let Ok(text) = child.utf8_text(content) {
                        return text.contains('.');
                    }
                    // If a marker is found but its text cannot be read, assume it's not an ordered list.
                    return false;
                }
            }
        }
        false
    }

    fn check_unordered_list_indentation(&mut self, _list_node: &Node, list_items: &[Node]) {
        let expected_indent = self.get_list_item_indentation(&list_items[0]);

        for item in list_items.iter().skip(1) {
            let actual_indent = self.get_list_item_indentation(item);

            if actual_indent != expected_indent {
                let message = format!(
                    "{} [Expected: {}; Actual: {}]",
                    MD005.description, expected_indent, actual_indent
                );

                self.violations.push(RuleViolation::new(
                    &MD005,
                    message,
                    self.context.file_path.clone(),
                    range_from_tree_sitter(&item.range()),
                ));
            }
        }
    }

    fn check_ordered_list_indentation(&mut self, _list_node: &Node, list_items: &[Node]) {
        // Mimic the original markdownlint algorithm more closely
        let expected_indent = self.get_list_item_indentation(&list_items[0]);
        let mut expected_end = 0;
        let mut end_matching = false;

        for item in list_items {
            let actual_indent = self.get_list_item_indentation(item);
            let marker_length = self.get_list_marker_text_length(item);
            let actual_end = actual_indent + marker_length;

            expected_end = if expected_end == 0 {
                actual_end
            } else {
                expected_end
            };

            if expected_indent != actual_indent || end_matching {
                if expected_end == actual_end {
                    end_matching = true;
                } else {
                    let detail = if end_matching {
                        format!("Expected: ({expected_end}); Actual: ({actual_end})")
                    } else {
                        format!("Expected: {expected_indent}; Actual: {actual_indent}")
                    };

                    self.violations.push(RuleViolation::new(
                        &MD005,
                        format!("{} [{}]", MD005.description, detail),
                        self.context.file_path.clone(),
                        range_from_tree_sitter(&item.range()),
                    ));
                }
            }
        }
    }

    fn get_list_marker_text_length(&self, list_item: &Node) -> usize {
        let mut cursor = list_item.walk();
        if let Some(marker_node) = list_item
            .children(&mut cursor)
            .find(|c| c.kind().starts_with("list_marker"))
        {
            let content = self.context.document_content.borrow();
            if let Ok(text) = marker_node.utf8_text(content.as_bytes()) {
                return text.trim().len();
            }
        }
        0
    }

    fn get_list_item_indentation(&self, list_item: &Node) -> usize {
        let content = self.context.document_content.borrow();
        let start_line = list_item.start_position().row;

        if let Some(line) = content.lines().nth(start_line) {
            // Count leading spaces/tabs (treating tabs as single characters for now)
            line.chars().take_while(|&c| c == ' ' || c == '\t').count()
        } else {
            0
        }
    }
}

pub const MD005: Rule = Rule {
    id: "MD005",
    alias: "list-indent",
    tags: &["bullet", "ul", "indentation"],
    description: "Inconsistent indentation for list items at the same level",
    rule_type: RuleType::Token,
    required_nodes: &["list"],
    new_linter: |context| Box::new(MD005Linter::new(context)),
};

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

    use crate::config::{QuickmarkConfig, RuleSeverity};
    use crate::linter::MultiRuleLinter;
    use crate::test_utils::test_helpers::test_config_with_rules;

    fn test_config() -> QuickmarkConfig {
        test_config_with_rules(vec![("list-indent", RuleSeverity::Error)])
    }

    #[test]
    fn test_consistent_unordered_list_indentation_no_violations() {
        let input = "* Item 1
* Item 2
* Item 3
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(
            0,
            violations.len(),
            "Consistent indentation should have no violations"
        );
    }

    #[test]
    fn test_inconsistent_unordered_list_indentation_has_violations() {
        let input = "* Item 1
 * Item 2 (1 space instead of 0)
* Item 3
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert!(
            !violations.is_empty(),
            "Inconsistent indentation should have violations"
        );
    }

    #[test]
    fn test_consistent_ordered_list_left_aligned_no_violations() {
        let input = "1. Item 1
2. Item 2
10. Item 10
11. Item 11
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(
            0,
            violations.len(),
            "Left-aligned ordered list should have no violations"
        );
    }

    #[test]
    fn test_consistent_ordered_list_right_aligned_no_violations() {
        let input = " 1. Item 1
 2. Item 2
10. Item 10
11. Item 11
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(
            0,
            violations.len(),
            "Right-aligned ordered list should have no violations"
        );
    }

    #[test]
    fn test_inconsistent_ordered_list_has_violations() {
        let input = "1. Item 1
 2. Item 2 (should be at same indent as item 1)
3. Item 3
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert!(
            !violations.is_empty(),
            "Inconsistent ordered list indentation should have violations"
        );
    }

    #[test]
    fn test_nested_lists_different_levels_no_violations() {
        let input = "* Item 1
  * Nested item 1
  * Nested item 2
* Item 2
  * Nested item 3
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(
            0,
            violations.len(),
            "Items at different nesting levels should not be compared"
        );
    }

    #[test]
    fn test_nested_lists_same_level_inconsistent() {
        let input = "* Item 1
  * Nested item 1
   * Nested item 2 (should be 2 spaces like item 1)
* Item 2
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert!(
            !violations.is_empty(),
            "Nested items at same level with inconsistent indent should have violations"
        );
    }

    #[test]
    fn test_mixed_ordered_unordered_lists() {
        let input = "1. Ordered item 1
2. Ordered item 2

* Unordered item 1  
* Unordered item 2
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(
            0,
            violations.len(),
            "Different list types should not interfere with each other"
        );
    }

    #[test]
    fn test_single_item_list_no_violations() {
        let input = "* Single item
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(
            0,
            violations.len(),
            "Single item lists should not have violations"
        );
    }

    #[test]
    fn test_empty_document_no_violations() {
        let input = "";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(
            0,
            violations.len(),
            "Empty documents should not have violations"
        );
    }

    #[test]
    fn test_ordered_list_with_different_number_lengths() {
        let input = " 1. Item 1
 2. Item 2
 3. Item 3
 4. Item 4
 5. Item 5
 6. Item 6
 7. Item 7
 8. Item 8
 9. Item 9
10. Item 10
11. Item 11
12. Item 12
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert_eq!(
            0,
            violations.len(),
            "Right-aligned numbers should be consistent"
        );
    }

    #[test]
    fn test_ordered_list_inconsistent_right_alignment() {
        let input = " 1. Item 1
 2. Item 2
10. Item 10
 11. Item 11 (should align with 10, not with 1/2)
";

        let config = test_config();
        let mut linter = MultiRuleLinter::new_for_document(PathBuf::from("test.md"), config, input);
        let violations = linter.analyze();
        assert!(
            !violations.is_empty(),
            "Inconsistent right alignment should have violations"
        );
    }
}