summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZheyu Zhang <zheyuzhang03@gmail.com>2021-11-29 22:17:57 +0800
committerGitHub <noreply@github.com>2021-11-29 09:17:57 -0500
commitbd989143e1a23366edb53b6b543eeb0ae0ff3e51 (patch)
tree6263637b32e80ef3b2258778996d664c418f213c
parent4a13c320d73b1d8a0da18effa81199ab2ea8ef55 (diff)
refactor(cli): simplify lint/format resolver logic (#12898)
-rw-r--r--cli/tools/fmt.rs49
-rw-r--r--cli/tools/lint.rs32
2 files changed, 39 insertions, 42 deletions
diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs
index 004b390d3..9a61851d3 100644
--- a/cli/tools/fmt.rs
+++ b/cli/tools/fmt.rs
@@ -87,35 +87,34 @@ pub async fn format(
let resolver = |changed: Option<Vec<PathBuf>>| {
let files_changed = changed.is_some();
- let collect_files =
- collect_files(&include_files, &exclude_files, is_supported_ext_fmt);
-
- let (result, should_refmt) = match collect_files {
- Ok(value) => {
- if let Some(paths) = changed {
- let refmt_files = value
- .clone()
- .into_iter()
- .filter(|path| paths.contains(path))
- .collect::<Vec<_>>();
-
- let should_refmt = !refmt_files.is_empty();
-
- if check {
- (Ok((value, fmt_options.clone())), Some(should_refmt))
+ let result =
+ collect_files(&include_files, &exclude_files, is_supported_ext_fmt).map(
+ |files| {
+ let refmt_files = if let Some(paths) = changed {
+ if check {
+ files
+ .iter()
+ .any(|path| paths.contains(path))
+ .then(|| files)
+ .unwrap_or_else(|| [].to_vec())
+ } else {
+ files
+ .into_iter()
+ .filter(|path| paths.contains(path))
+ .collect::<Vec<_>>()
+ }
} else {
- (Ok((refmt_files, fmt_options.clone())), Some(should_refmt))
- }
- } else {
- (Ok((value, fmt_options.clone())), None)
- }
- }
- Err(e) => (Err(e), None),
- };
+ files
+ };
+ (refmt_files, fmt_options.clone())
+ },
+ );
let paths_to_watch = include_files.clone();
async move {
- if files_changed && matches!(should_refmt, Some(false)) {
+ if files_changed
+ && matches!(result, Ok((ref files, _)) if files.is_empty())
+ {
ResolutionResult::Ignore
} else {
ResolutionResult::Restart {
diff --git a/cli/tools/lint.rs b/cli/tools/lint.rs
index b7ae88e4f..4a7e0115e 100644
--- a/cli/tools/lint.rs
+++ b/cli/tools/lint.rs
@@ -112,27 +112,25 @@ pub async fn lint(
let resolver = |changed: Option<Vec<PathBuf>>| {
let files_changed = changed.is_some();
- let collect_files =
- collect_files(&include_files, &exclude_files, is_supported_ext);
+ 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(|| files)
+ .unwrap_or_else(|| [].to_vec())
+ } else {
+ files
+ }
+ },
+ );
let paths_to_watch = include_files.clone();
- let (result, should_relint) = match collect_files {
- Ok(value) => {
- if let Some(paths) = changed {
- (
- Ok(value.clone()),
- Some(value.iter().any(|path| paths.contains(path))),
- )
- } else {
- (Ok(value), None)
- }
- }
- Err(e) => (Err(e), None),
- };
-
async move {
- if files_changed && matches!(should_relint, Some(false)) {
+ if files_changed && matches!(result, Ok(ref files) if files.is_empty()) {
ResolutionResult::Ignore
} else {
ResolutionResult::Restart {