Skip to content

Commit

Permalink
feat(fmt, lint): show number of checked files (denoland#7312)
Browse files Browse the repository at this point in the history
  • Loading branch information
magurotuna authored Sep 9, 2020
1 parent 1fcbf9c commit 857f9b3
Show file tree
Hide file tree
Showing 18 changed files with 79 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
24 changes: 21 additions & 3 deletions cli/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
)))
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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(())
}

Expand Down
27 changes: 17 additions & 10 deletions cli/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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) => {
Expand All @@ -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);
Expand Down Expand Up @@ -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) => {
Expand All @@ -177,7 +178,7 @@ fn lint_stdin(json: bool) -> Result<(), ErrBox> {
}
}

reporter.close();
reporter.close(1);

if has_error {
std::process::exit(1);
Expand All @@ -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)]
Expand All @@ -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 =
Expand All @@ -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),
_ => (),
}
}
}

Expand Down Expand Up @@ -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());
}

Expand All @@ -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());
Expand Down
1 change: 1 addition & 0 deletions cli/tests/fmt/expected_fmt_check_formatted_files.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Checked 2 files
1 change: 1 addition & 0 deletions cli/tests/fmt/expected_fmt_check_ignore.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Checked 1 file
2 changes: 2 additions & 0 deletions cli/tests/fmt/expected_fmt_check_tests_dir.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[WILDCARD]
error: Found 1 not formatted file in [WILDCARD] files
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Checked 2 files
2 changes: 2 additions & 0 deletions cli/tests/fmt/expected_fmt_check_verbose_tests_dir.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[WILDCARD]
error: Found 1 not formatted file in [WILDCARD] files
5 changes: 5 additions & 0 deletions cli/tests/fmt/formatted1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function foo() {
return 42;
}

foo();
5 changes: 5 additions & 0 deletions cli/tests/fmt/formatted2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function bar(): number {
return 42;
}

bar();
2 changes: 0 additions & 2 deletions cli/tests/fmt_check_tests_dir.out

This file was deleted.

14 changes: 13 additions & 1 deletion cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
1 change: 1 addition & 0 deletions cli/tests/lint/expected.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[WILDCARD]
Found 3 problems
Checked 3 files
1 change: 1 addition & 0 deletions cli/tests/lint/expected_from_stdin.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[WILDCARD]
Found 1 problem
Checked 1 file
1 change: 1 addition & 0 deletions cli/tests/lint/expected_glob.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[WILDCARD]
Found 3 problems
Checked 3 files
1 change: 1 addition & 0 deletions cli/tests/lint/expected_ignore.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[WILDCARD]
Found 1 problem
Checked 2 files
3 changes: 3 additions & 0 deletions cli/tests/lint/expected_verbose.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[WILDCARD]
Found 3 problems
Checked 3 files
5 changes: 3 additions & 2 deletions docs/tools/linter.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ 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`
- `no-misused-new`
- `no-mixed-spaces-and-tabs`
- `no-namespace`
- `no-new-symbol`
- `no-obj-call`
- `no-obj-calls`
- `no-octal`
- `no-prototype-builtins`
- `no-regex-spaces`
Expand All @@ -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`
Expand Down

0 comments on commit 857f9b3

Please sign in to comment.