From 4c91f16ae119572357c4b03b2417f81c6f30dab5 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 25 Sep 2023 12:11:43 -0500 Subject: [PATCH 1/6] fix(dict)!: Dont expose internals --- crates/typos-dict/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/typos-dict/src/lib.rs b/crates/typos-dict/src/lib.rs index 4619424..9f5d62a 100644 --- a/crates/typos-dict/src/lib.rs +++ b/crates/typos-dict/src/lib.rs @@ -1,3 +1,3 @@ mod dict_codegen; -pub use crate::dict_codegen::*; +pub use crate::dict_codegen::WORD_TRIE; From b798780a13151708093248498af3f2fcb56a4ac2 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 25 Sep 2023 12:13:53 -0500 Subject: [PATCH 2/6] refactor(dict): Make codegen more specific --- crates/typos-dict/src/lib.rs | 4 ++-- .../src/{dict_codegen.rs => word_codegen.rs} | 0 crates/typos-dict/tests/codegen.rs | 13 ++++++------- 3 files changed, 8 insertions(+), 9 deletions(-) rename crates/typos-dict/src/{dict_codegen.rs => word_codegen.rs} (100%) diff --git a/crates/typos-dict/src/lib.rs b/crates/typos-dict/src/lib.rs index 9f5d62a..808bec0 100644 --- a/crates/typos-dict/src/lib.rs +++ b/crates/typos-dict/src/lib.rs @@ -1,3 +1,3 @@ -mod dict_codegen; +mod word_codegen; -pub use crate::dict_codegen::WORD_TRIE; +pub use crate::word_codegen::WORD_TRIE; diff --git a/crates/typos-dict/src/dict_codegen.rs b/crates/typos-dict/src/word_codegen.rs similarity index 100% rename from crates/typos-dict/src/dict_codegen.rs rename to crates/typos-dict/src/word_codegen.rs diff --git a/crates/typos-dict/tests/codegen.rs b/crates/typos-dict/tests/codegen.rs index e7c393a..fb6a317 100644 --- a/crates/typos-dict/tests/codegen.rs +++ b/crates/typos-dict/tests/codegen.rs @@ -1,16 +1,15 @@ -const DICT: &[u8] = include_bytes!("../assets/words.csv"); - #[test] fn codegen() { let mut content = vec![]; - generate(&mut content); + const DICT: &[u8] = include_bytes!("../assets/words.csv"); + generate(&mut content, "WORD", DICT); let content = String::from_utf8(content).unwrap(); let content = codegenrs::rustfmt(&content, None).unwrap(); - snapbox::assert_eq_path("./src/dict_codegen.rs", content); + snapbox::assert_eq_path("./src/word_codegen.rs", content); } -fn generate(file: &mut W) { +fn generate(file: &mut W, prefix: &str, dict: &[u8]) { writeln!( file, "// This file is @generated by {}", @@ -23,13 +22,13 @@ fn generate(file: &mut W) { let records: Vec<_> = csv::ReaderBuilder::new() .has_headers(false) .flexible(true) - .from_reader(DICT) + .from_reader(dict) .records() .map(|r| r.unwrap()) .collect(); dictgen::generate_trie( file, - "WORD", + prefix, "&'static [&'static str]", records.iter().map(|record| { let mut record_fields = record.iter(); From fd6873b6e0d1dcca6e8fd739a9dc7844e7963e5d Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 25 Sep 2023 12:40:17 -0500 Subject: [PATCH 3/6] refactor(cli): Make room for ident correction --- crates/typos-cli/src/dict.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/crates/typos-cli/src/dict.rs b/crates/typos-cli/src/dict.rs index f69abe0..8a2c0fe 100644 --- a/crates/typos-cli/src/dict.rs +++ b/crates/typos-cli/src/dict.rs @@ -32,7 +32,7 @@ impl BuiltIn { let word = word_token.token(); let word_case = unicase::UniCase::new(word); - let mut corrections = if let Some(corrections) = self.correct_with_dict(word_case) { + let mut corrections = if let Some(corrections) = self.correct_word_with_dict(word_case) { if corrections.is_empty() { Status::Invalid } else { @@ -51,14 +51,20 @@ impl BuiltIn { #[cfg(feature = "dict")] impl BuiltIn { // Not using `Status` to avoid the allocations - fn correct_with_dict(&self, word: unicase::UniCase<&str>) -> Option<&'static [&'static str]> { + fn correct_word_with_dict( + &self, + word: unicase::UniCase<&str>, + ) -> Option<&'static [&'static str]> { typos_dict::WORD_TRIE.find(&word).copied() } } #[cfg(not(feature = "dict"))] impl BuiltIn { - fn correct_with_dict(&self, _word: unicase::UniCase<&str>) -> Option<&'static [&'static str]> { + fn correct_word_with_dict( + &self, + _word: unicase::UniCase<&str>, + ) -> Option<&'static [&'static str]> { None } } From 66a70abc406e524294ab5a93b5fc5e40ca93cabb Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 25 Sep 2023 12:42:18 -0500 Subject: [PATCH 4/6] refactor(cli): Make room for ident dict --- crates/typos-cli/src/dict.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/typos-cli/src/dict.rs b/crates/typos-cli/src/dict.rs index 8a2c0fe..33ea70d 100644 --- a/crates/typos-cli/src/dict.rs +++ b/crates/typos-cli/src/dict.rs @@ -20,9 +20,10 @@ impl BuiltIn { pub fn correct_ident<'s>( &'s self, - _ident: typos::tokens::Identifier<'_>, + ident_token: typos::tokens::Identifier<'_>, ) -> Option> { - None + let ident = ident_token.token(); + self.correct_ident_with_dict(ident) } pub fn correct_word<'s>(&'s self, word_token: typos::tokens::Word<'_>) -> Option> { @@ -50,6 +51,10 @@ impl BuiltIn { #[cfg(feature = "dict")] impl BuiltIn { + fn correct_ident_with_dict<'s>(&self, _ident: &str) -> Option> { + None + } + // Not using `Status` to avoid the allocations fn correct_word_with_dict( &self, @@ -61,6 +66,10 @@ impl BuiltIn { #[cfg(not(feature = "dict"))] impl BuiltIn { + fn correct_ident_with_dict<'s>(&self, _ident: &str) -> Option> { + None + } + fn correct_word_with_dict( &self, _word: unicase::UniCase<&str>, From 9f1d7017e2af7c126c9cc99feabdac5b182d1fe8 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 25 Sep 2023 12:46:00 -0500 Subject: [PATCH 5/6] test(cli): Show existing O_WRONLY behavior --- .../typos-cli/tests/cmd/false-positives.in/sample.py | 4 ++++ crates/typos-cli/tests/cmd/false-positives.toml | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/typos-cli/tests/cmd/false-positives.in/sample.py b/crates/typos-cli/tests/cmd/false-positives.in/sample.py index dcf674d..692ff28 100644 --- a/crates/typos-cli/tests/cmd/false-positives.in/sample.py +++ b/crates/typos-cli/tests/cmd/false-positives.in/sample.py @@ -1 +1,5 @@ +import os + from numpy.typing import NDArray # should work + +print(os.O_WRONLY) # should work diff --git a/crates/typos-cli/tests/cmd/false-positives.toml b/crates/typos-cli/tests/cmd/false-positives.toml index 324f41d..a582f88 100644 --- a/crates/typos-cli/tests/cmd/false-positives.toml +++ b/crates/typos-cli/tests/cmd/false-positives.toml @@ -1,4 +1,12 @@ bin.name = "typos" stdin = "" -stdout = "" +stdout = """ +error: `WRONLY` should be `WRONGLY` + --> ./sample.py:5:12 + | +5 | print(os.O_WRONLY) # should work + | ^^^^^^ + | +""" stderr = "" +status.code = 2 From 5fd0df28816209a5c3f67eefb1920b77d6ab5951 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 25 Sep 2023 12:50:07 -0500 Subject: [PATCH 6/6] fix(cli): Dont correct O_WRONLY Realized we could just hard code this for now rather creating a general identifier dictionary. Fixes #744 --- crates/typos-cli/src/dict.rs | 7 +++++-- crates/typos-cli/tests/cmd/false-positives.toml | 10 +--------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/crates/typos-cli/src/dict.rs b/crates/typos-cli/src/dict.rs index 33ea70d..7f09952 100644 --- a/crates/typos-cli/src/dict.rs +++ b/crates/typos-cli/src/dict.rs @@ -51,8 +51,11 @@ impl BuiltIn { #[cfg(feature = "dict")] impl BuiltIn { - fn correct_ident_with_dict<'s>(&self, _ident: &str) -> Option> { - None + fn correct_ident_with_dict<'s>(&self, ident: &str) -> Option> { + match ident { + "O_WRONLY" => Some(Status::Valid), + _ => None, + } } // Not using `Status` to avoid the allocations diff --git a/crates/typos-cli/tests/cmd/false-positives.toml b/crates/typos-cli/tests/cmd/false-positives.toml index a582f88..324f41d 100644 --- a/crates/typos-cli/tests/cmd/false-positives.toml +++ b/crates/typos-cli/tests/cmd/false-positives.toml @@ -1,12 +1,4 @@ bin.name = "typos" stdin = "" -stdout = """ -error: `WRONLY` should be `WRONGLY` - --> ./sample.py:5:12 - | -5 | print(os.O_WRONLY) # should work - | ^^^^^^ - | -""" +stdout = "" stderr = "" -status.code = 2