diff options
Diffstat (limited to 'cli/tools')
-rw-r--r-- | cli/tools/coverage/mod.rs | 10 | ||||
-rw-r--r-- | cli/tools/fmt.rs | 55 | ||||
-rw-r--r-- | cli/tools/lint.rs | 54 |
3 files changed, 54 insertions, 65 deletions
diff --git a/cli/tools/coverage/mod.rs b/cli/tools/coverage/mod.rs index f19cdfa3f..aacaf3d83 100644 --- a/cli/tools/coverage/mod.rs +++ b/cli/tools/coverage/mod.rs @@ -6,7 +6,7 @@ use crate::colors; use crate::emit::get_source_hash; use crate::proc_state::ProcState; use crate::tools::fmt::format_json; -use crate::util::fs::collect_files; +use crate::util::fs::FileCollector; use crate::util::text_encoding::source_map_from_code; use deno_ast::MediaType; @@ -558,9 +558,13 @@ fn collect_coverages( ignore: Vec<PathBuf>, ) -> Result<Vec<ScriptCoverage>, AnyError> { let mut coverages: Vec<ScriptCoverage> = Vec::new(); - let file_paths = collect_files(&files, &ignore, |file_path| { + let file_paths = FileCollector::new(|file_path| { file_path.extension().map_or(false, |ext| ext == "json") - })?; + }) + .ignore_git_folder() + .ignore_node_modules() + .add_ignore_paths(&ignore) + .collect_files(&files)?; for file_path in file_paths { let json = fs::read_to_string(file_path.as_path())?; diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs index 721937b8d..7b5797d88 100644 --- a/cli/tools/fmt.rs +++ b/cli/tools/fmt.rs @@ -15,7 +15,7 @@ use crate::colors; use crate::util::diff::diff; use crate::util::file_watcher; use crate::util::file_watcher::ResolutionResult; -use crate::util::fs::collect_files; +use crate::util::fs::FileCollector; use crate::util::path::get_extension; use crate::util::path::specifier_to_file_path; use crate::util::text_encoding; @@ -92,17 +92,11 @@ pub async fn format( maybe_fmt_config.map(|c| c.options).unwrap_or_default(), ); - let fmt_predicate = |path: &Path| { - is_supported_ext_fmt(path) - && !contains_git(path) - && !contains_node_modules(path) - }; - let resolver = |changed: Option<Vec<PathBuf>>| { let files_changed = changed.is_some(); - let result = collect_files(&include_files, &exclude_files, fmt_predicate) - .map(|files| { + let result = + collect_fmt_files(&include_files, &exclude_files).map(|files| { let refmt_files = if let Some(paths) = changed { if check { files @@ -164,8 +158,8 @@ pub async fn format( ) .await?; } else { - let files = collect_files(&include_files, &exclude_files, fmt_predicate) - .and_then(|files| { + let files = + collect_fmt_files(&include_files, &exclude_files).and_then(|files| { if files.is_empty() { Err(generic_error("No target files found.")) } else { @@ -178,6 +172,17 @@ pub async fn format( Ok(()) } +fn collect_fmt_files( + include_files: &[PathBuf], + exclude_files: &[PathBuf], +) -> Result<Vec<PathBuf>, AnyError> { + FileCollector::new(is_supported_ext_fmt) + .ignore_git_folder() + .ignore_node_modules() + .add_ignore_paths(exclude_files) + .collect_files(include_files) +} + /// Formats markdown (using <https://github.com/dprint/dprint-plugin-markdown>) and its code blocks /// (ts/tsx, js/jsx). fn format_markdown( @@ -734,14 +739,6 @@ fn is_supported_ext_fmt(path: &Path) -> bool { } } -fn contains_git(path: &Path) -> bool { - path.components().any(|c| c.as_os_str() == ".git") -} - -fn contains_node_modules(path: &Path) -> bool { - path.components().any(|c| c.as_os_str() == "node_modules") -} - #[cfg(test)] mod test { use super::*; @@ -774,26 +771,6 @@ mod test { } #[test] - fn test_is_located_in_git() { - assert!(contains_git(Path::new("test/.git"))); - assert!(contains_git(Path::new(".git/bad.json"))); - assert!(contains_git(Path::new("test/.git/bad.json"))); - assert!(!contains_git(Path::new("test/bad.git/bad.json"))); - } - - #[test] - fn test_is_located_in_node_modules() { - assert!(contains_node_modules(Path::new("test/node_modules"))); - assert!(contains_node_modules(Path::new("node_modules/bad.json"))); - assert!(contains_node_modules(Path::new( - "test/node_modules/bad.json" - ))); - assert!(!contains_node_modules(Path::new( - "test/bad.node_modules/bad.json" - ))); - } - - #[test] #[should_panic(expected = "Formatting not stable. Bailed after 5 tries.")] fn test_format_ensure_stable_unstable_format() { format_ensure_stable( diff --git a/cli/tools/lint.rs b/cli/tools/lint.rs index 2f7cd5111..1b2487e4c 100644 --- a/cli/tools/lint.rs +++ b/cli/tools/lint.rs @@ -14,7 +14,7 @@ use crate::proc_state::ProcState; use crate::tools::fmt::run_parallelized; use crate::util::file_watcher; use crate::util::file_watcher::ResolutionResult; -use crate::util::fs::collect_files; +use crate::util::fs::FileCollector; use crate::util::path::is_supported_ext; use crate::util::path::specifier_to_file_path; use deno_ast::MediaType; @@ -143,19 +143,17 @@ pub async fn lint(flags: Flags, lint_flags: LintFlags) -> Result<(), AnyError> { let resolver = |changed: Option<Vec<PathBuf>>| { let files_changed = changed.is_some(); let result = - collect_files(&include_files, &exclude_files, is_supported_ext).map( - |files| { - if let Some(paths) = changed { - files - .iter() - .any(|path| paths.contains(path)) - .then_some(files) - .unwrap_or_else(|| [].to_vec()) - } else { - files - } - }, - ); + collect_lint_files(&include_files, &exclude_files).map(|files| { + if let Some(paths) = changed { + files + .iter() + .any(|path| paths.contains(path)) + .then_some(files) + .unwrap_or_else(|| [].to_vec()) + } else { + files + } + }); let paths_to_watch = include_files.clone(); @@ -251,15 +249,14 @@ pub async fn lint(flags: Flags, lint_flags: LintFlags) -> Result<(), AnyError> { ); reporter_lock.lock().unwrap().close(1); } else { - let target_files = - collect_files(&include_files, &exclude_files, is_supported_ext) - .and_then(|files| { - if files.is_empty() { - Err(generic_error("No target files found.")) - } else { - Ok(files) - } - })?; + let target_files = collect_lint_files(&include_files, &exclude_files) + .and_then(|files| { + if files.is_empty() { + Err(generic_error("No target files found.")) + } else { + Ok(files) + } + })?; debug!("Found {} files", target_files.len()); operation(target_files).await?; }; @@ -272,6 +269,17 @@ pub async fn lint(flags: Flags, lint_flags: LintFlags) -> Result<(), AnyError> { Ok(()) } +fn collect_lint_files( + include_files: &[PathBuf], + exclude_files: &[PathBuf], +) -> Result<Vec<PathBuf>, AnyError> { + FileCollector::new(is_supported_ext) + .ignore_git_folder() + .ignore_node_modules() + .add_ignore_paths(exclude_files) + .collect_files(include_files) +} + pub fn print_rules_list(json: bool) { let lint_rules = rules::get_recommended_rules(); |