From 2b6012a79cb092e5d88c050cb494057efef28fc2 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 20 Nov 2021 18:56:26 +0530 Subject: introduce --config flag --- bin/src/fix/all.rs | 18 +++++++++++------- bin/src/fix/single.rs | 8 ++++---- 2 files changed, 15 insertions(+), 11 deletions(-) (limited to 'bin/src/fix') diff --git a/bin/src/fix/all.rs b/bin/src/fix/all.rs index 7f04f2c..bbc39e8 100644 --- a/bin/src/fix/all.rs +++ b/bin/src/fix/all.rs @@ -1,18 +1,21 @@ use std::borrow::Cow; -use lib::{Report, LINTS}; +use lib::Report; use rnix::{parser::ParseError as RnixParseErr, WalkEvent}; -use crate::fix::{FixResult, Fixed}; +use crate::{ + fix::{FixResult, Fixed}, + LintMap, +}; -fn collect_fixes(source: &str) -> Result, RnixParseErr> { +fn collect_fixes(source: &str, lints: &LintMap) -> Result, RnixParseErr> { let parsed = rnix::parse(source).as_result()?; Ok(parsed .node() .preorder_with_tokens() .filter_map(|event| match event { - WalkEvent::Enter(child) => LINTS.get(&child.kind()).map(|rules| { + WalkEvent::Enter(child) => lints.get(&child.kind()).map(|rules| { rules .iter() .filter_map(|rule| rule.validate(&child)) @@ -54,7 +57,7 @@ fn reorder(mut reports: Vec) -> Vec { impl<'a> Iterator for FixResult<'a> { type Item = FixResult<'a>; fn next(&mut self) -> Option { - let all_reports = collect_fixes(&self.src).ok()?; + let all_reports = collect_fixes(&self.src, &self.lints).ok()?; if all_reports.is_empty() { return None; } @@ -74,13 +77,14 @@ impl<'a> Iterator for FixResult<'a> { Some(FixResult { src: self.src.clone(), fixed, + lints: self.lints, }) } } -pub fn all(src: &str) -> Option { +pub fn all_with<'a>(src: &'a str, lints: &'a LintMap) -> Option> { let src = Cow::from(src); let _ = rnix::parse(&src).as_result().ok()?; - let initial = FixResult::empty(src); + let initial = FixResult::empty(src, lints); initial.into_iter().last() } diff --git a/bin/src/fix/single.rs b/bin/src/fix/single.rs index b91dc45..d95cfda 100644 --- a/bin/src/fix/single.rs +++ b/bin/src/fix/single.rs @@ -1,10 +1,9 @@ use std::{borrow::Cow, convert::TryFrom}; -use lib::{Report, LINTS}; +use lib::Report; use rnix::{TextSize, WalkEvent}; -use crate::err::SingleFixErr; -use crate::fix::Source; +use crate::{err::SingleFixErr, fix::Source, utils}; pub struct SingleFixResult<'δ> { pub src: Source<'δ>, @@ -31,12 +30,13 @@ fn pos_to_byte(line: usize, col: usize, src: &str) -> Result Result { // we don't really need the source to form a completely parsed tree let parsed = rnix::parse(src); + let lints = utils::lint_map(); parsed .node() .preorder_with_tokens() .filter_map(|event| match event { - WalkEvent::Enter(child) => LINTS.get(&child.kind()).map(|rules| { + WalkEvent::Enter(child) => lints.get(&child.kind()).map(|rules| { rules .iter() .filter_map(|rule| rule.validate(&child)) -- cgit v1.2.3