Skip to content

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

feat(core): added rule for removing redundant `the`

Elijah Potter2d9cc74a8779
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index eefced450..5b5101f4b 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -57,6 +57,7 @@ use super::spaces::Spaces;
use super::spell_check::SpellCheck;
use super::spelled_numbers::SpelledNumbers;
use super::that_which::ThatWhich;
+use super::the_how_why::TheHowWhy;
use super::then_than::ThenThan;
use super::unclosed_quotes::UnclosedQuotes;
use super::use_genitive::UseGenitive;
@@ -342,6 +343,7 @@ impl LintGroup {
insert_struct_rule!(LetsConfusion, true);
insert_pattern_rule!(DespiteOf, true);
insert_pattern_rule!(ChockFull, true);
+ insert_struct_rule!(TheHowWhy, true);
insert_struct_rule!(WidelyAccepted, true);
insert_pattern_rule!(Confident, true);
insert_pattern_rule!(Oxymorons, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 36372c277..c2c2c4dc1 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -60,6 +60,7 @@ mod spell_check;
mod spelled_numbers;
mod suggestion;
mod that_which;
+mod the_how_why;
mod then_than;
mod unclosed_quotes;
mod use_genitive;
@@ -122,6 +123,7 @@ pub use spell_check::SpellCheck;
pub use spelled_numbers::SpelledNumbers;
pub use suggestion::Suggestion;
pub use that_which::ThatWhich;
+pub use the_how_why::TheHowWhy;
pub use then_than::ThenThan;
pub use unclosed_quotes::UnclosedQuotes;
pub use use_genitive::UseGenitive;
diff --git a/harper-core/src/linting/the_how_why.rs b/harper-core/src/linting/the_how_why.rs
new file mode 100644
index 000000000..dc558aa76
--- /dev/null
+++ b/harper-core/src/linting/the_how_why.rs
@@ -0,0 +1,166 @@
+use crate::{
+ Token, TokenStringExt,
+ linting::{Lint, LintKind, PatternLinter, Suggestion},
+ patterns::{EitherPattern, Invert, Pattern, SequencePattern},
+};
+
+/// Suggests removing `the` when followed by how/why/who/when/what,
+/// skipping cases like `how to` and `who's who`.
+pub struct TheHowWhy {
+ pattern: EitherPattern,
+}
+
+impl Default for TheHowWhy {
+ fn default() -> Self {
+ let the_how = SequencePattern::default()
+ .t_aco("the")
+ .then_whitespace()
+ .t_aco("how")
+ .then(Invert::new(
+ SequencePattern::default().then_whitespace().t_aco("to"),
+ ));
+
+ let the_who = SequencePattern::default()
+ .t_aco("the")
+ .then_whitespace()
+ .t_aco("who")
+ .then(Invert::new(
+ SequencePattern::default()
+ .then_whitespace()
+ .t_aco("'s")
+ .then_whitespace()
+ .t_aco("who"),
+ ));
+
+ let the_why = SequencePattern::default()
+ .t_aco("the")
+ .then_whitespace()
+ .t_aco("why");
+
+ let the_when = SequencePattern::default()
+ .t_aco("the")
+ .then_whitespace()
+ .t_aco("when");
+
+ let the_what = SequencePattern::default()
+ .t_aco("the")
+ .then_whitespace()
+ .t_aco("what");
+
+ let pattern = EitherPattern::new(vec![
+ Box::new(the_how),
+ Box::new(the_who),
+ Box::new(the_why),
+ Box::new(the_when),
+ Box::new(the_what),
+ ]);
+
+ Self { pattern }
+ }
+}
+
+impl PatternLinter for TheHowWhy {
+ fn pattern(&self) -> &dyn Pattern {
+ &self.pattern
+ }
+
+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
+ let the_token_span = matched_tokens[0..2].span()?;
+ let question_word_token = matched_tokens.get(2)?;
+ let question_word = question_word_token.span.get_content(source);
+
+ Some(Lint {
+ span: the_token_span,
+ lint_kind: LintKind::Miscellaneous,
+ message: format!(
+ "Remove `the` before `{}`. In most contexts, `{}` alone is clearer.",
+ question_word.iter().collect::<String>(),
+ question_word.iter().collect::<String>()
+ ),
+ suggestions: vec![Suggestion::Remove],
+ priority: 31,
+ })
+ }
+
+ fn description(&self) -> &str {
+ "Removes the extra `the` from expressions like `the how`, skipping `how to` and `who's who`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::TheHowWhy;
+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+
+ #[test]
+ fn basic_the_how() {
+ assert_suggestion_result(
+ "This is the how it all started.",
+ TheHowWhy::default(),
+ "This is how it all started.",
+ );
+ }
+
+ #[test]
+ fn the_why() {
+ assert_suggestion_result(
+ "The important part is the why it matters.",
+ TheHowWhy::default(),
+ "The important part is why it matters.",
+ );
+ }
+
+ #[test]
+ fn skip_how_to() {
+ assert_lint_count(
+ "I'd like to explain the how to install this properly.",
+ TheHowWhy::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn skip_whos_who() {
+ assert_lint_count(
+ "We covered the who's who of corporate leadership last time.",
+ TheHowWhy::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn the_who() {
+ assert_suggestion_result(
+ "We must identify the who is responsible.",
+ TheHowWhy::default(),
+ "We must identify who is responsible.",
+ );
+ }
+
+ #[test]
+ fn the_when() {
+ assert_suggestion_result(
+ "He outlined the when the new phase will start.",
+ TheHowWhy::default(),
+ "He outlined when the new phase will start.",
+ );
+ }
+
+ #[test]
+ fn the_what() {
+ assert_suggestion_result(
+ "The presentation clarifies the what we intend to build.",
+ TheHowWhy::default(),
+ "The presentation clarifies what we intend to build.",
+ );
+ }
+
+ #[test]
+ fn no_false_positive() {
+ assert_lint_count(
+ "These tips examine the how to fix your code quickly, plus the what's next.",
+ TheHowWhy::default(),
+ 0,
+ );
+ }
+}