aboutsummaryrefslogtreecommitdiff
path: root/bin/src/fix/all.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-11-20 13:26:26 +0000
committerAkshay <[email protected]>2021-11-28 07:55:23 +0000
commit2b6012a79cb092e5d88c050cb494057efef28fc2 (patch)
treecd31973c32431a5cbf8c12ce574f799d065852be /bin/src/fix/all.rs
parent4e063b2abc402ac4d6902647e821978269025c7d (diff)
introduce --config flag
Diffstat (limited to 'bin/src/fix/all.rs')
-rw-r--r--bin/src/fix/all.rs18
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 @@
1use std::borrow::Cow; 1use std::borrow::Cow;
2 2
3use lib::{Report, LINTS}; 3use lib::Report;
4use rnix::{parser::ParseError as RnixParseErr, WalkEvent}; 4use rnix::{parser::ParseError as RnixParseErr, WalkEvent};
5 5
6use crate::fix::{FixResult, Fixed}; 6use crate::{
7 fix::{FixResult, Fixed},
8 LintMap,
9};
7 10
8fn collect_fixes(source: &str) -> Result<Vec<Report>, RnixParseErr> { 11fn 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> {
54impl<'a> Iterator for FixResult<'a> { 57impl<'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
81pub fn all(src: &str) -> Option<FixResult> { 85pub 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}