Skip to content

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

fix: don't flag "you and I are" (#1650)

Andrew Dunbar3e26a7a26ac3
diff --git a/harper-core/src/linting/i_am_agreement.rs b/harper-core/src/linting/i_am_agreement.rs
new file mode 100644
index 000000000..a2e40e9df
--- /dev/null
+++ b/harper-core/src/linting/i_am_agreement.rs
@@ -0,0 +1,150 @@
+use crate::{
+ Lrc, Token, TokenStringExt,
+ expr::{AnchorStart, Expr, FirstMatchOf, FixedPhrase, SequenceExpr},
+ linting::{ExprLinter, Lint, LintKind, Suggestion},
+};
+pub struct IAmAgreement {
+ expr: Box<dyn Expr>,
+}
+
+impl Default for IAmAgreement {
+ fn default() -> Self {
+ let i_are = Lrc::new(FixedPhrase::from_phrase("I are"));
+
+ let nothing_before_i_are = SequenceExpr::default()
+ .then(AnchorStart)
+ .then(i_are.clone());
+
+ let non_and_word_before_i_are = SequenceExpr::default()
+ .then(|tok: &Token, src: &[char]| {
+ !tok.kind.is_word() || tok.span.get_content_string(src).to_lowercase() != "and"
+ })
+ .t_ws()
+ .then(i_are);
+
+ let expr = FirstMatchOf::new(vec![
+ Box::new(nothing_before_i_are),
+ Box::new(non_and_word_before_i_are),
+ ]);
+
+ Self {
+ expr: Box::new(expr),
+ }
+ }
+}
+
+impl ExprLinter for IAmAgreement {
+ fn expr(&self) -> &dyn Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
+ let toks = &toks[toks.len() - 3..];
+ Some(Lint {
+ span: toks.span()?,
+ lint_kind: LintKind::Agreement,
+ suggestions: vec![Suggestion::replace_with_match_case(
+ "I am".chars().collect(),
+ toks.span()?.get_content(src),
+ )],
+ message: "The first-person singular pronoun `I` requires the verb form `am`; `are` belongs to second-person or plural contexts.".to_string(),
+ priority: 31,
+ })
+ }
+
+ fn description(&self) -> &str {
+ "Corrects `I are` to `I am`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+
+ #[test]
+ fn corrects_i_are_simple() {
+ assert_suggestion_result("I are", IAmAgreement::default(), "I am");
+ }
+
+ #[test]
+ fn corrects_i_are() {
+ assert_suggestion_result(
+ "I are really happy about this release.",
+ IAmAgreement::default(),
+ "I am really happy about this release.",
+ );
+ }
+
+ #[test]
+ fn dont_flag_you_and_i_are() {
+ assert_lint_count(
+ "You know, you and I are sitting on the Titanic and the iceberg is over there.",
+ IAmAgreement::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_mention_and_i_are() {
+ assert_lint_count(
+ "Hello, @another-rex and I are attempting to package packageurl-go for Debian as we need it for a build dependency.",
+ IAmAgreement::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_z_and_i_are() {
+ assert_lint_count(
+ "The url is copied from a manual search, and Z and I are modified.",
+ IAmAgreement::default(),
+ 0,
+ )
+ }
+
+ #[test]
+ fn dont_flag_name_and_i_are() {
+ assert_lint_count(
+ "Paper that Lena Baunaz and I are working on as part of my SNSF-funded 'Focus in diachrony'",
+ IAmAgreement::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn fix_so_i_are() {
+ assert_suggestion_result(
+ "I have not yet been able to reproduce this issue in my environment, so I are still trying to figure it out",
+ IAmAgreement::default(),
+ "I have not yet been able to reproduce this issue in my environment, so I am still trying to figure it out",
+ );
+ }
+
+ #[test]
+ fn fix_if_i_are() {
+ assert_suggestion_result(
+ "If i are on creative inventory, and try to clean my inventory holding shift is disconnected too.",
+ IAmAgreement::default(),
+ "If i am on creative inventory, and try to clean my inventory holding shift is disconnected too.",
+ );
+ }
+
+ #[test]
+ fn fix_what_i_are() {
+ assert_suggestion_result(
+ "in this situation I can't see what I are typing",
+ IAmAgreement::default(),
+ "in this situation I can't see what I am typing",
+ );
+ }
+
+ #[test]
+ fn fix_where_i_are() {
+ assert_suggestion_result(
+ "I have a logging application where I are append to a topic",
+ IAmAgreement::default(),
+ "I have a logging application where I am append to a topic",
+ );
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 0d413a433..327763434 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -49,6 +49,7 @@ use super::hereby::Hereby;
use super::hop_hope::HopHope;
use super::how_to::HowTo;
use super::hyphenate_number_day::HyphenateNumberDay;
+use super::i_am_agreement::IAmAgreement;
use super::in_on_the_cards::InOnTheCards;
use super::inflected_verb_after_to::InflectedVerbAfterTo;
use super::its_contraction::ItsContraction;
@@ -428,6 +429,7 @@ impl LintGroup {
insert_struct_rule!(HopHope, true);
insert_struct_rule!(HowTo, true);
insert_expr_rule!(HyphenateNumberDay, true);
+ insert_expr_rule!(IAmAgreement, true);
insert_struct_rule!(ItsContraction, true);
insert_struct_rule!(ItsPossessive, true);
insert_expr_rule!(LeftRightHand, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index b3449ebed..afdd5ff97 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -44,6 +44,7 @@ mod hop_hope;
mod hope_youre;
mod how_to;
mod hyphenate_number_day;
+mod i_am_agreement;
mod in_on_the_cards;
mod inflected_verb_after_to;
mod initialism_linter;
@@ -165,6 +166,7 @@ pub use hereby::Hereby;
pub use hop_hope::HopHope;
pub use how_to::HowTo;
pub use hyphenate_number_day::HyphenateNumberDay;
+pub use i_am_agreement::IAmAgreement;
pub use in_on_the_cards::InOnTheCards;
pub use inflected_verb_after_to::InflectedVerbAfterTo;
pub use initialism_linter::InitialismLinter;
diff --git a/harper-core/src/linting/phrase_corrections/mod.rs b/harper-core/src/linting/phrase_corrections/mod.rs
index 171f4c5ce..96cade6cf 100644
--- a/harper-core/src/linting/phrase_corrections/mod.rs
+++ b/harper-core/src/linting/phrase_corrections/mod.rs
@@ -543,13 +543,6 @@ pub fn lint_group() -> LintGroup {
"Fixes the incorrect spacing in `I a m` to properly form `I am`.",
LintKind::Typo
),
- "IAmAgreement" => (
- ["I are"],
- ["I am"],
- "The first-person singular pronoun `I` requires the verb form `am`; `are` belongs to second-person or plural contexts.",
- "Corrects `I are` to `I am`.",
- LintKind::Agreement
- ),
"IDo" => (
["I does"],
["I do"],
diff --git a/harper-core/src/linting/phrase_corrections/tests.rs b/harper-core/src/linting/phrase_corrections/tests.rs
index ffee00d01..28a6feea7 100644
--- a/harper-core/src/linting/phrase_corrections/tests.rs
+++ b/harper-core/src/linting/phrase_corrections/tests.rs
@@ -593,16 +593,6 @@ fn hunger_pain() {
// IAm
// -none-
-// IAmAgreement
-#[test]
-fn corrects_i_are() {
- assert_suggestion_result(
- "I are really happy about this release.",
- lint_group(),
- "I am really happy about this release.",
- );
-}
-
// IDo
#[test]
fn corrects_i_does() {