Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat(that-which): add lint for 'that that' -> 'that which'

Grant Lemons56a1745bc3f9
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 7c1106e5d..ea1e7f380 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -18,6 +18,7 @@ use super::spaces::Spaces;
use super::spell_check::SpellCheck;
use super::spelled_numbers::SpelledNumbers;
use super::terminating_conjunctions::TerminatingConjunctions;
+use super::that_which::ThatWhich;
use super::unclosed_quotes::UnclosedQuotes;
use super::use_genitive::UseGenitive;
use super::wrong_quotes::WrongQuotes;
@@ -129,7 +130,8 @@ create_lint_group_config!(
EllipsisLength => true,
DotInitialisms => true,
BoringWords => false,
- UseGenitive => true
+ UseGenitive => true,
+ ThatWhich => true
);
impl<T: Dictionary + Default> Default for LintGroup<T> {
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index f05f1225a..e2978c1ce 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -18,6 +18,7 @@ mod spaces;
mod spell_check;
mod spelled_numbers;
mod terminating_conjunctions;
+mod that_which;
mod unclosed_quotes;
mod use_genitive;
mod wrong_quotes;
@@ -42,6 +43,7 @@ pub use spaces::Spaces;
pub use spell_check::SpellCheck;
pub use spelled_numbers::SpelledNumbers;
pub use terminating_conjunctions::TerminatingConjunctions;
+pub use that_which::ThatWhich;
pub use unclosed_quotes::UnclosedQuotes;
pub use use_genitive::UseGenitive;
pub use wrong_quotes::WrongQuotes;
diff --git a/harper-core/src/linting/that_which.rs b/harper-core/src/linting/that_which.rs
new file mode 100644
index 000000000..2b147cfca
--- /dev/null
+++ b/harper-core/src/linting/that_which.rs
@@ -0,0 +1,68 @@
+use itertools::Itertools;
+
+use crate::{
+ patterns::{Pattern, SequencePattern, WordPatternGroup},
+ Token, TokenStringExt,
+};
+
+use super::{Lint, LintKind, PatternLinter, Suggestion};
+
+pub struct ThatWhich {
+ pattern: Box<dyn Pattern>,
+}
+
+impl Default for ThatWhich {
+ fn default() -> Self {
+ let mut pattern = WordPatternGroup::default();
+
+ pattern.add(
+ "that",
+ Box::new(
+ SequencePattern::default()
+ .then_exact_word_or_lowercase("That")
+ .then_whitespace()
+ .then_exact_word("that"),
+ ),
+ );
+ pattern.add(
+ "That",
+ Box::new(
+ SequencePattern::default()
+ .then_exact_word_or_lowercase("That")
+ .then_whitespace()
+ .then_exact_word("that"),
+ ),
+ );
+
+ Self {
+ pattern: Box::new(pattern),
+ }
+ }
+}
+
+impl PatternLinter for ThatWhich {
+ fn pattern(&self) -> &dyn Pattern {
+ self.pattern.as_ref()
+ }
+
+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {
+ let suggestion = format!(
+ "{} which",
+ matched_tokens[0]
+ .span
+ .get_content(source)
+ .iter()
+ .collect::<String>()
+ )
+ .chars()
+ .collect_vec();
+
+ Lint {
+ span: matched_tokens.span().unwrap(),
+ lint_kind: LintKind::Repetition,
+ suggestions: vec![Suggestion::ReplaceWith(suggestion)],
+ message: "“that that” sometimes means “that which”, which is clearer.".to_string(),
+ priority: 126,
+ }
+ }
+}