Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: no-to-little → little-to-no (#4857)
Andrew Dunbard72ddc5a1d2d
diff --git a/harper-core/default_config.json b/harper-core/default_config.jsonindex 78a1e6c72..73410d8f9 100644--- a/harper-core/default_config.json+++ b/harper-core/default_config.json@@ -5619,6 +5619,13 @@"label": "Little Known"}},+ {+ "Bool": {+ "name": "LittleToNo",+ "state": true,+ "label": "Little to No"+ }+ },{"Bool": {"name": "LongTimeAgo",diff --git a/harper-core/src/linting/lint_group/mod.rs b/harper-core/src/linting/lint_group/mod.rsindex 27f323c51..0a14d91aa 100644--- a/harper-core/src/linting/lint_group/mod.rs+++ b/harper-core/src/linting/lint_group/mod.rs@@ -174,6 +174,7 @@ use super::let_to_do::LetToDo;use super::lets_confusion::LetsConfusion;use super::likewise::Likewise;use super::little_known::LittleKnown;+use super::little_to_no::LittleToNo;use super::long_sentences::LongSentences;use super::long_time_ago::LongTimeAgo;use super::look_down_ones_nose::LookDownOnesNose;@@ -817,6 +818,7 @@ impl LintGroup {insert_struct_rule!(LetsConfusion);insert_expr_rule!(Likewise);insert_struct_rule!(LittleKnown);+ insert_struct_rule!(LittleToNo);insert_struct_rule!(LongSentences);insert_expr_rule!(LongTimeAgo);insert_expr_rule!(LookDownOnesNose);diff --git a/harper-core/src/linting/little_to_no.rs b/harper-core/src/linting/little_to_no.rsnew file mode 100644index 000000000..89dd73f81--- /dev/null+++ b/harper-core/src/linting/little_to_no.rs@@ -0,0 +1,102 @@+use crate::{+ Lint, Token, TokenStringExt,+ expr::{Expr, SequenceExpr},+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},+};++pub struct LittleToNo {+ expr: SequenceExpr,+}++impl Default for LittleToNo {+ fn default() -> Self {+ Self {+ expr: SequenceExpr::aco("no")+ .t_ws_h()+ .t_aco("to")+ .t_ws_h()+ .t_aco("little"),+ }+ }+}++impl ExprLinter for LittleToNo {+ type Unit = Chunk;++ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {+ if toks.len() < 5 {+ return None;+ }++ let correction: Vec<char> =+ [4, 1, 2, 3, 0].iter().try_fold(Vec::new(), |mut acc, &i| {+ acc.extend_from_slice(toks.get(i)?.get_ch(src));+ Some(acc)+ })?;++ let span = toks.span()?;++ Some(Lint {+ span,+ lint_kind: LintKind::Usage,+ suggestions: vec![Suggestion::replace_with_match_case(+ correction,+ span.get_content(src),+ )],+ message: "The phrase `little to no` is more idiomatic than `no to little`.".to_owned(),+ ..Default::default()+ })+ }++ fn expr(&self) -> &dyn Expr {+ &self.expr+ }++ fn description(&self) -> &str {+ "Converts the clunky or unidiomatic phrase `no to little` into the standard English idiom `little to no`."+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::assert_suggestion_result;++ use super::LittleToNo;++ #[test]+ fn hyphen_hyphen() {+ assert_suggestion_result(+ "parents who allow their 15 and 16 year olds to drive on the same streets with no-to-little training",+ LittleToNo::default(),+ "parents who allow their 15 and 16 year olds to drive on the same streets with little-to-no training",+ );+ }++ #[test]+ fn space_space() {+ assert_suggestion_result(+ "There was no to little industrialisation of India.",+ LittleToNo::default(),+ "There was little to no industrialisation of India.",+ );+ }++ #[test]+ #[ignore = "this is surely too rare and quirky to support"]+ fn hyphen_space_hyphen() {+ assert_suggestion_result(+ "The reflecter supports reflection utilities for node.js with no- to little impact on runtime performance.",+ LittleToNo::default(),+ "The reflecter supports reflection utilities for node.js with little- to no impact on runtime performance.",+ );+ }++ #[test]+ fn start_of_sentence() {+ assert_suggestion_result(+ "No to little increase in the number of metrics collected.",+ LittleToNo::default(),+ "Little to no increase in the number of metrics collected.",+ );+ }+}diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 3d3581cc1..f63cc5805 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -185,6 +185,7 @@ mod lint;mod lint_group;mod lint_kind;mod little_known;+mod little_to_no;mod long_sentences;mod long_time_ago;mod look_down_ones_nose;