Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): create new rule for detecting missing nouns (#2023)
Elijah Potter123e21426f5d
diff --git a/harper-core/src/linting/determiner_without_noun.rs b/harper-core/src/linting/determiner_without_noun.rsnew file mode 100644index 000000000..be9d00fe9--- /dev/null+++ b/harper-core/src/linting/determiner_without_noun.rs@@ -0,0 +1,79 @@+use crate::expr::Expr;+use crate::expr::SequenceExpr;+use crate::{Token, TokenStringExt};++use super::{ExprLinter, Lint, LintKind, Suggestion};++pub struct DeterminerWithoutNoun {+ expr: Box<dyn Expr>,+}++impl Default for DeterminerWithoutNoun {+ fn default() -> Self {+ let expr = SequenceExpr::default()+ .then(|tok: &Token, _: &[char]| tok.kind.is_determiner())+ .t_ws()+ .then_conjunction();++ Self {+ expr: Box::new(expr),+ }+ }+}++impl ExprLinter for DeterminerWithoutNoun {+ fn expr(&self) -> &dyn Expr {+ self.expr.as_ref()+ }++ fn match_to_lint(&self, matched_tokens: &[Token], _source: &[char]) -> Option<Lint> {+ let span = matched_tokens.span()?;+ Some(Lint {+ span,+ lint_kind: LintKind::Miscellaneous,+ suggestions: Vec::<Suggestion>::new(),+ message: "A determiner should not be immediately followed by a conjunction."+ .to_string(),+ priority: 32,+ })+ }++ fn description(&self) -> &'static str {+ "Flags sequences where a determiner (`a`, `an`, `the`, etc.) is directly followed by a coordinating or subordinating conjunction (`and`, `or`, `but`, `because`, etc.), indicating a missing noun."+ }+}++#[cfg(test)]+mod tests {+ use super::DeterminerWithoutNoun;+ use crate::linting::tests::assert_lint_count;++ #[test]+ fn flags_determiner_followed_by_conjunction() {+ assert_lint_count(+ "The and other options were ignored.",+ DeterminerWithoutNoun::default(),+ 1,+ );+ }++ #[test]+ fn flags_indefinite_article_followed_by_conjunction() {+ assert_lint_count("A because I said so.", DeterminerWithoutNoun::default(), 1);+ assert_lint_count("An because I said so.", DeterminerWithoutNoun::default(), 1);+ }++ #[test]+ fn allows_correct_use_with_noun() {+ assert_lint_count("The dog barked.", DeterminerWithoutNoun::default(), 0);+ }++ #[test]+ fn allows_determiner_noun_then_conjunction() {+ assert_lint_count(+ "The dog and the cat played.",+ DeterminerWithoutNoun::default(),+ 0,+ );+ }+}diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 3dc420b45..e3a47d8ea 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -29,6 +29,7 @@ mod criteria_phenomena;mod currency_placement;mod dashes;mod despite_of;+mod determiner_without_noun;mod didnt;mod discourse_markers;mod dot_initialisms;diff --git a/harper-core/src/linting/phrase_corrections/mod.rs b/harper-core/src/linting/phrase_corrections/mod.rsindex ad790f72f..5e07acfac 100644--- a/harper-core/src/linting/phrase_corrections/mod.rs+++ b/harper-core/src/linting/phrase_corrections/mod.rs@@ -1182,6 +1182,12 @@ pub fn lint_group() -> LintGroup {"`Wrought iron` is low-carbon, malleable iron used for decorative work; variants like `rod iron` or `rot iron` are phonetic misspellings that may confuse readers.",LintKind::Eggcorn),+ "ToWorryAbout" => (+ ["to worried about"],+ ["to worry about", "too worried about"],+ "Did you mean the progressive form?",+ "Fixes incorrect use of `to worried about`."+ ),"The" => (["teh"],["the"],diff --git a/harper-core/src/linting/phrase_corrections/tests.rs b/harper-core/src/linting/phrase_corrections/tests.rsindex 15f8b43ea..f54bdd157 100644--- a/harper-core/src/linting/phrase_corrections/tests.rs+++ b/harper-core/src/linting/phrase_corrections/tests.rs@@ -1562,6 +1562,20 @@ fn thanks_a_lot_clean() {assert_lint_count("thanks a lot", lint_group(), 0);}+#[test]+fn fixes_to_worry_about() {+ assert_top3_suggestion_result(+ "I don't want you to worried about it.",+ lint_group(),+ "I don't want you to worry about it.",+ );+ assert_top3_suggestion_result(+ "I don't want you to worried about it.",+ lint_group(),+ "I don't want you too worried about it.",+ );+}+// WroughtIron#[test]fn corrects_rod_iron() {