Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: aspire for → aspire to (#3004)
Andrew Dunbarcc0447499c90
diff --git a/harper-core/src/linting/aspire_to.rs b/harper-core/src/linting/aspire_to.rsnew file mode 100644index 000000000..e4215812c--- /dev/null+++ b/harper-core/src/linting/aspire_to.rs@@ -0,0 +1,153 @@+use crate::{+ CharStringExt, Lint, Token,+ expr::{Expr, SequenceExpr},+ linting::{+ ExprLinter, LintKind, Suggestion,+ expr_linter::{Sentence, at_start_of_sentence, followed_by_word, preceded_by_word},+ },+};++pub struct AspireTo {+ expr: SequenceExpr,+}++impl Default for AspireTo {+ fn default() -> Self {+ Self {+ expr: SequenceExpr::word_set(&["aspire", "aspired", "aspires", "aspiring"])+ .t_ws()+ .t_aco("for"),+ }+ }+}++impl ExprLinter for AspireTo {+ type Unit = Sentence;++ fn expr(&self) -> &dyn Expr {+ &self.expr+ }++ fn description(&self) -> &str {+ "Corrects `aspire for` to `aspire to`."+ }++ fn match_to_lint_with_context(+ &self,+ toks: &[Token],+ src: &[char],+ ctx: Option<(&[Token], &[Token])>,+ ) -> Option<Lint> {+ let (aspire_i, prep_i) = (0, 2);+ let (aspire_t, prep_t) = (&toks[aspire_i], &toks[prep_i]);+ let (aspire_s, prep_s) = (aspire_t.span, prep_t.span);++ if aspire_s.get_content(src) == ['A', 's', 'p', 'i', 'r', 'e'] && !at_start_of_sentence(ctx)+ {+ return None;+ }++ if preceded_by_word(ctx, |wt| {+ // .NET is unlintable and we can't use `.get_content()` on unlintable+ if wt.kind.is_preposition() || wt.kind.is_unlintable() {+ true+ } else {+ let chars = wt.span.get_content(src);++ chars == ['N', 'E', 'T']+ || chars.eq_any_ignore_ascii_case_str(&[+ // verbs that indicate Aspire is a tool or product+ "use", "used", "uses", "using",+ // other words that precede Aspire when it's not a verb+ "dotnet",+ ])+ }+ }) || followed_by_word(ctx, |wt| wt.span.get_content(src) == ['A', 'W', 'S'])+ {+ return None;+ }++ Some(Lint {+ span: prep_s,+ lint_kind: LintKind::Usage,+ suggestions: vec![Suggestion::replace_with_match_case_str(+ "to",+ prep_s.get_content(src),+ )],+ message: "Use `aspire to` instead of `aspire for`.".to_string(),+ ..Default::default()+ })+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::{assert_no_lints, assert_suggestion_result};++ use super::AspireTo;++ #[test]+ fn test_aspire_to() {+ assert_suggestion_result("aspire for", AspireTo::default(), "aspire to");+ }++ #[test]+ fn ignore_after_using() {+ assert_no_lints(+ "I am developing an application on ASP.NET Core 8 using Aspire for infrastructure management.",+ AspireTo::default(),+ );+ }++ #[test]+ fn ignore_after_all_caps_net() {+ assert_no_lints(+ "This repositry contains the integrations with .NET Aspire for AWS.",+ AspireTo::default(),+ );+ }++ #[test]+ fn ignore_after_all_caps_net_no_dot() {+ // NOTE the .NET gets tokenized as Unlintable!?+ assert_no_lints(+ "This repositry contains the integrations with NET Aspire for AWS.",+ AspireTo::default(),+ );+ }++ #[test]+ fn dont_ignore_after_lowercase_net() {+ assert_suggestion_result(+ "my net aspires for catch a bug",+ AspireTo::default(),+ "my net aspires to catch a bug",+ );+ }++ #[test]+ fn flag_at_start_of_doc_when_capitalized() {+ assert_suggestion_result(+ "Aspire for greatness, even at the start of a sentence.",+ AspireTo::default(),+ "Aspire to greatness, even at the start of a sentence.",+ );+ }++ #[test]+ fn flag_at_the_start_of_a_sentence_mid_document() {+ assert_suggestion_result(+ "This is a sentence. Aspire for greatness, even at the start of a mid-document sentence.",+ AspireTo::default(),+ "This is a sentence. Aspire to greatness, even at the start of a mid-document sentence.",+ );+ }++ #[test]+ fn dont_flag_capitalized_mid_document() {+ assert_no_lints(+ "I reccumend Aspire for your next project.",+ AspireTo::default(),+ );+ }+}diff --git a/harper-core/src/linting/expr_linter.rs b/harper-core/src/linting/expr_linter.rsindex baeaae036..675231e3f 100644--- a/harper-core/src/linting/expr_linter.rs+++ b/harper-core/src/linting/expr_linter.rs@@ -158,6 +158,17 @@ pub fn followed_by_hyphen(context: Option<(&[Token], &[Token])>) -> bool {.is_some_and(|hy| hy.kind.is_hyphen())}+/// Counterintuitively, a sentence includes the whitespace after+/// the sentence-final punctuation.+pub fn at_start_of_sentence(context: Option<(&[Token], &[Token])>) -> bool {+ if let Some((before, _)) = context+ && (before.is_empty() || (before.len() == 1 && before[0].kind.is_whitespace()))+ {+ return true;+ }+ false+}+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.rsindex 80cf1ae41..83ef6cf97 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -29,6 +29,7 @@ use super::another_thing_coming::AnotherThingComing;use super::another_think_coming::AnotherThinkComing;use super::apart_from::ApartFrom;use super::ask_no_preposition::AskNoPreposition;+use super::aspire_to::AspireTo;use super::avoid_curses::AvoidCurses;use super::back_in_the_day::BackInTheDay;use super::be_allowed::BeAllowed;@@ -834,6 +835,10 @@ impl LintGroup {insert_expr_rule!(WouldNeverHave, true);insert_expr_rule!(WrongApostrophe, true);+ // Uses Sentence rather than Chunk+ out.add("AspireTo", AspireTo::default());+ out.config.set_rule_enabled("AspireTo", true);+// Uses Sentence rather than Chunkout.add("Damages", Damages::default());out.config.set_rule_enabled("Damages", true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex b281ced90..e30c3738f 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -20,6 +20,7 @@ mod another_thing_coming;mod another_think_coming;mod apart_from;mod ask_no_preposition;+mod aspire_to;mod avoid_curses;mod back_in_the_day;mod be_allowed;