aboutsummaryrefslogtreecommitdiff
path: root/bin/src/lint.rs
blob: 76b2b8ce1ac7ce48da2cf9a3956c7c408e0f1acc (plain)
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
use crate::err::LintErr;

use lib::{LINTS, Report};
use rnix::WalkEvent;
use vfs::{VfsEntry, FileId};

#[derive(Debug)]
pub struct LintResult {
    pub file_id: FileId,
    pub reports: Vec<Report>,
}

pub fn lint(vfs_entry: VfsEntry) -> Result<LintResult, LintErr> {
    let source = vfs_entry.contents;
    let parsed = rnix::parse(source)
        .as_result()
        .map_err(|e| LintErr::Parse(vfs_entry.file_path.to_path_buf(), e))?;
    let reports = parsed
        .node()
        .preorder_with_tokens()
        .filter_map(|event| match event {
            WalkEvent::Enter(child) => LINTS.get(&child.kind()).map(|rules| {
                rules
                    .iter()
                    .filter_map(|rule| rule.validate(&child))
                    .collect::<Vec<_>>()
            }),
            _ => None,
        })
        .flatten()
        .collect();
    Ok(LintResult {
        file_id: vfs_entry.file_id,
        reports,
    })
}