Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): add to and tweak rule set (#1462)
Elijah Pottercc9a036b151e
diff --git a/harper-core/src/linting/have_pronoun.rs b/harper-core/src/linting/have_pronoun.rsnew file mode 100644index 000000000..d77068199--- /dev/null+++ b/harper-core/src/linting/have_pronoun.rs@@ -0,0 +1,109 @@+use crate::Token;+use crate::expr::{AnchorStart, Expr, SequenceExpr};++use super::{ExprLinter, Lint, LintKind, Suggestion};++pub struct HavePronoun {+ expr: Box<dyn Expr>,+}++impl Default for HavePronoun {+ fn default() -> Self {+ let expr = SequenceExpr::default()+ .then(AnchorStart)+ .t_aco("has")+ .t_ws()+ .then(|tok: &Token, _: &[char]| {+ tok.kind.is_first_person_singular_pronoun() || tok.kind.is_plural_pronoun()+ });++ Self {+ expr: Box::new(expr),+ }+ }+}++impl ExprLinter for HavePronoun {+ fn expr(&self) -> &dyn Expr {+ self.expr.as_ref()+ }++ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {+ // First real word in the match is always "has".+ let has_tok = toks.iter().find(|t| t.kind.is_word())?;+ let span = has_tok.span;+ let original = span.get_content(src);++ Some(Lint {+ span,+ lint_kind: LintKind::Miscellaneous,+ suggestions: vec![Suggestion::replace_with_match_case(+ "have".chars().collect(),+ original,+ )],+ message: "Use `have` with first-person singular or plural pronouns.".to_owned(),+ priority: 31,+ })+ }++ fn description(&self) -> &'static str {+ "Flags questions that begin with `has` followed by a pronoun that requires `have`, \+ such as `Has we …` or `Has I …`, and suggests the correct auxiliary."+ }+}++#[cfg(test)]+mod tests {+ use super::HavePronoun;+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};++ #[test]+ fn corrects_has_we() {+ assert_suggestion_result(+ "Has we finished the report?",+ HavePronoun::default(),+ "Have we finished the report?",+ );+ }++ #[test]+ fn corrects_has_you() {+ assert_suggestion_result(+ "Has you misunderstood?",+ HavePronoun::default(),+ "Have you misunderstood?",+ );+ }++ #[test]+ fn corrects_has_i() {+ assert_suggestion_result(+ "Has I misunderstood?",+ HavePronoun::default(),+ "Have I misunderstood?",+ );+ }++ #[test]+ fn corrects_has_they() {+ assert_suggestion_result(+ "Has they arrived yet?",+ HavePronoun::default(),+ "Have they arrived yet?",+ );+ }++ #[test]+ fn allows_has_he() {+ assert_lint_count("Has he arrived yet?", HavePronoun::default(), 0);+ }++ #[test]+ fn ignores_non_initial_usage() {+ assert_lint_count(+ "The system has we confused for a moment.",+ HavePronoun::default(),+ 0,+ );+ }+}diff --git a/harper-core/src/linting/its_contraction.rs b/harper-core/src/linting/its_contraction.rsindex 81bfbdfbc..173c82c54 100644--- a/harper-core/src/linting/its_contraction.rs+++ b/harper-core/src/linting/its_contraction.rs@@ -2,6 +2,7 @@ use harper_brill::UPOS;use crate::expr::All;use crate::expr::Expr;+use crate::expr::OwnedExprExt;use crate::expr::SequenceExpr;use crate::patterns::NominalPhrase;use crate::patterns::Pattern;@@ -30,7 +31,12 @@ impl Default for ItsContraction {let inverted = SequenceExpr::default().if_not_then_step_one(exceptions);- let expr = All::new(vec![Box::new(positive), Box::new(inverted)]);+ let expr =+ All::new(vec![Box::new(positive), Box::new(inverted)]).or(SequenceExpr::aco("its")+ .t_ws()+ .then_adjective()+ .t_ws()+ .t_aco("for"));Self {expr: Box::new(expr),@@ -103,6 +109,15 @@ mod tests {);}+ #[test]+ fn fixes_its_common() {+ assert_suggestion_result(+ "Its common for users to get frustrated.",+ ItsContraction::default(),+ "It's common for users to get frustrated.",+ );+ }+#[test]fn ignore_correct_contraction() {assert_lint_count(diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 143bb84a4..2d4c41b39 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -38,6 +38,7 @@ use super::expr_linter::run_on_chunk;use super::few_units_of_time_ago::FewUnitsOfTimeAgo;use super::first_aid_kit::FirstAidKit;use super::for_noun::ForNoun;+use super::have_pronoun::HavePronoun;use super::hedging::Hedging;use super::hereby::Hereby;use super::hop_hope::HopHope;@@ -89,6 +90,7 @@ use super::touristic::Touristic;use super::unclosed_quotes::UnclosedQuotes;use super::use_genitive::UseGenitive;use super::was_aloud::WasAloud;+use super::way_too_adjective::WayTooAdjective;use super::whereas::Whereas;use super::widely_accepted::WidelyAccepted;use super::win_prize::WinPrize;@@ -376,6 +378,8 @@ impl LintGroup {insert_pattern_rule!(BoringWords, false);insert_struct_rule!(CapitalizePersonalPronouns, true);insert_pattern_rule!(ChockFull, true);+ insert_pattern_rule!(WayTooAdjective, true);+ insert_pattern_rule!(HavePronoun, true);insert_pattern_rule!(PronounInflectionBe, true);insert_struct_rule!(CommaFixes, true);insert_struct_rule!(CompoundNouns, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 7247e377f..5f4ed1b32 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -32,6 +32,7 @@ mod expr_linter;mod few_units_of_time_ago;mod first_aid_kit;mod for_noun;+mod have_pronoun;mod hedging;mod hereby;mod hop_hope;@@ -98,6 +99,7 @@ mod touristic;mod unclosed_quotes;mod use_genitive;mod was_aloud;+mod way_too_adjective;mod whereas;mod widely_accepted;mod win_prize;@@ -130,6 +132,7 @@ pub use expand_time_shorthands::ExpandTimeShorthands;pub use expr_linter::ExprLinter;pub use few_units_of_time_ago::FewUnitsOfTimeAgo;pub use for_noun::ForNoun;+pub use have_pronoun::HavePronoun;pub use hedging::Hedging;pub use hereby::Hereby;pub use hop_hope::HopHope;@@ -187,6 +190,7 @@ pub use touristic::Touristic;pub use unclosed_quotes::UnclosedQuotes;pub use use_genitive::UseGenitive;pub use was_aloud::WasAloud;+pub use way_too_adjective::WayTooAdjective;pub use whereas::Whereas;pub use widely_accepted::WidelyAccepted;pub use win_prize::WinPrize;diff --git a/harper-core/src/linting/phrase_corrections/mod.rs b/harper-core/src/linting/phrase_corrections/mod.rsindex 4e391facd..5b00a4200 100644--- a/harper-core/src/linting/phrase_corrections/mod.rs+++ b/harper-core/src/linting/phrase_corrections/mod.rs@@ -1347,6 +1347,17 @@ pub fn lint_group() -> LintGroup {"Use `worst` for the extreme case. (`Worse` is for comparing)","Corrects `worse ever` to `worst ever` for proper comparative usage."),+ "BuiltIn" => (+ ["in built", "in-built", "built in"],+ ["built-in"],+ "Prefer the hyphenated compound `built-in`.",+ "English convention treats `built-in` as a single, attributive adjective—meaning something integrated from the outset—whereas other forms like `in built` are non-standard and can feel awkward to readers."+ ),+ "ThanksALot" => (+ ["thanks lot", "thanks alot"],+ ["thanks a lot"],+ "Prefer the two-word expression `thanks a lot`.",+ "`Thanks a lot` is the fixed, widely accepted form, while variants like `thanks lot` or `thanks alot` are non-standard and can jar readers.")});group.set_all_rules_to(Some(true));diff --git a/harper-core/src/linting/phrase_corrections/tests.rs b/harper-core/src/linting/phrase_corrections/tests.rsindex f0b47bd15..7c67ea148 100644--- a/harper-core/src/linting/phrase_corrections/tests.rs+++ b/harper-core/src/linting/phrase_corrections/tests.rs@@ -2038,3 +2038,19 @@ fn detect_worst_ever_real_world() {fn now_on_hold() {assert_lint_count("Those are now on hold for month.", lint_group(), 0);}++// ThanksALot+#[test]+fn thanks_lot() {+ assert_suggestion_result("thanks lot", lint_group(), "thanks a lot");+}++#[test]+fn thanks_alot() {+ assert_suggestion_result("thanks alot", lint_group(), "thanks a lot");+}++#[test]+fn thanks_a_lot_clean() {+ assert_lint_count("thanks a lot", lint_group(), 0);+}diff --git a/harper-core/src/linting/way_too_adjective.rs b/harper-core/src/linting/way_too_adjective.rsnew file mode 100644index 000000000..5a9f14f0c--- /dev/null+++ b/harper-core/src/linting/way_too_adjective.rs@@ -0,0 +1,108 @@+use harper_brill::UPOS;++use crate::Token;+use crate::expr::{All, Expr, OwnedExprExt, SequenceExpr};+use crate::patterns::{UPOSSet, WordSet};++use super::{ExprLinter, Lint, LintKind, Suggestion};++pub struct WayTooAdjective {+ expr: Box<dyn Expr>,+}++impl Default for WayTooAdjective {+ fn default() -> Self {+ let base = SequenceExpr::default()+ .t_aco("way")+ .t_ws()+ .t_aco("to")+ .t_ws()+ .then(UPOSSet::new(&[UPOS::ADJ]).or(WordSet::new(&["much"])));++ let exceptions = SequenceExpr::default()+ .t_any()+ .t_any()+ .t_any()+ .t_any()+ .then(WordSet::new(&["surface", "return", "aqua"]));++ let expr = All::new(vec![+ Box::new(base),+ Box::new(SequenceExpr::default().if_not_then_step_one(exceptions)),+ ]);++ Self {+ expr: Box::new(expr),+ }+ }+}++impl ExprLinter for WayTooAdjective {+ fn expr(&self) -> &dyn Expr {+ self.expr.as_ref()+ }++ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {+ let to_tok = toks.get(2)?;+ let span = to_tok.span;+ let original = span.get_content(src);++ Some(Lint {+ span,+ lint_kind: LintKind::Miscellaneous,+ suggestions: vec![Suggestion::replace_with_match_case(+ "too".chars().collect(),+ original,+ )],+ message: "Did you mean “too”?".into(),+ priority: 25,+ })+ }++ fn description(&self) -> &'static str {+ "Replaces the preposition `to` with the adverb `too` after `way` when followed by an \+ adjective (e.g. `way too fast`)"+ }+}++#[cfg(test)]+mod tests {+ use super::WayTooAdjective;+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};++ #[test]+ fn corrects_way_to_fast() {+ assert_suggestion_result(+ "You drive way to fast.",+ WayTooAdjective::default(),+ "You drive way too fast.",+ );+ }++ #[test]+ fn corrects_way_to_complicated() {+ assert_suggestion_result(+ "I think this would be way to complicated to implement.",+ WayTooAdjective::default(),+ "I think this would be way too complicated to implement.",+ );+ }++ #[test]+ fn corrects_way_to_much() {+ assert_suggestion_result(+ "…and ate way to much.",+ WayTooAdjective::default(),+ "…and ate way too much.",+ );+ }++ #[test]+ fn allows_fast_way_to_test() {+ assert_lint_count(+ "Fast way to test daily defence teams?",+ WayTooAdjective::default(),+ 0,+ );+ }+}