Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): allow the `ThenThan` linter to cover more cases
Elijah Pottere285594cab42
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dictindex 006c8d278..e880776e8 100644--- a/harper-core/dictionary.dict+++ b/harper-core/dictionary.dict@@ -30189,7 +30189,7 @@ lepton/1MSlesbian/514SMlesbianism/1Mlesion/14MS-less/+4517MNRX+less/+457NRXlessee/14MSlessen/47GDlesson/41MS@@ -32690,7 +32690,7 @@ morbidity/1Mmorbidness/1Mmordancy/1Mmordant/514SMY-more/8514MS+more/8514Smoreish/5morel/1SMmoreover/diff --git a/harper-core/src/linting/then_than.rs b/harper-core/src/linting/then_than.rsindex 2d7396690..f079f89ef 100644--- a/harper-core/src/linting/then_than.rs+++ b/harper-core/src/linting/then_than.rs@@ -1,25 +1,28 @@use super::{Lint, LintKind, PatternLinter};use crate::linting::Suggestion;-use crate::patterns::{OwnedPatternExt, Pattern, SequencePattern, WordSet};+use crate::patterns::{All, Invert, OwnedPatternExt, Pattern, SequencePattern, WordSet};use crate::Token;#[doc = "Corrects the misuse of `then` to `than`."]pub struct ThenThan {pattern: Box<dyn Pattern>,}+impl ThenThan {pub fn new() -> Self {Self {- pattern: Box::new(- SequencePattern::default()- .then(Box::new(WordSet::all(&["better", "other"]).or(Box::new(- |tok: &Token, _source: &[char]| {- tok.kind.is_adjective() && !tok.kind.is_noun()- },- ))))- .then_whitespace()- .then_any_capitalization_of("then"),- ),+ pattern: Box::new(All::new(vec![+ Box::new(+ SequencePattern::default()+ .then(Box::new(WordSet::all(&["better", "other"]).or(Box::new(+ |tok: &Token, _source: &[char]| tok.kind.is_adjective(),+ ))))+ .then_whitespace()+ .then_any_capitalization_of("then"),+ ),+ // Denotes exceptions to the rule.+ Box::new(Invert::new(Box::new(WordSet::all(&["back"])))),+ ])),}}}@@ -59,6 +62,56 @@ mod tests {use super::ThenThan;use crate::linting::tests::{assert_lint_count, assert_suggestion_result};+ #[test]+ fn allows_back_then() {+ assert_lint_count("I was a gross kid back then.", ThenThan::default(), 0);+ }++ #[test]+ fn catches_shorter_then() {+ assert_suggestion_result(+ "One was shorter then the other.",+ ThenThan::default(),+ "One was shorter than the other.",+ );+ }++ #[test]+ fn catches_better_then() {+ assert_suggestion_result(+ "One was better then the other.",+ ThenThan::default(),+ "One was better than the other.",+ );+ }++ #[test]+ fn catches_longer_then() {+ assert_suggestion_result(+ "One was longer then the other.",+ ThenThan::default(),+ "One was longer than the other.",+ );+ }++ #[test]+ fn catches_less_then() {+ assert_suggestion_result(+ "I eat less then you.",+ ThenThan::default(),+ "I eat less than you.",+ );+ }++ #[test]+ fn catches_more_then() {+ assert_suggestion_result(+ "I eat more then you.",+ ThenThan::default(),+ "I eat more than you.",+ );+ }+#[test]fn stronger_should_change() {assert_suggestion_result(@@ -108,4 +161,14 @@ mod tests {"There was no one other than us at the campsite.",);}++ #[test]+ fn allows_and_then() {+ assert_lint_count("And then we left.", ThenThan::default(), 0);+ }++ #[test]+ fn allows_this_then() {+ assert_lint_count("Do this then that.", ThenThan::default(), 0);+ }}diff --git a/harper-core/src/patterns/all.rs b/harper-core/src/patterns/all.rsindex a4ed834d0..d8342f4e9 100644--- a/harper-core/src/patterns/all.rs+++ b/harper-core/src/patterns/all.rs@@ -12,6 +12,10 @@ pub struct All {}impl All {+ pub fn new(children: Vec<Box<dyn Pattern>>) -> Self {+ Self { children }+ }+pub fn add(&mut self, p: Box<dyn Pattern>) {self.children.push(p);}