Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fmt): ignore node_modules directory #14943

Merged
merged 1 commit into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cli/tests/integration/fmt_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn fmt_ignore_unexplicit_files() {
}

#[test]
fn fmt_auto_ignore_git() {
fn fmt_auto_ignore_git_and_node_modules() {
use std::fs::{create_dir_all, File};
use std::io::Write;
use std::path::PathBuf;
Expand All @@ -139,10 +139,16 @@ fn fmt_auto_ignore_git() {
let t = temp_dir.path().join("target");
let nest_git = t.join("nest").join(".git");
let git_dir = t.join(".git");
let nest_node_modules = t.join("nest").join("node_modules");
let node_modules_dir = t.join("node_modules");
create_dir_all(&nest_git).unwrap();
create_dir_all(&git_dir).unwrap();
create_dir_all(&nest_node_modules).unwrap();
create_dir_all(&node_modules_dir).unwrap();
create_bad_json(nest_git);
create_bad_json(git_dir);
create_bad_json(nest_node_modules);
create_bad_json(node_modules_dir);
let output = util::deno_cmd()
.current_dir(t)
.env("NO_COLOR", "1")
Expand Down
33 changes: 26 additions & 7 deletions cli/tools/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,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) && !is_contain_git(path);
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();
Expand Down Expand Up @@ -730,10 +733,14 @@ fn is_supported_ext_fmt(path: &Path) -> bool {
}
}

fn is_contain_git(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::*;
Expand Down Expand Up @@ -767,10 +774,22 @@ mod test {

#[test]
fn test_is_located_in_git() {
assert!(is_contain_git(Path::new("test/.git")));
assert!(is_contain_git(Path::new(".git/bad.json")));
assert!(is_contain_git(Path::new("test/.git/bad.json")));
assert!(!is_contain_git(Path::new("test/bad.git/bad.json")));
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]
Expand Down