1use std::{
2 env, fs,
3 io::{self, BufRead},
4 path::{Path, PathBuf},
5 sync::Mutex,
6};
7
8use ignore::WalkBuilder;
9use nu_parser::parse;
10use nu_protocol::{
11 Span, Value,
12 ast::Block,
13 engine::{EngineState, FileStack, StateWorkingSet},
14};
15use rayon::prelude::*;
16
17use crate::{
18 LintError,
19 config::Config,
20 context::LintContext,
21 rules::USED_RULES,
22 violation::{SourceFile, Violation},
23};
24
25pub fn parse_source<'a>(
28 engine_state: &'a EngineState,
29 source: &[u8],
30) -> (Block, StateWorkingSet<'a>, usize) {
31 let mut working_set = StateWorkingSet::new(engine_state);
32 let file_offset = working_set.next_span_start();
34 let _file_id = working_set.add_file("source".to_string(), source);
36 working_set.files = FileStack::with_file(Path::new("source").to_path_buf());
38 let block = parse(&mut working_set, Some("source"), source, false);
39
40 ((*block).clone(), working_set, file_offset)
41}
42
43fn is_nushell_file(path: &Path) -> bool {
45 path.extension()
46 .and_then(|s| s.to_str())
47 .is_some_and(|ext| ext == "nu")
48 || fs::File::open(path)
49 .ok()
50 .and_then(|file| {
51 let mut reader = io::BufReader::new(file);
52 let mut first_line = String::new();
53 reader.read_line(&mut first_line).ok()?;
54 first_line.starts_with("#!").then(|| {
55 first_line
56 .split_whitespace()
57 .any(|word| word.ends_with("/nu") || word == "nu")
58 })
59 })
60 .unwrap_or(false)
61}
62
63#[must_use]
65pub fn collect_nu_files_from_dir(dir: &Path) -> Vec<PathBuf> {
66 WalkBuilder::new(dir)
67 .standard_filters(true)
68 .build()
69 .filter_map(|result| match result {
70 Ok(entry) => {
71 let path = entry.path().to_path_buf();
72 (path.is_file() && is_nushell_file(&path)).then_some(path)
73 }
74 Err(err) => {
75 log::warn!("Error walking directory: {err}");
76 None
77 }
78 })
79 .collect()
80}
81
82#[must_use]
87pub fn collect_nu_files(paths: &[PathBuf]) -> Vec<PathBuf> {
88 paths
89 .iter()
90 .flat_map(|path| {
91 if !path.exists() {
92 log::warn!("Path not found: {}", path.display());
93 return vec![];
94 }
95
96 if path.is_file() {
97 if is_nushell_file(path) {
98 vec![path.clone()]
99 } else {
100 vec![]
101 }
102 } else if path.is_dir() {
103 collect_nu_files_from_dir(path)
104 } else {
105 vec![]
106 }
107 })
108 .collect()
109}
110
111pub struct LintEngine {
112 pub(crate) config: Config,
113 engine_state: EngineState,
114}
115
116impl LintEngine {
117 #[must_use]
119 pub fn new_state() -> EngineState {
120 let mut engine_state = nu_cmd_lang::create_default_context();
121 engine_state = nu_command::add_shell_command_context(engine_state);
122 engine_state = nu_cli::add_cli_context(engine_state);
123
124 if let Ok(cwd) = env::current_dir()
126 && let Some(cwd) = cwd.to_str()
127 {
128 engine_state.add_env_var("PWD".into(), Value::string(cwd, Span::unknown()));
129 }
130
131 let delta = {
133 let mut working_set = StateWorkingSet::new(&engine_state);
134 working_set.add_decl(Box::new(nu_cli::Print));
135 working_set.render()
136 };
137 engine_state
138 .merge_delta(delta)
139 .expect("Failed to add Print command");
140
141 nu_std::load_standard_library(&mut engine_state).unwrap();
143
144 engine_state.generate_nu_constant();
146 engine_state
147 }
148
149 #[must_use]
150 pub fn new(config: Config) -> Self {
151 Self {
152 config,
153 engine_state: Self::new_state(),
154 }
155 }
156
157 pub(crate) fn lint_file(&self, path: &Path) -> Result<Vec<Violation>, LintError> {
163 log::debug!("Linting file: {}", path.display());
164 let source = fs::read_to_string(path).map_err(|source| LintError::Io {
165 path: path.to_path_buf(),
166 source,
167 })?;
168 let mut violations = self.lint_str(&source);
169
170 for violation in &mut violations {
171 violation.file = Some(path.into());
172 }
173
174 violations.sort_by(|a, b| {
175 a.file_span()
176 .start
177 .cmp(&b.file_span().start)
178 .then(a.lint_level.cmp(&b.lint_level))
179 });
180 Ok(violations)
181 }
182
183 #[must_use]
188 pub fn lint_files(&self, files: &[PathBuf]) -> Vec<Violation> {
189 let violations_mutex = Mutex::new(Vec::new());
190
191 let process_file = |path: &PathBuf| match self.lint_file(path) {
192 Ok(violations) => {
193 violations_mutex
194 .lock()
195 .expect("Failed to lock violations mutex")
196 .extend(violations);
197 }
198 Err(e) => {
199 log::error!("Error linting {}: {}", path.display(), e);
200 }
201 };
202
203 if self.config.sequential {
204 for path in files {
205 log::debug!("Processing file: {}", path.display());
206 process_file(path);
207 }
208 } else {
209 files.par_iter().for_each(process_file);
210 }
211
212 violations_mutex
213 .into_inner()
214 .expect("Failed to unwrap violations mutex")
215 }
216
217 #[must_use]
219 pub fn lint_stdin(&self, source: &str) -> Vec<Violation> {
220 let mut violations = self.lint_str(source);
221 let source_owned = source.to_string();
222
223 for violation in &mut violations {
224 violation.file = Some(SourceFile::Stdin);
225 violation.source = Some(source_owned.clone().into());
226 }
227
228 violations
229 }
230
231 #[must_use]
232 pub fn lint_str(&self, source: &str) -> Vec<Violation> {
233 let (block, working_set, file_offset) = parse_source(&self.engine_state, source.as_bytes());
234
235 let context = LintContext::new(
236 source,
237 &block,
238 &self.engine_state,
239 &working_set,
240 file_offset,
241 &self.config,
242 );
243
244 let mut violations = self.detect_with_fix_data(&context);
245
246 for violation in &mut violations {
248 violation.normalize_spans(file_offset);
249 }
250
251 violations
252 }
253
254 fn detect_with_fix_data(&self, context: &LintContext) -> Vec<Violation> {
256 USED_RULES
257 .iter()
258 .filter_map(|rule| {
259 let lint_level = self.config.get_lint_level(*rule)?;
260
261 let mut violations = rule.check(context);
262 for violation in &mut violations {
263 violation.set_rule_id(rule.id());
264 violation.set_lint_level(lint_level);
265 violation.set_doc_url(rule.doc_url());
266 }
267
268 (!violations.is_empty()).then_some(violations)
269 })
270 .flatten()
271 .collect()
272 }
273}