Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix(core): support progressive/perfect forms after "its" + 1 (#1469)
Dongsung "Donny" Kim740f255d9652
diff --git a/harper-core/src/linting/its_contraction.rs b/harper-core/src/linting/its_contraction.rsindex 173c82c54..bc921c9c5 100644--- a/harper-core/src/linting/its_contraction.rs+++ b/harper-core/src/linting/its_contraction.rs@@ -1,7 +1,9 @@use harper_brill::UPOS;+use crate::TokenStringExt;use crate::expr::All;use crate::expr::Expr;+use crate::expr::ExprExt;use crate::expr::OwnedExprExt;use crate::expr::SequenceExpr;use crate::patterns::NominalPhrase;@@ -9,8 +11,8 @@ use crate::patterns::Pattern;use crate::patterns::UPOSSet;use crate::patterns::WordSet;use crate::{- Token,- linting::{ExprLinter, Lint, LintKind, Suggestion},+ Document, Token,+ linting::{Lint, LintKind, Linter, Suggestion},};pub struct ItsContraction {@@ -44,16 +46,35 @@ impl Default for ItsContraction {}}-impl ExprLinter for ItsContraction {- fn expr(&self) -> &dyn Expr {- self.expr.as_ref()+impl Linter for ItsContraction {+ fn lint(&mut self, document: &Document) -> Vec<Lint> {+ let mut lints = Vec::new();+ let source = document.get_source();++ for chunk in document.iter_chunks() {+ lints.extend(+ self.expr+ .iter_matches(chunk, source)+ .filter_map(|match_span| {+ self.match_to_lint(&chunk[match_span.start..], source)+ }),+ );+ }++ lints+ }++ fn description(&self) -> &str {+ "Detects the possessive `its` before `had`, `been`, or `got` and offers `it's` or `it has`."}+}+impl ItsContraction {fn match_to_lint(&self, toks: &[Token], source: &[char]) -> Option<Lint> {let offender = toks.first()?;let offender_chars = offender.span.get_content(source);- if !toks.get(2)?.kind.is_upos(UPOS::AUX)+ if toks.get(2)?.kind.is_upos(UPOS::VERB)&& NominalPhrase.matches(&toks[2..], source).is_some(){return None;@@ -71,10 +92,6 @@ impl ExprLinter for ItsContraction {priority: 54,})}-- fn description(&self) -> &str {- "Detects the possessive `its` before `had`, `been`, or `got` and offers `it's` or `it has`."- }}#[cfg(test)]@@ -153,4 +170,31 @@ mod tests {"It's a nice day.",);}++ #[test]+ fn ignore_nominal_progressive() {+ assert_lint_count(+ "The class preserves its existing properties.",+ ItsContraction::default(),+ 0,+ );+ }++ #[test]+ fn ignore_nominal_perfect() {+ assert_lint_count(+ "The robot followed its predetermined route.",+ ItsContraction::default(),+ 0,+ );+ }++ #[test]+ fn ignore_nominal_long() {+ assert_lint_count(+ "I think of its exploding marvelous spectacular output.",+ ItsContraction::default(),+ 0,+ );+ }}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 6bb512be1..662bb87b2 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -403,7 +403,7 @@ impl LintGroup {insert_struct_rule!(HopHope, true);insert_struct_rule!(HowTo, true);insert_pattern_rule!(HyphenateNumberDay, true);- insert_pattern_rule!(ItsContraction, true);+ insert_struct_rule!(ItsContraction, true);insert_pattern_rule!(LeftRightHand, true);insert_pattern_rule!(LessWorse, true);insert_struct_rule!(LetsConfusion, true);diff --git a/harper-core/src/patterns/nominal_phrase.rs b/harper-core/src/patterns/nominal_phrase.rsindex 4b260ca5e..70135fd74 100644--- a/harper-core/src/patterns/nominal_phrase.rs+++ b/harper-core/src/patterns/nominal_phrase.rs@@ -12,15 +12,16 @@ impl Pattern for NominalPhrase {loop {let tok = tokens.get(cursor)?;- if tok.kind.is_adjective() || tok.kind.is_determiner() {- let next = tokens.get(cursor + 1)?;-- if !next.kind.is_whitespace() {- return None;+ if tok.kind.is_adjective()+ || tok.kind.is_determiner()+ || tok.kind.is_verb_progressive_form()+ {+ if let Some(next) = tokens.get(cursor + 1) {+ if next.kind.is_whitespace() {+ cursor += 2;+ continue;+ }}-- cursor += 2;- continue;}if tok.kind.is_nominal() {@@ -112,4 +113,34 @@ mod tests {vec!["My favorite foods", "pizza", "sushi", "tacos", "burgers"])}++ #[test]+ fn simplest_way() {+ let doc = Document::new_markdown_default_curated("a way");+ assert!(+ NominalPhrase+ .matches(doc.get_tokens(), doc.get_source())+ .is_some()+ );+ }++ #[test]+ fn progressive_way() {+ let doc = Document::new_markdown_default_curated("a winning way");+ assert!(+ NominalPhrase+ .matches(doc.get_tokens(), doc.get_source())+ .is_some()+ );+ }++ #[test]+ fn perfect_way() {+ let doc = Document::new_markdown_default_curated("a failed way");+ assert!(+ NominalPhrase+ .matches(doc.get_tokens(), doc.get_source())+ .is_some()+ );+ }}diff --git a/harper-core/tests/run_tests.rs b/harper-core/tests/run_tests.rsindex 71ae532be..536b63220 100644--- a/harper-core/tests/run_tests.rs+++ b/harper-core/tests/run_tests.rs@@ -85,7 +85,7 @@ create_test!(pr_452.md, 2, Dialect::American);create_test!(hex_basic_clean.md, 0, Dialect::American);create_test!(hex_basic_dirty.md, 1, Dialect::American);create_test!(misc_closed_compound_clean.md, 0, Dialect::American);-create_test!(yogurt_british_clean.md, 1, Dialect::British);+create_test!(yogurt_british_clean.md, 0, Dialect::British);// Make sure it doesn't paniccreate_test!(lukas_homework.md, 3, Dialect::American);diff --git a/harper-core/tests/text/linters/The Great Gatsby.snap.yml b/harper-core/tests/text/linters/The Great Gatsby.snap.ymlindex d1064cf44..7d99ab23d 100644--- a/harper-core/tests/text/linters/The Great Gatsby.snap.yml+++ b/harper-core/tests/text/linters/The Great Gatsby.snap.yml@@ -7549,16 +7549,6 @@ Suggest:-Lint: WordChoice (54 priority)-Message: |- 5814 | green breast of the new world. Its vanished trees, the trees that had made way- | ^~~ Use `it's` (short for `it has` or `it is`) here, not the possessive `its`.-Suggest:- - Replace with: “It's”- - Replace with: “It has”---Lint: Readability (127 priority)Message: |5814 | green breast of the new world. Its vanished trees, the trees that had made way