Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: upgrade `AndTheLike` `phrase_correction` to a full linter (#2225)
Andrew Dunbar8a5bf92b0022
diff --git a/harper-core/src/linting/and_the_like.rs b/harper-core/src/linting/and_the_like.rsnew file mode 100644index 000000000..3674996b9--- /dev/null+++ b/harper-core/src/linting/and_the_like.rs@@ -0,0 +1,162 @@+use crate::expr::{All, Expr, FixedPhrase, SequenceExpr};+use crate::linting::{ExprLinter, LintKind, Suggestion};+use crate::patterns::WordSet;+use crate::token_string_ext::TokenStringExt;+use crate::{Lint, Token};++pub struct AndTheLike {+ expr: Box<dyn Expr>,+}++impl Default for AndTheLike {+ fn default() -> Self {+ Self {+ expr: Box::new(All::new(vec![+ Box::new(+ // All known variants seen in the wild, good and bad+ SequenceExpr::word_set(&["and", "or", "an"])+ .t_ws()+ .then_optional(SequenceExpr::aco("the").t_ws())+ .then_word_set(&["alike", "alikes", "like", "likes"]),+ ),+ Box::new(SequenceExpr::unless(+ SequenceExpr::word_set(&["and", "or"])+ .t_ws()+ .then_any_of(vec![+ // But not the correct variants+ Box::new(FixedPhrase::from_phrase("the like")),+ // And not the phrases that were coincidentally caught in the net+ Box::new(WordSet::new(&["like", "likes"])),+ ]),+ )),+ ])),+ }+ }+}++impl ExprLinter for AndTheLike {+ fn expr(&self) -> &dyn Expr {+ self.expr.as_ref()+ }++ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {+ let (conj, ws) = (&toks[0], &toks[1]);++ let conj = if conj.span.get_content(src)[0] == 'a' {+ "and"+ } else {+ "or"+ };++ let corrected = format!("{}{}the like", conj, ws.span.get_content_string(src));++ Some(Lint {+ span: toks.span()?,+ lint_kind: LintKind::Usage,+ suggestions: vec![Suggestion::replace_with_match_case(+ corrected.chars().collect(),+ toks.span()?.get_content(src),+ )],+ message: "If you intended the idiom meaning `similar things`, the correct form is with `the like`.".to_string(),+ ..Default::default()+ })+ }++ fn description(&self) -> &str {+ "Corrects mistakes in `and the like` and `or the like`."+ }+}++#[cfg(test)]+mod tests {+ use super::AndTheLike;+ use crate::linting::tests::{assert_no_lints, assert_suggestion_result};++ #[test]+ fn dont_flag_and_the_like() {+ assert_no_lints(+ "The color of brackets and the like appears to be incorrect ...",+ AndTheLike::default(),+ );+ }++ #[test]+ fn dont_flag_or_the_like() {+ assert_no_lints(+ "Does WCAG apply only to English (or the like), or does it aim to cover all languages?",+ AndTheLike::default(),+ );+ }++ #[test]+ fn flag_an_the_likes() {+ assert_suggestion_result(+ "Allow jsSourceDir (an the likes) to refer to the project root. #5",+ AndTheLike::default(),+ "Allow jsSourceDir (and the like) to refer to the project root. #5",+ );+ }++ #[test]+ fn flag_and_alike() {+ assert_suggestion_result(+ "Latest release breaks FilePicker and alike",+ AndTheLike::default(),+ "Latest release breaks FilePicker and the like",+ );+ }++ #[test]+ fn flag_and_alikes() {+ assert_suggestion_result(+ "Compiled functions (and alikes) need to keep references for their module objects",+ AndTheLike::default(),+ "Compiled functions (and the like) need to keep references for their module objects",+ );+ }++ #[test]+ fn flag_and_the_alike() {+ assert_suggestion_result(+ "Suggestions, comments and the alike are welcome on http://waa.ai/4xtC",+ AndTheLike::default(),+ "Suggestions, comments and the like are welcome on http://waa.ai/4xtC",+ );+ }++ #[test]+ fn flag_and_the_likes() {+ assert_suggestion_result(+ "Don't report \"expected semicolon or line break\", \"expected comma\" and the likes at every token boundary",+ AndTheLike::default(),+ "Don't report \"expected semicolon or line break\", \"expected comma\" and the like at every token boundary",+ );+ }++ #[test]+ fn flag_or_alike() {+ assert_suggestion_result(+ "enable biome extension to \"monitor or alike\" the workspace.",+ AndTheLike::default(),+ "enable biome extension to \"monitor or the like\" the workspace.",+ );+ }++ #[test]+ fn flag_or_alikes() {+ assert_suggestion_result(+ "Persistent Compiler Caching with ccache or alikes",+ AndTheLike::default(),+ "Persistent Compiler Caching with ccache or the like",+ );+ }++ #[test]+ fn flag_or_the_likes() {+ assert_suggestion_result(+ "Description of the problem: Implement aria2c or the likes to resume partial downloads.",+ AndTheLike::default(),+ "Description of the problem: Implement aria2c or the like to resume partial downloads.",+ );+ }+}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex cd1bf850e..abd8fcb2e 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -22,6 +22,7 @@ use super::am_in_the_morning::AmInTheMorning;use super::amounts_for::AmountsFor;use super::an_a::AnA;use super::and_in::AndIn;+use super::and_the_like::AndTheLike;use super::another_thing_coming::AnotherThingComing;use super::another_think_coming::AnotherThinkComing;use super::ask_no_preposition::AskNoPreposition;@@ -468,6 +469,7 @@ impl LintGroup {insert_expr_rule!(AmountsFor, true);insert_struct_rule!(AnA, true);insert_expr_rule!(AndIn, true);+ insert_expr_rule!(AndTheLike, true);insert_expr_rule!(AnotherThingComing, true);insert_expr_rule!(AnotherThinkComing, false);insert_expr_rule!(AskNoPreposition, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex c132e33d0..0439cf970 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -13,6 +13,7 @@ mod am_in_the_morning;mod amounts_for;mod an_a;mod and_in;+mod and_the_like;mod another_thing_coming;mod another_think_coming;mod ask_no_preposition;@@ -205,6 +206,7 @@ pub use am_in_the_morning::AmInTheMorning;pub use amounts_for::AmountsFor;pub use an_a::AnA;pub use and_in::AndIn;+pub use and_the_like::AndTheLike;pub use another_thing_coming::AnotherThingComing;pub use another_think_coming::AnotherThinkComing;pub use ask_no_preposition::AskNoPreposition;diff --git a/harper-core/src/linting/phrase_corrections/mod.rs b/harper-core/src/linting/phrase_corrections/mod.rsindex df75d669c..7b7b63f60 100644--- a/harper-core/src/linting/phrase_corrections/mod.rs+++ b/harper-core/src/linting/phrase_corrections/mod.rs@@ -92,13 +92,6 @@ pub fn lint_group() -> LintGroup {"Corrects `an another` and `a another`.",LintKind::Redundancy),- "AndTheLike" => (- ["an the like"],- ["and the like"],- "Did you mean `and the like`?",- "Fixes the typo in `and the like`.",- LintKind::Typo- ),"AnotherAn" => (["another an"],["another"],diff --git a/harper-core/src/linting/phrase_corrections/tests.rs b/harper-core/src/linting/phrase_corrections/tests.rsindex 8980a2dad..7a67a1ab7 100644--- a/harper-core/src/linting/phrase_corrections/tests.rs+++ b/harper-core/src/linting/phrase_corrections/tests.rs@@ -250,9 +250,6 @@ fn correct_a_another() {);}-// AndTheLike-// -none--// AnotherAn#[test]fn correct_another_an() {