Skip to content

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

feat(core): avoid providing multiple lints for a single long hyphen sequence in `Dashes` (#1182)

Steve Kurch82234c480827
diff --git a/harper-core/src/linting/dashes.rs b/harper-core/src/linting/dashes.rs
index b8938fab1..9086135f3 100644
--- a/harper-core/src/linting/dashes.rs
+++ b/harper-core/src/linting/dashes.rs
@@ -5,6 +5,9 @@ use crate::{
use super::{Lint, LintKind, PatternLinter, Suggestion};
+const EN_DASH: char = '–';
+const EM_DASH: char = '—';
+
pub struct Dashes {
pattern: Box<dyn Pattern>,
}
@@ -12,12 +15,12 @@ pub struct Dashes {
impl Default for Dashes {
fn default() -> Self {
let en_dash = SequencePattern::default().then_hyphen().then_hyphen();
- let em_dash = SequencePattern::default()
+ let em_dash_or_longer = SequencePattern::default()
.then_hyphen()
.then_hyphen()
- .then_hyphen();
+ .then_one_or_more_hyphens();
- let pattern = EitherPattern::new(vec![Box::new(em_dash), Box::new(en_dash)]);
+ let pattern = EitherPattern::new(vec![Box::new(em_dash_or_longer), Box::new(en_dash)]);
Self {
pattern: Box::new(pattern),
@@ -38,17 +41,18 @@ impl PatternLinter for Dashes {
2 => Some(Lint {
span,
lint_kind,
- suggestions: vec![Suggestion::ReplaceWith(vec!['–'])],
+ suggestions: vec![Suggestion::ReplaceWith(vec![EN_DASH])],
message: "A sequence of hyphens is not an en dash.".to_owned(),
priority: 63,
}),
3 => Some(Lint {
span,
lint_kind,
- suggestions: vec![Suggestion::ReplaceWith(vec!['—'])],
+ suggestions: vec![Suggestion::ReplaceWith(vec![EM_DASH])],
message: "A sequence of hyphens is not an em dash.".to_owned(),
priority: 63,
}),
+ 4.. => None, // Ignore longer hyphen sequences.
_ => panic!("Received unexpected number of tokens."),
}
}
@@ -63,13 +67,14 @@ mod tests {
use crate::linting::tests::{assert_suggestion_count, assert_suggestion_result};
use super::Dashes;
+ use super::{EM_DASH, EN_DASH};
#[test]
fn catches_en_dash() {
assert_suggestion_result(
"pre--Industrial Revolution",
Dashes::default(),
- "pre–Industrial Revolution",
+ &format!("pre{EN_DASH}Industrial Revolution"),
);
}
@@ -78,7 +83,7 @@ mod tests {
assert_suggestion_result(
"'There is no box' --- Scott",
Dashes::default(),
- "'There is no box' — Scott",
+ &format!("'There is no box' {EM_DASH} Scott"),
);
}
@@ -86,4 +91,9 @@ mod tests {
fn no_overlaps() {
assert_suggestion_count("'There is no box' --- Scott", Dashes::default(), 1);
}
+
+ #[test]
+ fn no_lint_for_long_hyphen_sequences() {
+ assert_suggestion_count("'There is no box' ------ Scott", Dashes::default(), 0);
+ }
}