Skip to content

Commit

Permalink
Merge pull request #77 from huacnlee/feat/spellcheck-as-warning
Browse files Browse the repository at this point in the history
Spellcheck as warning
  • Loading branch information
huacnlee authored Sep 28, 2022
2 parents 702bb64 + 2bb3921 commit a0e36bb
Show file tree
Hide file tree
Showing 24 changed files with 666 additions and 392 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ LAST_TAG_VERSION=$(shell git describe --abbrev=0 --tags | sed "s/^v//")

bench:
cargo bench
run1:
cargo run -- --lint --config /Users/jason/work/translated-content/.autocorrectrc /Users/jason/work/translated-content/files/zh-cn/webassembly
run:
cargo run -- --lint --config $(WORKDIR)/.autocorrectrc.template
run\:json:
Expand Down
2 changes: 1 addition & 1 deletion autocorrect-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "MIT"
name = "autocorrect-cli"
readme = "../README.md"
repository = "https://github.com/huacnlee/autocorrect"
version = "1.10.9"
version = "1.11.0"

[[bin]]
name = "autocorrect"
Expand Down
50 changes: 43 additions & 7 deletions autocorrect-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod progress;
mod update;

use cli::Cli;
use colored::*;
use logger::Logger;
use logger::SystemTimeDuration;
use threadpool::ThreadPool;
Expand Down Expand Up @@ -75,6 +76,9 @@ pub fn main() {
let mut lint_results: Vec<String> = Vec::new();
let (tx, rx) = std::sync::mpsc::channel();

let lint_errors_count = std::sync::Arc::new(std::sync::Mutex::new(0));
let lint_warnings_count = std::sync::Arc::new(std::sync::Mutex::new(0));

let pool = ThreadPool::new(cli.threads);
// let mut threads = Vec::new();

Expand Down Expand Up @@ -124,6 +128,8 @@ pub fn main() {

let cli = cli.clone();
let tx = tx.clone();
let lint_errors_count = lint_errors_count.clone();
let lint_warnings_count = lint_warnings_count.clone();
let filepath = filepath.clone();
let filetype = filetype.clone();

Expand All @@ -133,7 +139,21 @@ pub fn main() {
log::debug!("Process {}", filepath);
if cli.lint {
let mut lint_results: Vec<String> = Vec::new();
lint_and_output(&filepath, &filetype, &raw, &cli, &mut lint_results);

let mut _err_count = 0;
let mut _warn_count = 0;
lint_and_output(
&filepath,
&filetype,
&raw,
&cli,
&mut lint_results,
&mut _err_count,
&mut _warn_count,
);

*lint_errors_count.lock().unwrap() += _err_count;
*lint_warnings_count.lock().unwrap() += _warn_count;

for lint_result in lint_results {
tx.send(lint_result).unwrap();
Expand Down Expand Up @@ -172,22 +192,29 @@ pub fn main() {
} else {
log::info!("\n");

let _err_count = *lint_errors_count.lock().unwrap();
let _warn_count = *lint_warnings_count.lock().unwrap();

log::info!(
"{}, {}\n",
format!("Error: {}", _err_count).red(),
format!("Warning: {}", _warn_count).yellow(),
);

if !lint_results.is_empty() {
// diff will use stderr output
log::error!("{}", lint_results.join("\n"));
log::info!("{}", lint_results.join("\n"));
}

// print time spend from start_t to now
log::info!("AutoCorrect spend time {}ms\n", start_t.elapsed_millis());

if !lint_results.is_empty() {
if _err_count > 0 {
// Exit with code = 1
std::process::exit(1);
}
}
} else if cli.fix {
log::info!("Done.\n");

// print time spend from start_t to now
log::info!("AutoCorrect spend time: {}ms\n", start_t.elapsed_millis());
}
Expand All @@ -197,7 +224,7 @@ fn read_file(filepath: &str) -> io::Result<String> {
let t = SystemTime::now();
log::debug!("Loading {} ...", filepath);

let out = fs::read_to_string(&filepath);
let out = fs::read_to_string(filepath);

log::debug!("Loaded {} {}ms", filepath, t.elapsed_millis());

Expand Down Expand Up @@ -240,18 +267,27 @@ fn lint_and_output(
raw: &str,
cli: &Cli,
results: &mut Vec<String>,
errors_count: &mut usize,
warings_count: &mut usize,
) {
let diff_mode = cli.formatter != "json";
let mut result = autocorrect::lint_for(raw, filetype);
result.filepath = String::from(filepath);

*errors_count += result.errors_count();
*warings_count += result.warnings_count();

// do not print anything, when not lint results
if !cli.debug {
if result.lines.is_empty() {
progress::ok(diff_mode);
return;
} else {
}

if *errors_count > 0 {
progress::err(diff_mode);
} else if *warings_count > 0 {
progress::warn(diff_mode);
}
}

Expand Down
23 changes: 15 additions & 8 deletions autocorrect-cli/src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@ use colored::*;
use std::io::{self, Write};

pub fn ok(show: bool) {
if show {
print!("{}", ".".green());
io::stdout().flush().unwrap();
}
if show {
print!("{}", ".".green());
io::stdout().flush().unwrap();
}
}

pub fn warn(show: bool) {
if show {
print!("{}", ".".yellow());
io::stdout().flush().unwrap();
}
}

pub fn err(show: bool) {
if show {
print!("{}", ".".red());
io::stdout().flush().unwrap();
}
if show {
print!("{}", ".".red());
io::stdout().flush().unwrap();
}
}
2 changes: 1 addition & 1 deletion autocorrect-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ keywords = ["autocorrect", "lint", "format"]
license = "MIT"
name = "autocorrect-derive"
readme = "../README.md"
version = "0.1.0"
version = "0.2.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
4 changes: 2 additions & 2 deletions autocorrect-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ pub fn derive_grammar_parser(input: TokenStream) -> TokenStream {
let code = format!(
r##"
#[allow(dead_code)]
pub fn format_{name}(text: &str) -> code::FormatResult {{
pub fn format_{name}(text: &str) -> FormatResult {{
let pairs = {struct_name}::parse(Rule::item, text);
let text = code::FormatResult::new(text);
code::format_pairs(text, pairs)
}}
#[allow(dead_code)]
pub fn lint_{name}(text: &str) -> code::LintResult {{
pub fn lint_{name}(text: &str) -> LintResult {{
let pairs = {struct_name}::parse(Rule::item, text);
let text = code::LintResult::new(text);
code::format_pairs(text, pairs)
Expand Down
2 changes: 1 addition & 1 deletion autocorrect-wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
edition = "2021"
name = "autocorrect-wasm"
version = "1.10.9"
version = "1.11.0"

[lib]
crate-type = ["cdylib", "rlib"]
Expand Down
8 changes: 5 additions & 3 deletions autocorrect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ license = "MIT"
name = "autocorrect"
readme = "../README.md"
repository = "https://github.com/huacnlee/autocorrect"
version = "1.10.9"
version = "1.11.0"

[lib]
name = "autocorrect"
path = "src/lib.rs"

[dependencies]
autocorrect-derive = {path = "../autocorrect-derive", version = ">=0.1.0"}
difference = "2.0"
autocorrect-derive = {path = "../autocorrect-derive", version = ">=0.2.0"}
colored = "2"
diff = "0.1.13"
ignore = "0.4"
lazy_static = "1.4.0"
pest = "2.3.1"
pest_derive = "2.3.1"
regex = "1"
serde = {version = "1", features = ["derive"]}
serde_json = "1.0.83"
serde_repr = "0.1"
serde_yaml = "0.9.9"

[dev-dependencies]
Expand Down
Loading

0 comments on commit a0e36bb

Please sign in to comment.