Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: analog/acoustic bike→pushbike/pedal bike (#3444)
Andrew Dunbara391e8a789e0
diff --git a/harper-core/default_config.json b/harper-core/default_config.jsonindex 8349ecd2d..632a7929d 100644--- a/harper-core/default_config.json+++ b/harper-core/default_config.json@@ -5208,6 +5208,13 @@"label": "Crave For"}},+ {+ "Bool": {+ "name": "AnalogAcousticBike",+ "state": false,+ "label": "Analog/Acoustic Bike"+ }+ },{"Bool": {"name": "InThisThatRegard",diff --git a/harper-core/src/linting/analog_acoustic_bike.rs b/harper-core/src/linting/analog_acoustic_bike.rsnew file mode 100644index 000000000..62aac6adf--- /dev/null+++ b/harper-core/src/linting/analog_acoustic_bike.rs@@ -0,0 +1,169 @@+use crate::{+ Dialect, Lint, Token, TokenStringExt,+ expr::{Expr, SequenceExpr},+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},+ patterns::IndefiniteArticle,+};++pub struct AnalogAcousticBike {+ expr: SequenceExpr,+ dialect: Dialect,+}++impl AnalogAcousticBike {+ pub fn new(dialect: Dialect) -> Self {+ let expr = SequenceExpr::optional(SequenceExpr::with(IndefiniteArticle::default()).t_ws())+ .t_set(&["acoustic", "analog", "analogue"])+ .t_ws()+ .t_set(&["bike", "bikes", "bicycle", "bicycles"]);++ Self { expr, dialect }+ }+}++impl ExprLinter for AnalogAcousticBike {+ type Unit = Chunk;++ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {+ // TODO: address the optional indefinite article+ let toks = &toks[toks.len() - 3..];++ let full_span = toks.span()?;++ let ws_cycle = toks[1..=2].get_ch(src)?;+ let cycle = toks[2..].get_ch(src)?;++ let join = |qualifier: &str, cycle: &[char]| -> Vec<char> {+ qualifier.chars().chain(cycle.iter().copied()).collect()+ };++ let is_bike = cycle.contains(&'k');+ let use_push = matches!(self.dialect, Dialect::British | Dialect::Australian);++ Some(Lint {+ span: full_span,+ lint_kind: LintKind::WordChoice,+ suggestions: [+ (use_push, "push", if is_bike { cycle } else { ws_cycle }),+ (true, "pedal", ws_cycle),+ ]+ .into_iter()+ .filter(|(add_ws, _, _)| *add_ws)+ .map(|(_, qualifier, cycle)| {+ Suggestion::replace_with_match_case(+ join(qualifier, cycle),+ full_span.get_content(src),+ )+ })+ .collect(),+ message: "Consider a more standard term.".to_string(),+ ..Default::default()+ })+ }++ fn expr(&self) -> &dyn Expr {+ &self.expr+ }++ fn description(&self) -> &str {+ "Suggests more standard terms for `analog/analogue bike` and `acoustic bike`."+ }+}++#[cfg(test)]+mod tests {+ use crate::{Dialect, linting::tests::assert_good_and_bad_suggestions};++ use super::AnalogAcousticBike;++ #[test]+ fn fix_acoustic_bike_us() {+ assert_good_and_bad_suggestions(+ "you might get something that helps you move around your city faster and with less sweat than an acoustic bike",+ AnalogAcousticBike::new(Dialect::American),+ &[+ "you might get something that helps you move around your city faster and with less sweat than an pedal bike",+ ],+ &[+ "you might get something that helps you move around your city faster and with less sweat than an pushbike",+ ],+ );+ }++ #[test]+ fn fix_acoustic_bikes_uk() {+ assert_good_and_bad_suggestions(+ "Subjectively, it feels as adequately braked as any of my acoustic bikes",+ AnalogAcousticBike::new(Dialect::British),+ &[+ "Subjectively, it feels as adequately braked as any of my pedal bikes",+ "Subjectively, it feels as adequately braked as any of my pushbikes",+ ],+ &[],+ );+ }++ #[test]+ fn fix_analog_bike_ca() {+ assert_good_and_bad_suggestions(+ "Pretty sure almost anyone can go faster downhill on an analog bike than the 25kph that Ebike pedal assistance is limited to.",+ AnalogAcousticBike::new(Dialect::Canadian),+ &[+ "Pretty sure almost anyone can go faster downhill on an pedal bike than the 25kph that Ebike pedal assistance is limited to.",+ ],+ &[+ "Pretty sure almost anyone can go faster downhill on an pushbike than the 25kph that Ebike pedal assistance is limited to.",+ ],+ );+ }++ #[test]+ fn fix_analog_bikes_au() {+ assert_good_and_bad_suggestions(+ "These bikes are not modified analog bikes — they are ground-up designs built for assisted momentum.",+ AnalogAcousticBike::new(Dialect::Australian),+ &[+ "These bikes are not modified pedal bikes — they are ground-up designs built for assisted momentum.",+ "These bikes are not modified pushbikes — they are ground-up designs built for assisted momentum.",+ ],+ &[],+ );+ }++ #[test]+ fn fix_acoustic_bicycle_uk() {+ assert_good_and_bad_suggestions(+ "I still play alcohol powered instruments (double bass, cello), and ride an acoustic bicycle",+ AnalogAcousticBike::new(Dialect::British),+ &[+ "I still play alcohol powered instruments (double bass, cello), and ride an pedal bicycle",+ ],+ &[+ "I still play alcohol powered instruments (double bass, cello), and ride an pushbike",+ ],+ );+ }++ #[test]+ fn fix_analog_bicycles_us() {+ assert_good_and_bad_suggestions(+ "both light electric bicycles and heavy analog bicycles exist",+ AnalogAcousticBike::new(Dialect::American),+ &["both light electric bicycles and heavy pedal bicycles exist"],+ &["both light electric bicycles and heavy pushbikes exist"],+ );+ }++ #[test]+ fn fix_analogue_bicycle_au() {+ assert_good_and_bad_suggestions(+ "There's crossover with a good ol' analogue bicycle, but I found it awkward to rock up at places and events sweating.",+ AnalogAcousticBike::new(Dialect::Australian),+ &[+ "There's crossover with a good ol' pedal bicycle, but I found it awkward to rock up at places and events sweating.",+ "There's crossover with a good ol' push bicycle, but I found it awkward to rock up at places and events sweating.",+ ],+ &[],+ );+ }+}diff --git a/harper-core/src/linting/lint_group/mod.rs b/harper-core/src/linting/lint_group/mod.rsindex b4e79171b..d5f866094 100644--- a/harper-core/src/linting/lint_group/mod.rs+++ b/harper-core/src/linting/lint_group/mod.rs@@ -23,6 +23,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::analog_acoustic_bike::AnalogAcousticBike;use super::and_the_like::AndTheLike;use super::another_thing_coming::AnotherThingComing;use super::another_think_coming::AnotherThinkComing;@@ -591,6 +592,7 @@ impl LintGroup {insert_expr_rule!(AmInTheMorning);insert_expr_rule!(AmountsFor);insert_struct_rule_with_dialect!(AnA);+ insert_expr_rule_with_dialect!(AnalogAcousticBike);insert_expr_rule!(AndTheLike);insert_expr_rule!(AnotherThingComing);insert_expr_rule!(AnotherThinkComing);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex ea69f5da9..27312f26d 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -15,6 +15,7 @@ mod allow_to;mod am_in_the_morning;mod amounts_for;mod an_a;+mod analog_acoustic_bike;mod and_the_like;mod another_thing_coming;mod another_think_coming;