Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: go so far to→go so far as to (#2188)
Andrew Dunbar3f26fdfa8b82
diff --git a/harper-core/src/linting/go_so_far_as_to.rs b/harper-core/src/linting/go_so_far_as_to.rsnew file mode 100644index 000000000..2da11708a--- /dev/null+++ b/harper-core/src/linting/go_so_far_as_to.rs@@ -0,0 +1,151 @@+use crate::Token;+use crate::TokenStringExt;+use crate::expr::{Expr, SequenceExpr};+use crate::linting::Suggestion;+use crate::linting::{ExprLinter, Lint, LintKind};++pub struct GoSoFarAsTo {+ exp: Box<dyn Expr>,+}++impl Default for GoSoFarAsTo {+ fn default() -> Self {+ Self {+ exp: Box::new(+ SequenceExpr::word_set(&["go", "goes", "going", "gone", "went"])+ .then_fixed_phrase(" so far to ")+ .then_optional(SequenceExpr::default().then_adverb().t_ws())+ .then_any_word(),+ ),+ }+ }+}++impl ExprLinter for GoSoFarAsTo {+ fn expr(&self) -> &dyn Expr {+ self.exp.as_ref()+ }++ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {+ let toks_len = toks.len();++ if toks_len != 9 && toks_len != 11 {+ return None;+ }++ let last = toks.last().unwrap();+ let penult = &toks[toks_len - 3];++ match (toks_len, penult.kind.is_adverb(), last.kind.is_verb_lemma()) {+ (11, true, true) => (),+ (9, _, true) => (),+ _ => return None,+ }++ let go_so_far_to_toks = &toks[0..=6];+ let go_so_far_to_span = go_so_far_to_toks.span()?;+ let go_so_far_toks = &toks[0..=4];+ let to_tok = &toks[6];++ let sugg = Suggestion::replace_with_match_case(+ format!(+ "{} as {}",+ go_so_far_toks.span()?.get_content_string(src),+ to_tok.span.get_content_string(src)+ )+ .chars()+ .collect(),+ go_so_far_to_span.get_content(src),+ );++ Some(Lint {+ span: go_so_far_to_span,+ lint_kind: LintKind::Nonstandard,+ suggestions: vec![sugg],+ message: "If this is intended to express going beyond what's expected, the standard idiom is `go so far as to`".to_string(),+ ..Default::default()+ })+ }++ fn description(&self) -> &'static str {+ "Flags 'go so far to' when it should be 'go so far as to' to express going beyond expectations"+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::{+ GoSoFarAsTo,+ tests::{assert_no_lints, assert_suggestion_result},+ };++ #[test]+ fn go_so_far_to() {+ assert_suggestion_result(+ "I'd even go so far to say as it's a good way to get started with getting things onto ...",+ GoSoFarAsTo::default(),+ "I'd even go so far as to say as it's a good way to get started with getting things onto ...",+ );+ }++ #[test]+ fn goes_so_far_to() {+ assert_suggestion_result(+ "I believe Java goes so far to even throw a runtime exception.",+ GoSoFarAsTo::default(),+ "I believe Java goes so far as to even throw a runtime exception.",+ );+ }++ #[test]+ fn gone_so_far_to() {+ assert_suggestion_result(+ "I've gone so far to reinstall Mac OS, which got the runner to finally start",+ GoSoFarAsTo::default(),+ "I've gone so far as to reinstall Mac OS, which got the runner to finally start",+ );+ }++ #[test]+ fn went_so_far_to() {+ assert_suggestion_result(+ "I've read these posts but only went so far to conclude that I need to potentially add sql statements into the blocks",+ GoSoFarAsTo::default(),+ "I've read these posts but only went so far as to conclude that I need to potentially add sql statements into the blocks",+ );+ }++ #[test]+ fn went_so_far_to_adverb() {+ assert_suggestion_result(+ "I even went so far to manually replace the .AppImage with a different file in the Applications folder",+ GoSoFarAsTo::default(),+ "I even went so far as to manually replace the .AppImage with a different file in the Applications folder",+ );+ }++ #[test]+ #[ignore = "A false positive we can't detect due to the next word being a verb lemma"]+ fn dont_flag_going_so_far_to() {+ assert_no_lints(+ "Why dictate that the system must be canonically described through a textual syntax – especially after going so far to make that unnecessary?",+ GoSoFarAsTo::default(),+ );+ }++ #[test]+ fn dont_flag_goes_so_far_to() {+ assert_no_lints(+ "... even so much that one line goes so far to the right",+ GoSoFarAsTo::default(),+ );+ }++ #[test]+ fn dont_flag_go_so_far_to() {+ assert_no_lints(+ "Unfortunetly, our logs don't go so far to that time, but I found something interesting.",+ GoSoFarAsTo::default(),+ );+ }+}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 8fc54b4f9..fd668d677 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -62,6 +62,7 @@ use super::first_aid_kit::FirstAidKit;use super::for_noun::ForNoun;use super::free_predicate::FreePredicate;use super::friend_of_me::FriendOfMe;+use super::go_so_far_as_to::GoSoFarAsTo;use super::have_pronoun::HavePronoun;use super::have_take_a_look::HaveTakeALook;use super::hedging::Hedging;@@ -505,6 +506,7 @@ impl LintGroup {insert_struct_rule!(ForNoun, true);insert_expr_rule!(FreePredicate, true);insert_expr_rule!(FriendOfMe, true);+ insert_expr_rule!(GoSoFarAsTo, true);insert_expr_rule!(HavePronoun, true);insert_expr_rule!(Hedging, true);insert_expr_rule!(HelloGreeting, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex d01d57a64..5b6aa34b8 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -58,6 +58,7 @@ mod first_aid_kit;mod for_noun;mod free_predicate;mod friend_of_me;+mod go_so_far_as_to;mod have_pronoun;mod have_take_a_look;mod hedging;@@ -239,6 +240,7 @@ pub use filler_words::FillerWords;pub use for_noun::ForNoun;pub use free_predicate::FreePredicate;pub use friend_of_me::FriendOfMe;+pub use go_so_far_as_to::GoSoFarAsTo;pub use have_pronoun::HavePronoun;pub use have_take_a_look::HaveTakeALook;pub use hedging::Hedging;