Skip to content

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

feat(core): added rule for hedging

Elijah Potter5175900e140b
diff --git a/harper-core/src/linting/hedging.rs b/harper-core/src/linting/hedging.rs
new file mode 100644
index 000000000..f67c82bc0
--- /dev/null
+++ b/harper-core/src/linting/hedging.rs
@@ -0,0 +1,106 @@
+use crate::linting::{Lint, LintKind, PatternLinter, Suggestion};
+use crate::patterns::{EitherPattern, ExactPhrase, Pattern};
+use crate::{Token, TokenStringExt};
+
+/// A linter that detects hedging language.
+pub struct Hedging {
+ pattern: Box<dyn Pattern>,
+}
+
+impl Default for Hedging {
+ fn default() -> Self {
+ let phrases = vec!["I would argue that", ", so to speak", "to a certain degree"];
+
+ let patterns: Vec<Box<dyn Pattern>> = phrases
+ .into_iter()
+ .map(|s| Box::new(ExactPhrase::from_phrase(s)) as Box<dyn Pattern>)
+ .collect();
+
+ let pattern = Box::new(EitherPattern::new(patterns));
+ Self { pattern }
+ }
+}
+
+impl PatternLinter for Hedging {
+ fn pattern(&self) -> &dyn Pattern {
+ self.pattern.as_ref()
+ }
+
+ fn match_to_lint(&self, matched_tokens: &[Token], _source: &[char]) -> Option<Lint> {
+ let span = matched_tokens.span()?;
+ Some(Lint {
+ span,
+ lint_kind: LintKind::Miscellaneous,
+ suggestions: Vec::new(),
+ message: "You're hedging.".to_string(),
+ priority: 31,
+ })
+ }
+
+ fn description(&self) -> &str {
+ "Flags hedging language (e.g. “I would argue that”, “, so to speak”, “to a certain degree”)."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::Hedging;
+ use crate::linting::tests::assert_lint_count;
+
+ #[test]
+ fn detects_hedging_phrase() {
+ assert_lint_count("I would argue that this is correct.", Hedging::default(), 1);
+ }
+
+ #[test]
+ fn does_not_flag_clean_text() {
+ assert_lint_count("This is clear and direct.", Hedging::default(), 0);
+ }
+
+ #[test]
+ fn lowercase_hedging() {
+ assert_lint_count(
+ "i would argue that the outcome is uncertain.",
+ Hedging::default(),
+ 1,
+ );
+ }
+
+ #[test]
+ fn incomplete_phrase_not_flagged() {
+ assert_lint_count("I would argue the data is clear.", Hedging::default(), 0);
+ }
+
+ #[test]
+ fn phrase_with_trailing_comma() {
+ let text = "I would argue that, this method works.";
+ assert_lint_count(text, Hedging::default(), 1);
+ }
+
+ #[test]
+ fn phrase_with_extra_whitespace() {
+ assert_lint_count(
+ "to a certain degree the results are ambiguous.",
+ Hedging::default(),
+ 1,
+ );
+ }
+
+ #[test]
+ fn does_not_flag_similar_but_incorrect_phrase() {
+ assert_lint_count(
+ "He spoke so to speakingly about the event.",
+ Hedging::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn phrase_split_by_line_break() {
+ assert_lint_count(
+ "I would argue\nthat this approach fails.",
+ Hedging::default(),
+ 1,
+ );
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index b8416397e..31d3240b7 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -19,6 +19,7 @@ use super::despite_of::DespiteOf;
use super::dot_initialisms::DotInitialisms;
use super::ellipsis_length::EllipsisLength;
use super::expand_time_shorthands::ExpandTimeShorthands;
+use super::hedging::Hedging;
use super::hereby::Hereby;
use super::hop_hope::HopHope;
use super::hyphenate_number_day::HyphenateNumberDay;
@@ -251,6 +252,7 @@ impl LintGroup {
insert_struct_rule!(LetsConfusion, true);
insert_struct_rule!(DespiteOf, true);
insert_struct_rule!(ChockFull, true);
+ insert_struct_rule!(Hedging, true);
insert_struct_rule!(ExpandTimeShorthands, true);
insert_struct_rule!(ModalOf, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 5a8f66d1b..431dc2660 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -17,6 +17,7 @@ mod despite_of;
mod dot_initialisms;
mod ellipsis_length;
mod expand_time_shorthands;
+mod hedging;
mod hereby;
mod hop_hope;
mod hyphenate_number_day;
@@ -76,6 +77,7 @@ pub use despite_of::DespiteOf;
pub use dot_initialisms::DotInitialisms;
pub use ellipsis_length::EllipsisLength;
pub use expand_time_shorthands::ExpandTimeShorthands;
+pub use hedging::Hedging;
pub use hereby::Hereby;
pub use hop_hope::HopHope;
pub use hyphenate_number_day::HyphenateNumberDay;