Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: handle some false positives in let/lets/let's/let us
hippietrailc915391dcd75
diff --git a/harper-core/src/linting/compound_nouns/general_compound_nouns.rs b/harper-core/src/linting/compound_nouns/general_compound_nouns.rsindex 7a50fd897..d05bcaa85 100644--- a/harper-core/src/linting/compound_nouns/general_compound_nouns.rs+++ b/harper-core/src/linting/compound_nouns/general_compound_nouns.rs@@ -11,15 +11,20 @@ use crate::{patterns::{Pattern, SequencePattern},};-/// Covers the general cases of accidentally split compound nouns.+/// Two adjacent words separated by whitespace that if joined would be a valid noun.pub struct GeneralCompoundNouns {pattern: Box<dyn Pattern>,split_pattern: Lrc<SplitCompoundWord>,}+// This heuristic identifies potential compound nouns by:+// 1. Looking for a determiner or adjective (e.g., "a", "big", "red")+// 2. Followed by two content words (not determiners, adverbs, or prepositions)+// 3. Finally, checking if the combination forms a noun in the dictionary+// that is not also an adjectiveimpl Default for GeneralCompoundNouns {fn default() -> Self {- let exceptions_pattern = SequencePattern::default()+ let context_pattern = SequencePattern::default().then(|tok: &Token, _: &[char]| {let Some(Some(meta)) = tok.kind.as_word() else {return false;@@ -44,22 +49,22 @@ impl Default for GeneralCompoundNouns {tok.span.len() > 1 && !meta.determiner && !meta.is_adverb() && !meta.preposition});- let split_pattern = Lrc::new(SplitCompoundWord::new(|meta| {- meta.is_nominal() && !meta.is_adjective()+ let compound_pattern = Lrc::new(SplitCompoundWord::new(|meta| {+ meta.is_noun() && !meta.is_adjective()}));let mut pattern = All::default();- pattern.add(Box::new(exceptions_pattern));+ pattern.add(Box::new(context_pattern));pattern.add(Box::new(SequencePattern::default().then_anything().then_anything()- .then(split_pattern.clone()),+ .then(compound_pattern.clone()),));Self {pattern: Box::new(pattern),- split_pattern,+ split_pattern: compound_pattern,}}}diff --git a/harper-core/src/linting/compound_nouns/implied_ownership_compound_nouns.rs b/harper-core/src/linting/compound_nouns/implied_ownership_compound_nouns.rsindex 9c476d47b..707de606e 100644--- a/harper-core/src/linting/compound_nouns/implied_ownership_compound_nouns.rs+++ b/harper-core/src/linting/compound_nouns/implied_ownership_compound_nouns.rs@@ -11,6 +11,11 @@ use crate::{/// Looks for closed compound nouns which can be condensed due to their position after a/// possessive noun (which implies ownership).+/// See also:+/// harper-core/src/linting/lets_confusion/mod.rs+/// harper-core/src/linting/lets_confusion/let_us_redundancy.rs+/// harper-core/src/linting/lets_confusion/no_contraction_with_verb.rs+/// harper-core/src/linting/pronoun_contraction/should_contract.rspub struct ImpliedOwnershipCompoundNouns {pattern: Box<dyn Pattern>,split_pattern: Lrc<SplitCompoundWord>,@@ -38,9 +43,14 @@ impl PatternLinter for ImpliedOwnershipCompoundNouns {fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {// "Let's" can technically be a possessive noun (of a lease, or a let in tennis, etc.)- // but in practice it's almost always a contraction of "let us" before a verb.- let possessive = matched_tokens[0].span.get_content(source);- if possessive == ['l', 'e', 't', '\'', 's'] || possessive == ['L', 'e', 't', '\'', 's'] {+ // but in practice it's almost always a contraction of "let us" before a verb+ // or a mistake for "lets", the 3rd person singular present form of "to let".+ let word_apostrophe_s = matched_tokens[0].span.get_content(source);+ if word_apostrophe_s+ .iter()+ .map(|&c| c.to_ascii_lowercase())+ .eq(['l', 'e', 't', '\'', 's'].iter().copied())+ {return None;}let span = matched_tokens[2..].span()?;diff --git a/harper-core/src/linting/lets_confusion/let_us_redundancy.rs b/harper-core/src/linting/lets_confusion/let_us_redundancy.rsindex 3f959865d..35dee917e 100644--- a/harper-core/src/linting/lets_confusion/let_us_redundancy.rs+++ b/harper-core/src/linting/lets_confusion/let_us_redundancy.rs@@ -5,6 +5,11 @@ use crate::{use crate::linting::{Lint, LintKind, PatternLinter, Suggestion};+/// See also:+/// harper-core/src/linting/compound_nouns/implied_ownership_compound_nouns.rs+/// harper-core/src/linting/lets_confusion/mod.rs+/// harper-core/src/linting/lets_confusion/no_contraction_with_verb.rs+/// harper-core/src/linting/pronoun_contraction/should_contract.rspub struct LetUsRedundancy {pattern: Box<dyn Pattern>,}diff --git a/harper-core/src/linting/lets_confusion/mod.rs b/harper-core/src/linting/lets_confusion/mod.rsindex 455f3c752..147dd1e23 100644--- a/harper-core/src/linting/lets_confusion/mod.rs+++ b/harper-core/src/linting/lets_confusion/mod.rs@@ -5,6 +5,11 @@ use super::merge_linters::merge_linters;use let_us_redundancy::LetUsRedundancy;use no_contraction_with_verb::NoContractionWithVerb;+// See also:+// harper-core/src/linting/compound_nouns/implied_ownership_compound_nouns.rs+// harper-core/src/linting/lets_confusion/let_us_redundancy.rs+// harper-core/src/linting/lets_confusion/no_contraction_with_verb.rs+// harper-core/src/linting/pronoun_contraction/should_contract.rsmerge_linters!(LetsConfusion => LetUsRedundancy, NoContractionWithVerb => "It's often hard to determine where the subject should go with the word `let`. This rule attempts to find common errors with redundancy and contractions that may lead to confusion for readers.");#[cfg(test)]@@ -32,23 +37,44 @@ mod tests {assert_suggestion_result("let's me do", LetsConfusion::default(), "lets me do");}+ // "push" is also a noun so in a context like "He always lets push come to shove".+ // #[test]+ // fn from_harper_docs() {+ // assert_suggestion_result(+ // "Often the longest and the shortest words are the most helpful, so lets push them first.",+ // LetsConfusion::default(),+ // "Often the longest and the shortest words are the most helpful, so let's push them first.",+ // );+ // }+#[test]- fn from_harper_docs() {+ fn like_the_one_from_harper_docs() {assert_suggestion_result(- "Often the longest and the shortest words are the most helpful, so lets push them first.",+ "Often the longest and the shortest words are the most helpful, so lets prioritize them.",LetsConfusion::default(),- "Often the longest and the shortest words are the most helpful, so let's push them first.",+ "Often the longest and the shortest words are the most helpful, so let's prioritize them.",);}+ // "play" is also a noun so in a context like "Sometimes the umpire lets play continue"+ // #[test]+ // fn issue_470_missing_apostrophe() {+ // assert_suggestion_result("lets play", LetsConfusion::default(), "let's play");+ // }++ // #[test]+ // fn issue_470_missing_subject() {+ // assert_suggestion_result("let play", LetsConfusion::default(), "let's play");+ // }+#[test]fn issue_470_missing_apostrophe() {- assert_suggestion_result("lets play", LetsConfusion::default(), "let's play");+ assert_suggestion_result("lets proceed", LetsConfusion::default(), "let's proceed");}#[test]fn issue_470_missing_subject() {- assert_suggestion_result("let play", LetsConfusion::default(), "let's play");+ assert_suggestion_result("let proceed", LetsConfusion::default(), "let's proceed");}#[test]diff --git a/harper-core/src/linting/lets_confusion/no_contraction_with_verb.rs b/harper-core/src/linting/lets_confusion/no_contraction_with_verb.rsindex 74f426b92..2cc56d9aa 100644--- a/harper-core/src/linting/lets_confusion/no_contraction_with_verb.rs+++ b/harper-core/src/linting/lets_confusion/no_contraction_with_verb.rs@@ -6,6 +6,11 @@ use crate::{use crate::linting::PatternLinter;+/// See also:+/// harper-core/src/linting/compound_nouns/implied_ownership_compound_nouns.rs+/// harper-core/src/linting/lets_confusion/mod.rs+/// harper-core/src/linting/lets_confusion/let_us_redundancy.rs+/// harper-core/src/linting/pronoun_contraction/should_contract.rspub struct NoContractionWithVerb {pattern: Box<dyn Pattern>,}@@ -15,7 +20,32 @@ impl Default for NoContractionWithVerb {let pattern = SequencePattern::default().then(WordSet::new(&["lets", "let"])).then_whitespace()- .then_verb();+ .then(|tok: &Token, source: &[char]| {+ let Some(Some(meta)) = tok.kind.as_word() else {+ return false;+ };++ if !meta.is_verb() || meta.is_noun() || meta.is_adjective() {+ return false;+ }++ let tok_chars = tok.span.get_content(source);++ // If it ends with 'ing' and is at least 5 chars long, it could be a gerund or past participle+ // TODO: replace with metadata check when affix system supports verb forms+ if tok_chars.len() < 5 {+ return true;+ }++ let is_ing_form = tok_chars+ .iter()+ .skip(tok_chars.len() - 3)+ .map(|&c| c.to_ascii_lowercase())+ .collect::<Vec<_>>()+ .ends_with(&['i', 'n', 'g']);++ !is_ing_form+ });Self {pattern: Box::new(pattern),@@ -48,3 +78,81 @@ impl PatternLinter for NoContractionWithVerb {"Make sure you include a subject when giving permission to it."}}++#[cfg(test)]+mod tests {+ use super::NoContractionWithVerb;+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};++ // Corrections++ #[test]+ fn fix_lets_runs() {+ assert_suggestion_result(+ "Now lets runs liminal",+ NoContractionWithVerb::default(),+ "Now let's runs liminal",+ );+ }++ // False positives where verb is also a noun++ #[test]+ fn dont_flag_let_chance() {+ assert_lint_count("Let chance decide", NoContractionWithVerb::default(), 0);+ }++ #[test]+ fn dont_flag_let_time() {+ assert_lint_count(+ "Let time granularity be parametrized",+ NoContractionWithVerb::default(),+ 0,+ );+ }++ #[test]+ fn dont_flag_lets_staff() {+ assert_lint_count(+ "A plugin that backs up player's inventories and lets staff restore them or export it as a shulker.",+ NoContractionWithVerb::default(),+ 0,+ );+ }++ #[test]+ fn dont_flag_lets_time() {+ assert_lint_count(+ "This is very different than demo recording, which just simulates a network level connection and lets time move at its own rate.",+ NoContractionWithVerb::default(),+ 0,+ );+ }++ #[test]+ fn dont_flag_lets_play() {+ assert_lint_count("Sometimes the umpire lets play continue", NoContractionWithVerb::default(), 0);+ }++ // False positives where verb is a gerund/past participle++ #[test]+ fn dont_flag_let_sleeping() {+ assert_lint_count(+ "Let sleeping logs lie.",+ NoContractionWithVerb::default(),+ 0,+ );+ }++ // False positives where verb is also an adjective++ #[test]+ fn dont_flag_let_processed() {+ assert_lint_count(+ "Let processed response be a new structure analogous to server auction response.",+ NoContractionWithVerb::default(),+ 0,+ );+ }+}diff --git a/harper-core/src/linting/pronoun_contraction/should_contract.rs b/harper-core/src/linting/pronoun_contraction/should_contract.rsindex f4127699e..a47888e54 100644--- a/harper-core/src/linting/pronoun_contraction/should_contract.rs+++ b/harper-core/src/linting/pronoun_contraction/should_contract.rs@@ -6,6 +6,11 @@ use crate::{use crate::Lint;use crate::linting::{LintKind, PatternLinter, Suggestion};+/// See also:+/// harper-core/src/linting/compound_nouns/implied_ownership_compound_nouns.rs+/// harper-core/src/linting/lets_confusion/mod.rs+/// harper-core/src/linting/lets_confusion/let_us_redundancy.rs+/// harper-core/src/linting/lets_confusion/no_contraction_with_verb.rspub struct ShouldContract {pattern: Box<dyn Pattern>,}diff --git a/harper-core/tests/run_tests.rs b/harper-core/tests/run_tests.rsindex 5a3729153..521fd5a99 100644--- a/harper-core/tests/run_tests.rs+++ b/harper-core/tests/run_tests.rs@@ -48,7 +48,8 @@ create_test!(issue_195.md, 0, Dialect::American);create_test!(issue_118.md, 0, Dialect::American);create_test!(lots_of_latin.md, 0, Dialect::American);create_test!(pr_504.md, 1, Dialect::American);-create_test!(pr_452.md, 2, Dialect::American);+// 452.md is based on a verb which is also a noun, breaking a heuristic that detects false positives+//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);