-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): move em and en dash rules to their own linter
- Loading branch information
1 parent
7bdc753
commit 91adb6e
Showing
3 changed files
with
90 additions
and
13 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,89 @@ | ||
use crate::{ | ||
patterns::{EitherPattern, Pattern, SequencePattern}, | ||
Token, TokenStringExt, | ||
}; | ||
|
||
use super::{Lint, LintKind, PatternLinter, Suggestion}; | ||
|
||
pub struct Dashes { | ||
pattern: Box<dyn Pattern>, | ||
} | ||
|
||
impl Default for Dashes { | ||
fn default() -> Self { | ||
let en_dash = SequencePattern::default().then_hyphen().then_hyphen(); | ||
let em_dash = SequencePattern::default() | ||
.then_hyphen() | ||
.then_hyphen() | ||
.then_hyphen(); | ||
|
||
let pattern = EitherPattern::new(vec![Box::new(em_dash), Box::new(en_dash)]); | ||
|
||
Self { | ||
pattern: Box::new(pattern), | ||
} | ||
} | ||
} | ||
|
||
impl PatternLinter for Dashes { | ||
fn pattern(&self) -> &dyn Pattern { | ||
self.pattern.as_ref() | ||
} | ||
|
||
fn match_to_lint(&self, matched_tokens: &[Token], _source: &[char]) -> Lint { | ||
let span = matched_tokens.span().unwrap(); | ||
let lint_kind = LintKind::Formatting; | ||
|
||
match matched_tokens.len() { | ||
2 => Lint { | ||
span, | ||
lint_kind, | ||
suggestions: vec![Suggestion::ReplaceWith(vec!['–'])], | ||
message: "A sequence of hyphens is not an en dash.".to_owned(), | ||
priority: 63, | ||
}, | ||
3 => Lint { | ||
span, | ||
lint_kind, | ||
suggestions: vec![Suggestion::ReplaceWith(vec!['—'])], | ||
message: "A sequence of hyphens is not an em dash.".to_owned(), | ||
priority: 63, | ||
}, | ||
_ => panic!("Received unexpected number of tokens."), | ||
} | ||
} | ||
|
||
fn description(&self) -> &'static str { | ||
"Rather than outright using an em dash or en dash, authors often use a sequence of hyphens, expecting them to be condensed.\nThis rule does so." | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::linting::tests::{assert_suggestion_count, assert_suggestion_result}; | ||
|
||
use super::Dashes; | ||
|
||
#[test] | ||
fn catches_en_dash() { | ||
assert_suggestion_result( | ||
"pre--Industrial Revolution", | ||
Dashes::default(), | ||
"pre–Industrial Revolution", | ||
); | ||
} | ||
|
||
#[test] | ||
fn catches_em_dash() { | ||
assert_suggestion_result( | ||
"'There is no box' --- Scott", | ||
Dashes::default(), | ||
"'There is no box' — Scott", | ||
); | ||
} | ||
|
||
#[test] | ||
fn no_overlaps() { | ||
assert_suggestion_count("'There is no box' --- Scott", Dashes::default(), 1); | ||
} | ||
} |
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