Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: jump a gun/jump the guns→jump the gun (#3404)
Andrew Dunbarf631dc0a9b5f
diff --git a/harper-core/default_config.json b/harper-core/default_config.jsonindex a102c26cf..debcb5a57 100644--- a/harper-core/default_config.json+++ b/harper-core/default_config.json@@ -5159,6 +5159,13 @@"label": "Long Time Ago"}},+ {+ "Bool": {+ "name": "JumpTheGun",+ "state": true,+ "label": "Jump The Gun"+ }+ },{"Bool": {"name": "CraveFor",diff --git a/harper-core/src/linting/jump_the_gun.rs b/harper-core/src/linting/jump_the_gun.rsnew file mode 100644index 000000000..6786aebf1--- /dev/null+++ b/harper-core/src/linting/jump_the_gun.rs@@ -0,0 +1,105 @@+use crate::{+ Lint, Token, TokenStringExt,+ expr::{All, Expr, OwnedExprExt, SequenceExpr},+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},+};++pub struct JumpTheGun {+ expr: All,+}++impl Default for JumpTheGun {+ fn default() -> Self {+ Self {+ expr: SequenceExpr::word_set(&["jump", "jumped", "jumping", "jumps"])+ .t_ws()+ .t_set(&["a", "an", "the"])+ .t_ws()+ .t_set(&["gun", "guns"])+ .but_not(+ SequenceExpr::anything()+ .t_any()+ .t_aco("the")+ .t_ws()+ .t_aco("gun"),+ ),+ }+ }+}++impl ExprLinter for JumpTheGun {+ type Unit = Chunk;++ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {+ let det_ws_gun_span = matched_tokens[2..].span()?;++ Some(Lint {+ span: det_ws_gun_span,+ lint_kind: LintKind::Usage,+ suggestions: vec![Suggestion::replace_with_match_case_str(+ "the gun",+ det_ws_gun_span.get_content(source),+ )],+ message: "The correct idiom is `jump the gun`".to_string(),+ ..Default::default()+ })+ }++ fn expr(&self) -> &dyn Expr {+ &self.expr+ }++ fn description(&self) -> &str {+ "Detects incorrect usage of the `jump the gun` idiom."+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::{assert_no_lints, assert_suggestion_result};++ use super::JumpTheGun;++ #[test]+ fn dont_flag_jump_the_gun() {+ assert_no_lints("jump the gun", JumpTheGun::default());+ }++ // Real-world tests++ #[test]+ fn fix_jump_a_gun() {+ assert_suggestion_result(+ "But in Race Max Pro, it's possible to get a happy finger case and jump a gun.",+ JumpTheGun::default(),+ "But in Race Max Pro, it's possible to get a happy finger case and jump the gun.",+ );+ }++ #[test]+ fn fix_jumped_a_gun() {+ assert_suggestion_result(+ "Well, I may have jumped a gun here creating this issue.",+ JumpTheGun::default(),+ "Well, I may have jumped the gun here creating this issue.",+ );+ }++ #[test]+ fn fix_jumped_the_guns() {+ assert_suggestion_result(+ "Lexxy being a js library I jumped the guns and felt removing the button completely on initialization was safer.",+ JumpTheGun::default(),+ "Lexxy being a js library I jumped the gun and felt removing the button completely on initialization was safer.",+ );+ }++ #[test]+ fn fix_jumping_the_guns() {+ assert_suggestion_result(+ "To make sure I'm not jumping the guns, @hkuadithya how did you change the JDK version exactly?",+ JumpTheGun::default(),+ "To make sure I'm not jumping the gun, @hkuadithya how did you change the JDK version exactly?",+ );+ }+}diff --git a/harper-core/src/linting/lint_group/mod.rs b/harper-core/src/linting/lint_group/mod.rsindex 6ce7ac7ca..35f0bd45d 100644--- a/harper-core/src/linting/lint_group/mod.rs+++ b/harper-core/src/linting/lint_group/mod.rs@@ -125,6 +125,7 @@ use super::its_contraction::ItsContraction;use super::its_possessive::ItsPossessive;use super::jealous_of::JealousOf;use super::johns_hopkins::JohnsHopkins;+use super::jump_the_gun::JumpTheGun;use super::lead_rise_to::LeadRiseTo;use super::leaving_in_droves::LeavingInDroves;use super::left_right_hand::LeftRightHand;@@ -697,6 +698,7 @@ impl LintGroup {insert_expr_rule!(ItsPossessive, true);insert_expr_rule!(JealousOf, true);insert_expr_rule!(JohnsHopkins, true);+ insert_expr_rule!(JumpTheGun, true);insert_expr_rule!(LeadRiseTo, true);insert_expr_rule!(LeavingInDroves, true);insert_expr_rule!(LeftRightHand, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 91a7bc895..1848ae6bf 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -127,6 +127,7 @@ mod its_contraction;mod its_possessive;mod jealous_of;mod johns_hopkins;+mod jump_the_gun;mod lead_rise_to;mod leaving_in_droves;mod left_right_hand;