feat(fmt, lint): show number of checked files (#7312)

This commit is contained in:
Yusuke Tanaka 2020-09-09 23:45:31 +09:00 committed by GitHub
parent 1fcbf9cb8a
commit 857f9b32e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 79 additions and 19 deletions

View file

@ -1732,7 +1732,7 @@ mod tests {
subcommand: DenoSubcommand::Fmt {
ignore: vec![],
check: false,
files: vec!["script_1.ts".to_string(), "script_2.ts".to_string()]
files: vec!["script_1.ts".to_string(), "script_2.ts".to_string()],
},
..Flags::default()
}

View file

@ -58,13 +58,16 @@ async fn check_source_files(
paths: Vec<PathBuf>,
) -> Result<(), ErrBox> {
let not_formatted_files_count = Arc::new(AtomicUsize::new(0));
let checked_files_count = Arc::new(AtomicUsize::new(0));
// prevent threads outputting at the same time
let output_lock = Arc::new(Mutex::new(0));
run_parallelized(paths, {
let not_formatted_files_count = not_formatted_files_count.clone();
let checked_files_count = checked_files_count.clone();
move |file_path| {
checked_files_count.fetch_add(1, Ordering::Relaxed);
let file_text = read_file_contents(&file_path)?.text;
let r = dprint::format_text(&file_path, &file_text, &config);
match r {
@ -105,13 +108,17 @@ async fn check_source_files(
let not_formatted_files_count =
not_formatted_files_count.load(Ordering::Relaxed);
let checked_files_count = checked_files_count.load(Ordering::Relaxed);
let checked_files_str =
format!("{} {}", checked_files_count, files_str(checked_files_count));
if not_formatted_files_count == 0 {
println!("Checked {}", checked_files_str);
Ok(())
} else {
let not_formatted_files_str = files_str(not_formatted_files_count);
Err(ErrBox::error(format!(
"Found {} not formatted {}",
not_formatted_files_count,
files_str(not_formatted_files_count),
"Found {} not formatted {} in {}",
not_formatted_files_count, not_formatted_files_str, checked_files_str,
)))
}
}
@ -121,11 +128,14 @@ async fn format_source_files(
paths: Vec<PathBuf>,
) -> Result<(), ErrBox> {
let formatted_files_count = Arc::new(AtomicUsize::new(0));
let checked_files_count = Arc::new(AtomicUsize::new(0));
let output_lock = Arc::new(Mutex::new(0)); // prevent threads outputting at the same time
run_parallelized(paths, {
let formatted_files_count = formatted_files_count.clone();
let checked_files_count = checked_files_count.clone();
move |file_path| {
checked_files_count.fetch_add(1, Ordering::Relaxed);
let file_contents = read_file_contents(&file_path)?;
let r = dprint::format_text(&file_path, &file_contents.text, &config);
match r {
@ -160,6 +170,14 @@ async fn format_source_files(
formatted_files_count,
files_str(formatted_files_count),
);
let checked_files_count = checked_files_count.load(Ordering::Relaxed);
println!(
"Checked {} {}",
checked_files_count,
files_str(checked_files_count)
);
Ok(())
}

View file

@ -55,6 +55,7 @@ pub async fn lint_files(
target_files.retain(|f| !ignore_files.contains(&f));
}
debug!("Found {} files", target_files.len());
let target_files_len = target_files.len();
let has_error = Arc::new(AtomicBool::new(false));
@ -77,7 +78,7 @@ pub async fn lint_files(
sort_diagnostics(&mut file_diagnostics);
for d in file_diagnostics.iter() {
has_error.store(true, Ordering::Relaxed);
reporter.visit(&d, source.split('\n').collect());
reporter.visit_diagnostic(&d, source.split('\n').collect());
}
}
Err(err) => {
@ -92,7 +93,7 @@ pub async fn lint_files(
let has_error = has_error.load(Ordering::Relaxed);
reporter_lock.lock().unwrap().close();
reporter_lock.lock().unwrap().close(target_files_len);
if has_error {
std::process::exit(1);
@ -168,7 +169,7 @@ fn lint_stdin(json: bool) -> Result<(), ErrBox> {
Ok(diagnostics) => {
for d in diagnostics {
has_error = true;
reporter.visit(&d, source.split('\n').collect());
reporter.visit_diagnostic(&d, source.split('\n').collect());
}
}
Err(err) => {
@ -177,7 +178,7 @@ fn lint_stdin(json: bool) -> Result<(), ErrBox> {
}
}
reporter.close();
reporter.close(1);
if has_error {
std::process::exit(1);
@ -187,9 +188,9 @@ fn lint_stdin(json: bool) -> Result<(), ErrBox> {
}
trait LintReporter {
fn visit(&mut self, d: &LintDiagnostic, source_lines: Vec<&str>);
fn visit_diagnostic(&mut self, d: &LintDiagnostic, source_lines: Vec<&str>);
fn visit_error(&mut self, file_path: &str, err: &ErrBox);
fn close(&mut self);
fn close(&mut self, check_count: usize);
}
#[derive(Serialize)]
@ -209,7 +210,7 @@ impl PrettyLintReporter {
}
impl LintReporter for PrettyLintReporter {
fn visit(&mut self, d: &LintDiagnostic, source_lines: Vec<&str>) {
fn visit_diagnostic(&mut self, d: &LintDiagnostic, source_lines: Vec<&str>) {
self.lint_count += 1;
let pretty_message =
@ -234,12 +235,18 @@ impl LintReporter for PrettyLintReporter {
eprintln!(" {}", err);
}
fn close(&mut self) {
fn close(&mut self, check_count: usize) {
match self.lint_count {
1 => eprintln!("Found 1 problem"),
n if n > 1 => eprintln!("Found {} problems", self.lint_count),
_ => (),
}
match check_count {
1 => println!("Checked 1 file"),
n if n > 1 => println!("Checked {} files", n),
_ => (),
}
}
}
@ -299,7 +306,7 @@ impl JsonLintReporter {
}
impl LintReporter for JsonLintReporter {
fn visit(&mut self, d: &LintDiagnostic, _source_lines: Vec<&str>) {
fn visit_diagnostic(&mut self, d: &LintDiagnostic, _source_lines: Vec<&str>) {
self.diagnostics.push(d.clone());
}
@ -310,7 +317,7 @@ impl LintReporter for JsonLintReporter {
});
}
fn close(&mut self) {
fn close(&mut self, _check_count: usize) {
sort_diagnostics(&mut self.diagnostics);
let json = serde_json::to_string_pretty(&self);
eprintln!("{}", json.unwrap());

View file

@ -0,0 +1 @@
Checked 2 files

View file

@ -0,0 +1 @@
Checked 1 file

View file

@ -0,0 +1,2 @@
[WILDCARD]
error: Found 1 not formatted file in [WILDCARD] files

View file

@ -0,0 +1 @@
Checked 2 files

View file

@ -0,0 +1,2 @@
[WILDCARD]
error: Found 1 not formatted file in [WILDCARD] files

View file

@ -0,0 +1,5 @@
function foo() {
return 42;
}
foo();

View file

@ -0,0 +1,5 @@
function bar(): number {
return 42;
}
bar();

View file

@ -1,2 +0,0 @@
[WILDCARD]
error: Found 1 not formatted file

View file

@ -1720,10 +1720,22 @@ itest!(bundle {
itest!(fmt_check_tests_dir {
args: "fmt --check ./",
output: "fmt_check_tests_dir.out",
output: "fmt/expected_fmt_check_tests_dir.out",
exit_code: 1,
});
itest!(fmt_check_formatted_files {
args: "fmt --check fmt/formatted1.js fmt/formatted2.ts",
output: "fmt/expected_fmt_check_formatted_files.out",
exit_code: 0,
});
itest!(fmt_check_ignore {
args: "fmt --check --unstable --ignore=fmt/formatted1.js fmt/",
output: "fmt/expected_fmt_check_ignore.out",
exit_code: 0,
});
itest!(fmt_stdin {
args: "fmt -",
input: Some("const a = 1\n"),

View file

@ -1,2 +1,3 @@
[WILDCARD]
Found 3 problems
Checked 3 files

View file

@ -1,2 +1,3 @@
[WILDCARD]
Found 1 problem
Checked 1 file

View file

@ -1,2 +1,3 @@
[WILDCARD]
Found 3 problems
Checked 3 files

View file

@ -1,2 +1,3 @@
[WILDCARD]
Found 1 problem
Checked 2 files

View file

@ -0,0 +1,3 @@
[WILDCARD]
Found 3 problems
Checked 3 files

View file

@ -52,6 +52,7 @@ For more detail, run `deno lint --help`.
- `no-extra-non-null-assertion`
- `no-extra-semi`
- `no-func-assign`
- `no-inner-declarations`
- `no-inferrable-types`
- `no-invalid-regexp`
- `no-irregular-whitespace`
@ -59,7 +60,7 @@ For more detail, run `deno lint --help`.
- `no-mixed-spaces-and-tabs`
- `no-namespace`
- `no-new-symbol`
- `no-obj-call`
- `no-obj-calls`
- `no-octal`
- `no-prototype-builtins`
- `no-regex-spaces`
@ -68,7 +69,7 @@ For more detail, run `deno lint --help`.
- `no-shadow-restricted-names`
- `no-this-alias`
- `no-this-before-super`
- `no-unexpected-multiline`
- `no-unreachable`
- `no-unsafe-finally`
- `no-unsafe-negation`
- `no-unused-labels`