Skip to content

Commit

Permalink
Merge pull request #531 from hippietrail/despite_of_lint
Browse files Browse the repository at this point in the history
feat(core): implement "despite of" lint
  • Loading branch information
elijah-potter authored Jan 30, 2025
2 parents c43ca99 + 3688c54 commit 6990d06
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
81 changes: 81 additions & 0 deletions harper-core/src/linting/despite_of.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use crate::{
patterns::{Pattern, SequencePattern},
Token, TokenStringExt,
};

use super::{Lint, LintKind, PatternLinter, Suggestion};

pub struct DespiteOf {
pattern: Box<dyn Pattern>,
}

impl Default for DespiteOf {
fn default() -> Self {
let pattern = SequencePattern::aco("despite")
.then_whitespace()
.then_exact_word("of");

Self {
pattern: Box::new(pattern),
}
}
}

impl PatternLinter for DespiteOf {
fn pattern(&self) -> &dyn Pattern {
self.pattern.as_ref()
}

fn match_to_lint(&self, matched: &[Token], source: &[char]) -> Lint {
let span = matched.span().unwrap();
let matched = span.get_content(source);

Lint {
span,
lint_kind: LintKind::WordChoice,
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 {
"Corrects the misuse of `despite of` and suggests the proper alternatives `despite` or `in spite of`."
}
}

#[cfg(test)]
mod tests {
use super::DespiteOf;
use crate::linting::tests::{assert_lint_count, assert_suggestion_result};

#[test]
fn catches_lowercase() {
assert_suggestion_result(
"The team performed well, despite of the difficulties they faced.",
DespiteOf::default(),
"The team performed well, despite the difficulties they faced.",
);
}

#[test]
fn catches_different_cases() {
assert_lint_count(
"Despite of the rain, we went for a walk.",
DespiteOf::default(),
1,
);
}

#[test]
fn likes_correction() {
assert_lint_count(
"The team performed well, despite the difficulties they faced. In spite of the rain, we went for a walk.",
DespiteOf::default(),
0,
);
}
}
2 changes: 2 additions & 0 deletions harper-core/src/linting/lint_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::avoid_curses::AvoidCurses;
use super::boring_words::BoringWords;
use super::capitalize_personal_pronouns::CapitalizePersonalPronouns;
use super::correct_number_suffix::CorrectNumberSuffix;
use super::despite_of::DespiteOf;
use super::dot_initialisms::DotInitialisms;
use super::ellipsis_length::EllipsisLength;
use super::lets_confusion::LetsConfusion;
Expand Down Expand Up @@ -193,6 +194,7 @@ create_lint_group_config!(
CurrencyPlacement => true,
SomewhatSomething => true,
LetsConfusion => true,
DespiteOf => true,
);

impl<T: Dictionary + Default> Default for LintGroup<T> {
Expand Down
2 changes: 2 additions & 0 deletions harper-core/src/linting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod capitalize_personal_pronouns;
mod correct_number_suffix;
mod currency_placement;
mod dashes;
mod despite_of;
mod dot_initialisms;
mod ellipsis_length;
mod lets_confusion;
Expand Down Expand Up @@ -44,6 +45,7 @@ pub use boring_words::BoringWords;
pub use capitalize_personal_pronouns::CapitalizePersonalPronouns;
pub use correct_number_suffix::CorrectNumberSuffix;
pub use currency_placement::CurrencyPlacement;
pub use despite_of::DespiteOf;
pub use dot_initialisms::DotInitialisms;
pub use ellipsis_length::EllipsisLength;
pub use lets_confusion::LetsConfusion;
Expand Down

0 comments on commit 6990d06

Please sign in to comment.