Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat(core): detect improper contraction usage before verbs

Elijah Potter663f717452b3
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dict
index aa29a4e70..d0da6e465 100644
--- a/harper-core/dictionary.dict
+++ b/harper-core/dictionary.dict
@@ -47557,6 +47557,7 @@ usurp/4SDRZG
usurpation/1M
usurper/1M
usury/1M
+us/8
utensil/1SM
uteri/1
uterine/51
diff --git a/harper-core/src/linting/let_us_redundancy.rs b/harper-core/src/linting/lets_confusion/let_us_redundancy.rs
similarity index 72%
rename from harper-core/src/linting/let_us_redundancy.rs
rename to harper-core/src/linting/lets_confusion/let_us_redundancy.rs
index ef85b0fe1..b42c21e30 100644
--- a/harper-core/src/linting/let_us_redundancy.rs
+++ b/harper-core/src/linting/lets_confusion/let_us_redundancy.rs
@@ -1,9 +1,9 @@
use crate::{
- patterns::{Pattern, SequencePattern, WordSet},
+ patterns::{Pattern, SequencePattern},
Token, TokenStringExt,
};
-use super::{Lint, LintKind, PatternLinter, Suggestion};
+use crate::linting::{Lint, LintKind, PatternLinter, Suggestion};
pub struct LetUsRedundancy {
pattern: Box<dyn Pattern>,
@@ -13,7 +13,7 @@ impl Default for LetUsRedundancy {
fn default() -> Self {
let pattern = SequencePattern::aco("let's")
.then_whitespace()
- .then_word_set(WordSet::all(&["us", "me"]));
+ .then_pronoun();
Self {
pattern: Box::new(pattern),
@@ -41,15 +41,3 @@ impl PatternLinter for LetUsRedundancy {
"Many are not aware that the contraction `let's` is short for `let us`. As a result, many will incorrectly use it before a pronoun, such as in the phrase `let's us do`."
}
}
-
-#[cfg(test)]
-mod tests {
- use crate::linting::tests::assert_suggestion_result;
-
- use super::LetUsRedundancy;
-
- #[test]
- fn issue_426() {
- assert_suggestion_result("let's us do", LetUsRedundancy::default(), "let's do");
- }
-}
diff --git a/harper-core/src/linting/lets_confusion/mod.rs b/harper-core/src/linting/lets_confusion/mod.rs
new file mode 100644
index 000000000..d0e3f0fa0
--- /dev/null
+++ b/harper-core/src/linting/lets_confusion/mod.rs
@@ -0,0 +1,30 @@
+mod let_us_redundancy;
+mod no_contraction_with_verb;
+
+use super::merge_linters::merge_linters;
+use let_us_redundancy::LetUsRedundancy;
+use no_contraction_with_verb::NoContractionWithVerb;
+
+merge_linters!(LetsConfusion => LetUsRedundancy, NoContractionWithVerb => "TODO");
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::tests::assert_suggestion_result;
+
+ use super::LetsConfusion;
+
+ #[test]
+ fn issue_426_us() {
+ assert_suggestion_result("let's us do", LetsConfusion::default(), "let's do");
+ }
+
+ #[test]
+ fn issue_426_me() {
+ assert_suggestion_result("let's me do", LetsConfusion::default(), "let's do");
+ }
+
+ #[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.");
+ }
+}
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.rs
new file mode 100644
index 000000000..7d7925f8e
--- /dev/null
+++ b/harper-core/src/linting/lets_confusion/no_contraction_with_verb.rs
@@ -0,0 +1,47 @@
+use crate::{
+ linting::{Lint, LintKind, Suggestion},
+ patterns::{Pattern, SequencePattern},
+ Token,
+};
+
+use crate::linting::PatternLinter;
+
+pub struct NoContractionWithVerb {
+ pattern: Box<dyn Pattern>,
+}
+
+impl Default for NoContractionWithVerb {
+ fn default() -> Self {
+ let pattern = SequencePattern::aco("lets").then_whitespace().then_verb();
+
+ Self {
+ pattern: Box::new(pattern),
+ }
+ }
+}
+
+impl PatternLinter for NoContractionWithVerb {
+ fn pattern(&self) -> &dyn Pattern {
+ self.pattern.as_ref()
+ }
+
+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {
+ let problem_span = matched_tokens.first().unwrap().span;
+ let template = problem_span.get_content(source);
+
+ Lint {
+ span: problem_span,
+ lint_kind: LintKind::WordChoice,
+ suggestions: vec![
+ Suggestion::replace_with_match_case_str("let's", template),
+ Suggestion::replace_with_match_case_str("let us", template),
+ ],
+ message: "It seems you forgot to include a subject here.".to_owned(),
+ priority: 31,
+ }
+ }
+
+ fn description(&self) -> &'static str {
+ "Make sure you include a subject."
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 0a9cffb0b..2dcb27312 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -8,7 +8,7 @@ use super::capitalize_personal_pronouns::CapitalizePersonalPronouns;
use super::correct_number_suffix::CorrectNumberSuffix;
use super::dot_initialisms::DotInitialisms;
use super::ellipsis_length::EllipsisLength;
-use super::let_us_redundancy::LetUsRedundancy;
+use super::lets_confusion::LetsConfusion;
use super::linking_verbs::LinkingVerbs;
use super::long_sentences::LongSentences;
use super::matcher::Matcher;
@@ -190,8 +190,8 @@ create_lint_group_config!(
OxfordComma => true,
PronounContraction => true,
CurrencyPlacement => true,
- LetUsRedundancy => true,
- SomewhatSomething => true
+ SomewhatSomething => true,
+ LetsConfusion => true
);
impl<T: Dictionary + Default> Default for LintGroup<T> {
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 86df0b61e..a68b2aca9 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -7,7 +7,7 @@ mod currency_placement;
mod dashes;
mod dot_initialisms;
mod ellipsis_length;
-mod let_us_redundancy;
+mod lets_confusion;
mod linking_verbs;
mod lint;
mod lint_group;
@@ -42,7 +42,7 @@ pub use correct_number_suffix::CorrectNumberSuffix;
pub use currency_placement::CurrencyPlacement;
pub use dot_initialisms::DotInitialisms;
pub use ellipsis_length::EllipsisLength;
-pub use let_us_redundancy::LetUsRedundancy;
+pub use lets_confusion::LetsConfusion;
pub use linking_verbs::LinkingVerbs;
pub use lint::{Lint, LintKind, Suggestion};
pub use lint_group::{LintGroup, LintGroupConfig};
diff --git a/harper-core/tests/run_tests.rs b/harper-core/tests/run_tests.rs
index 39f1e511f..98eb60b69 100644
--- a/harper-core/tests/run_tests.rs
+++ b/harper-core/tests/run_tests.rs
@@ -48,6 +48,7 @@ create_test!(amazon_hostname.md, 0);
create_test!(issue_159.md, 1);
create_test!(issue_358.md, 0);
create_test!(issue_195.md, 0);
+create_test!(pr_452.md, 2);
// Make sure it doesn't panic
create_test!(lukas_homework.md, 3);
diff --git a/harper-core/tests/test_sources/pr_452.md b/harper-core/tests/test_sources/pr_452.md
new file mode 100644
index 000000000..2c66b29b0
--- /dev/null
+++ b/harper-core/tests/test_sources/pr_452.md
@@ -0,0 +1 @@
+Lets go and check if this lint let's us catch this class of errors.