Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
refactor(core): generalize `OtherThen` to more cases
Elijah Pottera6f8ee8b3b22
diff --git a/harper-core/affixes.json b/harper-core/affixes.jsonindex d182bb1f8..7a6d29a7c 100644--- a/harper-core/affixes.json+++ b/harper-core/affixes.json@@ -144,7 +144,9 @@"condition": "[^ey]"}],- "adds_metadata": {},+ "adds_metadata": {+ "adjective": {}+ },"gifts_metadata": {}},"C": {diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 24c41e896..30c3c6b4d 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -38,7 +38,6 @@ use super::phrase_corrections::EludedTo;use super::phrase_corrections::FaceFirst;use super::phrase_corrections::FastPaste;use super::phrase_corrections::MutePoint;-use super::phrase_corrections::OtherThen;use super::phrase_corrections::StateOfTheArt;use super::phrase_corrections::WantBe;use super::phrase_corrections::{@@ -62,6 +61,7 @@ use super::spell_check::SpellCheck;use super::spelled_numbers::SpelledNumbers;use super::terminating_conjunctions::TerminatingConjunctions;use super::that_which::ThatWhich;+use super::then_than::ThenThan;use super::unclosed_quotes::UnclosedQuotes;use super::use_genitive::UseGenitive;use super::was_aloud::WasAloud;@@ -189,12 +189,12 @@ macro_rules! create_lint_group_config {}create_lint_group_config!(+ ThenThan => true,MutePoint => true,PiqueInterest => true,BareInMind => true,BaitedBreath => true,EludedTo => true,- OtherThen => true,WasAloud => true,HyphenateNumberDay => true,FaceFirst => true,diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex f9269e767..e79b99479 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -49,6 +49,7 @@ mod spelled_numbers;mod suggestion;mod terminating_conjunctions;mod that_which;+mod then_than;mod unclosed_quotes;mod use_genitive;mod was_aloud;@@ -99,7 +100,6 @@ pub use phrase_corrections::EludedTo;pub use phrase_corrections::FaceFirst;pub use phrase_corrections::FastPaste;pub use phrase_corrections::MutePoint;-pub use phrase_corrections::OtherThen;pub use phrase_corrections::StateOfTheArt;pub use phrase_corrections::WantBe;pub use phrase_corrections::{@@ -124,6 +124,7 @@ pub use spelled_numbers::SpelledNumbers;pub use suggestion::Suggestion;pub use terminating_conjunctions::TerminatingConjunctions;pub use that_which::ThatWhich;+pub use then_than::ThenThan;pub use unclosed_quotes::UnclosedQuotes;pub use use_genitive::UseGenitive;pub use was_aloud::WasAloud;diff --git a/harper-core/src/linting/phrase_corrections.rs b/harper-core/src/linting/phrase_corrections.rsindex 933325f73..7b2ad1c4d 100644--- a/harper-core/src/linting/phrase_corrections.rs+++ b/harper-core/src/linting/phrase_corrections.rs@@ -105,14 +105,6 @@ create_linter_map_phrase!("Ensures `face first` is correctly hyphenated as `face-first` when used before `into`.");-create_linter_map_phrase!(- OtherThen,- ExactPhrase::from_phrase("other then"),- "other than",- "Did you mean `other than`?",- "Corrects the misuse of `other then` to `other than`."-);-create_linter_map_phrase!(EludedTo,ExactPhrase::from_phrase("eluded to"),diff --git a/harper-core/src/linting/then_than.rs b/harper-core/src/linting/then_than.rsnew file mode 100644index 000000000..2d7396690--- /dev/null+++ b/harper-core/src/linting/then_than.rs@@ -0,0 +1,111 @@+use super::{Lint, LintKind, PatternLinter};+use crate::linting::Suggestion;+use crate::patterns::{OwnedPatternExt, Pattern, SequencePattern, WordSet};+use crate::Token;++#[doc = "Corrects the misuse of `then` to `than`."]+pub struct ThenThan {+ pattern: Box<dyn Pattern>,+}+impl ThenThan {+ pub fn new() -> Self {+ Self {+ pattern: Box::new(+ SequencePattern::default()+ .then(Box::new(WordSet::all(&["better", "other"]).or(Box::new(+ |tok: &Token, _source: &[char]| {+ tok.kind.is_adjective() && !tok.kind.is_noun()+ },+ ))))+ .then_whitespace()+ .then_any_capitalization_of("then"),+ ),+ }+ }+}++impl Default for ThenThan {+ fn default() -> Self {+ Self::new()+ }+}++impl PatternLinter for ThenThan {+ fn pattern(&self) -> &dyn Pattern {+ self.pattern.as_ref()+ }+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {+ let span = matched_tokens.last().unwrap().span;+ let offending_text = span.get_content(source);++ Lint {+ span,+ lint_kind: LintKind::Miscellaneous,+ suggestions: vec![Suggestion::replace_with_match_case(+ "than".chars().collect(),+ offending_text,+ )],+ message: "Did you mean `than`?".to_string(),+ priority: 31,+ }+ }+ fn description(&self) -> &'static str {+ "Corrects the misuse of `then` to `than`."+ }+}++#[cfg(test)]+mod tests {+ use super::ThenThan;+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};++ #[test]+ fn stronger_should_change() {+ assert_suggestion_result(+ "a chain is no stronger then its weakest link",+ ThenThan::default(),+ "a chain is no stronger than its weakest link",+ );+ }++ #[test]+ fn half_a_loaf_should_change() {+ assert_suggestion_result(+ "half a loaf is better then no bread",+ ThenThan::default(),+ "half a loaf is better than no bread",+ );+ }++ #[test]+ fn then_everyone_clapped_should_be_allowed() {+ assert_lint_count("and then everyone clapped", ThenThan::default(), 0);+ }++ #[test]+ fn crazier_than_rat_should_change() {+ assert_suggestion_result(+ "crazier then a shithouse rat",+ ThenThan::default(),+ "crazier than a shithouse rat",+ );+ }++ #[test]+ fn poke_in_eye_should_change() {+ assert_suggestion_result(+ "better then a poke in the eye with a sharp stick",+ ThenThan::default(),+ "better than a poke in the eye with a sharp stick",+ );+ }++ #[test]+ fn other_then_should_change() {+ assert_suggestion_result(+ "There was no one other then us at the campsite.",+ ThenThan::default(),+ "There was no one other than us at the campsite.",+ );+ }+}