diff options
Diffstat (limited to 'bin/src/lint.rs')
-rw-r--r-- | bin/src/lint.rs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/bin/src/lint.rs b/bin/src/lint.rs new file mode 100644 index 0000000..76b2b8c --- /dev/null +++ b/bin/src/lint.rs | |||
@@ -0,0 +1,36 @@ | |||
1 | use crate::err::LintErr; | ||
2 | |||
3 | use lib::{LINTS, Report}; | ||
4 | use rnix::WalkEvent; | ||
5 | use vfs::{VfsEntry, FileId}; | ||
6 | |||
7 | #[derive(Debug)] | ||
8 | pub struct LintResult { | ||
9 | pub file_id: FileId, | ||
10 | pub reports: Vec<Report>, | ||
11 | } | ||
12 | |||
13 | pub fn lint(vfs_entry: VfsEntry) -> Result<LintResult, LintErr> { | ||
14 | let source = vfs_entry.contents; | ||
15 | let parsed = rnix::parse(source) | ||
16 | .as_result() | ||
17 | .map_err(|e| LintErr::Parse(vfs_entry.file_path.to_path_buf(), e))?; | ||
18 | let reports = parsed | ||
19 | .node() | ||
20 | .preorder_with_tokens() | ||
21 | .filter_map(|event| match event { | ||
22 | WalkEvent::Enter(child) => LINTS.get(&child.kind()).map(|rules| { | ||
23 | rules | ||
24 | .iter() | ||
25 | .filter_map(|rule| rule.validate(&child)) | ||
26 | .collect::<Vec<_>>() | ||
27 | }), | ||
28 | _ => None, | ||
29 | }) | ||
30 | .flatten() | ||
31 | .collect(); | ||
32 | Ok(LintResult { | ||
33 | file_id: vfs_entry.file_id, | ||
34 | reports, | ||
35 | }) | ||
36 | } | ||