Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): add `these` vs `theses` linter (#2215)
Elijah Potterd3eb71e7e60a
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 4a06f7f4a..e85fe9e62 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -152,6 +152,7 @@ use super::the_how_why::TheHowWhy;use super::the_my::TheMy;use super::then_than::ThenThan;use super::theres::Theres;+use super::theses_these::ThesesThese;use super::thing_think::ThingThink;use super::though_thought::ThoughThought;use super::throw_away::ThrowAway;@@ -593,6 +594,7 @@ impl LintGroup {insert_struct_rule!(TheMy, true);insert_expr_rule!(ThenThan, true);insert_expr_rule!(Theres, true);+ insert_expr_rule!(ThesesThese, true);insert_expr_rule!(ThingThink, true);insert_expr_rule!(ThoughThought, true);insert_expr_rule!(ThrowAway, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex a29e538da..4f9ac4f64 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -168,6 +168,7 @@ mod the_how_why;mod the_my;mod then_than;mod theres;+mod theses_these;mod thing_think;mod though_thought;mod throw_away;@@ -340,6 +341,7 @@ pub use the_how_why::TheHowWhy;pub use the_my::TheMy;pub use then_than::ThenThan;pub use theres::Theres;+pub use theses_these::ThesesThese;pub use thing_think::ThingThink;pub use though_thought::ThoughThought;pub use throw_away::ThrowAway;diff --git a/harper-core/src/linting/theses_these.rs b/harper-core/src/linting/theses_these.rsnew file mode 100644index 000000000..7227a83ea--- /dev/null+++ b/harper-core/src/linting/theses_these.rs@@ -0,0 +1,148 @@+use super::{ExprLinter, Lint, LintKind, Suggestion};+use crate::Token;+use crate::expr::{Expr, SequenceExpr};+use crate::patterns::UPOSSet;+use harper_brill::UPOS;++pub struct ThesesThese {+ expr: Box<dyn Expr>,+}++impl Default for ThesesThese {+ fn default() -> Self {+ let expr = SequenceExpr::default()+ .t_aco("theses")+ .t_ws()+ .then(UPOSSet::new(&[UPOS::NOUN, UPOS::PROPN]));++ Self {+ expr: Box::new(expr),+ }+ }+}++impl ExprLinter for ThesesThese {+ fn expr(&self) -> &dyn Expr {+ self.expr.as_ref()+ }++ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {+ let theses_token = matched_tokens.first()?;++ let content = theses_token.span.get_content(source);++ let suggestions = vec![Suggestion::replace_with_match_case_str("these", content)];++ Some(Lint {+ span: theses_token.span,+ lint_kind: LintKind::Spelling,+ suggestions,+ message: "Did you mean `these`?".to_string(),+ priority: 1,+ })+ }++ fn description(&self) -> &'static str {+ "Corrects the common misspelling of `these` as `theses`."+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::{assert_lint_count, assert_no_lints, assert_suggestion_result};++ use super::ThesesThese;++ #[test]+ fn corrects_theses_scenes() {+ assert_suggestion_result(+ "Are theses scenes from a novel?",+ ThesesThese::default(),+ "Are these scenes from a novel?",+ );+ }++ #[test]+ fn corrects_theses_days() {+ assert_suggestion_result(+ "That's why the two countries look as they do theses days.",+ ThesesThese::default(),+ "That's why the two countries look as they do these days.",+ );+ }++ #[test]+ fn allows_correct_theses() {+ assert_no_lints(+ "There are universities that are dedicated just to this field, thousands of people doing theses on Picasso, for example.",+ ThesesThese::default(),+ );+ }++ #[test]+ fn allows_theses_followed_by_verb() {+ assert_no_lints(+ "Theses are the times that try men's souls.",+ ThesesThese::default(),+ );+ }++ #[test]+ fn works_with_capitalization() {+ assert_suggestion_result(+ "THESES BOOKS ARE GREAT.",+ ThesesThese::default(),+ "THESE BOOKS ARE GREAT.",+ );+ }++ #[test]+ fn works_with_mixed_capitalization() {+ assert_suggestion_result(+ "Theses Books Are My Favorite.",+ ThesesThese::default(),+ "These Books Are My Favorite.",+ );+ }++ #[test]+ fn simple_case() {+ assert_suggestion_result(+ "I like theses apples.",+ ThesesThese::default(),+ "I like these apples.",+ );+ }++ #[test]+ fn with_punctuation() {+ assert_no_lints("Are theses, books good?", ThesesThese::default());+ }++ #[test]+ fn in_the_middle_of_sentence() {+ assert_suggestion_result(+ "I saw theses movies yesterday.",+ ThesesThese::default(),+ "I saw these movies yesterday.",+ );+ }++ #[test]+ fn another_example() {+ assert_lint_count("I have theses books.", ThesesThese::default(), 1);+ }++ #[test]+ fn allows_band_name() {+ assert_no_lints("Theses are a great band.", ThesesThese::default());+ }++ #[test]+ fn does_not_correct_valid_theses() {+ assert_no_lints(+ "She wrote multiple theses on the topic.",+ ThesesThese::default(),+ );+ }+}