Skip to content

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

address elijah-potter's concerns, refactor

hippietraild0bb53a5b65b
diff --git a/harper-core/src/linting/despite_of.rs b/harper-core/src/linting/despite_of.rs
index 9b044154e..32986dd7d 100644
--- a/harper-core/src/linting/despite_of.rs
+++ b/harper-core/src/linting/despite_of.rs
@@ -1,8 +1,6 @@
-use itertools::Itertools;
-
use crate::{
- patterns::{Pattern, SequencePattern, WordPatternGroup},
- Lrc, Span, Token,
+ patterns::{Pattern, SequencePattern},
+ Token, TokenStringExt,
};
use super::{Lint, LintKind, PatternLinter, Suggestion};
@@ -13,19 +11,7 @@ pub struct DespiteOf {
impl Default for DespiteOf {
fn default() -> Self {
- let mut pattern = WordPatternGroup::default();
-
- let matching_pattern = Lrc::new(
- SequencePattern::default()
- .then_exact_word_or_lowercase("Despite")
- .then_whitespace()
- .then_exact_word("of"),
- );
-
- pattern.add("in spite of", Box::new(matching_pattern.clone()));
- pattern.add("In spite of", Box::new(matching_pattern.clone()));
- pattern.add("despite", Box::new(matching_pattern.clone()));
- pattern.add("Despite", Box::new(matching_pattern));
+ let pattern = SequencePattern::aco("despite").then_whitespace().then_exact_word("of");
Self {
pattern: Box::new(pattern),
@@ -39,40 +25,23 @@ impl PatternLinter for DespiteOf {
}
fn match_to_lint(&self, matched: &[Token], source: &[char]) -> Lint {
- let despite_of = 0..3; // despite + space + of
-
- let cap = matched[0]
- .span
- .get_content(source)
- .first()
- .unwrap()
- .is_uppercase();
-
- // generate suggestion based on case
- let suggest_case = |lower: &str, upper: &str| {
- if cap {
- upper.to_string()
- } else {
- lower.to_string()
- }
- };
-
- let despite = suggest_case("despite", "Despite").chars().collect_vec();
- let in_spite_of = suggest_case("in spite of", "In spite of")
- .chars()
- .collect_vec();
-
+ let span = matched.span().unwrap();
+ let matched = span.get_content(source);
+
Lint {
- span: Span::new(matched[despite_of.start].span.start, matched[despite_of.end - 1].span.end),
+ span,
lint_kind: LintKind::WordChoice,
- suggestions: vec![Suggestion::ReplaceWith(despite), Suggestion::ReplaceWith(in_spite_of)],
- message: "The phrase “despite of” is incorrect in English. Use either “despite” or “in spite of”.".to_string(),
+ suggestions: vec![
+ Suggestion::replace_with_match_case_str("despite", matched),
+ Suggestion::replace_with_match_case_str("in spite of", matched)
+ ],
+ message: "The phrase “despite of” is incorrect. Please use either “despite” or “in spite of” instead.".to_string(),
priority: 126,
}
}
fn description(&self) -> &'static str {
- "Don\"t use \"despite of\". Both \"despite\" and \"in spite of\" are correct English."
+ "Corrects the misuse of `despite of` and suggests the proper alternatives `despite` or `in spite of`."
}
}
@@ -81,19 +50,10 @@ mod tests {
use super::DespiteOf;
use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
- #[test]
- fn catches_uppercase() {
- assert_suggestion_result(
- "Despite of the rain, we went for a walk.",
- DespiteOf::default(),
- "Despite the rain, we went for a walk.",
- );
- }
-
#[test]
fn catches_lowercase() {
assert_suggestion_result(
- "The team performed well, despite the difficulties they faced.",
+ "The team performed well, despite of the difficulties they faced.",
DespiteOf::default(),
"The team performed well, despite the difficulties they faced.",
);