Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix(harper-core): keep possessive its unflagged after prepositions (#4437)
JS0759a1b65911
diff --git a/harper-core/src/linting/its_contraction/general.rs b/harper-core/src/linting/its_contraction/general.rsindex d320f958f..6c28a8778 100644--- a/harper-core/src/linting/its_contraction/general.rs+++ b/harper-core/src/linting/its_contraction/general.rs@@ -54,7 +54,11 @@ impl Linter for General {self.expr.iter_matches(chunk, source).filter_map(|match_span| {- self.match_to_lint(&chunk[match_span.start..], source)+ let preceding_token = chunk[..match_span.start]+ .iter()+ .rev()+ .find(|tok| !tok.kind.is_whitespace());+ self.match_to_lint(&chunk[match_span.start..], preceding_token, source)}),);}@@ -68,7 +72,12 @@ impl Linter for General {}impl General {- fn match_to_lint(&self, toks: &[Token], source: &[char]) -> Option<Lint> {+ fn match_to_lint(+ &self,+ toks: &[Token],+ preceding_token: Option<&Token>,+ source: &[char],+ ) -> Option<Lint> {let offender = toks.first()?;let offender_chars = offender.get_ch(source);@@ -115,6 +124,15 @@ impl General {"had", "been", "got", "called", "named", "known", "termed", "titled",];+ // A possessive `its` introduced by a preposition is part of a noun phrase, even+ // when the next word is tagged as a verb: "in its reading", "of its making".+ if modifier.kind.is_upos(UPOS::VERB)+ && !strong_predicative_verbs.contains(&modifier_lower.as_str())+ && preceding_token.is_some_and(|tok| tok.kind.is_preposition())+ {+ return None;+ }+let should_consider = if exact_contraction_words.contains(&modifier_lower.as_str())|| determiner_like_words.contains(&modifier_lower.as_str()){diff --git a/harper-core/src/linting/its_contraction/mod.rs b/harper-core/src/linting/its_contraction/mod.rsindex a8ecff46e..e420c8758 100644--- a/harper-core/src/linting/its_contraction/mod.rs+++ b/harper-core/src/linting/its_contraction/mod.rs@@ -462,4 +462,28 @@ mod tests {fn allows_its_starting_level() {assert_no_lints("Reduce the tracker to its starting level.", test_linter());}++ #[test]+ fn issue_4435() {+ assert_no_lints(+ "I spent more effort in its reading than he did in its creation.",+ test_linter(),+ );+ }++ #[test]+ fn allows_its_gerund_after_preposition() {+ assert_no_lints(+ "The value comes from its measuring of the output.",+ test_linter(),+ );+ }++ #[test]+ fn allows_its_gerund_after_unlisted_preposition() {+ assert_no_lints(+ "I spent more effort during its reading than he did during its creation.",+ test_linter(),+ );+ }}