From d1ff222bcf94152cd657233cffd8c14a45788c26 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 29 Dec 2021 10:53:38 +0530 Subject: allow for version based lints --- bin/src/fix/all.rs | 21 +++++++++++++++------ bin/src/fix/single.rs | 15 ++++++++++----- 2 files changed, 25 insertions(+), 11 deletions(-) (limited to 'bin/src/fix') diff --git a/bin/src/fix/all.rs b/bin/src/fix/all.rs index 7e51d16..d7f7fff 100644 --- a/bin/src/fix/all.rs +++ b/bin/src/fix/all.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; -use lib::Report; +use lib::{session::SessionInfo, Report}; use rnix::{parser::ParseError as RnixParseErr, WalkEvent}; use crate::{ @@ -8,7 +8,11 @@ use crate::{ LintMap, }; -fn collect_fixes(source: &str, lints: &LintMap) -> Result, RnixParseErr> { +fn collect_fixes( + source: &str, + lints: &LintMap, + sess: &SessionInfo, +) -> Result, RnixParseErr> { let parsed = rnix::parse(source).as_result()?; Ok(parsed @@ -18,7 +22,7 @@ fn collect_fixes(source: &str, lints: &LintMap) -> Result, RnixParse WalkEvent::Enter(child) => lints.get(&child.kind()).map(|rules| { rules .iter() - .filter_map(|rule| rule.validate(&child)) + .filter_map(|rule| rule.validate(&child, sess)) .filter(|report| report.total_suggestion_range().is_some()) .collect::>() }), @@ -57,7 +61,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, self.lints).ok()?; + let all_reports = collect_fixes(&self.src, self.lints, &self.sess).ok()?; if all_reports.is_empty() { return None; } @@ -78,13 +82,18 @@ impl<'a> Iterator for FixResult<'a> { src: self.src.clone(), fixed, lints: self.lints, + sess: self.sess, }) } } -pub fn all_with<'a>(src: &'a str, lints: &'a LintMap) -> Option> { +pub fn all_with<'a>( + src: &'a str, + lints: &'a LintMap, + sess: &'a SessionInfo, +) -> Option> { let src = Cow::from(src); let _ = rnix::parse(&src).as_result().ok()?; - let initial = FixResult::empty(src, lints); + let initial = FixResult::empty(src, lints, sess); initial.into_iter().last() } diff --git a/bin/src/fix/single.rs b/bin/src/fix/single.rs index d95cfda..67b6b8f 100644 --- a/bin/src/fix/single.rs +++ b/bin/src/fix/single.rs @@ -1,6 +1,6 @@ use std::{borrow::Cow, convert::TryFrom}; -use lib::Report; +use lib::{session::SessionInfo, Report}; use rnix::{TextSize, WalkEvent}; use crate::{err::SingleFixErr, fix::Source, utils}; @@ -27,7 +27,7 @@ fn pos_to_byte(line: usize, col: usize, src: &str) -> Result Result { +fn find(offset: TextSize, src: &str, sess: &SessionInfo) -> Result { // we don't really need the source to form a completely parsed tree let parsed = rnix::parse(src); let lints = utils::lint_map(); @@ -39,7 +39,7 @@ fn find(offset: TextSize, src: &str) -> Result { WalkEvent::Enter(child) => lints.get(&child.kind()).map(|rules| { rules .iter() - .filter_map(|rule| rule.validate(&child)) + .filter_map(|rule| rule.validate(&child, sess)) .find(|report| report.total_suggestion_range().is_some()) }), _ => None, @@ -49,10 +49,15 @@ fn find(offset: TextSize, src: &str) -> Result { .ok_or(SingleFixErr::NoOp) } -pub fn single(line: usize, col: usize, src: &str) -> Result { +pub fn single<'a, 'b>( + line: usize, + col: usize, + src: &'a str, + sess: &'b SessionInfo, +) -> Result, SingleFixErr> { let mut src = Cow::from(src); let offset = pos_to_byte(line, col, &*src)?; - let report = find(offset, &*src)?; + let report = find(offset, &*src, &sess)?; report.apply(src.to_mut()); -- cgit v1.2.3