Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix(core): false positive from `ItsContraction` (#2720)
Elijah Pottera6ab10f330c4
diff --git a/harper-core/src/linting/its_contraction/general.rs b/harper-core/src/linting/its_contraction/general.rsindex f186a0308..608224ce2 100644--- a/harper-core/src/linting/its_contraction/general.rs+++ b/harper-core/src/linting/its_contraction/general.rs@@ -66,12 +66,21 @@ impl General {let offender = toks.first()?;let offender_chars = offender.span.get_content(source);- if toks.get(2)?.kind.is_upos(UPOS::VERB)+ let modifier = toks.get(2)?;++ if modifier.kind.is_upos(UPOS::VERB)&& NominalPhrase.matches(&toks[2..], source).is_some()+ && !Self::is_likely_predicative_participle(modifier, source){return None;}+ // Past-participle modifiers can be tagged as verbs even in possessive noun phrases:+ // "its abetted parameter", "its associated parameter", etc.+ if self.is_possessive_participle_noun_phrase(toks, source) {+ return None;+ }+Some(Lint {span: offender.span,lint_kind: LintKind::Punctuation,@@ -84,4 +93,42 @@ impl General {priority: 54,})}++ fn is_possessive_participle_noun_phrase(&self, toks: &[Token], source: &[char]) -> bool {+ let Some(modifier) = toks.get(2) else {+ return false;+ };+ let Some(gap) = toks.get(3) else {+ return false;+ };+ let Some(head) = toks.get(4) else {+ return false;+ };++ if !modifier.kind.is_verb_past_participle_form() || !gap.kind.is_whitespace() {+ return false;+ }++ if !(head.kind.is_noun() || head.kind.is_proper_noun()) {+ return false;+ }++ if Self::is_likely_predicative_participle(modifier, source) {+ return false;+ }++ let modifier_text = modifier.span.get_content_string(source);++ !["had", "been", "got"]+ .iter()+ .any(|word| modifier_text.eq_ignore_ascii_case(word))+ }++ fn is_likely_predicative_participle(tok: &Token, source: &[char]) -> bool {+ let text = tok.span.get_content_string(source);++ ["called", "named", "known", "termed", "titled"]+ .iter()+ .any(|word| text.eq_ignore_ascii_case(word))+ }}diff --git a/harper-core/src/linting/its_contraction/mod.rs b/harper-core/src/linting/its_contraction/mod.rsindex 50b94616e..7b45f5c11 100644--- a/harper-core/src/linting/its_contraction/mod.rs+++ b/harper-core/src/linting/its_contraction/mod.rs@@ -283,4 +283,46 @@ mod tests {0,);}++ #[test]+ fn issue_2547() {+ assert_no_lints(+ "using the foo feature and its associated parameter",+ ItsContraction::default(),+ );+ }++ #[test]+ fn ignore_past_participle_noun_phrase() {+ assert_no_lints(+ "using the foo feature and its abetted parameter",+ ItsContraction::default(),+ );+ }++ #[test]+ fn corrects_predicative_called() {+ assert_suggestion_result(+ "Its called recursion.",+ ItsContraction::default(),+ "It's called recursion.",+ );+ }++ #[test]+ fn corrects_predicative_named() {+ assert_suggestion_result(+ "Its named Manhattan.",+ ItsContraction::default(),+ "It's named Manhattan.",+ );+ }++ #[test]+ fn allows_possessive_generated_code() {+ assert_no_lints(+ "The compiler emits its generated code.",+ ItsContraction::default(),+ );+ }}