diff options
Diffstat (limited to 'bin/src/fix/all.rs')
-rw-r--r-- | bin/src/fix/all.rs | 18 |
1 files changed, 11 insertions, 7 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 | } |