Skip to content

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

feat: I am worry→I am worried (#2943)

Andrew Dunbarc0ac23cb6204
diff --git a/harper-core/src/linting/be_worried.rs b/harper-core/src/linting/be_worried.rs
new file mode 100644
index 000000000..fe8787c49
--- /dev/null
+++ b/harper-core/src/linting/be_worried.rs
@@ -0,0 +1,244 @@
+use crate::{
+ CharStringExt, Token,
+ expr::{All, Expr, OwnedExprExt, SequenceExpr},
+ linting::{
+ ExprLinter, Lint, LintKind, Suggestion,
+ expr_linter::{Chunk, followed_by_hyphen, followed_by_word},
+ },
+ patterns::{Word, WordSet},
+};
+
+pub struct BeWorried {
+ expr: All,
+}
+
+impl Default for BeWorried {
+ fn default() -> Self {
+ Self {
+ expr: SequenceExpr::default()
+ .then_any_of(vec![
+ Box::new(
+ SequenceExpr::default()
+ .then_subject_pronoun()
+ .t_ws()
+ .t_set(&["am", "are", "is", "was", "were"]),
+ ),
+ Box::new(WordSet::new(&[
+ "i'm", "we're", "you're", "he's", "she's", "they're",
+ ])),
+ ])
+ .t_ws()
+ .t_aco("worry")
+ .and_not(Word::new("it")),
+ }
+ }
+}
+
+impl ExprLinter for BeWorried {
+ type Unit = Chunk;
+
+ fn expr(&self) -> &dyn Expr {
+ &self.expr
+ }
+
+ fn match_to_lint_with_context(
+ &self,
+ toks: &[Token],
+ src: &[char],
+ ctx: Option<(&[Token], &[Token])>,
+ ) -> Option<Lint> {
+ let wtok = toks.last()?;
+
+ if followed_by_hyphen(ctx)
+ || followed_by_word(ctx, |w| {
+ w.span
+ .get_content(src)
+ .eq_any_ignore_ascii_case_str(&["free", "warts"])
+ })
+ {
+ return None;
+ }
+
+ Some(Lint {
+ span: wtok.span,
+ lint_kind: LintKind::Usage,
+ suggestions: vec![Suggestion::replace_with_match_case_str(
+ "worried",
+ wtok.span.get_content(src),
+ )],
+ message: "Use 'worried' instead of 'worry'.".to_string(),
+ ..Default::default()
+ })
+ }
+
+ fn description(&self) -> &'static str {
+ "Detects incorrect use of 'be worry' instead of `be worried`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::tests::{
+ assert_good_and_bad_suggestions, assert_no_lints, assert_suggestion_result,
+ };
+
+ use super::BeWorried;
+
+ #[test]
+ fn he_is() {
+ assert_suggestion_result(
+ "I guess he is worry about \" * user * \" tag.",
+ BeWorried::default(),
+ "I guess he is worried about \" * user * \" tag.",
+ );
+ }
+
+ #[test]
+ fn he_was() {
+ assert_suggestion_result(
+ "So he was worry about her. Especially, when he got no response by calling her on her phone nor ranging her doorbell.",
+ BeWorried::default(),
+ "So he was worried about her. Especially, when he got no response by calling her on her phone nor ranging her doorbell.",
+ );
+ }
+
+ #[test]
+ fn i_am() {
+ assert_suggestion_result(
+ "I didn't see any section dedicated to this so I am worry about:",
+ BeWorried::default(),
+ "I didn't see any section dedicated to this so I am worried about:",
+ );
+ }
+
+ #[test]
+ fn i_was() {
+ assert_suggestion_result(
+ "So that's why I was worry.",
+ BeWorried::default(),
+ "So that's why I was worried.",
+ );
+ }
+
+ #[test]
+ fn i_were() {
+ assert_suggestion_result(
+ "The only things that I were worry about is the data that could be lost using this deletion.",
+ BeWorried::default(),
+ "The only things that I were worried about is the data that could be lost using this deletion.",
+ );
+ }
+
+ #[test]
+ fn they_are() {
+ assert_suggestion_result(
+ "at the same time they are worry about the price for the upgrade each 3 years",
+ BeWorried::default(),
+ "at the same time they are worried about the price for the upgrade each 3 years",
+ );
+ }
+
+ #[test]
+ fn theyre_worry() {
+ assert_suggestion_result(
+ "Because they're worry this link is spam or they scare have to pay more money.",
+ BeWorried::default(),
+ "Because they're worried this link is spam or they scare have to pay more money.",
+ );
+ }
+
+ #[test]
+ fn we_are() {
+ assert_suggestion_result(
+ "We are analised this and we are worry because when our platform go to market",
+ BeWorried::default(),
+ "We are analised this and we are worried because when our platform go to market",
+ );
+ }
+
+ #[test]
+ fn were() {
+ assert_suggestion_result(
+ "We're worry about all kinds of minority representation in TV.",
+ BeWorried::default(),
+ "We're worried about all kinds of minority representation in TV.",
+ );
+ }
+
+ #[test]
+ fn you_are() {
+ assert_suggestion_result(
+ "You are worry because we are not annotating view interface itself, right?",
+ BeWorried::default(),
+ "You are worried because we are not annotating view interface itself, right?",
+ );
+ }
+
+ #[test]
+ fn youre() {
+ assert_suggestion_result(
+ "You're worry about memory usage and wanna be sure that a Sequence-class won't hold your activity against GC — declare this class as static",
+ BeWorried::default(),
+ "You're worried about memory usage and wanna be sure that a Sequence-class won't hold your activity against GC — declare this class as static",
+ );
+ }
+
+ #[test]
+ fn dont_flag_it_is() {
+ assert_no_lints(
+ "Part of it is worry that my bosses will get angry and fire me.",
+ BeWorried::default(),
+ );
+ }
+
+ #[test]
+ fn dont_flag_it_was() {
+ assert_no_lints(
+ "Because what followed wasn't indifference, it was worry.",
+ BeWorried::default(),
+ );
+ }
+
+ #[test]
+ fn dont_flag_she_was_worry_free() {
+ assert_no_lints("textFinally, she was worry-free.", BeWorried::default());
+ }
+
+ #[test]
+ fn dont_flag_theyre_worry_free() {
+ assert_no_lints(
+ "They don't pretend they're worry-free.",
+ BeWorried::default(),
+ );
+ }
+
+ #[test]
+ fn dont_flag_worry_warts() {
+ assert_no_lints(
+ "Thanks to jQuery, we're worry warts from browser compatibility.",
+ BeWorried::default(),
+ );
+ }
+
+ #[test]
+ fn dont_flag_were_worry_space_free() {
+ assert_no_lints(
+ "Thanks to jQuery, we're worry free from browser compatibility.",
+ BeWorried::default(),
+ );
+ }
+
+ #[test]
+ #[ignore = "edge case not yet handled"]
+ fn cant_fix_edge_case_yet() {
+ assert_good_and_bad_suggestions(
+ "Myself along with others are using it on an iPad successfully, so it is worry to hear that is broken for you.",
+ BeWorried::default(),
+ &[
+ "Myself along with others are using it on an iPad successfully, so it is worrying to hear that is broken for you.",
+ "Myself along with others are using it on an iPad successfully, so it is a worry to hear that is broken for you.",
+ ],
+ &[],
+ );
+ }
+}
diff --git a/harper-core/src/linting/expr_linter.rs b/harper-core/src/linting/expr_linter.rs
index 3647efb02..baeaae036 100644
--- a/harper-core/src/linting/expr_linter.rs
+++ b/harper-core/src/linting/expr_linter.rs
@@ -152,6 +152,12 @@ pub fn followed_by_word(
false
}
+pub fn followed_by_hyphen(context: Option<(&[Token], &[Token])>) -> bool {
+ context
+ .and_then(|(_, after)| after.first())
+ .is_some_and(|hy| hy.kind.is_hyphen())
+}
+
pub fn preceded_by_word(
context: Option<(&[Token], &[Token])>,
predicate: impl Fn(&Token) -> bool,
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 1a7c3ad7c..b514f216d 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -32,6 +32,7 @@ use super::ask_no_preposition::AskNoPreposition;
use super::avoid_curses::AvoidCurses;
use super::back_in_the_day::BackInTheDay;
use super::be_allowed::BeAllowed;
+use super::be_worried::BeWorried;
use super::behind_the_scenes::BehindTheScenes;
use super::best_of_all_time::BestOfAllTime;
use super::boring_words::BoringWords;
@@ -619,6 +620,7 @@ impl LintGroup {
insert_expr_rule!(AvoidCurses, true);
insert_expr_rule!(BackInTheDay, true);
insert_expr_rule!(BeAllowed, true);
+ insert_expr_rule!(BeWorried, true);
insert_expr_rule!(BehindTheScenes, true);
insert_struct_rule!(BestOfAllTime, true);
insert_expr_rule!(BoringWords, false);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 0b7af3e9d..6417df6d4 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -23,6 +23,7 @@ mod ask_no_preposition;
mod avoid_curses;
mod back_in_the_day;
mod be_allowed;
+mod be_worried;
mod behind_the_scenes;
mod best_of_all_time;
mod boring_words;