Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): create more specialized rule for #2144 (#2253)
Elijah Potter235a4016ea58
diff --git a/harper-core/src/linting/a_while.rs b/harper-core/src/linting/a_while.rsnew file mode 100644index 000000000..6c6cf8185--- /dev/null+++ b/harper-core/src/linting/a_while.rs@@ -0,0 +1,148 @@+use std::sync::Arc;++use harper_brill::UPOS;++use crate::char_string::char_string;+use crate::expr::{Expr, ExprMap, SequenceExpr};+use crate::patterns::UPOSSet;+use crate::{CharString, Token, TokenStringExt};++use super::expr_linter::Chunk;+use super::{ExprLinter, Lint, LintKind, Suggestion};++pub struct AWhile {+ expr: Box<dyn Expr>,+ map: Arc<ExprMap<(CharString, &'static str)>>,+}++impl Default for AWhile {+ fn default() -> Self {+ let mut map = ExprMap::default();++ let a = SequenceExpr::default()+ .then(UPOSSet::new(&[UPOS::VERB]))+ .t_ws()+ .t_aco("a")+ .t_ws()+ .t_aco("while");++ map.insert(+ a,+ (+ char_string!("awhile"),+ "Use the single word `awhile` when it follows a verb.",+ ),+ );++ let b = SequenceExpr::default()+ .then_unless(UPOSSet::new(&[UPOS::VERB]))+ .t_ws()+ .t_aco("awhile");++ map.insert(+ b,+ (+ char_string!("a while"),+ "When not used after a verb, spell this duration as `a while`.",+ ),+ );++ let map = Arc::new(map);++ Self {+ expr: Box::new(map.clone()),+ map,+ }+ }+}++impl ExprLinter for AWhile {+ type Unit = Chunk;++ fn expr(&self) -> &dyn Expr {+ self.expr.as_ref()+ }++ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {+ let &(ref suggestion, message) = self.map.lookup(0, matched_tokens, source)?;+ let span = matched_tokens[2..].span()?;+ let suggestion =+ Suggestion::replace_with_match_case(suggestion.to_vec(), span.get_content(source));++ Some(Lint {+ span,+ lint_kind: LintKind::Typo,+ suggestions: vec![suggestion],+ message: message.to_owned(),+ ..Default::default()+ })+ }++ fn description(&self) -> &'static str {+ "Enforces `awhile` after verbs and `a while` everywhere else."+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::{assert_no_lints, assert_suggestion_result};++ use super::AWhile;++ #[test]+ fn allow_issue_2144() {+ assert_no_lints(+ "After thinking awhile, I decided to foo a bar.",+ AWhile::default(),+ );+ assert_no_lints(+ "After thinking for a while, I decided to foo a bar.",+ AWhile::default(),+ );+ }++ #[test]+ fn fix_issue_2144() {+ assert_suggestion_result(+ "After thinking a while, I decided to foo a bar.",+ AWhile::default(),+ "After thinking awhile, I decided to foo a bar.",+ );+ }++ #[test]+ fn correct_in_quite_a_while() {+ assert_suggestion_result(+ "I haven't seen him in quite awhile.",+ AWhile::default(),+ "I haven't seen him in quite a while.",+ );+ }++ #[test]+ fn correct_in_a_while() {+ assert_suggestion_result(+ "I haven't checked in awhile.",+ AWhile::default(),+ "I haven't checked in a while.",+ );+ }++ #[test]+ fn correct_for_awhile() {+ assert_suggestion_result(+ "Video Element Error: MEDA_ERR_DECODE when chrome is left open for awhile",+ AWhile::default(),+ "Video Element Error: MEDA_ERR_DECODE when chrome is left open for a while",+ );+ }++ #[test]+ fn correct_after_awhile() {+ assert_suggestion_result(+ "Links on portal stop working after awhile, requiring page refresh.",+ AWhile::default(),+ "Links on portal stop working after a while, requiring page refresh.",+ );+ }+}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex d618c9139..506b2bff0 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -12,6 +12,7 @@ use lru::LruCache;use serde::{Deserialize, Deserializer, Serialize, Serializer};use super::a_part::APart;+use super::a_while::AWhile;use super::addicting::Addicting;use super::adjective_double_degree::AdjectiveDoubleDegree;use super::adjective_of_a::AdjectiveOfA;@@ -497,6 +498,7 @@ impl LintGroup {insert_expr_rule!(CautionaryTale, true);insert_expr_rule!(ChangeTack, true);insert_expr_rule!(ChockFull, true);+ insert_expr_rule!(AWhile, true);insert_struct_rule!(SubjectPronoun, true);insert_struct_rule!(FindFine, true);insert_struct_rule!(CommaFixes, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex ff4ff1919..52347c41b 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -3,6 +3,7 @@//! See the [`Linter`] trait and the [documentation for authoring a rule](https://writewithharper.com/docs/contributors/author-a-rule) for more information.mod a_part;+mod a_while;mod addicting;mod adjective_double_degree;mod adjective_of_a;diff --git a/harper-core/src/linting/open_compounds.rs b/harper-core/src/linting/open_compounds.rsindex 2d0d5b5f8..f2ab6c08e 100644--- a/harper-core/src/linting/open_compounds.rs+++ b/harper-core/src/linting/open_compounds.rs@@ -15,7 +15,6 @@ impl Default for OpenCompounds {let phrases = ["a few","a lot",- "a while","as well","at least","each other",@@ -261,49 +260,6 @@ mod tests {assert_suggestion_result("Alot", OpenCompounds::default(), "A lot");}- // A while-- #[test]- fn correct_awhile_atomic() {- assert_suggestion_result("Awhile", OpenCompounds::default(), "A while");- }-- #[test]- fn test_in_quite_a_while() {- assert_suggestion_result(- "I haven’t seen him in quite awhile.",- OpenCompounds::default(),- "I haven’t seen him in quite a while.",- );- }-- #[test]- fn test_in_a_while() {- assert_suggestion_result(- "I haven't checked in awhile.",- OpenCompounds::default(),- "I haven't checked in a while.",- );- }-- #[test]- fn correct_for_awhile() {- assert_suggestion_result(- "Video Element Error: MEDA_ERR_DECODE when chrome is left open for awhile",- OpenCompounds::default(),- "Video Element Error: MEDA_ERR_DECODE when chrome is left open for a while",- );- }-- #[test]- fn correct_after_awhile() {- assert_suggestion_result(- "Links on portal stop working after awhile, requiring page refresh.",- OpenCompounds::default(),- "Links on portal stop working after a while, requiring page refresh.",- );- }-// As well#[test]