Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix(#267): ignore cases preceded by an adjective or an exception
Elijah Potterc800367b930e
diff --git a/harper-core/src/linting/use_genitive.rs b/harper-core/src/linting/use_genitive.rsindex 8d0635000..7c7abf996 100644--- a/harper-core/src/linting/use_genitive.rs+++ b/harper-core/src/linting/use_genitive.rs@@ -1,5 +1,5 @@use crate::linting::{LintKind, PatternLinter, Suggestion};-use crate::patterns::{EitherPattern, Pattern, SequencePattern, WordPatternGroup};+use crate::patterns::{EitherPattern, Invert, Pattern, SequencePattern, WordPatternGroup};use crate::{Lint, Lrc, Token};// Looks for places where the genitive case _isn't_ being used, and should be.@@ -9,25 +9,25 @@ pub struct UseGenitive {impl UseGenitive {fn new() -> Self {- // Define the environment in which the genitive case should be used in.+ // Define the environment in 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()),- ]),+ 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();+ let mut primary_pattern = WordPatternGroup::default();for word in trigger_words {- pattern.add(+ primary_pattern.add(word,Box::new(SequencePattern::default()@@ -37,8 +37,18 @@ impl UseGenitive {)}+ // Add a prelude to remove false-positives.+ let full_pattern = SequencePattern::default()+ .then(Box::new(Invert::new(Box::new(EitherPattern::new(vec![+ Box::new(SequencePattern::default().then_exact_word_or_lowercase("Is")),+ Box::new(SequencePattern::default().then_exact_word_or_lowercase("Were")),+ Box::new(SequencePattern::default().then_adjective()),+ ])))))+ .then_whitespace()+ .then(Box::new(primary_pattern));+Self {- pattern: Box::new(pattern),+ pattern: Box::new(full_pattern),}}}@@ -50,7 +60,7 @@ impl PatternLinter for UseGenitive {fn match_to_lint(&self, matched_tokens: &[Token], _source: &[char]) -> Lint {Lint {- span: matched_tokens[0].span,+ span: matched_tokens[2].span,lint_kind: LintKind::Miscellaneous,suggestions: vec![Suggestion::ReplaceWith(vec!['t', 'h', 'e', 'i', 'r'])],message: "Use the genitive case.".to_string(),@@ -120,4 +130,45 @@ mod tests {"The students received their test results today.",)}++ #[test]+ fn allows_grantlemons_issue_267_cat() {+ assert_lint_count("Were there cats at her house?", UseGenitive::default(), 0);+ }++ #[test]+ fn allows_grantlemons_issue_267_apple() {+ assert_lint_count(+ "Were there any apples at the store?",+ UseGenitive::default(),+ 0,+ );+ }++ #[test]+ fn allows_grantlemons_issue_267_fruit() {+ assert_lint_count(+ "Were there many kinds of fruit at the store?",+ UseGenitive::default(),+ 0,+ );+ }++ #[test]+ fn allows_grantlemons_issue_267_people() {+ assert_lint_count(+ "Were there more than, or less than six people at the party?",+ UseGenitive::default(),+ 0,+ );+ }++ #[test]+ fn allows_faster_at_running() {+ assert_lint_count(+ "Melissa was faster at running than her friend.",+ UseGenitive::default(),+ 0,+ );+ }}diff --git a/harper-core/src/patterns/invert.rs b/harper-core/src/patterns/invert.rsnew file mode 100644index 000000000..7a1878032--- /dev/null+++ b/harper-core/src/patterns/invert.rs@@ -0,0 +1,24 @@+use crate::Token;++use super::Pattern;++/// A struct that matches any pattern __except__ the one provided.+pub struct Invert {+ inner: Box<dyn Pattern>,+}++impl Invert {+ pub fn new(inner: Box<dyn Pattern>) -> Self {+ Self { inner }+ }+}++impl Pattern for Invert {+ fn matches(&self, tokens: &[Token], source: &[char]) -> usize {+ if self.inner.matches(tokens, source) != 0 {+ 0+ } else {+ 1+ }+ }+}diff --git a/harper-core/src/patterns/mod.rs b/harper-core/src/patterns/mod.rsindex 82cf04b0c..dc6b2edf7 100644--- a/harper-core/src/patterns/mod.rs+++ b/harper-core/src/patterns/mod.rs@@ -5,6 +5,7 @@ use crate::{Span, Token, VecExt};mod any_pattern;mod consumes_remaining_pattern;mod either_pattern;+mod invert;mod naive_pattern_group;mod repeating_pattern;mod sequence_pattern;@@ -16,6 +17,7 @@ pub use any_pattern::AnyPattern;use blanket::blanket;pub use consumes_remaining_pattern::ConsumesRemainingPattern;pub use either_pattern::EitherPattern;+pub use invert::Invert;pub use naive_pattern_group::NaivePatternGroup;pub use repeating_pattern::RepeatingPattern;pub use sequence_pattern::SequencePattern;diff --git a/harper-core/tests/run_tests.rs b/harper-core/tests/run_tests.rsindex 058b039d7..2832132f6 100644--- a/harper-core/tests/run_tests.rs+++ b/harper-core/tests/run_tests.rs@@ -42,3 +42,4 @@ create_test!(issue_109.md, 0);create_test!(issue_109_ext.md, 0);create_test!(chinese_lorem_ipsum.md, 2);create_test!(obsidian_links.md, 2);+create_test!(issue_267.md, 0);diff --git a/harper-core/tests/test_sources/issue_267.md b/harper-core/tests/test_sources/issue_267.mdnew file mode 100644index 000000000..ade8f13ed--- /dev/null+++ b/harper-core/tests/test_sources/issue_267.md@@ -0,0 +1 @@+<p id="question">What did you learn from the assignment? Were there any special insights you had? What did you find that you already knew?</p>