Skip to content

Commit 6990d06

Browse files
Merge pull request #531 from hippietrail/despite_of_lint
feat(core): implement "despite of" lint
2 parents c43ca99 + 3688c54 commit 6990d06

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

harper-core/src/linting/despite_of.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use crate::{
2+
patterns::{Pattern, SequencePattern},
3+
Token, TokenStringExt,
4+
};
5+
6+
use super::{Lint, LintKind, PatternLinter, Suggestion};
7+
8+
pub struct DespiteOf {
9+
pattern: Box<dyn Pattern>,
10+
}
11+
12+
impl Default for DespiteOf {
13+
fn default() -> Self {
14+
let pattern = SequencePattern::aco("despite")
15+
.then_whitespace()
16+
.then_exact_word("of");
17+
18+
Self {
19+
pattern: Box::new(pattern),
20+
}
21+
}
22+
}
23+
24+
impl PatternLinter for DespiteOf {
25+
fn pattern(&self) -> &dyn Pattern {
26+
self.pattern.as_ref()
27+
}
28+
29+
fn match_to_lint(&self, matched: &[Token], source: &[char]) -> Lint {
30+
let span = matched.span().unwrap();
31+
let matched = span.get_content(source);
32+
33+
Lint {
34+
span,
35+
lint_kind: LintKind::WordChoice,
36+
suggestions: vec![
37+
Suggestion::replace_with_match_case_str("despite", matched),
38+
Suggestion::replace_with_match_case_str("in spite of", matched)
39+
],
40+
message: "The phrase “despite of” is incorrect. Please use either “despite” or “in spite of” instead.".to_string(),
41+
priority: 126,
42+
}
43+
}
44+
45+
fn description(&self) -> &'static str {
46+
"Corrects the misuse of `despite of` and suggests the proper alternatives `despite` or `in spite of`."
47+
}
48+
}
49+
50+
#[cfg(test)]
51+
mod tests {
52+
use super::DespiteOf;
53+
use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
54+
55+
#[test]
56+
fn catches_lowercase() {
57+
assert_suggestion_result(
58+
"The team performed well, despite of the difficulties they faced.",
59+
DespiteOf::default(),
60+
"The team performed well, despite the difficulties they faced.",
61+
);
62+
}
63+
64+
#[test]
65+
fn catches_different_cases() {
66+
assert_lint_count(
67+
"Despite of the rain, we went for a walk.",
68+
DespiteOf::default(),
69+
1,
70+
);
71+
}
72+
73+
#[test]
74+
fn likes_correction() {
75+
assert_lint_count(
76+
"The team performed well, despite the difficulties they faced. In spite of the rain, we went for a walk.",
77+
DespiteOf::default(),
78+
0,
79+
);
80+
}
81+
}

harper-core/src/linting/lint_group.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use super::avoid_curses::AvoidCurses;
66
use super::boring_words::BoringWords;
77
use super::capitalize_personal_pronouns::CapitalizePersonalPronouns;
88
use super::correct_number_suffix::CorrectNumberSuffix;
9+
use super::despite_of::DespiteOf;
910
use super::dot_initialisms::DotInitialisms;
1011
use super::ellipsis_length::EllipsisLength;
1112
use super::lets_confusion::LetsConfusion;
@@ -193,6 +194,7 @@ create_lint_group_config!(
193194
CurrencyPlacement => true,
194195
SomewhatSomething => true,
195196
LetsConfusion => true,
197+
DespiteOf => true,
196198
);
197199

198200
impl<T: Dictionary + Default> Default for LintGroup<T> {

harper-core/src/linting/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod capitalize_personal_pronouns;
99
mod correct_number_suffix;
1010
mod currency_placement;
1111
mod dashes;
12+
mod despite_of;
1213
mod dot_initialisms;
1314
mod ellipsis_length;
1415
mod lets_confusion;
@@ -44,6 +45,7 @@ pub use boring_words::BoringWords;
4445
pub use capitalize_personal_pronouns::CapitalizePersonalPronouns;
4546
pub use correct_number_suffix::CorrectNumberSuffix;
4647
pub use currency_placement::CurrencyPlacement;
48+
pub use despite_of::DespiteOf;
4749
pub use dot_initialisms::DotInitialisms;
4850
pub use ellipsis_length::EllipsisLength;
4951
pub use lets_confusion::LetsConfusion;

0 commit comments

Comments
 (0)