Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix: change `AndIn` from `phrase_correction` to a linter (#2207)
Andrew Dunbarb284ac04bda6
diff --git a/harper-core/src/linting/and_in.rs b/harper-core/src/linting/and_in.rsnew file mode 100644index 000000000..09b3d30d2--- /dev/null+++ b/harper-core/src/linting/and_in.rs@@ -0,0 +1,92 @@+use crate::Token;+use crate::expr::{Expr, SequenceExpr};+use crate::linting::{ExprLinter, Lint, LintKind, Suggestion};++pub struct AndIn {+ expr: Box<dyn Expr>,+}++impl Default for AndIn {+ fn default() -> Self {+ Self {+ expr: Box::new(SequenceExpr::fixed_phrase("an in").then_optional_hyphen()),+ }+ }+}++impl ExprLinter for AndIn {+ fn expr(&self) -> &dyn Expr {+ &*self.expr+ }++ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {+ if toks.len() != 3 {+ return None;+ }++ Some(Lint {+ span: toks[0].span,+ lint_kind: LintKind::Typo,+ message: "Did you mean `and in`?".to_string(),+ suggestions: vec![Suggestion::replace_with_match_case(+ ['a', 'n', 'd'].to_vec(),+ toks[2].span.get_content(src),+ )],+ ..Default::default()+ })+ }++ fn description(&self) -> &str {+ "Fixes the incorrect phrase `an in` to `and in` for proper conjunction usage."+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::{+ AndIn,+ tests::{assert_no_lints, assert_suggestion_result},+ };++ #[test]+ fn dont_flag_an_in_house() {+ assert_no_lints(+ "for several years as an in-house engine, used to ...",+ AndIn::default(),+ );+ }++ #[test]+ fn dont_flag_an_in_memory() {+ assert_no_lints(+ "including an in-memory real-time Vector Index,",+ AndIn::default(),+ );+ }++ #[test]+ fn dont_flag_an_in_the_moment() {+ assert_no_lints(+ "His words serve as an in-the-moment explanation for what had happened.",+ AndIn::default(),+ );+ }++ #[test]+ fn fix_an_in_to_and_in() {+ assert_suggestion_result(+ "This is an expensive operation, so try to only do it at startup an in tests.",+ AndIn::default(),+ "This is an expensive operation, so try to only do it at startup and in tests.",+ );+ }++ #[test]+ #[ignore = "This is a known false positive - `an in` can be valid in some contexts"]+ fn dont_flag_an_in_with_company() {+ assert_no_lints(+ "His parents got him an in with the company.",+ AndIn::default(),+ );+ }+}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex ae480c093..3ebd9f7e8 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -21,6 +21,7 @@ use super::allow_to::AllowTo;use super::am_in_the_morning::AmInTheMorning;use super::amounts_for::AmountsFor;use super::an_a::AnA;+use super::and_in::AndIn;use super::another_thing_coming::AnotherThingComing;use super::another_think_coming::AnotherThinkComing;use super::ask_no_preposition::AskNoPreposition;@@ -461,6 +462,7 @@ impl LintGroup {insert_struct_rule!(AmInTheMorning, true);insert_expr_rule!(AmountsFor, true);insert_struct_rule!(AnA, true);+ insert_expr_rule!(AndIn, true);insert_expr_rule!(AnotherThingComing, true);insert_expr_rule!(AnotherThinkComing, false);insert_expr_rule!(AskNoPreposition, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 6d44b5711..d01d57a64 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -12,6 +12,7 @@ mod allow_to;mod am_in_the_morning;mod amounts_for;mod an_a;+mod and_in;mod another_thing_coming;mod another_think_coming;mod ask_no_preposition;@@ -197,6 +198,7 @@ pub use allow_to::AllowTo;pub use am_in_the_morning::AmInTheMorning;pub use amounts_for::AmountsFor;pub use an_a::AnA;+pub use and_in::AndIn;pub use another_thing_coming::AnotherThingComing;pub use another_think_coming::AnotherThinkComing;pub use ask_no_preposition::AskNoPreposition;diff --git a/harper-core/src/linting/phrase_corrections/mod.rs b/harper-core/src/linting/phrase_corrections/mod.rsindex 81e9ff65f..1068f91d2 100644--- a/harper-core/src/linting/phrase_corrections/mod.rs+++ b/harper-core/src/linting/phrase_corrections/mod.rs@@ -92,13 +92,6 @@ pub fn lint_group() -> LintGroup {"Corrects `an another` and `a another`.",LintKind::Redundancy),- "AndIn" => (- ["an in"],- ["and in"],- "Did you mean `and in`?",- "Fixes the incorrect phrase `an in` to `and in` for proper conjunction usage.",- LintKind::Typo- ),"AndTheLike" => (["an the like"],["and the like"],diff --git a/harper-core/src/linting/phrase_corrections/tests.rs b/harper-core/src/linting/phrase_corrections/tests.rsindex 89fff0a7f..6e7dc9e6d 100644--- a/harper-core/src/linting/phrase_corrections/tests.rs+++ b/harper-core/src/linting/phrase_corrections/tests.rs@@ -250,9 +250,6 @@ fn correct_a_another() {);}-// AndIn-// -none--// AndTheLike// -none-diff --git a/harper-core/tests/text/linters/Difficult sentences.snap.yml b/harper-core/tests/text/linters/Difficult sentences.snap.ymlindex 3dd2647c6..e6084fa65 100644--- a/harper-core/tests/text/linters/Difficult sentences.snap.yml+++ b/harper-core/tests/text/linters/Difficult sentences.snap.yml@@ -105,12 +105,12 @@ Suggest:-Lint: Typo (31 priority)+Lint: Typo (127 priority)Message: |243 | His parents got him an in with the company.- | ^~~~~ Did you mean `and in`?+ | ^~ Did you mean `and in`?Suggest:- - Replace with: “and in”+ - Replace with: “and”