supa_mdx_lint/
lib.rs

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
use anyhow::{Context as _, Result};
use bon::bon;
use context::Context;
use rules::RuleFilter;
use std::env;
use std::path::{Path, PathBuf};
use std::{fs, io::Read};
use utils::is_lintable;

mod app_error;
mod comments;
mod config;
mod context;
pub mod errors;
pub mod fix;
mod geometry;
mod output;
mod parser;
pub mod rope;
pub mod rules;
pub mod utils;

pub use crate::config::Config;
pub use crate::errors::LintLevel;
pub use crate::output::{rdf::RdfFormatter, simple::SimpleFormatter, LintOutput, OutputFormatter};

use crate::parser::parse;

#[derive(Debug)]
pub struct Linter {
    config: Config,
}

#[derive(Debug)]
pub enum LintTarget<'a> {
    FileOrDirectory(PathBuf),
    String(&'a str),
}

struct LintSourceReference<'reference>(Option<&'reference Path>);

#[bon]
impl Linter {
    #[builder]
    pub fn new(config: Option<Config>) -> Result<Self> {
        let mut this = Self {
            config: config.unwrap_or_default(),
        };

        this.config
            .rule_registry
            .setup(&mut this.config.rule_specific_settings)?;

        Ok(this)
    }

    pub fn lint(&self, input: &LintTarget) -> Result<Vec<LintOutput>> {
        self.lint_internal(input, None)
    }

    pub fn lint_only_rule(&self, rule_id: &str, input: &LintTarget) -> Result<Vec<LintOutput>> {
        self.lint_internal(input, Some(&[rule_id]))
    }

    fn lint_internal(
        &self,
        input: &LintTarget,
        check_only_rules: RuleFilter,
    ) -> Result<Vec<LintOutput>> {
        match input {
            LintTarget::FileOrDirectory(path) => {
                self.lint_file_or_directory(path, check_only_rules)
            }
            LintTarget::String(string) => {
                self.lint_string(string, LintSourceReference(None), check_only_rules)
            }
        }
    }

    fn lint_file_or_directory(
        &self,
        path: &PathBuf,
        check_only_rules: RuleFilter,
    ) -> Result<Vec<LintOutput>> {
        if path.is_file() {
            if self.config.is_ignored(path) {
                return Ok(Vec::new());
            }

            let mut file = fs::File::open(path)?;
            let mut contents = String::new();
            file.read_to_string(&mut contents)?;
            self.lint_string(&contents, LintSourceReference(Some(path)), check_only_rules)
        } else if path.is_dir() {
            let collected_vec = fs::read_dir(path)?
                .filter_map(Result::ok)
                .filter(|dir_entry| is_lintable(dir_entry.path()))
                .flat_map(|entry| {
                    self.lint_file_or_directory(&entry.path(), check_only_rules)
                        .unwrap_or_default()
                })
                .collect::<Vec<_>>();
            Ok(collected_vec)
        } else {
            Err(anyhow::anyhow!(
                "Path is neither a file nor a directory: {:?}",
                path
            ))
        }
    }

    fn lint_string(
        &self,
        string: &str,
        source: LintSourceReference,
        check_only_rules: RuleFilter,
    ) -> Result<Vec<LintOutput>> {
        let parse_result = parse(string)?;
        let rule_context = Context::builder()
            .parse_result(&parse_result)
            .maybe_check_only_rules(check_only_rules)
            .build()?;
        match self.config.rule_registry.run(&rule_context) {
            Ok(diagnostics) => {
                let source = match source.0 {
                    Some(path) => {
                        let current_dir =
                            env::current_dir().context("Failed to get current directory")?;
                        let relative_path = match path.strip_prefix(&current_dir) {
                            Ok(relative_path) => relative_path,
                            Err(_) => path,
                        };
                        &relative_path.to_string_lossy()
                    }
                    None => "[direct input]",
                };
                Ok(vec![LintOutput::new(source, diagnostics)])
            }
            Err(err) => Err(err),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use ctor::ctor;

    #[ctor]
    fn init_test_logger() {
        env_logger::builder().is_test(true).try_init().unwrap();
    }

    #[test]
    fn test_lint_valid_string() -> Result<()> {
        let mut linter = Linter::builder().build()?;
        linter
            .config
            .rule_registry
            .deactivate_all_but("Rule001HeadingCase");

        let valid_mdx = "# Hello, world!\n\nThis is a valid document.";
        let result = linter.lint(&LintTarget::String(&valid_mdx.to_string()))?;

        assert!(
            result.get(0).unwrap().errors().is_empty(),
            "Expected no lint errors for valid MDX, got {:?}",
            result
        );
        Ok(())
    }

    #[test]
    fn test_lint_invalid_string() -> Result<()> {
        let mut linter = Linter::builder().build()?;
        linter
            .config
            .rule_registry
            .deactivate_all_but("Rule001HeadingCase");

        let invalid_mdx = "# Incorrect Heading\n\nThis is an invalid document.";
        let result = linter.lint(&LintTarget::String(&invalid_mdx.to_string()))?;

        assert!(
            !result.get(0).unwrap().errors().is_empty(),
            "Expected lint errors for invalid MDX"
        );
        Ok(())
    }
}