Skip to content

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

fix(core): coverage for confusion of `they're`/`there`/`their` (#2798)

Elijah Potter3dc73db36e16
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 9b7fceb73..bfbcaedb7 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -199,6 +199,7 @@ use super::the_proper_noun_possessive::TheProperNounPossessive;
use super::then_than::ThenThan;
use super::theres::Theres;
use super::theses_these::ThesesThese;
+use super::theyre_confusions::TheyreConfusions;
use super::thing_think::ThingThink;
use super::this_type_of_thing::ThisTypeOfThing;
use super::though_thought::ThoughThought;
@@ -603,6 +604,7 @@ impl LintGroup {
insert_struct_rule!(ToTwoToo, true);
insert_expr_rule!(Touristic, true);
insert_expr_rule!(TryOnesHandAt, true);
+ insert_struct_rule!(TheyreConfusions, true);
insert_struct_rule!(UnclosedQuotes, true);
insert_expr_rule!(UpdatePlaceNames, true);
insert_expr_rule!(VerbToAdjective, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index f1cd03535..ab19dafdf 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -210,6 +210,7 @@ mod the_proper_noun_possessive;
mod then_than;
mod theres;
mod theses_these;
+mod theyre_confusions;
mod thing_think;
mod this_type_of_thing;
mod though_thought;
diff --git a/harper-core/src/linting/theyre_confusions/mod.rs b/harper-core/src/linting/theyre_confusions/mod.rs
new file mode 100644
index 000000000..2a7317189
--- /dev/null
+++ b/harper-core/src/linting/theyre_confusions/mod.rs
@@ -0,0 +1,77 @@
+mod over_theyre_to_there;
+mod typographic_theyre_to_their;
+
+use crate::Token;
+use crate::char_ext::CharExt;
+use crate::char_string::CharStringExt;
+
+use super::merge_linters::merge_linters;
+use over_theyre_to_there::OverTheyreToThere;
+use typographic_theyre_to_their::TypographicTheyreToTheir;
+
+fn token_is_theyre(token: &Token, source: &[char]) -> bool {
+ if !token.kind.is_word() {
+ return false;
+ }
+
+ token
+ .span
+ .get_content(source)
+ .normalized()
+ .eq_ignore_ascii_case_str("they're")
+}
+
+fn token_is_typographic_theyre(token: &Token, source: &[char]) -> bool {
+ if !token.kind.is_word() {
+ return false;
+ }
+
+ let content = token.span.get_content(source);
+ content.iter().any(|c| c.normalized() != *c)
+ && content.normalized().eq_ignore_ascii_case_str("they're")
+}
+
+fn token_is_likely_their_possession(token: &Token, source: &[char]) -> bool {
+ if !token.kind.is_word() {
+ return false;
+ }
+
+ let normalized = token.span.get_content(source).normalized();
+ matches!(
+ normalized.as_ref(),
+ ['b', 'a', 'c', 'k', 'p', 'a', 'c', 'k', 's']
+ | ['p', 'a', 't', 'i', 'e', 'n', 'c', 'e']
+ | ['d', 'o', 'g']
+ | ['p', 'r', 'o', 'p', 'o', 's', 'a', 'l']
+ | ['l', 'a', 'u', 'g', 'h', 't', 'e', 'r']
+ | ['l', 'a', 'd', 'd', 'e', 'r']
+ | ['a', 'p', 'a', 'r', 't', 'm', 'e', 'n', 't']
+ | ['m', 'i', 't', 't', 'e', 'n', 's']
+ | ['a', 'n', 's', 'w', 'e', 'r']
+ | ['s', 'k', 'e', 't', 'c', 'h', 'e', 's']
+ | ['s', 'e', 'r', 'v', 'e', 'r']
+ | ['b', 'a', 'c', 'k', 'u', 'p']
+ | ['e', 'v', 'i', 'd', 'e', 'n', 'c', 'e']
+ | ['g', 'a', 'r', 'd', 'e', 'n']
+ | ['m', 'a', 'p', 's']
+ | ['t', 'e', 'a', 'm']
+ | ['p', 'a', 's', 't']
+ | ['n', 'e', 'e', 'd', 's']
+ | ['p', 'a', 'w', 'n']
+ | ['a', 'b', 'i', 'l', 'i', 't', 'y']
+ | ['r', 'e', 't', 'u', 'r', 'n']
+ | ['h', 'e', 'a', 'r', 'i', 'n', 'g']
+ | ['h', 'o', 'u', 's', 'e']
+ | ['c', 'o', 'a', 't', 's']
+ | ['p', 'r', 'o', 'b', 'l', 'e', 'm', 's']
+ | [
+ 'u', 'n', 'd', 'e', 'r', 's', 't', 'a', 'n', 'd', 'i', 'n', 'g'
+ ]
+ | ['n', 'e', 'w']
+ )
+}
+
+merge_linters!(
+ TheyreConfusions => OverTheyreToThere, TypographicTheyreToTheir =>
+ "Detects apostrophe and locative edge cases that are awkward to model with standard contraction checks."
+);
diff --git a/harper-core/src/linting/theyre_confusions/over_theyre_to_there.rs b/harper-core/src/linting/theyre_confusions/over_theyre_to_there.rs
new file mode 100644
index 000000000..99e8103ff
--- /dev/null
+++ b/harper-core/src/linting/theyre_confusions/over_theyre_to_there.rs
@@ -0,0 +1,81 @@
+use super::token_is_theyre;
+use crate::linting::expr_linter::Chunk;
+use crate::{
+ Token,
+ expr::SequenceExpr,
+ linting::{ExprLinter, Lint, LintKind, Suggestion},
+};
+
+pub struct OverTheyreToThere {
+ expr: Box<dyn crate::expr::Expr>,
+}
+
+impl Default for OverTheyreToThere {
+ fn default() -> Self {
+ let expr = SequenceExpr::aco("over")
+ .t_ws()
+ .then(token_is_theyre as fn(&Token, &[char]) -> bool);
+
+ Self {
+ expr: Box::new(expr),
+ }
+ }
+}
+
+impl ExprLinter for OverTheyreToThere {
+ type Unit = Chunk;
+
+ fn expr(&self) -> &dyn crate::expr::Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
+ let offender = matched_tokens.last()?;
+ let template = offender.span.get_content(source);
+
+ Some(Lint {
+ span: offender.span,
+ lint_kind: LintKind::Grammar,
+ suggestions: vec![Suggestion::replace_with_match_case_str("there", template)],
+ message: "Did you mean `there`?".to_owned(),
+ priority: 31,
+ })
+ }
+
+ fn description(&self) -> &'static str {
+ "Corrects locative `over they're` to `over there`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::OverTheyreToThere;
+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+
+ #[test]
+ fn corrects_locative_ascii_apostrophe() {
+ assert_suggestion_result(
+ "Put the chairs over they're by the window.",
+ OverTheyreToThere::default(),
+ "Put the chairs over there by the window.",
+ );
+ }
+
+ #[test]
+ fn corrects_locative_smart_apostrophe() {
+ assert_suggestion_result(
+ "Is that their car parked over they’re?",
+ OverTheyreToThere::default(),
+ "Is that their car parked over there?",
+ );
+ }
+
+ #[test]
+ fn ignores_correct_form() {
+ assert_lint_count(
+ "Put the chairs over there by the window.",
+ OverTheyreToThere::default(),
+ 0,
+ );
+ }
+}
diff --git a/harper-core/src/linting/theyre_confusions/typographic_theyre_to_their.rs b/harper-core/src/linting/theyre_confusions/typographic_theyre_to_their.rs
new file mode 100644
index 000000000..d2ce7a71b
--- /dev/null
+++ b/harper-core/src/linting/theyre_confusions/typographic_theyre_to_their.rs
@@ -0,0 +1,101 @@
+use super::{token_is_likely_their_possession, token_is_typographic_theyre};
+use crate::linting::expr_linter::Chunk;
+use crate::{
+ Token,
+ expr::SequenceExpr,
+ linting::{ExprLinter, Lint, LintKind, Suggestion},
+};
+
+pub struct TypographicTheyreToTheir {
+ expr: Box<dyn crate::expr::Expr>,
+}
+
+impl Default for TypographicTheyreToTheir {
+ fn default() -> Self {
+ let expr = SequenceExpr::with(token_is_typographic_theyre as fn(&Token, &[char]) -> bool)
+ .t_ws()
+ .then(token_is_likely_their_possession as fn(&Token, &[char]) -> bool);
+
+ Self {
+ expr: Box::new(expr),
+ }
+ }
+}
+
+impl ExprLinter for TypographicTheyreToTheir {
+ type Unit = Chunk;
+
+ fn expr(&self) -> &dyn crate::expr::Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
+ let offender = matched_tokens.first()?;
+ let template = offender.span.get_content(source);
+
+ Some(Lint {
+ span: offender.span,
+ lint_kind: LintKind::Grammar,
+ suggestions: vec![Suggestion::replace_with_match_case_str("their", template)],
+ message: "Did you mean `their`?".to_owned(),
+ priority: 31,
+ })
+ }
+
+ fn description(&self) -> &'static str {
+ "Corrects smart-apostrophe `they’re` to `their` in possessive noun contexts."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::TypographicTheyreToTheir;
+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+
+ #[test]
+ fn corrects_smart_apostrophe_possessive() {
+ assert_suggestion_result(
+ "I think they’re house is the blue one.",
+ TypographicTheyreToTheir::default(),
+ "I think their house is the blue one.",
+ );
+ }
+
+ #[test]
+ fn ignores_ascii_form_for_existing_rule() {
+ assert_lint_count(
+ "I think they're house is the blue one.",
+ TypographicTheyreToTheir::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn ignores_non_possessive_usage() {
+ assert_lint_count(
+ "I think they’re coming tonight.",
+ TypographicTheyreToTheir::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn ignores_contraction_examples_from_books() {
+ assert_lint_count("No, they’re not.", TypographicTheyreToTheir::default(), 0);
+ assert_lint_count(
+ "I don’t know what they’re like.",
+ TypographicTheyreToTheir::default(),
+ 0,
+ );
+ assert_lint_count(
+ "They’re all over crumbs.",
+ TypographicTheyreToTheir::default(),
+ 0,
+ );
+ assert_lint_count(
+ "They’re done with blacking, I believe.",
+ TypographicTheyreToTheir::default(),
+ 0,
+ );
+ }
+}
diff --git a/harper-core/src/linting/weir_rules/TheirToThere.weir b/harper-core/src/linting/weir_rules/TheirToThere.weir
index f2895625d..5079ab811 100644
--- a/harper-core/src/linting/weir_rules/TheirToThere.weir
+++ b/harper-core/src/linting/weir_rules/TheirToThere.weir
@@ -1,4 +1,4 @@
-expr main [<(their [is, are, was, were, isn't, aren't, wasn't, weren't, (will be), (can be), (could be), (should be), (would be), (may be), (might be), (must be), (has been), (have been)]), their>, <(their [on, after, beside, in, by, until, again, to, and, right]), their>, <(their,), their>, their's]
+expr main [<(their [is, are, was, were, isn't, aren't, wasn't, weren't, (will be), (can be), (could be), (should be), (would be), (may be), (might be), (must be), (has been), (have been)]), their>, <(their [on, after, beside, in, by, until, again, to, and, right, at]), their>, <((over their), their)>, <((over they're), they're)>, <(their,), their>, their's]
let message "Did you mean `there`?"
let description "Corrects `their` when the intended meaning is `there`."
@@ -59,3 +59,6 @@ test "Their is a quiet confidence in her voice." "There is a quiet confidence in
test "The answer is right their in the first paragraph." "The answer is right there in the first paragraph."
test "Their are deadlines, and then their are consequences." "There are deadlines, and then there are consequences."
test "I can't believe you're still standing their, smiling." "I can't believe you're still standing there, smiling."
+test "I left my keys over their on the table." "I left my keys over there on the table."
+test "We should meet their at noon." "We should meet there at noon."
+test "Their is no reason to panic." "There is no reason to panic."
diff --git a/harper-core/src/linting/weir_rules/TheirToTheyre.weir b/harper-core/src/linting/weir_rules/TheirToTheyre.weir
index 0e1ae371c..2112abe8b 100644
--- a/harper-core/src/linting/weir_rules/TheirToTheyre.weir
+++ b/harper-core/src/linting/weir_rules/TheirToTheyre.weir
@@ -1,4 +1,4 @@
-expr main [<(their [AUX, (supposed to), (allowed to), (invited to), (going to), (trying to), (coming over), (leaving in), (moving to), (planning a), (waiting for), (answering any), (wondering where), (running behind), (far too), even, (already here), (in the), (on the), (at the), (a lot), an, two, offline, (ADV [PROG, AUX]), (ADV VERB), (PART [PROG, VERB, ADJ, AUX]), (ready to), the]), their>]
+expr main [<(their [AUX, (supposed to), (allowed to), (invited to), (going to), (trying to), (coming over), (coming tonight), (leaving in), (moving to), (planning a), (waiting for), (answering any), (wondering where), (running behind), (running through), (far too), even, (already here), (in the), (on the), (at the), (a lot), an, two, offline, (ADV [PROG, AUX]), (ADV VERB), (PART [PROG, VERB, ADJ, AUX]), (ready to), (always [late, early]), the]), their>, <(there [(moving next), (planning a)]), there>]
let message "Did you mean `they're`?"
let description "Corrects `their` when the intended meaning is `they're`."
@@ -30,6 +30,12 @@ test "Their ready to start whenever you are." "They're ready to start whenever y
test "Their not answering any messages." "They're not answering any messages."
test "Their going to be a problem if we don't hurry." "They're going to be a problem if we don't hurry."
test "Their probably wondering where we went." "They're probably wondering where we went."
+test "Their always late." "They're always late."
+test "Their going to the store after work." "They're going to the store after work."
+test "Do you know if their coming tonight?" "Do you know if they're coming tonight?"
+test "The neighbors said there moving next month." "The neighbors said they're moving next month."
+test "I saw their running through the park." "I saw they're running through the park."
+test "I heard there planning a surprise party." "I heard they're planning a surprise party."
allows "Their backpacks were stacked neatly by the door."
allows "I admired their patience during the long, glitchy delay."
diff --git a/harper-core/src/linting/weir_rules/ThereToTheir.weir b/harper-core/src/linting/weir_rules/ThereToTheir.weir
index 79f9ef21c..b6ab9ef55 100644
--- a/harper-core/src/linting/weir_rules/ThereToTheir.weir
+++ b/harper-core/src/linting/weir_rules/ThereToTheir.weir
@@ -6,6 +6,9 @@ let kind "Grammar"
let becomes "their"
test "There backpacks were stacked neatly by the door." "Their backpacks were stacked neatly by the door."
+test "Is that there dog?" "Is that their dog?"
+test "There dog is barking again." "Their dog is barking again."
+test "The students forgot there backpacks." "The students forgot their backpacks."
test "I admired there patience during the long, glitchy delay." "I admired their patience during the long, glitchy delay."
test "There dog sprinted across the yard like a furry comet." "Their dog sprinted across the yard like a furry comet."
test "The committee revised there proposal after the budget changed." "The committee revised their proposal after the budget changed."
@@ -19,6 +22,7 @@ test "There server crashed, but there backup plan worked flawlessly." "Their ser
test "I couldn't ignore there evidence; it was meticulously documented." "I couldn't ignore their evidence; it was meticulously documented."
test "There garden is a riot of basil, tomatoes, and bee traffic." "Their garden is a riot of basil, tomatoes, and bee traffic."
test "The hikers checked there maps twice before leaving the trailhead." "The hikers checked their maps twice before leaving the trailhead."
+test "They left there books in the classroom." "They left their books in the classroom."
test "There team celebrated quietly--relieved more than triumphant." "Their team celebrated quietly--relieved more than triumphant."
test "Users should be able to visualize there connected notes as a knowledge graph, allowing them to explore relationships and identify patterns." "Users should be able to visualize their connected notes as a knowledge graph, allowing them to explore relationships and identify patterns."
test "The frustration was palpable; the team felt they were fighting a constant uphill battle against there own data infrastructure." "The frustration was palpable; the team felt they were fighting a constant uphill battle against their own data infrastructure."
@@ -46,4 +50,4 @@ allows "The answer is right there in the first paragraph."
allows "Are there known issues between these specific versions of Elementor and ACF that I’m unaware of?"
allows "There are lots of different phases."
allows "Yet, there are pockets of silence, moments where the urban soundscape recedes."
-allows "While there are areas for improvement, the plugin is already a valuable tool for developers who want a modern and efficient debugging experience."
\ No newline at end of file
+allows "While there are areas for improvement, the plugin is already a valuable tool for developers who want a modern and efficient debugging experience."
diff --git a/harper-core/src/linting/weir_rules/TheyreToTheir.weir b/harper-core/src/linting/weir_rules/TheyreToTheir.weir
index 17ed3f6e8..9e0e66990 100644
--- a/harper-core/src/linting/weir_rules/TheyreToTheir.weir
+++ b/harper-core/src/linting/weir_rules/TheyreToTheir.weir
@@ -1,4 +1,4 @@
-expr main [<(they're [NOUN, (NOUN NOUN), (ADJ NOUN), (ADV ADJ NOUN), (VERB NOUN), (ADV VERB NOUN), (ADV ADV VERB NOUN), (ADJ PUNCT ADJ NOUN), (mittens), (sketches), (past), (needs), (pawn), (ability), (return), (potential to), (understanding), (new)]), they're>]
+expr main <(they're [backpacks, patience, dog, proposal, laughter, ladder, apartment, mittens, answer, sketches, server, backup, evidence, garden, maps, team, past, needs, pawn, ability, return, hearing, house, coats, problems, (connected notes), (own data infrastructure), (ancestral lands), (most recently accessed projects), (potential to), (understanding), (new), (relatively low density)]), they're>
let message "Did you mean `their`?"
let description "Corrects `they're` when the intended meaning is `their`."
@@ -30,7 +30,13 @@ allows "They're ready to start whenever you are."
allows "They're not answering any messages."
allows "They're going to be a problem if we don't hurry."
allows "They're probably wondering where we went."
+allows "No, they're not."
+allows "They're done with blacking, I believe."
+allows "I don't know what they're like."
+allows "They're all over crumbs."
test "They're backpacks were stacked neatly by the door." "Their backpacks were stacked neatly by the door."
+test "What are they're problems?" "What are their problems?"
+test "I think they're house is the blue one." "I think their house is the blue one."
test "I admired they're patience during the long, glitchy delay." "I admired their patience during the long, glitchy delay."
test "They're dog sprinted across the yard like a furry comet." "Their dog sprinted across the yard like a furry comet."
test "The committee revised they're proposal after the budget changed." "The committee revised their proposal after the budget changed."
@@ -58,3 +64,4 @@ test "For instance, a musician who has lost they're hearing might collaborate wi
test "They're relatively low density facilitates handling and transportation." "Their relatively low density facilitates handling and transportation."
test "The growing interest in mycelium-based construction materials stems from they're potential to offer a sustainable alternative to conventional building materials." "The growing interest in mycelium-based construction materials stems from their potential to offer a sustainable alternative to conventional building materials."
test "Similarly, the individual experiencing sensory loss must actively reconstruct they're understanding of the world, mapping the contours of they're new, altered reality." "Similarly, the individual experiencing sensory loss must actively reconstruct their understanding of the world, mapping the contours of their new, altered reality."
+test "Can you hand me they're coats?" "Can you hand me their coats?"