aboutsummaryrefslogtreecommitdiff
path: root/bin/src
diff options
context:
space:
mode:
Diffstat (limited to 'bin/src')
-rw-r--r--bin/src/fix.rs30
-rw-r--r--bin/src/fix/all.rs21
-rw-r--r--bin/src/fix/single.rs15
-rw-r--r--bin/src/lib.rs1
-rw-r--r--bin/src/lint.rs23
-rw-r--r--bin/src/session.rs0
-rw-r--r--bin/src/utils.rs11
7 files changed, 80 insertions, 21 deletions
diff --git a/bin/src/fix.rs b/bin/src/fix.rs
index a035379..4b2ce0c 100644
--- a/bin/src/fix.rs
+++ b/bin/src/fix.rs
@@ -2,6 +2,7 @@ use std::borrow::Cow;
2 2
3use crate::LintMap; 3use crate::LintMap;
4 4
5use lib::session::SessionInfo;
5use rnix::TextRange; 6use rnix::TextRange;
6 7
7mod all; 8mod all;
@@ -16,6 +17,7 @@ pub struct FixResult<'a> {
16 pub src: Source<'a>, 17 pub src: Source<'a>,
17 pub fixed: Vec<Fixed>, 18 pub fixed: Vec<Fixed>,
18 pub lints: &'a LintMap, 19 pub lints: &'a LintMap,
20 pub sess: &'a SessionInfo,
19} 21}
20 22
21#[derive(Debug, Clone)] 23#[derive(Debug, Clone)]
@@ -25,11 +27,12 @@ pub struct Fixed {
25} 27}
26 28
27impl<'a> FixResult<'a> { 29impl<'a> FixResult<'a> {
28 fn empty(src: Source<'a>, lints: &'a LintMap) -> Self { 30 fn empty(src: Source<'a>, lints: &'a LintMap, sess: &'a SessionInfo) -> Self {
29 Self { 31 Self {
30 src, 32 src,
31 fixed: Vec::new(), 33 fixed: Vec::new(),
32 lints, 34 lints,
35 sess,
33 } 36 }
34 } 37 }
35} 38}
@@ -40,15 +43,27 @@ pub mod main {
40 use crate::{ 43 use crate::{
41 config::{Fix as FixConfig, FixOut, Single as SingleConfig}, 44 config::{Fix as FixConfig, FixOut, Single as SingleConfig},
42 err::{FixErr, StatixErr}, 45 err::{FixErr, StatixErr},
46 utils,
43 }; 47 };
44 48
49 use lib::session::{SessionInfo, Version};
45 use similar::TextDiff; 50 use similar::TextDiff;
46 51
47 pub fn all(fix_config: FixConfig) -> Result<(), StatixErr> { 52 pub fn all(fix_config: FixConfig) -> Result<(), StatixErr> {
48 let vfs = fix_config.vfs()?; 53 let vfs = fix_config.vfs()?;
49 let lints = fix_config.lints()?; 54 let lints = fix_config.lints()?;
55
56 let version = utils::get_version_info()
57 .unwrap()
58 .parse::<Version>()
59 .unwrap();
60 let session = SessionInfo::from_version(version);
61
50 for entry in vfs.iter() { 62 for entry in vfs.iter() {
51 match (fix_config.out(), super::all_with(entry.contents, &lints)) { 63 match (
64 fix_config.out(),
65 super::all_with(entry.contents, &lints, &session),
66 ) {
52 (FixOut::Diff, fix_result) => { 67 (FixOut::Diff, fix_result) => {
53 let src = fix_result 68 let src = fix_result
54 .map(|r| r.src) 69 .map(|r| r.src)
@@ -87,7 +102,16 @@ pub mod main {
87 let original_src = entry.contents; 102 let original_src = entry.contents;
88 let (line, col) = single_config.position; 103 let (line, col) = single_config.position;
89 104
90 match (single_config.out(), super::single(line, col, original_src)) { 105 let version = utils::get_version_info()
106 .unwrap()
107 .parse::<Version>()
108 .unwrap();
109 let session = SessionInfo::from_version(version);
110
111 match (
112 single_config.out(),
113 super::single(line, col, original_src, &session),
114 ) {
91 (FixOut::Diff, single_result) => { 115 (FixOut::Diff, single_result) => {
92 let fixed_src = single_result 116 let fixed_src = single_result
93 .map(|r| r.src) 117 .map(|r| r.src)
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 @@
1use std::borrow::Cow; 1use std::borrow::Cow;
2 2
3use lib::Report; 3use lib::{session::SessionInfo, Report};
4use rnix::{parser::ParseError as RnixParseErr, WalkEvent}; 4use rnix::{parser::ParseError as RnixParseErr, WalkEvent};
5 5
6use crate::{ 6use crate::{
@@ -8,7 +8,11 @@ use crate::{
8 LintMap, 8 LintMap,
9}; 9};
10 10
11fn collect_fixes(source: &str, lints: &LintMap) -> Result<Vec<Report>, RnixParseErr> { 11fn collect_fixes(
12 source: &str,
13 lints: &LintMap,
14 sess: &SessionInfo,
15) -> Result<Vec<Report>, RnixParseErr> {
12 let parsed = rnix::parse(source).as_result()?; 16 let parsed = rnix::parse(source).as_result()?;
13 17
14 Ok(parsed 18 Ok(parsed
@@ -18,7 +22,7 @@ fn collect_fixes(source: &str, lints: &LintMap) -> Result<Vec<Report>, RnixParse
18 WalkEvent::Enter(child) => lints.get(&child.kind()).map(|rules| { 22 WalkEvent::Enter(child) => lints.get(&child.kind()).map(|rules| {
19 rules 23 rules
20 .iter() 24 .iter()
21 .filter_map(|rule| rule.validate(&child)) 25 .filter_map(|rule| rule.validate(&child, sess))
22 .filter(|report| report.total_suggestion_range().is_some()) 26 .filter(|report| report.total_suggestion_range().is_some())
23 .collect::<Vec<_>>() 27 .collect::<Vec<_>>()
24 }), 28 }),
@@ -57,7 +61,7 @@ fn reorder(mut reports: Vec<Report>) -> Vec<Report> {
57impl<'a> Iterator for FixResult<'a> { 61impl<'a> Iterator for FixResult<'a> {
58 type Item = FixResult<'a>; 62 type Item = FixResult<'a>;
59 fn next(&mut self) -> Option<Self::Item> { 63 fn next(&mut self) -> Option<Self::Item> {
60 let all_reports = collect_fixes(&self.src, self.lints).ok()?; 64 let all_reports = collect_fixes(&self.src, self.lints, &self.sess).ok()?;
61 if all_reports.is_empty() { 65 if all_reports.is_empty() {
62 return None; 66 return None;
63 } 67 }
@@ -78,13 +82,18 @@ impl<'a> Iterator for FixResult<'a> {
78 src: self.src.clone(), 82 src: self.src.clone(),
79 fixed, 83 fixed,
80 lints: self.lints, 84 lints: self.lints,
85 sess: self.sess,
81 }) 86 })
82 } 87 }
83} 88}
84 89
85pub fn all_with<'a>(src: &'a str, lints: &'a LintMap) -> Option<FixResult<'a>> { 90pub fn all_with<'a>(
91 src: &'a str,
92 lints: &'a LintMap,
93 sess: &'a SessionInfo,
94) -> Option<FixResult<'a>> {
86 let src = Cow::from(src); 95 let src = Cow::from(src);
87 let _ = rnix::parse(&src).as_result().ok()?; 96 let _ = rnix::parse(&src).as_result().ok()?;
88 let initial = FixResult::empty(src, lints); 97 let initial = FixResult::empty(src, lints, sess);
89 initial.into_iter().last() 98 initial.into_iter().last()
90} 99}
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 @@
1use std::{borrow::Cow, convert::TryFrom}; 1use std::{borrow::Cow, convert::TryFrom};
2 2
3use lib::Report; 3use lib::{session::SessionInfo, Report};
4use rnix::{TextSize, WalkEvent}; 4use rnix::{TextSize, WalkEvent};
5 5
6use crate::{err::SingleFixErr, fix::Source, utils}; 6use crate::{err::SingleFixErr, fix::Source, utils};
@@ -27,7 +27,7 @@ fn pos_to_byte(line: usize, col: usize, src: &str) -> Result<TextSize, SingleFix
27 } 27 }
28} 28}
29 29
30fn find(offset: TextSize, src: &str) -> Result<Report, SingleFixErr> { 30fn find(offset: TextSize, src: &str, sess: &SessionInfo) -> Result<Report, SingleFixErr> {
31 // 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
32 let parsed = rnix::parse(src); 32 let parsed = rnix::parse(src);
33 let lints = utils::lint_map(); 33 let lints = utils::lint_map();
@@ -39,7 +39,7 @@ fn find(offset: TextSize, src: &str) -> Result<Report, SingleFixErr> {
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, sess))
43 .find(|report| report.total_suggestion_range().is_some()) 43 .find(|report| report.total_suggestion_range().is_some())
44 }), 44 }),
45 _ => None, 45 _ => None,
@@ -49,10 +49,15 @@ fn find(offset: TextSize, src: &str) -> Result<Report, SingleFixErr> {
49 .ok_or(SingleFixErr::NoOp) 49 .ok_or(SingleFixErr::NoOp)
50} 50}
51 51
52pub fn single(line: usize, col: usize, src: &str) -> Result<SingleFixResult, SingleFixErr> { 52pub fn single<'a, 'b>(
53 line: usize,
54 col: usize,
55 src: &'a str,
56 sess: &'b SessionInfo,
57) -> Result<SingleFixResult<'a>, SingleFixErr> {
53 let mut src = Cow::from(src); 58 let mut src = Cow::from(src);
54 let offset = pos_to_byte(line, col, &*src)?; 59 let offset = pos_to_byte(line, col, &*src)?;
55 let report = find(offset, &*src)?; 60 let report = find(offset, &*src, &sess)?;
56 61
57 report.apply(src.to_mut()); 62 report.apply(src.to_mut());
58 63
diff --git a/bin/src/lib.rs b/bin/src/lib.rs
index 0105334..01d3ea7 100644
--- a/bin/src/lib.rs
+++ b/bin/src/lib.rs
@@ -4,6 +4,7 @@ pub mod err;
4pub mod explain; 4pub mod explain;
5pub mod fix; 5pub mod fix;
6pub mod lint; 6pub mod lint;
7pub mod session;
7pub mod traits; 8pub mod traits;
8 9
9mod utils; 10mod utils;
diff --git a/bin/src/lint.rs b/bin/src/lint.rs
index 3482d46..c385007 100644
--- a/bin/src/lint.rs
+++ b/bin/src/lint.rs
@@ -1,6 +1,6 @@
1use crate::{utils, LintMap}; 1use crate::{utils, LintMap};
2 2
3use lib::Report; 3use lib::{session::SessionInfo, Report};
4use rnix::WalkEvent; 4use rnix::WalkEvent;
5use vfs::{FileId, VfsEntry}; 5use vfs::{FileId, VfsEntry};
6 6
@@ -10,7 +10,7 @@ pub struct LintResult {
10 pub reports: Vec<Report>, 10 pub reports: Vec<Report>,
11} 11}
12 12
13pub fn lint_with(vfs_entry: VfsEntry, lints: &LintMap) -> LintResult { 13pub fn lint_with(vfs_entry: VfsEntry, lints: &LintMap, sess: &SessionInfo) -> LintResult {
14 let file_id = vfs_entry.file_id; 14 let file_id = vfs_entry.file_id;
15 let source = vfs_entry.contents; 15 let source = vfs_entry.contents;
16 let parsed = rnix::parse(source); 16 let parsed = rnix::parse(source);
@@ -23,7 +23,7 @@ pub fn lint_with(vfs_entry: VfsEntry, lints: &LintMap) -> LintResult {
23 WalkEvent::Enter(child) => lints.get(&child.kind()).map(|rules| { 23 WalkEvent::Enter(child) => lints.get(&child.kind()).map(|rules| {
24 rules 24 rules
25 .iter() 25 .iter()
26 .filter_map(|rule| rule.validate(&child)) 26 .filter_map(|rule| rule.validate(&child, sess))
27 .collect::<Vec<_>>() 27 .collect::<Vec<_>>()
28 }), 28 }),
29 _ => None, 29 _ => None,
@@ -35,21 +35,30 @@ pub fn lint_with(vfs_entry: VfsEntry, lints: &LintMap) -> LintResult {
35 LintResult { file_id, reports } 35 LintResult { file_id, reports }
36} 36}
37 37
38pub fn lint(vfs_entry: VfsEntry) -> LintResult { 38pub fn lint(vfs_entry: VfsEntry, sess: &SessionInfo) -> LintResult {
39 lint_with(vfs_entry, &utils::lint_map()) 39 lint_with(vfs_entry, &utils::lint_map(), &sess)
40} 40}
41 41
42pub mod main { 42pub mod main {
43 use std::io; 43 use std::io;
44 44
45 use super::lint_with; 45 use super::lint_with;
46 use crate::{config::Check as CheckConfig, err::StatixErr, traits::WriteDiagnostic}; 46 use crate::{config::Check as CheckConfig, err::StatixErr, traits::WriteDiagnostic, utils};
47
48 use lib::session::{SessionInfo, Version};
47 49
48 pub fn main(check_config: CheckConfig) -> Result<(), StatixErr> { 50 pub fn main(check_config: CheckConfig) -> Result<(), StatixErr> {
49 let vfs = check_config.vfs()?; 51 let vfs = check_config.vfs()?;
50 let mut stdout = io::stdout(); 52 let mut stdout = io::stdout();
51 let lints = check_config.lints()?; 53 let lints = check_config.lints()?;
52 let lint = |vfs_entry| lint_with(vfs_entry, &lints); 54
55 let version = utils::get_version_info()
56 .unwrap()
57 .parse::<Version>()
58 .unwrap();
59 let session = SessionInfo::from_version(version);
60
61 let lint = |vfs_entry| lint_with(vfs_entry, &lints, &session);
53 vfs.iter().map(lint).for_each(|r| { 62 vfs.iter().map(lint).for_each(|r| {
54 stdout.write(&r, &vfs, check_config.format).unwrap(); 63 stdout.write(&r, &vfs, check_config.format).unwrap();
55 }); 64 });
diff --git a/bin/src/session.rs b/bin/src/session.rs
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/bin/src/session.rs
diff --git a/bin/src/utils.rs b/bin/src/utils.rs
index 747a761..d374b4b 100644
--- a/bin/src/utils.rs
+++ b/bin/src/utils.rs
@@ -22,3 +22,14 @@ pub fn lint_map_of(
22pub fn lint_map() -> HashMap<SyntaxKind, Vec<&'static Box<dyn Lint>>> { 22pub fn lint_map() -> HashMap<SyntaxKind, Vec<&'static Box<dyn Lint>>> {
23 lint_map_of(&*LINTS) 23 lint_map_of(&*LINTS)
24} 24}
25
26pub fn get_version_info() -> Option<String> {
27 use std::process::Command;
28 let program = Command::new("nix")
29 .arg("--version")
30 .output()
31 .expect("failed to execute");
32 std::str::from_utf8(&program.stdout)
33 .ok()
34 .map(ToOwned::to_owned)
35}