Skip to content

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

refactor(core): move em and en dash rules to their own linter

Elijah Potter91adb6e68f22
diff --git a/harper-core/src/linting/dashes.rs b/harper-core/src/linting/dashes.rs
new file mode 100644
index 000000000..0aef6e1d9
--- /dev/null
+++ b/harper-core/src/linting/dashes.rs
@@ -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);
+ }
+}
diff --git a/harper-core/src/linting/matcher.rs b/harper-core/src/linting/matcher.rs
index 6e34785ef..82582635d 100644
--- a/harper-core/src/linting/matcher.rs
+++ b/harper-core/src/linting/matcher.rs
@@ -190,19 +190,6 @@ impl Matcher {
"take", "a", "decision" => "make a decision"
};
- // TODO: Improve the description for this lint specifically.
- // We need to be more explicit that we are replacing with an Em dash
- triggers.push(Rule {
- pattern: vec![pt!(Hyphen), pt!(Hyphen), pt!(Hyphen)],
- replace_with: vecword!("—"),
- });
-
- // Same goes for this En dash
- triggers.push(Rule {
- pattern: vec![pt!(Hyphen), pt!(Hyphen)],
- replace_with: vecword!("–"),
- });
-
triggers.push(Rule {
pattern: vec![pt!("L"), pt!(Period), pt!("L"), pt!(Period), pt!("M")],
replace_with: vecword!("large language model"),
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 87e26c911..03ce9bbe2 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -4,6 +4,7 @@ mod boring_words;
mod capitalize_personal_pronouns;
mod compound_words;
mod correct_number_suffix;
+mod dashes;
mod dot_initialisms;
mod ellipsis_length;
mod linking_verbs;