aboutsummaryrefslogtreecommitdiff
path: root/bin/src/lint.rs
blob: e1544e1b91fe6f165b0e437ff40314629c9ec4d8 (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
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
use crate::{utils, LintMap};

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

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

pub fn lint_with(vfs_entry: VfsEntry, lints: &LintMap, sess: &SessionInfo) -> LintResult {
    let file_id = vfs_entry.file_id;
    let source = vfs_entry.contents;
    let parsed = rnix::parse(source);

    let error_reports = parsed.errors().into_iter().map(Report::from_parse_err);
    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, sess))
                    .collect::<Vec<_>>()
            }),
            _ => None,
        })
        .flatten()
        .chain(error_reports)
        .collect();

    LintResult { file_id, reports }
}

pub fn lint(vfs_entry: VfsEntry, sess: &SessionInfo) -> LintResult {
    lint_with(vfs_entry, &utils::lint_map(), &sess)
}

pub mod main {
    use std::io;

    use super::lint_with;
    use crate::{
        config::{Check as CheckConfig, ConfFile},
        err::StatixErr,
        traits::WriteDiagnostic,
    };

    use lib::session::SessionInfo;
    use rayon::prelude::*;

    pub fn main(check_config: CheckConfig) -> Result<(), StatixErr> {
        let vfs = check_config.vfs()?;
        let mut stdout = io::stdout();
        let conf_file = ConfFile::discover(&check_config.conf_path)?;
        let lints = conf_file.lints();
        let version = conf_file.version()?;
        let session = SessionInfo::from_version(version);
        let lint = |vfs_entry| lint_with(vfs_entry, &lints, &session);
        let results = vfs
            .par_iter()
            .map(lint)
            .filter(|lr| !lr.reports.is_empty())
            .collect::<Vec<_>>();

        if results.len() != 0 {
            for r in &results {
                stdout.write(&r, &vfs, check_config.format).unwrap();
            }
            std::process::exit(1);
        }

        std::process::exit(0);
    }
}