Skip to content

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

feat:far bit it for→far bit it from (#2152)

Andrew Dunbar953b6e635a1c
diff --git a/harper-core/src/linting/far_be_it.rs b/harper-core/src/linting/far_be_it.rs
new file mode 100644
index 000000000..9457bc327
--- /dev/null
+++ b/harper-core/src/linting/far_be_it.rs
@@ -0,0 +1,123 @@
+use crate::char_string::CharStringExt;
+use crate::expr::{Expr, SequenceExpr};
+use crate::linting::{ExprLinter, Lint, LintKind, Suggestion};
+use crate::token::Token;
+
+pub struct FarBeIt {
+ expr: Box<dyn Expr>,
+}
+
+impl Default for FarBeIt {
+ fn default() -> Self {
+ Self {
+ expr: Box::new(
+ SequenceExpr::default()
+ .t_aco("far")
+ .t_ws()
+ .t_aco("be")
+ .t_ws()
+ .t_aco("it")
+ .t_ws()
+ .then_word_except(&["from"]),
+ ),
+ }
+ }
+}
+
+impl ExprLinter for FarBeIt {
+ fn expr(&self) -> &dyn Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
+ let span = toks.last()?.span;
+ let content = span.get_content(src);
+
+ // We can only correct using `far be it for`, otherwise we recommend rephrasing the sentence.
+ let (suggestions, message) = if span.get_content(src).eq_ignore_ascii_case_str("for") {
+ (
+ vec![Suggestion::replace_with_match_case(
+ vec!['f', 'r', 'o', 'm'],
+ content,
+ )],
+ "`Far be it for` is a common error for `far be it from`".to_string(),
+ )
+ } else {
+ (vec![], "The correct usage of the idiom is `far be it from` [someone] to [do something]. Try to rephrase the sentence.".to_string())
+ };
+
+ Some(Lint {
+ span,
+ lint_kind: LintKind::Usage,
+ suggestions,
+ message,
+ ..Default::default()
+ })
+ }
+
+ fn description(&self) -> &'static str {
+ "Flags misuse of `far be it` and suggests using `from` when it is followed by `for`"
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::{
+ FarBeIt,
+ tests::{assert_no_lints, assert_suggestion_count, assert_suggestion_result},
+ };
+
+ #[test]
+ fn far_be_it_for_me_capitalized() {
+ assert_suggestion_result(
+ "Far be it for me to suggestion that additional cardinality be added to the already TOO MUCH CARDINALITY metric space.",
+ FarBeIt::default(),
+ "Far be it from me to suggestion that additional cardinality be added to the already TOO MUCH CARDINALITY metric space.",
+ );
+ }
+
+ #[test]
+ fn far_be_it_for_me_lowercase() {
+ assert_suggestion_result(
+ "Far be it for me to tell people what to do so I'm not earnestly proposing to take away the ability to add literals to lazyframes.",
+ FarBeIt::default(),
+ "Far be it from me to tell people what to do so I'm not earnestly proposing to take away the ability to add literals to lazyframes.",
+ );
+ }
+
+ #[test]
+ fn far_be_it_that() {
+ assert_suggestion_count(
+ "Far be it that I get in the middle of this thread (and the complexity WebAuthn has spawned)",
+ FarBeIt::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn far_be_it_for_the_software() {
+ assert_suggestion_result(
+ "Far be it for the software to give any indication of that fact.",
+ FarBeIt::default(),
+ "Far be it from the software to give any indication of that fact.",
+ );
+ }
+
+ #[test]
+ #[ignore = "No punctuation between '... so far' and 'be it ...'"]
+ fn missing_punctuation_false_positive() {
+ assert_no_lints(
+ "but it is failing for master and all the 11.x branches i have tried so far be it 11.0.0, 11.0.1 ...",
+ FarBeIt::default(),
+ );
+ }
+
+ #[test]
+ fn far_be_it_to() {
+ assert_suggestion_count(
+ "I'm not a marketing guy, so far be it to second guess that.",
+ FarBeIt::default(),
+ 0,
+ );
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index a12068900..ae480c093 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -53,6 +53,7 @@ use super::everyday::Everyday;
use super::expand_memory_shorthands::ExpandMemoryShorthands;
use super::expand_time_shorthands::ExpandTimeShorthands;
use super::expr_linter::run_on_chunk;
+use super::far_be_it::FarBeIt;
use super::feel_fell::FeelFell;
use super::few_units_of_time_ago::FewUnitsOfTimeAgo;
use super::filler_words::FillerWords;
@@ -494,6 +495,7 @@ impl LintGroup {
insert_struct_rule!(Everyday, true);
insert_expr_rule!(ExpandMemoryShorthands, true);
insert_expr_rule!(ExpandTimeShorthands, true);
+ insert_expr_rule!(FarBeIt, true);
insert_expr_rule!(FeelFell, true);
insert_expr_rule!(FewUnitsOfTimeAgo, true);
insert_expr_rule!(FillerWords, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index c9c92153d..6d44b5711 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -49,6 +49,7 @@ mod everyday;
mod expand_memory_shorthands;
mod expand_time_shorthands;
mod expr_linter;
+mod far_be_it;
mod feel_fell;
mod few_units_of_time_ago;
mod filler_words;
@@ -229,6 +230,7 @@ pub use everyday::Everyday;
pub use expand_memory_shorthands::ExpandMemoryShorthands;
pub use expand_time_shorthands::ExpandTimeShorthands;
pub use expr_linter::ExprLinter;
+pub use far_be_it::FarBeIt;
pub use feel_fell::FeelFell;
pub use few_units_of_time_ago::FewUnitsOfTimeAgo;
pub use filler_words::FillerWords;