diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2024-01-08 12:18:42 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-08 17:18:42 +0000 |
commit | e212e1fc35ddae63f457f0f2a2e95154e008941f (patch) | |
tree | 6c1f553fbc529bfcab6413049af8f32ed31d1dfd /cli/tools/fmt.rs | |
parent | ee45d5bf8f2e1826b2a106b030abe891cfc0b37c (diff) |
perf: skip expanding exclude globs (#21817)
We were calling `expand_glob` on our excludes, which is very expensive
and unnecessary because we can pattern match while traversing instead.
1. Doesn't expand "exclude" globs. Instead pattern matches while walking
the directory.
2. Splits up the "include" into base paths and applicable file patterns.
This causes less pattern matching to occur because we're only pattern
matching on patterns that might match and not ones in completely
unrelated directories.
Diffstat (limited to 'cli/tools/fmt.rs')
-rw-r--r-- | cli/tools/fmt.rs | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs index 111632d4a..c35c72844 100644 --- a/cli/tools/fmt.rs +++ b/cli/tools/fmt.rs @@ -8,7 +8,6 @@ //! the same functions as ops available in JS runtime. use crate::args::CliOptions; -use crate::args::FilesConfig; use crate::args::Flags; use crate::args::FmtFlags; use crate::args::FmtOptions; @@ -18,7 +17,9 @@ use crate::colors; use crate::factory::CliFactory; use crate::util::diff::diff; use crate::util::file_watcher; +use crate::util::fs::canonicalize_path; use crate::util::fs::FileCollector; +use crate::util::glob::FilePatterns; use crate::util::path::get_extension; use crate::util::text_encoding; use deno_ast::ParsedSource; @@ -72,7 +73,7 @@ pub async fn format(flags: Flags, fmt_flags: FmtFlags) -> Result<(), AnyError> { let cli_options = factory.cli_options(); let fmt_options = cli_options.resolve_fmt_options(fmt_flags)?; let files = - collect_fmt_files(&fmt_options.files).and_then(|files| { + collect_fmt_files(fmt_options.files.clone()).and_then(|files| { if files.is_empty() { Err(generic_error("No target files found.")) } else { @@ -85,13 +86,21 @@ pub async fn format(flags: Flags, fmt_flags: FmtFlags) -> Result<(), AnyError> { // check all files on any changed (https://github.com/denoland/deno/issues/12446) files .iter() - .any(|path| paths.contains(path)) + .any(|path| { + canonicalize_path(path) + .map(|path| paths.contains(&path)) + .unwrap_or(false) + }) .then_some(files) .unwrap_or_else(|| [].to_vec()) } else { files .into_iter() - .filter(|path| paths.contains(path)) + .filter(|path| { + canonicalize_path(path) + .map(|path| paths.contains(&path)) + .unwrap_or(false) + }) .collect::<Vec<_>>() } } else { @@ -108,13 +117,14 @@ pub async fn format(flags: Flags, fmt_flags: FmtFlags) -> Result<(), AnyError> { let factory = CliFactory::from_flags(flags).await?; let cli_options = factory.cli_options(); let fmt_options = cli_options.resolve_fmt_options(fmt_flags)?; - let files = collect_fmt_files(&fmt_options.files).and_then(|files| { - if files.is_empty() { - Err(generic_error("No target files found.")) - } else { - Ok(files) - } - })?; + let files = + collect_fmt_files(fmt_options.files.clone()).and_then(|files| { + if files.is_empty() { + Err(generic_error("No target files found.")) + } else { + Ok(files) + } + })?; format_files(factory, fmt_options, files).await?; } @@ -144,13 +154,12 @@ async fn format_files( Ok(()) } -fn collect_fmt_files(files: &FilesConfig) -> Result<Vec<PathBuf>, AnyError> { - FileCollector::new(is_supported_ext_fmt) +fn collect_fmt_files(files: FilePatterns) -> Result<Vec<PathBuf>, AnyError> { + FileCollector::new(|path, _| is_supported_ext_fmt(path)) .ignore_git_folder() .ignore_node_modules() .ignore_vendor_folder() - .add_ignore_paths(&files.exclude) - .collect_files(files.include.as_deref()) + .collect_file_patterns(files) } /// Formats markdown (using <https://github.com/dprint/dprint-plugin-markdown>) and its code blocks |