Skip to content

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

feat(core): rule that requires that the dash between numbers in a range is an en dash (#2961)

Elijah Pottere1d1f6d5dd0e
diff --git a/harper-core/default_config.json b/harper-core/default_config.json
index 87dd793bf..ac91faaba 100644
--- a/harper-core/default_config.json
+++ b/harper-core/default_config.json
@@ -3899,6 +3899,13 @@
"label": "No Match For"
}
},
+ {
+ "Bool": {
+ "name": "NumericRangeEnDash",
+ "state": true,
+ "label": "Numeric Range En Dash"
+ }
+ },
{
"Bool": {
"name": "Nobody",
diff --git a/harper-core/src/linting/lint_group/mod.rs b/harper-core/src/linting/lint_group/mod.rs
index bbfb6136c..d0693c5d1 100644
--- a/harper-core/src/linting/lint_group/mod.rs
+++ b/harper-core/src/linting/lint_group/mod.rs
@@ -145,6 +145,7 @@ use super::nor_modal_pronoun::NorModalPronoun;
use super::not_only_inversion::NotOnlyInversion;
use super::noun_verb_confusion::NounVerbConfusion;
use super::number_suffix_capitalization::NumberSuffixCapitalization;
+use super::numeric_range_en_dash::NumericRangeEnDash;
use super::obsess_preposition::ObsessPreposition;
use super::of_course::OfCourse;
use super::oldest_in_the_book::OldestInTheBook;
@@ -633,6 +634,7 @@ impl LintGroup {
insert_expr_rule!(NotOnlyInversion, true);
insert_struct_rule!(NounVerbConfusion, true);
insert_struct_rule!(NumberSuffixCapitalization, true);
+ insert_expr_rule!(NumericRangeEnDash, true);
insert_expr_rule!(ObsessPreposition, true);
insert_expr_rule!(OfCourse, true);
insert_expr_rule!(OldestInTheBook, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 08bfd72ef..4315d9fe2 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -154,6 +154,7 @@ mod nor_modal_pronoun;
mod not_only_inversion;
mod noun_verb_confusion;
mod number_suffix_capitalization;
+mod numeric_range_en_dash;
mod obsess_preposition;
mod of_course;
mod oldest_in_the_book;
diff --git a/harper-core/src/linting/numeric_range_en_dash.rs b/harper-core/src/linting/numeric_range_en_dash.rs
new file mode 100644
index 000000000..236ead8ff
--- /dev/null
+++ b/harper-core/src/linting/numeric_range_en_dash.rs
@@ -0,0 +1,221 @@
+use crate::Token;
+use crate::expr::{Expr, SequenceExpr};
+use crate::linting::expr_linter::Chunk;
+use crate::punctuation::Punctuation;
+
+use super::{ExprLinter, Lint, LintKind, Suggestion};
+
+const EN_DASH: char = '–';
+
+fn is_target_dash(token: &Token, _source: &[char]) -> bool {
+ matches!(
+ token.kind.as_punctuation(),
+ Some(Punctuation::Hyphen | Punctuation::EmDash)
+ )
+}
+
+fn is_numeric_range_dash(token: &Token) -> bool {
+ matches!(
+ token.kind.as_punctuation(),
+ Some(Punctuation::Hyphen | Punctuation::EnDash | Punctuation::EmDash)
+ )
+}
+
+fn is_chained_numeric_form(context: Option<(&[Token], &[Token])>) -> bool {
+ let Some((before, after)) = context else {
+ return false;
+ };
+
+ // Skip multipart numeric chains like dates (`2026-03-18`) and versions (`1-2-3`).
+ let preceded_by_numeric_dash = matches!(
+ before,
+ [.., number, dash] if number.kind.is_number() && is_numeric_range_dash(dash)
+ );
+
+ let followed_by_dash_numeric = matches!(
+ after,
+ [dash, number, ..] if is_numeric_range_dash(dash) && number.kind.is_number()
+ );
+
+ preceded_by_numeric_dash || followed_by_dash_numeric
+}
+
+pub struct NumericRangeEnDash {
+ expr: SequenceExpr,
+}
+
+impl Default for NumericRangeEnDash {
+ fn default() -> Self {
+ // Match isolated numeric ranges like `12-14` or `3—5`.
+ // The context check below skips dates, version chains, and similar
+ // multipart numeric forms that should keep their existing separators.
+ let pattern = SequenceExpr::default()
+ .then_number()
+ .then(is_target_dash)
+ .then_number();
+
+ Self { expr: pattern }
+ }
+}
+
+impl ExprLinter for NumericRangeEnDash {
+ type Unit = Chunk;
+
+ fn expr(&self) -> &dyn Expr {
+ &self.expr
+ }
+
+ fn match_to_lint_with_context(
+ &self,
+ matched_tokens: &[Token],
+ _source: &[char],
+ context: Option<(&[Token], &[Token])>,
+ ) -> Option<Lint> {
+ if is_chained_numeric_form(context) {
+ return None;
+ }
+
+ Some(Lint {
+ span: matched_tokens[1].span,
+ lint_kind: LintKind::Formatting,
+ suggestions: vec![Suggestion::ReplaceWith(vec![EN_DASH])],
+ message: "Use an en dash (–) in ranges of numbers. Ignore this if it is math."
+ .to_owned(),
+ priority: 63,
+ })
+ }
+
+ fn description(&self) -> &'static str {
+ "Replaces hyphens and em dashes with en dashes in isolated numeric ranges such as `12–14`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::NumericRangeEnDash;
+ use crate::linting::tests::{assert_no_lints, assert_suggestion_result};
+
+ #[test]
+ fn corrects_basic_page_range() {
+ assert_suggestion_result(
+ "See pages 12-14 for the full table.",
+ NumericRangeEnDash::default(),
+ "See pages 12–14 for the full table.",
+ );
+ }
+
+ #[test]
+ fn corrects_basic_em_dash_range() {
+ assert_suggestion_result(
+ "Read chapters 3—5 before class.",
+ NumericRangeEnDash::default(),
+ "Read chapters 3–5 before class.",
+ );
+ }
+
+ #[test]
+ fn corrects_score_range() {
+ assert_suggestion_result(
+ "The final score was 10-8 after overtime.",
+ NumericRangeEnDash::default(),
+ "The final score was 10–8 after overtime.",
+ );
+ }
+
+ #[test]
+ fn corrects_decimal_range() {
+ assert_suggestion_result(
+ "Keep the ratio between 1.5-2.5 during calibration.",
+ NumericRangeEnDash::default(),
+ "Keep the ratio between 1.5–2.5 during calibration.",
+ );
+ }
+
+ #[test]
+ fn corrects_percent_range() {
+ assert_suggestion_result(
+ "Expect a 5-10% improvement after tuning.",
+ NumericRangeEnDash::default(),
+ "Expect a 5–10% improvement after tuning.",
+ );
+ }
+
+ #[test]
+ fn corrects_year_span() {
+ assert_suggestion_result(
+ "The archive covers 1990-1995.",
+ NumericRangeEnDash::default(),
+ "The archive covers 1990–1995.",
+ );
+ }
+
+ #[test]
+ fn corrects_zero_padded_range() {
+ assert_suggestion_result(
+ "Use files 01-03 for the demo.",
+ NumericRangeEnDash::default(),
+ "Use files 01–03 for the demo.",
+ );
+ }
+
+ #[test]
+ fn corrects_sentence_final_range() {
+ assert_suggestion_result(
+ "Valid values are 2-4.",
+ NumericRangeEnDash::default(),
+ "Valid values are 2–4.",
+ );
+ }
+
+ #[test]
+ fn ignores_existing_en_dash() {
+ assert_no_lints(
+ "See pages 12–14 for the full table.",
+ NumericRangeEnDash::default(),
+ );
+ }
+
+ #[test]
+ fn ignores_spaced_hyphen_range() {
+ assert_no_lints(
+ "See pages 12 - 14 for the full table.",
+ NumericRangeEnDash::default(),
+ );
+ }
+
+ #[test]
+ fn ignores_spaced_em_dash_range() {
+ assert_no_lints(
+ "See pages 12 — 14 for the full table.",
+ NumericRangeEnDash::default(),
+ );
+ }
+
+ #[test]
+ fn ignores_hyphenated_date_chain() {
+ assert_no_lints("Today is 2026-03-18.", NumericRangeEnDash::default());
+ }
+
+ #[test]
+ fn ignores_em_dash_date_chain() {
+ assert_no_lints("Today is 2026—03—18.", NumericRangeEnDash::default());
+ }
+
+ #[test]
+ fn ignores_three_part_version_chain() {
+ assert_no_lints("The build number is 1-2-3.", NumericRangeEnDash::default());
+ }
+
+ #[test]
+ fn ignores_mixed_dash_chain() {
+ assert_no_lints("The timeline reads 1-2—3.", NumericRangeEnDash::default());
+ }
+
+ #[test]
+ fn ignores_longer_numeric_chain() {
+ assert_no_lints(
+ "The code spans 12-14-16 in the export.",
+ NumericRangeEnDash::default(),
+ );
+ }
+}
diff --git a/harper-core/tests/text/linters/Difficult sentences.snap.yml b/harper-core/tests/text/linters/Difficult sentences.snap.yml
index 57da14a59..dba28242f 100644
--- a/harper-core/tests/text/linters/Difficult sentences.snap.yml
+++ b/harper-core/tests/text/linters/Difficult sentences.snap.yml
@@ -327,6 +327,15 @@ Suggest:
+Lint: Formatting (63 priority)
+Message: |
+ 440 | The match result was 10-5, with John scoring three goals.
+ | ^ Use an en dash (–) in ranges of numbers. Ignore this if it is math.
+Suggest:
+ - Replace with: “–”
+
+
+
Lint: Capitalization (31 priority)
Message: |
449 | slain with robbers.