diff options
Diffstat (limited to 'bin/src/fix')
-rw-r--r-- | bin/src/fix/all.rs | 18 | ||||
-rw-r--r-- | bin/src/fix/single.rs | 8 |
2 files changed, 15 insertions, 11 deletions
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 @@ | |||
1 | use std::borrow::Cow; | 1 | use std::borrow::Cow; |
2 | 2 | ||
3 | use lib::{Report, LINTS}; | 3 | use lib::Report; |
4 | use rnix::{parser::ParseError as RnixParseErr, WalkEvent}; | 4 | use rnix::{parser::ParseError as RnixParseErr, WalkEvent}; |
5 | 5 | ||
6 | use crate::fix::{FixResult, Fixed}; | 6 | use crate::{ |
7 | fix::{FixResult, Fixed}, | ||
8 | LintMap, | ||
9 | }; | ||
7 | 10 | ||
8 | fn collect_fixes(source: &str) -> Result<Vec<Report>, RnixParseErr> { | 11 | fn collect_fixes(source: &str, lints: &LintMap) -> Result<Vec<Report>, RnixParseErr> { |
9 | let parsed = rnix::parse(source).as_result()?; | 12 | let parsed = rnix::parse(source).as_result()?; |
10 | 13 | ||
11 | Ok(parsed | 14 | Ok(parsed |
12 | .node() | 15 | .node() |
13 | .preorder_with_tokens() | 16 | .preorder_with_tokens() |
14 | .filter_map(|event| match event { | 17 | .filter_map(|event| match event { |
15 | WalkEvent::Enter(child) => LINTS.get(&child.kind()).map(|rules| { | 18 | WalkEvent::Enter(child) => lints.get(&child.kind()).map(|rules| { |
16 | rules | 19 | rules |
17 | .iter() | 20 | .iter() |
18 | .filter_map(|rule| rule.validate(&child)) | 21 | .filter_map(|rule| rule.validate(&child)) |
@@ -54,7 +57,7 @@ fn reorder(mut reports: Vec<Report>) -> Vec<Report> { | |||
54 | impl<'a> Iterator for FixResult<'a> { | 57 | impl<'a> Iterator for FixResult<'a> { |
55 | type Item = FixResult<'a>; | 58 | type Item = FixResult<'a>; |
56 | fn next(&mut self) -> Option<Self::Item> { | 59 | fn next(&mut self) -> Option<Self::Item> { |
57 | let all_reports = collect_fixes(&self.src).ok()?; | 60 | let all_reports = collect_fixes(&self.src, &self.lints).ok()?; |
58 | if all_reports.is_empty() { | 61 | if all_reports.is_empty() { |
59 | return None; | 62 | return None; |
60 | } | 63 | } |
@@ -74,13 +77,14 @@ impl<'a> Iterator for FixResult<'a> { | |||
74 | Some(FixResult { | 77 | Some(FixResult { |
75 | src: self.src.clone(), | 78 | src: self.src.clone(), |
76 | fixed, | 79 | fixed, |
80 | lints: self.lints, | ||
77 | }) | 81 | }) |
78 | } | 82 | } |
79 | } | 83 | } |
80 | 84 | ||
81 | pub fn all(src: &str) -> Option<FixResult> { | 85 | pub fn all_with<'a>(src: &'a str, lints: &'a LintMap) -> Option<FixResult<'a>> { |
82 | let src = Cow::from(src); | 86 | let src = Cow::from(src); |
83 | let _ = rnix::parse(&src).as_result().ok()?; | 87 | let _ = rnix::parse(&src).as_result().ok()?; |
84 | let initial = FixResult::empty(src); | 88 | let initial = FixResult::empty(src, lints); |
85 | initial.into_iter().last() | 89 | initial.into_iter().last() |
86 | } | 90 | } |
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 @@ | |||
1 | use std::{borrow::Cow, convert::TryFrom}; | 1 | use std::{borrow::Cow, convert::TryFrom}; |
2 | 2 | ||
3 | use lib::{Report, LINTS}; | 3 | use lib::Report; |
4 | use rnix::{TextSize, WalkEvent}; | 4 | use rnix::{TextSize, WalkEvent}; |
5 | 5 | ||
6 | use crate::err::SingleFixErr; | 6 | use crate::{err::SingleFixErr, fix::Source, utils}; |
7 | use crate::fix::Source; | ||
8 | 7 | ||
9 | pub struct SingleFixResult<'δ> { | 8 | pub struct SingleFixResult<'δ> { |
10 | pub src: Source<'δ>, | 9 | pub src: Source<'δ>, |
@@ -31,12 +30,13 @@ fn pos_to_byte(line: usize, col: usize, src: &str) -> Result<TextSize, SingleFix | |||
31 | fn find(offset: TextSize, src: &str) -> Result<Report, SingleFixErr> { | 30 | fn find(offset: TextSize, src: &str) -> Result<Report, SingleFixErr> { |
32 | // we don't really need the source to form a completely parsed tree | 31 | // we don't really need the source to form a completely parsed tree |
33 | let parsed = rnix::parse(src); | 32 | let parsed = rnix::parse(src); |
33 | let lints = utils::lint_map(); | ||
34 | 34 | ||
35 | parsed | 35 | parsed |
36 | .node() | 36 | .node() |
37 | .preorder_with_tokens() | 37 | .preorder_with_tokens() |
38 | .filter_map(|event| match event { | 38 | .filter_map(|event| match event { |
39 | WalkEvent::Enter(child) => LINTS.get(&child.kind()).map(|rules| { | 39 | WalkEvent::Enter(child) => lints.get(&child.kind()).map(|rules| { |
40 | rules | 40 | rules |
41 | .iter() | 41 | .iter() |
42 | .filter_map(|rule| rule.validate(&child)) | 42 | .filter_map(|rule| rule.validate(&child)) |