Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
refactor: improve logic and robstness of then→than
hippietrail3d96d2b07933
diff --git a/harper-core/src/linting/then_than.rs b/harper-core/src/linting/then_than.rsindex 146e62d0f..eeb7227ae 100644--- a/harper-core/src/linting/then_than.rs+++ b/harper-core/src/linting/then_than.rs@@ -2,7 +2,8 @@ use super::{Lint, LintKind, PatternLinter};use crate::Token;use crate::linting::Suggestion;use crate::patterns::{- All, AnyCapitalization, Invert, OwnedPatternExt, Pattern, SequencePattern, WordSet,+ All, AnyCapitalization, EitherPattern, Invert, OwnedPatternExt, Pattern, SequencePattern,+ WordSet,};#[doc = "Corrects the misuse of `then` to `than`."]@@ -14,23 +15,54 @@ impl ThenThan {pub fn new() -> Self {Self {pattern: Box::new(All::new(vec![- Box::new(- SequencePattern::default()- .then(WordSet::new(&["better", "other"]).or(Box::new(- |tok: &Token, _source: &[char]| tok.kind.is_adjective(),- )))- .then_whitespace()- .then_any_capitalization_of("then")- .then_whitespace()- .then(Invert::new(AnyCapitalization::of("that"))),- ),- // Denotes exceptions to the rule.+ Box::new(EitherPattern::new(vec![+ // Comparative form of adjective+ Box::new(+ SequencePattern::default()+ .then(WordSet::new(&["other"]).or(Box::new(+ |tok: &Token, source: &[char]| {+ is_comparative_adjective(tok, source)+ },+ )))+ .then_whitespace()+ .then_any_capitalization_of("then")+ .then_whitespace()+ .then(Invert::new(AnyCapitalization::of("that"))),+ ),+ // Positive form of adjective following "more" or "less"+ Box::new(+ SequencePattern::default()+ .then(WordSet::new(&["more", "less"]))+ .then_whitespace()+ .then_adjective()+ .then_whitespace()+ .then_any_capitalization_of("then")+ .then_whitespace()+ .then(Invert::new(AnyCapitalization::of("that"))),+ ),+ ])),+ // Exceptions to the rule.Box::new(Invert::new(WordSet::new(&["back", "this", "so", "but"]))),])),}}}+// TODO: This can be simplified or eliminated when the adjective improvements make it into the affix system.+fn is_comparative_adjective(tok: &Token, source: &[char]) -> bool {+ tok.kind+ .is_adjective()+ .then(|| tok.span.get_content(source))+ .map_or(false, |src| {+ // Regular comparative form?+ src.ends_with(&['e', 'r'])+ // Irregular comparatives.+ || src == &['l', 'e', 's', 's']+ || src == &['m', 'o', 'r', 'e']+ || src == &['w', 'o', 'r', 's', 'e']+ })+}+impl Default for ThenThan {fn default() -> Self {Self::new()@@ -42,7 +74,8 @@ impl PatternLinter for ThenThan {self.pattern.as_ref()}fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {- let span = matched_tokens[2].span;+ // For both "stupider then X" and "more stupid then X", "then" is 3rd last token.+ let span = matched_tokens[matched_tokens.len() - 3].span;let offending_text = span.get_content(source);Some(Lint {@@ -332,4 +365,40 @@ mod tests {"We submitted the patch more recently than last week, so they should have it already.",);}++ #[test]+ fn allows_well_then() {+ assert_lint_count(+ "Well then we're just going to raise all of these taxes",+ ThenThan::default(),+ 0,+ );+ }++ #[test]+ fn allows_nervous_then() {+ assert_lint_count(+ "I think both of us were getting nervous then because the system would have automatically aborted.",+ ThenThan::default(),+ 0,+ );+ }++ #[test]+ fn flags_stupider_then_and_more_and_less_stupid_then() {+ assert_lint_count(+ "He was stupider then her but she was more stupid then some. Then again he was less stupid then some too.",+ ThenThan::default(),+ 3,+ );+ }++ #[test]+ fn patch_worse_then() {+ assert_suggestion_result(+ "He was worse then her at writing code.",+ ThenThan::default(),+ "He was worse than her at writing code.",+ );+ }}