summaryrefslogtreecommitdiff
path: root/cli/tools/fmt.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2022-12-07 13:10:10 -0500
committerGitHub <noreply@github.com>2022-12-07 13:10:10 -0500
commit9c1ab39e19073501618947ffa370ba59b04ec6cc (patch)
treeba729bae7d261bf38ede4179c98fb5e17928b8ea /cli/tools/fmt.rs
parentf4385866f89e0abd3f5f1b0281abf00f1c562be9 (diff)
feat: ignore `node_modules` and `.git` folders when collecting files everywhere (#16862)
We currently only do this for fmt. This makes it so they're excluded by default, but you can still opt into these directories by explicitly specifying them.
Diffstat (limited to 'cli/tools/fmt.rs')
-rw-r--r--cli/tools/fmt.rs55
1 files changed, 16 insertions, 39 deletions
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(