Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): created initial draft of genitive case linter for #240
Elijah Potter153888e3142b
diff --git a/harper-core/affixes.json b/harper-core/affixes.jsonindex 5c257dbba..61198d498 100644--- a/harper-core/affixes.json+++ b/harper-core/affixes.json@@ -553,8 +553,7 @@"adds_metadata": {},"gifts_metadata": {"noun": {- "is_pronoun": true,- "is_common": true+ "is_pronoun": true}}},diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dictindex 386e0d072..57842e29b 100644--- a/harper-core/dictionary.dict+++ b/harper-core/dictionary.dict@@ -12510,7 +12510,7 @@ ardent/5Yardor/1MSarduous/5YParduousness/1M-are/41SMB+are/4SBarea/1SMareal/5aren't/4@@ -45508,7 +45508,8 @@ therapeutically/therapeutics/1Mtherapist/1SMtherapy/14SM-there/18M~+there/~+there'sthereabout/Sthereafter/1thereat/diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex fbbc79f79..7c1106e5d 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -19,6 +19,7 @@ use super::spell_check::SpellCheck;use super::spelled_numbers::SpelledNumbers;use super::terminating_conjunctions::TerminatingConjunctions;use super::unclosed_quotes::UnclosedQuotes;+use super::use_genitive::UseGenitive;use super::wrong_quotes::WrongQuotes;use super::{Lint, Linter};use crate::{Dictionary, Document};@@ -127,7 +128,8 @@ create_lint_group_config!(TerminatingConjunctions => true,EllipsisLength => true,DotInitialisms => true,- BoringWords => false+ BoringWords => false,+ UseGenitive => true);impl<T: Dictionary + Default> Default for LintGroup<T> {diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 78f74039a..f05f1225a 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -19,6 +19,7 @@ mod spell_check;mod spelled_numbers;mod terminating_conjunctions;mod unclosed_quotes;+mod use_genitive;mod wrong_quotes;pub use an_a::AnA;@@ -42,6 +43,7 @@ pub use spell_check::SpellCheck;pub use spelled_numbers::SpelledNumbers;pub use terminating_conjunctions::TerminatingConjunctions;pub use unclosed_quotes::UnclosedQuotes;+pub use use_genitive::UseGenitive;pub use wrong_quotes::WrongQuotes;use crate::Document;diff --git a/harper-core/src/linting/use_genitive.rs b/harper-core/src/linting/use_genitive.rsnew file mode 100644index 000000000..8d0635000--- /dev/null+++ b/harper-core/src/linting/use_genitive.rs@@ -0,0 +1,123 @@+use crate::linting::{LintKind, PatternLinter, Suggestion};+use crate::patterns::{EitherPattern, Pattern, SequencePattern, WordPatternGroup};+use crate::{Lint, Lrc, Token};++// Looks for places where the genitive case _isn't_ being used, and should be.+pub struct UseGenitive {+ pattern: Box<dyn Pattern>,+}++impl UseGenitive {+ fn new() -> Self {+ // Define the environment in which the genitive case should be used in.+ let environment = Lrc::new(SequencePattern::default().then_whitespace().then(Box::new(+ EitherPattern::new(vec![+ Box::new(+ SequencePattern::default()+ .then_one_or_more_adjectives()+ .then_whitespace()+ .then_noun(),+ ),+ Box::new(SequencePattern::default().then_noun()),+ ]),+ )));++ let trigger_words = ["there", "they're"];++ let mut pattern = WordPatternGroup::default();++ for word in trigger_words {+ pattern.add(+ word,+ Box::new(+ SequencePattern::default()+ .then_exact_word(word)+ .then(Box::new(environment.clone())),+ ),+ )+ }++ Self {+ pattern: Box::new(pattern),+ }+ }+}++impl PatternLinter for UseGenitive {+ fn pattern(&self) -> &dyn crate::patterns::Pattern {+ self.pattern.as_ref()+ }++ fn match_to_lint(&self, matched_tokens: &[Token], _source: &[char]) -> Lint {+ Lint {+ span: matched_tokens[0].span,+ lint_kind: LintKind::Miscellaneous,+ suggestions: vec![Suggestion::ReplaceWith(vec!['t', 'h', 'e', 'i', 'r'])],+ message: "Use the genitive case.".to_string(),+ priority: 31,+ }+ }+}++impl Default for UseGenitive {+ fn default() -> Self {+ Self::new()+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};++ use super::UseGenitive;++ #[test]+ fn catches_adjective_noun() {+ assert_suggestion_result(+ "What are there big problems?",+ UseGenitive::default(),+ "What are their big problems?",+ )+ }++ #[test]+ fn catches_just_noun() {+ assert_suggestion_result(+ "What are there problems?",+ UseGenitive::default(),+ "What are their problems?",+ )+ }++ #[test]+ fn allows_clause_termination() {+ assert_lint_count("Look there!", UseGenitive::default(), 0)+ }++ #[test]+ fn allows_there_are() {+ assert_lint_count(+ "Since there are people here, we should be socially aware.",+ UseGenitive::default(),+ 0,+ )+ }++ #[test]+ fn allows_there_at_beginning() {+ assert_lint_count(+ "There is a cute cat sitting on the chair at home.",+ UseGenitive::default(),+ 0,+ )+ }++ #[test]+ fn catches_they_are() {+ assert_suggestion_result(+ "The students received they're test results today.",+ UseGenitive::default(),+ "The students received their test results today.",+ )+ }+}diff --git a/harper-core/src/patterns/either_pattern.rs b/harper-core/src/patterns/either_pattern.rsnew file mode 100644index 000000000..c0ed7e1a7--- /dev/null+++ b/harper-core/src/patterns/either_pattern.rs@@ -0,0 +1,29 @@+use crate::Token;++use super::Pattern;++/// A pattern that returns the value of the first non-zero match in a list.+#[derive(Default)]+pub struct EitherPattern {+ patterns: Vec<Box<dyn Pattern>>,+}++impl EitherPattern {+ pub fn new(patterns: Vec<Box<dyn Pattern>>) -> Self {+ Self { patterns }+ }+}++impl Pattern for EitherPattern {+ fn matches(&self, tokens: &[Token], source: &[char]) -> usize {+ for pattern in self.patterns.iter() {+ let match_len = pattern.matches(tokens, source);++ if match_len > 0 {+ return match_len;+ }+ }++ 0+ }+}diff --git a/harper-core/src/patterns/mod.rs b/harper-core/src/patterns/mod.rsindex 799ab5477..82cf04b0c 100644--- a/harper-core/src/patterns/mod.rs+++ b/harper-core/src/patterns/mod.rs@@ -4,6 +4,7 @@ use crate::{Span, Token, VecExt};mod any_pattern;mod consumes_remaining_pattern;+mod either_pattern;mod naive_pattern_group;mod repeating_pattern;mod sequence_pattern;@@ -12,7 +13,9 @@ mod whitespace_pattern;mod word_pattern_group;pub use any_pattern::AnyPattern;+use blanket::blanket;pub use consumes_remaining_pattern::ConsumesRemainingPattern;+pub use either_pattern::EitherPattern;pub use naive_pattern_group::NaivePatternGroup;pub use repeating_pattern::RepeatingPattern;pub use sequence_pattern::SequencePattern;@@ -21,11 +24,13 @@ pub use whitespace_pattern::WhitespacePattern;pub use word_pattern_group::WordPatternGroup;#[cfg(not(feature = "concurrent"))]+#[blanket(derive(Rc, Arc))]pub trait Pattern {fn matches(&self, tokens: &[Token], source: &[char]) -> usize;}#[cfg(feature = "concurrent")]+#[blanket(derive(Arc))]pub trait Pattern: Send + Sync {fn matches(&self, tokens: &[Token], source: &[char]) -> usize;}diff --git a/harper-core/src/patterns/sequence_pattern.rs b/harper-core/src/patterns/sequence_pattern.rsindex c452a9f2b..8d0da98b5 100644--- a/harper-core/src/patterns/sequence_pattern.rs+++ b/harper-core/src/patterns/sequence_pattern.rs@@ -21,6 +21,12 @@ macro_rules! gen_then_from_is {self}++ pub fn [< then_one_or_more_$quality s >] (self) -> Self{+ self.then_one_or_more(Box::new(|tok: &Token, _source: &[char]| {+ tok.kind.[< is_$quality >]()+ }))+ }}};}@@ -35,6 +41,8 @@ impl SequencePattern {gen_then_from_is!(comma);gen_then_from_is!(period);gen_then_from_is!(case_separator);+ gen_then_from_is!(adverb);+ gen_then_from_is!(adjective);pub fn then_exact_word(mut self, word: &'static str) -> Self {self.token_patterns@@ -102,6 +110,11 @@ impl SequencePattern {.push(Box::new(RepeatingPattern::new(pat)));self}++ pub fn then(mut self, pat: Box<dyn Pattern>) -> Self {+ self.token_patterns.push(pat);+ self+ }}impl Pattern for SequencePattern {diff --git a/harper-core/src/token.rs b/harper-core/src/token.rsindex d6455f96d..c4316ba23 100644--- a/harper-core/src/token.rs+++ b/harper-core/src/token.rs@@ -130,6 +130,26 @@ impl TokenKind {matches!(self, TokenKind::Punctuation(Punctuation::Ellipsis))}+ pub fn is_adjective(&self) -> bool {+ matches!(+ self,+ TokenKind::Word(WordMetadata {+ adjective: Some(_),+ ..+ })+ )+ }++ pub fn is_adverb(&self) -> bool {+ matches!(+ self,+ TokenKind::Word(WordMetadata {+ adverb: Some(_),+ ..+ })+ )+ }+pub fn is_swear(&self) -> bool {matches!(self,