aboutsummaryrefslogtreecommitdiff
path: root/bin/src/fix
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-11-20 13:27:28 +0000
committerAkshay <[email protected]>2021-11-20 13:27:28 +0000
commitf27372061a0effe3b00d400f4e577b9d9e0ad4c0 (patch)
treeedb9a9644a7ce6e8c28e937cdc5e4d62d45b4cb3 /bin/src/fix
parenta5c3e679b06536bb43085b1a978854a73274af10 (diff)
add config option to fix and check
Diffstat (limited to 'bin/src/fix')
-rw-r--r--bin/src/fix/all.rs18
-rw-r--r--bin/src/fix/single.rs8
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 @@
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}
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 @@
1use std::{borrow::Cow, convert::TryFrom}; 1use std::{borrow::Cow, convert::TryFrom};
2 2
3use lib::{Report, LINTS}; 3use lib::Report;
4use rnix::{TextSize, WalkEvent}; 4use rnix::{TextSize, WalkEvent};
5 5
6use crate::err::SingleFixErr; 6use crate::{err::SingleFixErr, fix::Source, utils};
7use crate::fix::Source;
8 7
9pub struct SingleFixResult<'δ> { 8pub 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
31fn find(offset: TextSize, src: &str) -> Result<Report, SingleFixErr> { 30fn 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))