Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: wip: do a mistake→make a mistake (#2463)
Andrew Dunbar1c033ca5b43e
diff --git a/harper-core/src/linting/despite_it_is.rs b/harper-core/src/linting/despite_it_is.rsindex 5af18279b..d6d90406b 100644--- a/harper-core/src/linting/despite_it_is.rs+++ b/harper-core/src/linting/despite_it_is.rs@@ -6,7 +6,10 @@ use crate::{CharStringExt, Token, TokenStringExt,dict_word_metadata::Person,expr::{Expr, SequenceExpr},- linting::{ExprLinter, Lint, LintKind, Suggestion, expr_linter::Chunk},+ linting::{+ ExprLinter, Lint, LintKind, Suggestion,+ expr_linter::{Chunk, followed_by_word},+ },patterns::WordSet,};@@ -53,12 +56,7 @@ impl ExprLinter for DespiteItIs {src: &[char],ctx: Option<(&[Token], &[Token])>,) -> Option<Lint> {- let next_is_ing = ctx.is_some_and(|(_, then)| {- then.first().is_some_and(|t| t.kind.is_whitespace())- && then- .get(1)- .is_some_and(|t| t.kind.is_verb_progressive_form())- });+ let next_is_ing = followed_by_word(ctx, |nw| nw.kind.is_verb_progressive_form());let subj = toks.get(2)?;let be = toks.get(4)?;diff --git a/harper-core/src/linting/do_mistake.rs b/harper-core/src/linting/do_mistake.rsnew file mode 100644index 000000000..162868031--- /dev/null+++ b/harper-core/src/linting/do_mistake.rs@@ -0,0 +1,304 @@+use crate::{+ CharStringExt, Lint, Token,+ expr::{Expr, FixedPhrase, SequenceExpr},+ linting::{+ ExprLinter, LintKind, Suggestion,+ expr_linter::{Chunk, followed_by_word},+ },+ patterns::WordSet,+};++pub struct DoMistake {+ expr: Box<dyn Expr>,+}++impl Default for DoMistake {+ fn default() -> Self {+ Self {+ expr: Box::new(+ SequenceExpr::word_set(&["do", "did", "does", "doing", "done"])+ .t_ws()+ .then_longest_of(vec![+ Box::new(WordSet::new(&[+ "a", "an", "the", "that", "these", "this", "those", "another", "many",+ "several", "some", "my", "our", "your", "his", "her", "its", "their",+ ])),+ Box::new(FixedPhrase::from_phrase("a lot of")),+ Box::new(FixedPhrase::from_phrase("lots of")),+ Box::new(FixedPhrase::from_phrase("that kind of")),+ Box::new(FixedPhrase::from_phrase("these kinds of")),+ Box::new(FixedPhrase::from_phrase("this kind of")),+ Box::new(FixedPhrase::from_phrase("those kinds of")),+ Box::new(FixedPhrase::from_phrase("so many")),+ Box::new(FixedPhrase::from_phrase("too many")),+ Box::new(FixedPhrase::from_phrase("tons of")),+ Box::new(FixedPhrase::from_phrase("tonnes of")),+ ])+ .t_ws()+ .then_word_set(&["mistake", "mistakes"]),+ ),+ }+ }+}++impl ExprLinter for DoMistake {+ type Unit = Chunk;++ fn match_to_lint_with_context(+ &self,+ toks: &[Token],+ src: &[char],+ ctx: Option<(&[Token], &[Token])>,+ ) -> Option<Lint> {+ let tok = toks.first()?;+ let span = tok.span;+ let chars = span.get_content(src);++ if followed_by_word(ctx, |nw| {+ nw.kind.is_verb() && !nw.kind.is_verb_progressive_form()+ }) {+ return None;+ }++ let make = if chars.eq_ignore_ascii_case_str("do") {+ "make"+ } else if chars.eq_ignore_ascii_case_str("did") || chars.eq_ignore_ascii_case_str("done") {+ "made"+ } else if chars.eq_ignore_ascii_case_str("does") {+ "makes"+ } else if chars.eq_ignore_ascii_case_str("doing") {+ "making"+ } else {+ return None;+ }+ .chars()+ .collect();++ Some(Lint {+ span,+ lint_kind: LintKind::Usage,+ suggestions: vec![Suggestion::replace_with_match_case(make, chars)],+ message: "In English we `make` mistakes, not `do` them".to_string(),+ ..Default::default()+ })+ }++ fn description(&self) -> &str {+ "Corrects `do a mistake` to `make a mistake`."+ }++ fn expr(&self) -> &dyn Expr {+ &*self.expr+ }+}++#[cfg(test)]+mod tests {+ use super::DoMistake;+ use crate::linting::tests::{assert_no_lints, assert_suggestion_result};++ #[test]+ fn did_a_mistake() {+ assert_suggestion_result(+ "Hi, I did a mistake in my NGINX config file and so once the container is launched, it logs the error...",+ DoMistake::default(),+ "Hi, I made a mistake in my NGINX config file and so once the container is launched, it logs the error...",+ );+ }++ #[test]+ fn did_my_mistakes() {+ assert_suggestion_result(+ "Where i did my mistakes?",+ DoMistake::default(),+ "Where i made my mistakes?",+ );+ }++ #[test]+ fn did_several_mistakes() {+ assert_suggestion_result(+ "Maybe I did several mistakes, but I can only find a message about one?",+ DoMistake::default(),+ "Maybe I made several mistakes, but I can only find a message about one?",+ );+ }++ #[test]+ fn did_some_mistakes() {+ assert_suggestion_result(+ "I made this program to learn goto use. but did some mistakes somewhere",+ DoMistake::default(),+ "I made this program to learn goto use. but made some mistakes somewhere",+ );+ }++ #[test]+ fn did_that_mistake() {+ assert_suggestion_result(+ "and believe me, I did that mistake too",+ DoMistake::default(),+ "and believe me, I made that mistake too",+ );+ }++ #[test]+ fn did_the_mistake() {+ assert_suggestion_result(+ "The issue describe is the person who did the mistake in the past & that same person is NOW correcting other people",+ DoMistake::default(),+ "The issue describe is the person who made the mistake in the past & that same person is NOW correcting other people",+ );+ }++ #[test]+ fn did_this_mistake() {+ assert_suggestion_result(+ "Are there famous mathematicians who did this mistake?",+ DoMistake::default(),+ "Are there famous mathematicians who made this mistake?",+ );+ }++ #[test]+ fn do_many_mistakes() {+ assert_suggestion_result(+ "I observed that my coworkers do many mistakes using the field calculator",+ DoMistake::default(),+ "I observed that my coworkers make many mistakes using the field calculator",+ );+ }++ #[test]+ fn do_mistake() {+ assert_suggestion_result(+ "If you do a mistake that causes alot of problems, please use the command to redo",+ DoMistake::default(),+ "If you make a mistake that causes alot of problems, please use the command to redo",+ );+ }++ #[test]+ fn do_some_mistakes() {+ assert_suggestion_result(+ "so probably if my colleagues do some mistakes I tend to learn them as well",+ DoMistake::default(),+ "so probably if my colleagues make some mistakes I tend to learn them as well",+ );+ }++ #[test]+ fn do_the_mistake() {+ assert_suggestion_result(+ "do I need to explicitly mention that I did not do the mistake to do not lose the point?",+ DoMistake::default(),+ "do I need to explicitly mention that I did not make the mistake to do not lose the point?",+ );+ }++ #[test]+ fn do_this_mistake() {+ assert_suggestion_result(+ "I barely remember any frontend developer that wouldn't do this mistake at least once.",+ DoMistake::default(),+ "I barely remember any frontend developer that wouldn't make this mistake at least once.",+ );+ }++ #[test]+ fn do_this_mistakes() {+ assert_suggestion_result(+ "I do this mistakes to check the command detekt with type resolution",+ DoMistake::default(),+ "I make this mistakes to check the command detekt with type resolution",+ );+ }++ #[test]+ fn do_those_mistakes() {+ assert_suggestion_result(+ "An experienced developer could do those mistakes as well",+ DoMistake::default(),+ "An experienced developer could make those mistakes as well",+ );+ }++ #[test]+ fn doing_a_mistake() {+ assert_suggestion_result(+ "Here at work, a colleague asked if we were doing a mistake by using the ReactDOM.renderToStaticMarkup on the client side.",+ DoMistake::default(),+ "Here at work, a colleague asked if we were making a mistake by using the ReactDOM.renderToStaticMarkup on the client side.",+ );+ }++ #[test]+ fn doing_several_mistakes() {+ assert_suggestion_result(+ "I realized I was doing several mistakes",+ DoMistake::default(),+ "I realized I was making several mistakes",+ );+ }++ #[test]+ fn doing_the_mistkae() {+ assert_suggestion_result(+ "where am i doing the mistake?",+ DoMistake::default(),+ "where am i making the mistake?",+ );+ }++ #[test]+ fn done_some_mistake() {+ assert_suggestion_result(+ "Might be I have done some mistake, that I do not know.",+ DoMistake::default(),+ "Might be I have made some mistake, that I do not know.",+ );+ }++ #[test]+ fn done_this_mistake() {+ assert_suggestion_result(+ "how many more users have done this mistake?",+ DoMistake::default(),+ "how many more users have made this mistake?",+ );+ }++ // False positives++ #[test]+ fn dont_flag_when_does_a_mistake() {+ assert_no_lints(+ "When does a mistake become standard usage? ",+ DoMistake::default(),+ );+ }++ #[test]+ fn dont_flag_did_that_mistake_verb() {+ assert_no_lints(+ "Did that mistake occurred before or after the day 2 backup?",+ DoMistake::default(),+ );+ }++ #[test]+ fn dont_flag_does_this_mistake_verb() {+ assert_no_lints(+ "Does this mistake invalidate your thesis?",+ DoMistake::default(),+ );+ }++ #[test]+ fn dont_flag_does_the_mistake_verb() {+ assert_no_lints(+ "Does the mistake change the meaning of the quotation?",+ DoMistake::default(),+ );+ }+}diff --git a/harper-core/src/linting/expr_linter.rs b/harper-core/src/linting/expr_linter.rsindex 549f58969..3647efb02 100644--- a/harper-core/src/linting/expr_linter.rs+++ b/harper-core/src/linting/expr_linter.rs@@ -132,6 +132,39 @@ pub fn run_on_chunk<'a>(})}+/// Check for sentence continuation after a matched span.+///+/// Validates that the "after" context starts with whitespace followed by a word token,+/// allowing flexible inspection of that word's properties (POS tags, etc.) via the predicate.+/// The predicate can be used to confirm matches, suppress false positives, or apply conditional logic.+///+/// Returns `false` if context is `None`, missing tokens, or the structure is malformed.+pub fn followed_by_word(+ context: Option<(&[Token], &[Token])>,+ predicate: impl Fn(&Token) -> bool,+) -> bool {+ if let Some((_, after)) = context+ && let [ws, word, ..] = after+ && ws.kind.is_whitespace()+ {+ return predicate(word);+ }+ false+}++pub fn preceded_by_word(+ context: Option<(&[Token], &[Token])>,+ predicate: impl Fn(&Token) -> bool,+) -> bool {+ if let Some((before, _)) = context+ && let [.., word, ws] = before+ && ws.kind.is_whitespace()+ {+ return predicate(word);+ }+ false+}+#[cfg(test)]mod tests_context {use crate::expr::{Expr, FixedPhrase};diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 333b329ef..c5a94bc91 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -59,6 +59,7 @@ use super::did_past::DidPast;use super::didnt::Didnt;use super::discourse_markers::DiscourseMarkers;use super::disjoint_prefixes::DisjointPrefixes;+use super::do_mistake::DoMistake;use super::dot_initialisms::DotInitialisms;use super::double_click::DoubleClick;use super::double_modal::DoubleModal;@@ -475,6 +476,7 @@ impl LintGroup {insert_expr_rule!(DespiteOf, true);insert_expr_rule!(Didnt, true);insert_struct_rule!(DiscourseMarkers, true);+ insert_expr_rule!(DoMistake, true);insert_expr_rule!(DotInitialisms, true);insert_expr_rule!(DoubleClick, true);insert_expr_rule!(DoubleModal, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex ba9de913b..13c988de5 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -54,6 +54,7 @@ mod did_past;mod didnt;mod discourse_markers;mod disjoint_prefixes;+mod do_mistake;mod dot_initialisms;mod double_click;mod double_modal;diff --git a/harper-core/src/linting/modal_be_adjective.rs b/harper-core/src/linting/modal_be_adjective.rsindex f8b5d55c3..ad1861bd9 100644--- a/harper-core/src/linting/modal_be_adjective.rs+++ b/harper-core/src/linting/modal_be_adjective.rs@@ -1,7 +1,10 @@use crate::{CharStringExt, Lint, Token, TokenKind,expr::{Expr, SequenceExpr},- linting::{ExprLinter, Suggestion, expr_linter::Chunk},+ linting::{+ ExprLinter, Suggestion,+ expr_linter::{Chunk, followed_by_word},+ },patterns::ModalVerb,};@@ -47,35 +50,21 @@ impl ExprLinter for ModalBeAdjective {src: &[char],ctx: Option<(&[Token], &[Token])>,) -> Option<Lint> {- if let Some((_, after)) = ctx- && after.len() >= 2- && after[0].kind.is_whitespace()- {- // If the 'after' context is whitespace followed by a noun, there is no error- // (Not including these marginal nouns: "at", "by", "if")- if after[1].kind.is_noun()- && !after[1]+ if followed_by_word(ctx, |nw| {+ (nw.kind.is_noun()+ && !nw.span.get_content(src)- .eq_any_ignore_ascii_case_str(&["at", "by", "if"])- {- return None;- }-- // If the adjective plus the next word is "kind of"- if toks- .last()- .unwrap()- .span- .get_content_string(src)- .eq_ignore_ascii_case("kind")- && after[1]+ .eq_any_ignore_ascii_case_str(&["at", "by", "if"]))+ || (toks+ .last()+ .unwrap().span.get_content(src)- .eq_ignore_ascii_case_str("of")- {- return None;- }+ .eq_ignore_ascii_case_str("kind")+ && nw.span.get_content(src).eq_ignore_ascii_case_str("of"))+ }) {+ return None;}Some(Lint {diff --git a/harper-core/src/linting/possessive_your.rs b/harper-core/src/linting/possessive_your.rsindex 0e9101a09..2ca0dcd06 100644--- a/harper-core/src/linting/possessive_your.rs+++ b/harper-core/src/linting/possessive_your.rs@@ -1,10 +1,11 @@-use crate::Token;-use crate::TokenKind;-use crate::expr::Expr;-use crate::expr::SequenceExpr;--use super::{ExprLinter, Lint, LintKind, Suggestion};-use crate::linting::expr_linter::Chunk;+use crate::{+ Token, TokenKind,+ expr::{Expr, SequenceExpr},+ linting::{+ ExprLinter, Lint, LintKind, Suggestion,+ expr_linter::{Chunk, preceded_by_word},+ },+};pub struct PossessiveYour {expr: Box<dyn Expr>,@@ -39,11 +40,7 @@ impl ExprLinter for PossessiveYour {source: &[char],ctx: Option<(&[Token], &[Token])>,) -> Option<Lint> {- // Is 'you' the object of a verb? (#1920)- if let [.., v, ws] = ctx?.0- && ws.kind.is_whitespace()- && v.kind.is_verb()- {+ if preceded_by_word(ctx, |pw| pw.kind.is_verb()) {return None;}diff --git a/harper-core/src/linting/take_a_look_to.rs b/harper-core/src/linting/take_a_look_to.rsindex c5ec6b80a..439604b27 100644--- a/harper-core/src/linting/take_a_look_to.rs+++ b/harper-core/src/linting/take_a_look_to.rs@@ -1,7 +1,10 @@use crate::{CharStringExt, Lint, Token,expr::{Expr, SequenceExpr},- linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},+ linting::{+ ExprLinter, LintKind, Suggestion,+ expr_linter::{Chunk, followed_by_word},+ },patterns::WordSet,};@@ -48,26 +51,17 @@ impl ExprLinter for TakeALookTo {src: &[char],ctx: Option<(&[Token], &[Token])>,) -> Option<Lint> {- let next_word_tok =- ctx.and_then(|(_, after)| after.get(..2))- .and_then(|tokens| match tokens {- [ws, next, ..] if ws.kind.is_whitespace() => Some(next),- _ => None,- });-- // Exception 1. Have/take a look to see if everything is ok- if next_word_tok.is_some_and(|nw| nw.kind.is_verb_lemma()) {- return None;- }-- // Exception 2. It has a look to it that I don't like- if next_word_tok.is_some_and(|xyz| xyz.span.get_content(src).eq_ignore_ascii_case_str("it"))- && toks.first().is_some_and(|tok| {- tok.span- .get_content(src)- .eq_any_ignore_ascii_case_str(HAVE_FORMS)- })- {+ if followed_by_word(ctx, |nw| {+ nw.kind.is_verb_lemma()+ // Exception 1. Have/take a look to see if everything is ok+ || (nw.span.get_content(src).eq_ignore_ascii_case_str("it")+ // Exception 2. It has a look to it that I don't like+ && toks.first().is_some_and(|tok| {+ tok.span+ .get_content(src)+ .eq_any_ignore_ascii_case_str(HAVE_FORMS)+ }))+ }) {return None;}