Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix(core): `VerbToAdjective` false positives (#2906)
Elijah Potter2aad037fef6b
diff --git a/harper-core/src/linting/verb_to_adjective.rs b/harper-core/src/linting/verb_to_adjective.rsindex 9415e7a7a..2d4e6807e 100644--- a/harper-core/src/linting/verb_to_adjective.rs+++ b/harper-core/src/linting/verb_to_adjective.rs@@ -1,9 +1,5 @@-use harper_brill::UPOS;--use crate::expr::All;use crate::expr::Expr;use crate::expr::SequenceExpr;-use crate::patterns::UPOSSet;use crate::patterns::WordSet;use crate::{Token, TokenStringExt};@@ -18,22 +14,14 @@ impl Default for VerbToAdjective {fn default() -> Self {let expr = SequenceExpr::word_set(&["the", "a", "an"]).t_ws()- .then_kind_where(|kind| {- (kind.is_verb()- && !kind.is_verb_past_form()- && !kind.is_adjective()- && !kind.is_noun())- || kind.is_degree_adverb()- })+ .then_kind_where(|kind| kind.is_degree_adverb()).t_ws()- .then(UPOSSet::new(&[UPOS::NOUN, UPOS::PROPN]));-- let exceptions = SequenceExpr::anything()- .t_any()- .then_unless(WordSet::new(&["very"]));+ .then_kind_where(|kind| kind.is_noun() && kind.is_verb_progressive_form())+ .t_ws()+ .then(WordSet::new(&["of"]));Self {- expr: Box::new(All::new(vec![Box::new(expr), Box::new(exceptions)])),+ expr: Box::new(expr),}}}@@ -45,86 +33,115 @@ impl ExprLinter for VerbToAdjective {self.expr.as_ref()}- fn match_to_lint(&self, matched_tokens: &[Token], _source: &[char]) -> Option<Lint> {+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {+ let words: Vec<_> = matched_tokens+ .iter()+ .filter(|tok| tok.kind.is_word())+ .collect();+ let [_, adverb, noun, _] = words.as_slice() else {+ return None;+ };+Some(Lint {span: matched_tokens.span()?,lint_kind: LintKind::Typo,suggestions: vec![],- message: "Did you intend to use an adjective here?".to_owned(),+ message: format!(+ "`{}` is an adverb. Before the noun `{}`, this phrase more likely needs an adjective.",+ adverb.span.get_content_string(source),+ noun.span.get_content_string(source),+ ),priority: 31,})}fn description(&self) -> &'static str {- "Looks for situations where a verb was written where an adjective is often intended."+ "Looks for article-led gerund noun phrases like `a fully accounting of`, where an adjective is more likely than an adverb."}}#[cfg(test)]mod tests {- use crate::linting::tests::{assert_lint_count, assert_no_lints};+ use crate::linting::tests::{assert_lint_count, assert_lint_message, assert_no_lints};use super::VerbToAdjective;- #[test]- fn fully_accounting() {- assert_lint_count(- "By scheduling time to do a fully accounting of where your CPU cycles are going, you can preemptively save yourself (and your contributors) a lot of time.",- VerbToAdjective::default(),- 1,- );+ fn assert_each_lints(cases: &[&str]) {+ for case in cases {+ assert_lint_count(case, VerbToAdjective::default(), 1);+ }}- #[test]- fn new_car_is_valid() {- assert_no_lints("I really like my new car.", VerbToAdjective::default());+ fn assert_each_no_lints(cases: &[&str]) {+ for case in cases {+ assert_no_lints(case, VerbToAdjective::default());+ }}#[test]- fn new_sentence_is_valid() {- assert_no_lints(- "I want you to write a new sentence for me.",- VerbToAdjective::default(),- );+ fn flags_issue_2855_and_similar_gerund_noun_phrases() {+ assert_each_lints(&[+ "By scheduling time to do a fully accounting of where your CPU cycles are going, you can preemptively save yourself (and your contributors) a lot of time.",+ "We need a fully understanding of the risk before launch.",+ "The mockup offered a fully rendering of the scene.",+ "An entirely recording of the hearing was never released.",+ "The appendix contained a completely listing of every exception.",+ "The report ended with a highly framing of the debate.",+ "A fully mapping of the cave is still useful to explorers.",+ "The board requested a truly accounting of the losses.",+ ]);}#[test]- fn correct_term_is_valid() {- assert_no_lints(- "Ensure the correct term is used for individuals residing abroad.",+ fn message_points_to_the_adverb_and_noun() {+ assert_lint_message(+ "We need a fully understanding of the risk before launch.",VerbToAdjective::default(),+ "`fully` is an adverb. Before the noun `understanding`, this phrase more likely needs an adjective.",);}#[test]- fn causes_amazement_is_valid() {- assert_no_lints(- "It is something that causes amazement.",- VerbToAdjective::default(),- );+ fn allows_the_issue_2855_regressions() {+ assert_each_no_lints(&[+ "South Korea Set To Get a Fully Functioning Google Maps integration.",+ "This allows our clients to embrace a truly data-driven approach.",+ "Before he could press the transmit button, the sphere emitted a high-pitched whine.",+ "The target of coordinated, algorithmic pursuit kept moving.",+ "A fully functioning prototype shipped yesterday.",+ "A truly remarkable outcome followed.",+ "A completely different problem remains.",+ "A fully rendered image loaded instantly.",+ ]);}#[test]- fn correct_auxiliary_is_valid() {- assert_no_lints(- "Can you suggest a correct auxiliary?",- VerbToAdjective::default(),- );+ fn allows_correct_adjective_plus_gerund_noun_of_phrases() {+ assert_each_no_lints(&[+ "We need a full understanding of the risk before launch.",+ "The audit delivered a full accounting of the losses.",+ "The appendix included a complete listing of the requirements.",+ "The demo provided a detailed rendering of the scene.",+ "They archived a proper recording of the interview.",+ "The memo offered a clear framing of the issue.",+ "The atlas provides a partial mapping of the cave.",+ "The exhibit included the recording of the speech.",+ ]);}#[test]- fn submitted_form_data_is_valid() {- assert_no_lints(+ fn allows_nonmatching_determiner_phrases_and_previous_regressions() {+ assert_each_no_lints(&[+ "I really like my new car.",+ "I want you to write a new sentence for me.",+ "Ensure the correct term is used for individuals residing abroad.",+ "It is something that causes amazement.",+ "Can you suggest a correct auxiliary?","This is the email address that will receive the submitted form data.",- VerbToAdjective::default(),- );- }-- #[test]- fn the_unexplored_territories_is_valid() {- assert_no_lints("Not the unexplored territories, ripe for discovery, but the areas actively erased, obscured, or simply deemed unworthy of representation?",- VerbToAdjective::default(),- );+ "A local mapping service is sufficient here.",+ "The transmit queue drained normally.",+ "A mostly accurate summary is still useful.",+ ]);}}diff --git a/harper-core/tests/text/linters/The Great Gatsby.snap.yml b/harper-core/tests/text/linters/The Great Gatsby.snap.ymlindex 4a59ea0ab..e4b5a7d38 100644--- a/harper-core/tests/text/linters/The Great Gatsby.snap.yml+++ b/harper-core/tests/text/linters/The Great Gatsby.snap.yml@@ -3518,14 +3518,6 @@ Suggest:-Lint: Typo (31 priority)-Message: |- 2286 | (said Jordan Baker that afternoon, sitting up very straight on a straight chair- | ^~~~~~~~~~~~~~~~ Did you intend to use an adjective here?- 2287 | in the tea-garden at the Plaza Hotel)---Lint: Readability (127 priority)Message: |2291 | with rubber nobs on the soles that bit into the soft ground. I had on a new