Skip to content

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

feat: implement #1559 semicolon typos (#1560)

Andrew Dunbar175ed313a208
diff --git a/harper-core/src/expr/sequence_expr.rs b/harper-core/src/expr/sequence_expr.rs
index 1be58f1c8..8aae69c00 100644
--- a/harper-core/src/expr/sequence_expr.rs
+++ b/harper-core/src/expr/sequence_expr.rs
@@ -173,34 +173,48 @@ impl SequenceExpr {
self.then(AnyPattern)
}
- gen_then_from_is!(nominal);
+ // POS - Nominals - Nouns
gen_then_from_is!(noun);
+ gen_then_from_is!(proper_noun);
+
+ // POS - Nominals - Pronouns
+ gen_then_from_is!(pronoun);
+ gen_then_from_is!(third_person_pronoun);
+ gen_then_from_is!(third_person_singular_pronoun);
+ gen_then_from_is!(third_person_plural_pronoun);
+ gen_then_from_is!(first_person_singular_pronoun);
+ gen_then_from_is!(first_person_plural_pronoun);
+ gen_then_from_is!(second_person_pronoun);
+
+ // POS - Nominals
+ gen_then_from_is!(nominal);
gen_then_from_is!(possessive_nominal);
gen_then_from_is!(plural_nominal);
+ gen_then_from_is!(non_plural_nominal);
+
+ // POS - Verbs
gen_then_from_is!(verb);
gen_then_from_is!(auxiliary_verb);
gen_then_from_is!(linking_verb);
- gen_then_from_is!(pronoun);
- gen_then_from_is!(punctuation);
+
+ // POS - Other
+ gen_then_from_is!(adverb);
+ gen_then_from_is!(adjective);
gen_then_from_is!(conjunction);
+ gen_then_from_is!(determiner);
+ gen_then_from_is!(preposition);
+
+ // Punctuation
+ gen_then_from_is!(punctuation);
+ gen_then_from_is!(apostrophe);
gen_then_from_is!(comma);
+ gen_then_from_is!(hyphen);
gen_then_from_is!(period);
+ gen_then_from_is!(semicolon);
+
+ // Other
gen_then_from_is!(number);
gen_then_from_is!(case_separator);
- gen_then_from_is!(adverb);
- gen_then_from_is!(adjective);
- gen_then_from_is!(apostrophe);
- gen_then_from_is!(hyphen);
- gen_then_from_is!(determiner);
- gen_then_from_is!(proper_noun);
- gen_then_from_is!(preposition);
- gen_then_from_is!(third_person_pronoun);
- gen_then_from_is!(third_person_singular_pronoun);
- gen_then_from_is!(third_person_plural_pronoun);
- gen_then_from_is!(first_person_singular_pronoun);
- gen_then_from_is!(first_person_plural_pronoun);
- gen_then_from_is!(second_person_pronoun);
- gen_then_from_is!(non_plural_nominal);
}
impl<S> From<S> for SequenceExpr
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 1ece67b54..5b8569aca 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -80,6 +80,7 @@ use super::redundant_additive_adverbs::RedundantAdditiveAdverbs;
use super::regionalisms::Regionalisms;
use super::repeated_words::RepeatedWords;
use super::save_to_safe::SaveToSafe;
+use super::semicolon_apostrophe::SemicolonApostrophe;
use super::sentence_capitalization::SentenceCapitalization;
use super::shoot_oneself_in_the_foot::ShootOneselfInTheFoot;
use super::since_duration::SinceDuration;
@@ -442,6 +443,7 @@ impl LintGroup {
insert_expr_rule!(RedundantAdditiveAdverbs, true);
insert_struct_rule!(RepeatedWords, true);
insert_struct_rule!(SaveToSafe, true);
+ insert_expr_rule!(SemicolonApostrophe, true);
insert_expr_rule!(SinceDuration, true);
insert_expr_rule!(ShootOneselfInTheFoot, true);
insert_expr_rule!(SomewhatSomething, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index c3907c479..74f095513 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -90,6 +90,7 @@ mod redundant_additive_adverbs;
mod regionalisms;
mod repeated_words;
mod save_to_safe;
+mod semicolon_apostrophe;
mod sentence_capitalization;
mod shoot_oneself_in_the_foot;
mod since_duration;
@@ -189,6 +190,7 @@ pub use redundant_additive_adverbs::RedundantAdditiveAdverbs;
pub use regionalisms::Regionalisms;
pub use repeated_words::RepeatedWords;
pub use save_to_safe::SaveToSafe;
+pub use semicolon_apostrophe::SemicolonApostrophe;
pub use sentence_capitalization::SentenceCapitalization;
pub use shoot_oneself_in_the_foot::ShootOneselfInTheFoot;
pub use since_duration::SinceDuration;
diff --git a/harper-core/src/linting/semicolon_apostrophe.rs b/harper-core/src/linting/semicolon_apostrophe.rs
new file mode 100644
index 000000000..2fc6482fe
--- /dev/null
+++ b/harper-core/src/linting/semicolon_apostrophe.rs
@@ -0,0 +1,119 @@
+use crate::{
+ Token, TokenStringExt,
+ expr::{Expr, SequenceExpr},
+ linting::{ExprLinter, Lint, LintKind, Suggestion},
+ patterns::WordSet,
+};
+
+const CONTRACTION_AND_POSSESSIVE_ENDINGS: [&str; 7] = ["d", "ll", "m", "re", "s", "t", "ve"];
+
+pub struct SemicolonApostrophe {
+ expr: Box<dyn Expr>,
+}
+
+impl Default for SemicolonApostrophe {
+ fn default() -> Self {
+ Self {
+ expr: Box::new(
+ SequenceExpr::default()
+ .then_any_word()
+ .then_semicolon()
+ .then(WordSet::new(&CONTRACTION_AND_POSSESSIVE_ENDINGS)),
+ ),
+ }
+ }
+}
+
+impl ExprLinter for SemicolonApostrophe {
+ fn expr(&self) -> &dyn Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
+ let whole_span = toks.span()?;
+ let base = &toks.first()?;
+ let ending = &toks.last()?;
+
+ let replacement_str = format!(
+ "{}'{}",
+ base.span.get_content_string(src).to_lowercase(),
+ ending.span.get_content_string(src).to_lowercase()
+ );
+
+ let lettercase_template_char_slice = base.span.get_content(src);
+
+ Some(Lint {
+ span: whole_span,
+ lint_kind: LintKind::Typo,
+ suggestions: vec![Suggestion::replace_with_match_case(
+ replacement_str.chars().collect(),
+ lettercase_template_char_slice,
+ )],
+ message: format!("Did you mean `{replacement_str}`?"),
+ priority: 57,
+ })
+ }
+
+ fn description(&self) -> &str {
+ "Corrects semicolons accidentally typed instead of apostrophes."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::{
+ SemicolonApostrophe,
+ tests::{assert_lint_count, assert_suggestion_result},
+ };
+
+ #[test]
+ fn fix_dont_with_semicolon_to_apostrophe() {
+ assert_suggestion_result(
+ "It's better if you don;t type like this.",
+ SemicolonApostrophe::default(),
+ "It's better if you don't type like this.",
+ );
+ }
+
+ #[test]
+ fn ignore_correct() {
+ assert_lint_count("I don't doubt it.", SemicolonApostrophe::default(), 0);
+ }
+
+ #[test]
+ fn fix_title_case() {
+ assert_suggestion_result(
+ "Don;t type like this.",
+ SemicolonApostrophe::default(),
+ "Don't type like this.",
+ );
+ }
+
+ #[test]
+ fn fix_all_caps() {
+ assert_suggestion_result(
+ "DON;T TRY THIS AT HOME.",
+ SemicolonApostrophe::default(),
+ "DON'T TRY THIS AT HOME.",
+ );
+ }
+
+ #[test]
+ #[ignore = "replace_with_match_case has a bug turning `I'll` into `I'LL`"]
+ fn fix_ill_and_monkeys() {
+ assert_suggestion_result(
+ "Well I;ll be a monkey;s uncle!",
+ SemicolonApostrophe::default(),
+ "Well I'll be a monkey's uncle!",
+ )
+ }
+
+ #[test]
+ fn fix_other_contractions_and_possessives() {
+ assert_suggestion_result(
+ "Let;s see if we;ve fixed patrakov;s bug. Fun wasn;t it?",
+ SemicolonApostrophe::default(),
+ "Let's see if we've fixed patrakov's bug. Fun wasn't it?",
+ )
+ }
+}
diff --git a/harper-core/src/token_kind.rs b/harper-core/src/token_kind.rs
index aec71f512..0096de34c 100644
--- a/harper-core/src/token_kind.rs
+++ b/harper-core/src/token_kind.rs
@@ -184,6 +184,10 @@ impl TokenKind {
matches!(self, TokenKind::Punctuation(Punctuation::Comma))
}
+ pub fn is_semicolon(&self) -> bool {
+ matches!(self, TokenKind::Punctuation(Punctuation::Semicolon))
+ }
+
// Miscellaneous is-methods
/// Checks whether a token is word-like--meaning it is more complex than punctuation and can