Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat: implement #993

hippietrail75e65f51ab7b
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rs
index 8ec8916f5..2c3ced3d0 100644
--- a/harper-core/src/document.rs
+++ b/harper-core/src/document.rs
@@ -581,6 +581,8 @@ impl TokenStringExt for Document {
create_fns_on_doc!(likely_homograph);
create_fns_on_doc!(comma);
create_fns_on_doc!(adjective);
+ create_fns_on_doc!(verb);
+ create_fns_on_doc!(preposition);
fn first_sentence_word(&self) -> Option<&Token> {
self.tokens.first_sentence_word()
diff --git a/harper-core/src/linting/inflected_verb_after_to.rs b/harper-core/src/linting/inflected_verb_after_to.rs
new file mode 100644
index 000000000..1c9d14b1b
--- /dev/null
+++ b/harper-core/src/linting/inflected_verb_after_to.rs
@@ -0,0 +1,153 @@
+use std::sync::Arc;
+
+use crate::{Dictionary, Document, FstDictionary, Span, TokenStringExt};
+
+use super::{Lint, LintKind, Linter, Suggestion};
+
+pub struct InflectedVerbAfterTo {
+ pub dictionary: Arc<FstDictionary>,
+}
+
+impl InflectedVerbAfterTo {
+ pub fn new() -> Self {
+ Self {
+ dictionary: FstDictionary::curated(),
+ }
+ }
+}
+
+impl Default for InflectedVerbAfterTo {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl Linter for InflectedVerbAfterTo {
+ fn lint(&mut self, document: &Document) -> Vec<Lint> {
+ let mut lints = Vec::new();
+ for pi in document.iter_preposition_indices() {
+ let Some(prep) = document.get_token(pi) else {
+ continue;
+ };
+ let Some(space) = document.get_token(pi + 1) else {
+ continue;
+ };
+ let Some(word) = document.get_token(pi + 2) else {
+ continue;
+ };
+ if !space.kind.is_whitespace() || !word.kind.is_word() {
+ continue;
+ }
+ let prep_to = document.get_span_content(&prep.span);
+ if prep_to != ['t', 'o'] && prep_to != ['T', 'o'] {
+ continue;
+ }
+
+ let chars = document.get_span_content(&word.span);
+ let (len, form) = match word.kind {
+ _ if word.kind.is_verb() => match chars {
+ // [.., 'i', 'n', 'g'] => (3, "continuous"), // breaks the Laravel test at "prior to deploying the application"
+ [.., 'e', 'd'] => (2, "past"),
+ [.., 'e', 's'] => (2, "3rd person singular present"),
+ [.., 's'] => (1, "3rd person singular present"),
+ _ => continue,
+ },
+ _ if word.kind.is_plural_noun() => match chars {
+ [.., 'e', 's'] => (2, "plural"),
+ [.., 's'] => (1, "plural"),
+ _ => continue,
+ },
+ _ => continue,
+ };
+ let stem = chars[..chars.len() - len].to_vec();
+ let Some(md) = self.dictionary.get_word_metadata(&stem) else {
+ continue;
+ };
+ if !md.is_verb() {
+ continue;
+ }
+ if word.kind.is_plural_noun() && md.is_noun() {
+ continue;
+ }
+
+ lints.push(Lint {
+ span: Span::new(prep.span.start, word.span.end),
+ lint_kind: LintKind::WordChoice,
+ message: format!("This verb seems to be in the {} form.", form).to_string(),
+ suggestions: vec![Suggestion::ReplaceWith(
+ prep_to
+ .iter()
+ .chain([' '].iter())
+ .chain(stem.iter())
+ .copied()
+ .collect(),
+ )],
+ ..Default::default()
+ });
+ }
+ lints
+ }
+
+ fn description(&self) -> &str {
+ "This rule looks for `to verb` where `verb` is not in the infinitive form."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::InflectedVerbAfterTo;
+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+
+ #[test]
+ fn dont_flag_to_check() {
+ assert_lint_count("to check", InflectedVerbAfterTo::default(), 0);
+ }
+
+ #[test]
+ fn dont_flag_to_checks() {
+ assert_lint_count("to checks", InflectedVerbAfterTo::default(), 0);
+ }
+
+ #[test]
+ fn dont_flag_to_cheques() {
+ assert_lint_count("to cheques", InflectedVerbAfterTo::default(), 0);
+ }
+
+ // #[test]
+ // fn flag_to_checking() {
+ // assert_lint_count("to checking", InflectedVerbAfterTo::default(), 1);
+ // }
+
+ #[test]
+ fn flag_to_checked() {
+ assert_lint_count("to checked", InflectedVerbAfterTo::default(), 1);
+ }
+
+ #[test]
+ fn dont_flag_plural_noun() {
+ assert_lint_count("to beliefs", InflectedVerbAfterTo::default(), 0);
+ }
+
+ #[test]
+ fn dont_flag_plural_meats() {
+ assert_lint_count("to meats", InflectedVerbAfterTo::default(), 0);
+ }
+
+ // #[test]
+ // fn check_993_suggestions() {
+ // assert_suggestion_result(
+ // "A location-agnostic structure that attempts to captures the context and content that a Lint occurred.",
+ // InflectedVerbAfterTo::default(),
+ // "A location-agnostic structure that attempts to capture the context and content that a Lint occurred.",
+ // );
+ // }
+
+ #[test]
+ fn dont_flag_plural_embarrass() {
+ assert_lint_count(
+ "Second I'm going to embarrass you for a.",
+ InflectedVerbAfterTo::default(),
+ 0,
+ );
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 99b623ace..c4378d9ce 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -29,6 +29,7 @@ use super::hedging::Hedging;
use super::hereby::Hereby;
use super::hop_hope::HopHope;
use super::hyphenate_number_day::HyphenateNumberDay;
+use super::inflected_verb_after_to::InflectedVerbAfterTo;
use super::left_right_hand::LeftRightHand;
use super::lets_confusion::LetsConfusion;
use super::likewise::Likewise;
@@ -343,6 +344,7 @@ impl LintGroup {
insert_pattern_rule!(Hedging, true);
insert_pattern_rule!(ExpandTimeShorthands, true);
insert_pattern_rule!(ModalOf, true);
+ insert_struct_rule!(InflectedVerbAfterTo, true);
out.add("SpellCheck", Box::new(SpellCheck::new(dictionary, dialect)));
out.config.set_rule_enabled("SpellCheck", true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 582f12268..82b9be4f4 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -23,6 +23,7 @@ mod hedging;
mod hereby;
mod hop_hope;
mod hyphenate_number_day;
+mod inflected_verb_after_to;
mod left_right_hand;
mod lets_confusion;
mod likewise;
@@ -86,6 +87,7 @@ pub use hedging::Hedging;
pub use hereby::Hereby;
pub use hop_hope::HopHope;
pub use hyphenate_number_day::HyphenateNumberDay;
+pub use inflected_verb_after_to::InflectedVerbAfterTo;
pub use left_right_hand::LeftRightHand;
pub use lets_confusion::LetsConfusion;
pub use likewise::Likewise;
diff --git a/harper-core/src/token_string_ext.rs b/harper-core/src/token_string_ext.rs
index 0fc9dad97..f65d86fea 100644
--- a/harper-core/src/token_string_ext.rs
+++ b/harper-core/src/token_string_ext.rs
@@ -75,6 +75,8 @@ pub trait TokenStringExt {
create_decl_for!(likely_homograph);
create_decl_for!(comma);
create_decl_for!(adjective);
+ create_decl_for!(verb);
+ create_decl_for!(preposition);
fn iter_linking_verb_indices(&self) -> impl Iterator<Item = usize> + '_;
fn iter_linking_verbs(&self) -> impl Iterator<Item = &Token> + '_;
@@ -119,6 +121,8 @@ impl TokenStringExt for [Token] {
create_fns_for!(likely_homograph);
create_fns_for!(comma);
create_fns_for!(adjective);
+ create_fns_for!(verb);
+ create_fns_for!(preposition);
fn first_non_whitespace(&self) -> Option<&Token> {
self.iter().find(|t| !t.kind.is_whitespace())