From e4c4a77d03ffe02ec5b3fda71eb4ccd97e9cdf58 Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 19 Oct 2021 19:31:01 +0530 Subject: refactor out lint runner into lint module --- bin/src/lint.rs | 36 ++++++++++++++++++++++++++++++++++++ bin/src/main.rs | 43 ++++++------------------------------------- bin/src/traits.rs | 18 +++++++----------- 3 files changed, 49 insertions(+), 48 deletions(-) create mode 100644 bin/src/lint.rs 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 @@ +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, +} + +pub fn lint(vfs_entry: VfsEntry) -> Result { + 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::>() + }), + _ => None, + }) + .flatten() + .collect(); + Ok(LintResult { + file_id: vfs_entry.file_id, + reports, + }) +} diff --git a/bin/src/main.rs b/bin/src/main.rs index b26151d..a3f04d7 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -1,57 +1,26 @@ -#![feature(path_try_exists)] - mod config; mod err; +mod lint; mod traits; use std::io; -use crate::{ - err::{LintErr, StatixErr}, - traits::{LintResult, WriteDiagnostic}, -}; +use crate::{err::StatixErr, traits::WriteDiagnostic}; use clap::Clap; use config::{LintConfig, Opts, SubCommand}; -use lib::LINTS; -use rnix::WalkEvent; -use vfs::VfsEntry; - -fn analyze<'ρ>(vfs_entry: VfsEntry<'ρ>) -> Result { - 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::>() - }), - _ => None, - }) - .flatten() - .collect(); - Ok(LintResult { - file_id: vfs_entry.file_id, - reports, - }) -} fn _main() -> Result<(), StatixErr> { - // TODO: accept cli args, construct a CLI config with a list of files to analyze let opts = Opts::parse(); match opts.subcmd { - Some(SubCommand::Fix(_)) => {} + Some(SubCommand::Fix(_)) => { + eprintln!("`fix` not yet supported"); + } None => { let lint_config = LintConfig::from_opts(opts)?; let vfs = lint_config.vfs()?; let (reports, errors): (Vec<_>, Vec<_>) = - vfs.iter().map(analyze).partition(Result::is_ok); + vfs.iter().map(lint::lint).partition(Result::is_ok); let lint_results: Vec<_> = reports.into_iter().map(Result::unwrap).collect(); let errors: Vec<_> = errors.into_iter().map(Result::unwrap_err).collect(); diff --git a/bin/src/traits.rs b/bin/src/traits.rs index 1807ad0..c3427df 100644 --- a/bin/src/traits.rs +++ b/bin/src/traits.rs @@ -3,19 +3,14 @@ use std::{ str, }; +use crate::lint::LintResult; + use ariadne::{ - CharSet, Color, Config as CliConfig, Label, LabelAttach, Report as CliReport, - ReportKind as CliReportKind, Source, Fmt + CharSet, Color, Config as CliConfig, Fmt, Label, LabelAttach, Report as CliReport, + ReportKind as CliReportKind, Source, }; -use lib::Report; use rnix::TextRange; -use vfs::{FileId, ReadOnlyVfs}; - -#[derive(Debug)] -pub struct LintResult { - pub file_id: FileId, - pub reports: Vec, -} +use vfs::ReadOnlyVfs; pub trait WriteDiagnostic { fn write(&mut self, report: &LintResult, vfs: &ReadOnlyVfs) -> io::Result<()>; @@ -69,7 +64,8 @@ where // everything within backticks is colorized, backticks are removed fn colorize(message: &str) -> String { - message.split('`') + message + .split('`') .enumerate() .map(|(idx, part)| { if idx % 2 == 1 { -- cgit v1.2.3