-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #531 from hippietrail/despite_of_lint
feat(core): implement "despite of" lint
- Loading branch information
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters