Skip to content

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

feat(core): cover more cases with `PiqueInterest`

Elijah Potter9f8e8ec8373b
diff --git a/harper-core/src/linting/pique_interest.rs b/harper-core/src/linting/pique_interest.rs
index 1579452b4..018985928 100644
--- a/harper-core/src/linting/pique_interest.rs
+++ b/harper-core/src/linting/pique_interest.rs
@@ -1,6 +1,7 @@
use crate::{
- patterns::{Pattern, SequencePattern},
- Token,
+ char_string::char_string,
+ patterns::{Pattern, SequencePattern, WordSet},
+ CharString, CharStringExt, Token,
};
use super::{Lint, LintKind, PatternLinter, Suggestion};
@@ -11,7 +12,10 @@ pub struct PiqueInterest {
impl Default for PiqueInterest {
fn default() -> Self {
- let pattern = SequencePattern::aco("peak")
+ let pattern = SequencePattern::default()
+ .then_word_set(WordSet::all(&[
+ "peak", "peaked", "peek", "peeked", "peeking", "peaking",
+ ]))
.then_whitespace()
.then_singular_subject()
.then_whitespace()
@@ -23,6 +27,20 @@ impl Default for PiqueInterest {
}
}
+impl PiqueInterest {
+ fn to_correct(word: &str) -> Option<CharString> {
+ Some(match word.to_lowercase().as_str() {
+ "peak" => char_string!("pique"),
+ "peek" => char_string!("pique"),
+ "peeked" => char_string!("piqued"),
+ "peaked" => char_string!("piqued"),
+ "peaking" => char_string!("piquing"),
+ "peeking" => char_string!("piquing"),
+ _ => return None,
+ })
+ }
+}
+
impl PatternLinter for PiqueInterest {
fn pattern(&self) -> &dyn Pattern {
self.pattern.as_ref()
@@ -30,21 +48,27 @@ impl PatternLinter for PiqueInterest {
fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {
let span = matched_tokens[0].span;
+ let word = span.get_content_string(source).to_lowercase();
+ let correct = Self::to_correct(&word).unwrap();
Lint {
span,
lint_kind: LintKind::WordChoice,
suggestions: vec![Suggestion::replace_with_match_case(
- "pique".chars().collect(),
+ correct.to_vec(),
matched_tokens[0].span.get_content(source),
)],
- message: "Did you mean `pique` instead of `peak`?".to_owned(),
+ message: format!(
+ "Did you mean `{}` instead of `{}`?",
+ correct.to_string(),
+ word,
+ ),
priority: 31,
}
}
fn description(&self) -> &'static str {
- "Ensures `pique interest` is used correctly instead of `peak interest`."
+ "Detects incorrect usage of `peak` or `peek` when the intended word is `pique`, as in the phrase `you've peaked my interest`."
}
}
@@ -62,6 +86,33 @@ mod tests {
);
}
+ #[test]
+ fn corrects_peeked_interest_at_start() {
+ assert_suggestion_result(
+ "Peeked his interest, did she?",
+ PiqueInterest::default(),
+ "Piqued his interest, did she?",
+ );
+ }
+
+ #[test]
+ fn corrects_peak_interest_in_middle() {
+ assert_suggestion_result(
+ "She tried to peak his interest during the lecture.",
+ PiqueInterest::default(),
+ "She tried to pique his interest during the lecture.",
+ );
+ }
+
+ #[test]
+ fn corrects_peaked_interest_at_end() {
+ assert_suggestion_result(
+ "All along, she hoped she peaked his interest.",
+ PiqueInterest::default(),
+ "All along, she hoped she piqued his interest.",
+ );
+ }
+
#[test]
fn does_not_correct_unrelated_peak() {
assert_suggestion_result(
@@ -70,4 +121,13 @@ mod tests {
"He reached the peak of the mountain.",
);
}
+
+ #[test]
+ fn corrects_peaking_interest() {
+ assert_suggestion_result(
+ "She was peaking his interest with her stories.",
+ PiqueInterest::default(),
+ "She was piquing his interest with her stories.",
+ );
+ }
}