Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): add rule for missing preposition (#1530)
Elijah Potter4d2175f6cf33
diff --git a/harper-core/src/expr/sequence_expr.rs b/harper-core/src/expr/sequence_expr.rsindex 8aae69c00..664d73c81 100644--- a/harper-core/src/expr/sequence_expr.rs+++ b/harper-core/src/expr/sequence_expr.rs@@ -23,6 +23,13 @@ macro_rules! gen_then_from_is {})}+ #[doc = concat!("Adds an optional step matching a token where [`TokenKind::is_", stringify!($quality), "()`] returns true.")]+ pub fn [< then_optional_$quality >] (self) -> Self{+ self.then_optional(|tok: &Token, _source: &[char]| {+ tok.kind.[< is_$quality >]()+ })+ }+#[doc = concat!("Adds a step matching one or more consecutive tokens where [`TokenKind::is_", stringify!($quality), "()`] returns true.")]pub fn [< then_one_or_more_$quality s >] (self) -> Self{self.then_one_or_more(Box::new(|tok: &Token, _source: &[char]| {diff --git a/harper-core/src/linting/expr_linter.rs b/harper-core/src/linting/expr_linter.rsindex bbf0c4822..b2533476b 100644--- a/harper-core/src/linting/expr_linter.rs+++ b/harper-core/src/linting/expr_linter.rs@@ -11,7 +11,7 @@ use super::{Lint, Linter};/// boundaries.#[blanket(derive(Box))]pub trait ExprLinter: LSend {- /// A simple getter for the pattern to be searched for.+ /// A simple getter for the expression you want Harper to search for.fn expr(&self) -> &dyn Expr;/// If any portions of a [`Document`] match [`Self::expr`], they are passed through [`ExprLinter::match_to_lint`] to be/// transformed into a [`Lint`] for editor consumption.diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 32d749625..8d3bb9ea3 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -54,6 +54,7 @@ use super::lets_confusion::LetsConfusion;use super::likewise::Likewise;use super::long_sentences::LongSentences;use super::merge_words::MergeWords;+use super::missing_preposition::MissingPreposition;use super::modal_of::ModalOf;use super::most_number::MostNumber;use super::multiple_sequential_pronouns::MultipleSequentialPronouns;@@ -388,6 +389,7 @@ impl LintGroup {insert_expr_rule!(BoringWords, false);insert_struct_rule!(CapitalizePersonalPronouns, true);insert_expr_rule!(ChockFull, true);+ insert_expr_rule!(MissingPreposition, true);insert_struct_rule!(DiscourseMarkers, true);insert_expr_rule!(WayTooAdjective, true);insert_expr_rule!(HavePronoun, true);diff --git a/harper-core/src/linting/missing_preposition.rs b/harper-core/src/linting/missing_preposition.rsnew file mode 100644index 000000000..95bfbea22--- /dev/null+++ b/harper-core/src/linting/missing_preposition.rs@@ -0,0 +1,139 @@+use harper_brill::UPOS;++use crate::expr::AnchorStart;+use crate::expr::Expr;+use crate::expr::OwnedExprExt;+use crate::expr::SequenceExpr;+use crate::patterns::AnyPattern;+use crate::patterns::UPOSSet;+use crate::{Token, TokenStringExt};++use super::{ExprLinter, Lint, LintKind};++pub struct MissingPreposition {+ expr: Box<dyn Expr>,+}++impl Default for MissingPreposition {+ fn default() -> Self {+ let expr = SequenceExpr::default()+ .then(+ AnchorStart.or(SequenceExpr::default()+ .then(UPOSSet::new(&[UPOS::DET]))+ .t_ws()),+ )+ .then(UPOSSet::new(&[UPOS::NOUN, UPOS::PRON, UPOS::PROPN]))+ .t_ws()+ .then(UPOSSet::new(&[UPOS::AUX]))+ .t_ws()+ .then(UPOSSet::new(&[UPOS::ADJ]))+ .t_ws()+ .then(UPOSSet::new(&[UPOS::NOUN, UPOS::PRON, UPOS::PROPN]))+ .then_optional(AnyPattern)+ .then_optional(AnyPattern);++ Self {+ expr: Box::new(expr),+ }+ }+}++impl ExprLinter for MissingPreposition {+ fn expr(&self) -> &dyn Expr {+ self.expr.as_ref()+ }++ fn match_to_lint(&self, matched_tokens: &[Token], _source: &[char]) -> Option<Lint> {+ if matched_tokens.last()?.kind.is_upos(UPOS::ADP) {+ return None;+ }++ Some({+ Lint {+ span: matched_tokens[2..4].span()?,+ lint_kind: LintKind::Miscellaneous,+ suggestions: vec![],+ message: "You may be missing a preposition here.".to_owned(),+ priority: 31,+ }+ })+ }++ fn description(&self) -> &'static str {+ "Locates potentially missing prepositions."+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::{assert_lint_count, assert_no_lints};++ use super::MissingPreposition;++ #[test]+ fn fixes_issue_1513() {+ assert_lint_count(+ "The city is famous its beaches.",+ MissingPreposition::default(),+ 1,+ );+ assert_lint_count(+ "The students are interested learning.",+ MissingPreposition::default(),+ 1,+ );+ }++ #[test]+ fn allows_corrected_issue_1513() {+ assert_no_lints(+ "The city is famous for its beaches.",+ MissingPreposition::default(),+ );+ assert_no_lints(+ "The students are interested in learning.",+ MissingPreposition::default(),+ );+ }++ #[test]+ fn no_lint_without_adj_noun_sequence() {+ assert_lint_count("She is happy.", MissingPreposition::default(), 0);+ }++ #[test]+ fn no_lint_with_preposition_present() {+ assert_lint_count("They are fond of music.", MissingPreposition::default(), 0);+ assert_lint_count(+ "Students are interested in history.",+ MissingPreposition::default(),+ 0,+ );+ }++ #[test]+ fn flag_adj_pron_pair() {+ assert_lint_count("He was angry him.", MissingPreposition::default(), 1);+ }++ #[test]+ fn no_lint_empty() {+ assert_lint_count("", MissingPreposition::default(), 0);+ }++ #[test]+ fn allows_tired_herself() {+ assert_no_lints(+ "She had tired herself out with trying.",+ MissingPreposition::default(),+ );+ }++ #[test]+ fn allows_terrible_stuff() {+ assert_no_lints(+ "Either it was terrible stuff or the whiskey distorted things.",+ MissingPreposition::default(),+ );+ }+}diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex af42155f3..2fcc15696 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -59,6 +59,7 @@ mod map_phrase_linter;mod map_phrase_set_linter;mod merge_linters;mod merge_words;+mod missing_preposition;mod modal_of;mod most_number;mod multiple_sequential_pronouns;@@ -166,6 +167,7 @@ pub use long_sentences::LongSentences;pub use map_phrase_linter::MapPhraseLinter;pub use map_phrase_set_linter::MapPhraseSetLinter;pub use merge_words::MergeWords;+pub use missing_preposition::MissingPreposition;pub use modal_of::ModalOf;pub use most_number::MostNumber;pub use multiple_sequential_pronouns::MultipleSequentialPronouns;@@ -258,7 +260,7 @@ pub mod tests {use crate::{Document, parsers::PlainEnglish};#[track_caller]- pub fn assert_no_lints(text: &str, mut linter: impl Linter) {+ pub fn assert_no_lints(text: &str, linter: impl Linter) {assert_lint_count(text, linter, 0);}diff --git a/harper-core/tests/text/linters/Alice's Adventures in Wonderland.snap.yml b/harper-core/tests/text/linters/Alice's Adventures in Wonderland.snap.ymlindex ab5bac2a0..94e04c80c 100644--- a/harper-core/tests/text/linters/Alice's Adventures in Wonderland.snap.yml+++ b/harper-core/tests/text/linters/Alice's Adventures in Wonderland.snap.yml@@ -670,6 +670,13 @@ Message: |+Lint: Miscellaneous (31 priority)+Message: |+ 361 | It was high time to go, for the pool was getting quite crowded with the birds+ | ^~~~ You may be missing a preposition here.+++Lint: Spelling (63 priority)Message: |362 | and animals that had fallen into it: there were a Duck and a Dodo, a Lory and andiff --git a/harper-core/tests/text/linters/The Great Gatsby.snap.yml b/harper-core/tests/text/linters/The Great Gatsby.snap.ymlindex a244ebe89..1568eb2c2 100644--- a/harper-core/tests/text/linters/The Great Gatsby.snap.yml+++ b/harper-core/tests/text/linters/The Great Gatsby.snap.yml@@ -6969,6 +6969,13 @@ Suggest:+Lint: Miscellaneous (31 priority)+Message: |+ 5405 | “We were close friends.”+ | ^~~~~ You may be missing a preposition here.+++Lint: Spelling (63 priority)Message: |5412 | “If he’d of lived, he’d of been a great man. A man like James J. Hill. He’d of