Skip to content

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

feat: wish x can → wish x could (#2291)

Andrew Dunbar8cfb6847244a
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 506b2bff0..d1ee5698b 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -184,6 +184,7 @@ use super::well_educated::WellEducated;
use super::whereas::Whereas;
use super::widely_accepted::WidelyAccepted;
use super::win_prize::WinPrize;
+use super::wish_could::WishCould;
use super::wordpress_dotcom::WordPressDotcom;
use super::would_never_have::WouldNeverHave;
use super::{ExprLinter, Lint};
@@ -473,6 +474,7 @@ impl LintGroup {
// Please maintain alphabetical order.
// On *nix you can maintain sort order with `sort -t'(' -k2`
insert_expr_rule!(APart, true);
+ insert_expr_rule!(AWhile, true);
insert_expr_rule!(Addicting, true);
insert_expr_rule!(AdjectiveDoubleDegree, true);
insert_struct_rule!(AdjectiveOfA, true);
@@ -498,9 +500,6 @@ impl LintGroup {
insert_expr_rule!(CautionaryTale, true);
insert_expr_rule!(ChangeTack, true);
insert_expr_rule!(ChockFull, true);
- insert_expr_rule!(AWhile, true);
- insert_struct_rule!(SubjectPronoun, true);
- insert_struct_rule!(FindFine, true);
insert_struct_rule!(CommaFixes, true);
insert_struct_rule!(CompoundNouns, true);
insert_expr_rule!(CompoundSubjectI, true);
@@ -524,6 +523,7 @@ impl LintGroup {
insert_expr_rule!(FeelFell, true);
insert_expr_rule!(FewUnitsOfTimeAgo, true);
insert_expr_rule!(FillerWords, true);
+ insert_struct_rule!(FindFine, true);
insert_expr_rule!(FirstAidKit, true);
insert_expr_rule!(ForNoun, true);
insert_expr_rule!(FreePredicate, true);
@@ -611,6 +611,7 @@ impl LintGroup {
insert_struct_rule!(Spaces, true);
insert_struct_rule!(SpelledNumbers, false);
insert_expr_rule!(SplitWords, true);
+ insert_struct_rule!(SubjectPronoun, true);
insert_expr_rule!(ThatThan, true);
insert_expr_rule!(ThatWhich, true);
insert_expr_rule!(TheHowWhy, true);
@@ -637,6 +638,7 @@ impl LintGroup {
insert_expr_rule!(Whereas, true);
insert_expr_rule!(WidelyAccepted, true);
insert_expr_rule!(WinPrize, true);
+ insert_expr_rule!(WishCould, true);
insert_struct_rule!(WordPressDotcom, true);
insert_expr_rule!(WouldNeverHave, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 52347c41b..47e6fed39 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -196,6 +196,7 @@ mod well_educated;
mod whereas;
mod widely_accepted;
mod win_prize;
+mod wish_could;
mod wordpress_dotcom;
mod would_never_have;
diff --git a/harper-core/src/linting/wish_could.rs b/harper-core/src/linting/wish_could.rs
new file mode 100644
index 000000000..c8165d0ab
--- /dev/null
+++ b/harper-core/src/linting/wish_could.rs
@@ -0,0 +1,167 @@
+use super::{Lint, LintKind, Suggestion};
+use crate::Token;
+use crate::expr::{Expr, SequenceExpr};
+use crate::linting::{ExprLinter, expr_linter::Chunk};
+
+pub struct WishCould {
+ expr: Box<dyn Expr>,
+}
+
+impl Default for WishCould {
+ fn default() -> Self {
+ Self {
+ expr: Box::new(
+ SequenceExpr::word_set(&["wish", "wished", "wishes", "wishing"])
+ .t_ws()
+ .then_any_of(vec![
+ Box::new(SequenceExpr::default().then_subject_pronoun()),
+ Box::new(SequenceExpr::word_set(&[
+ // Elective existential indefinite pronouns
+ "anybody",
+ "anyone",
+ // Universal indefinite pronouns
+ "everybody",
+ "everyone",
+ // Negative indefinite pronouns (correct)
+ "nobody",
+ // Negative indefinite pronouns (incorrect)
+ "noone",
+ // Assertive existential indefinite pronouns
+ "somebody",
+ "someone",
+ // Demonstrative pronouns
+ "these",
+ "this",
+ "those",
+ ])),
+ ])
+ .t_ws()
+ .t_aco("can"),
+ ),
+ }
+ }
+}
+
+impl ExprLinter for WishCould {
+ type Unit = Chunk;
+
+ fn expr(&self) -> &dyn Expr {
+ &*self.expr
+ }
+
+ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
+ let can_tok = toks.last()?;
+ let can_span = can_tok.span;
+
+ Some(Lint {
+ span: can_span,
+ lint_kind: LintKind::Grammar,
+ suggestions: vec![Suggestion::replace_with_match_case_str(
+ "could",
+ can_span.get_content(src),
+ )],
+ message: "Use 'could' instead of 'can' after 'wish'.".to_string(),
+ ..Default::default()
+ })
+ }
+
+ fn description(&self) -> &str {
+ "Checks for `can` being used after `wish` when it should be `could`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::linting::tests::assert_suggestion_result;
+
+ #[test]
+ fn flag_wish_we_can() {
+ assert_suggestion_result(
+ "i wish we can spend more time together",
+ WishCould::default(),
+ "i wish we could spend more time together",
+ );
+ }
+
+ #[test]
+ fn flag_wish_i_can() {
+ assert_suggestion_result(
+ "I wish I can finally forgive myself for all the things I am not.",
+ WishCould::default(),
+ "I wish I could finally forgive myself for all the things I am not.",
+ );
+ }
+
+ #[test]
+ fn flag_wish_you_can() {
+ assert_suggestion_result(
+ "I wish you can find your true love.",
+ WishCould::default(),
+ "I wish you could find your true love.",
+ );
+ }
+
+ #[test]
+ fn flag_wishes_they_can() {
+ assert_suggestion_result(
+ "What your Therapist wishes they can tell you.",
+ WishCould::default(),
+ "What your Therapist wishes they could tell you.",
+ );
+ }
+
+ #[test]
+ fn flag_wishing_someone_can() {
+ assert_suggestion_result(
+ "Forever wishing someone can point me in the right direction",
+ WishCould::default(),
+ "Forever wishing someone could point me in the right direction",
+ );
+ }
+
+ #[test]
+ fn flag_wish_they_can() {
+ assert_suggestion_result(
+ "I wish they can plant more trees on this road.",
+ WishCould::default(),
+ "I wish they could plant more trees on this road.",
+ );
+ }
+
+ #[test]
+ fn flag_wished_he_can() {
+ assert_suggestion_result(
+ "I just wished he can talk and tell me how he feels",
+ WishCould::default(),
+ "I just wished he could talk and tell me how he feels",
+ );
+ }
+
+ #[test]
+ fn wish_this_can() {
+ assert_suggestion_result(
+ "but I wish this can be fixed by Electron team",
+ WishCould::default(),
+ "but I wish this could be fixed by Electron team",
+ )
+ }
+
+ #[test]
+ fn wish_it_can() {
+ assert_suggestion_result(
+ "Wish it can be supported.",
+ WishCould::default(),
+ "Wish it could be supported.",
+ )
+ }
+
+ #[test]
+ fn wish_somebody_can() {
+ assert_suggestion_result(
+ "I wish somebody can fix this issue.",
+ WishCould::default(),
+ "I wish somebody could fix this issue.",
+ )
+ }
+}