Skip to content

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

feat: added linter to detect correct number suffixes

Elijah Potterc946b96f6534
diff --git a/harper-core/src/linting/correct_number_suffix.rs b/harper-core/src/linting/correct_number_suffix.rs
new file mode 100644
index 000000000..8c2a2a20a
--- /dev/null
+++ b/harper-core/src/linting/correct_number_suffix.rs
@@ -0,0 +1,54 @@
+use super::{Lint, LintKind, Linter};
+use crate::token::{NumberSuffix, TokenStringExt};
+use crate::{Document, Span, Suggestion, TokenKind};
+
+/// Detect and warn that the sentence is too long.
+#[derive(Debug, Clone, Copy, Default)]
+pub struct CorrectNumberSuffix;
+
+impl Linter for CorrectNumberSuffix {
+ fn lint(&mut self, document: &Document) -> Vec<Lint> {
+ let mut output = Vec::new();
+
+ for number_tok in document.iter_numbers() {
+ let suffix_span = Span::new_with_len(number_tok.span.end, 2).pulled_by(2);
+
+ if let TokenKind::Number(number, Some(suffix)) = number_tok.kind {
+ if let Some(correct_suffix) = NumberSuffix::correct_suffix_for(number) {
+ if suffix != correct_suffix {
+ output.push(Lint {
+ span: suffix_span,
+ lint_kind: LintKind::Miscellaneous,
+ message: "This number needs a different suffix to sound right."
+ .to_string(),
+ suggestions: vec![Suggestion::ReplaceWith(correct_suffix.to_chars())],
+ ..Default::default()
+ })
+ }
+ }
+ }
+ }
+
+ output
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::CorrectNumberSuffix;
+ use crate::linting::tests::assert_lint_count;
+
+ #[test]
+ fn passes_correct_cases() {
+ assert_lint_count("2nd", CorrectNumberSuffix, 0);
+ assert_lint_count("101st", CorrectNumberSuffix, 0);
+ assert_lint_count("1012th", CorrectNumberSuffix, 0);
+ }
+
+ #[test]
+ fn detects_incorrect_cases() {
+ assert_lint_count("2st", CorrectNumberSuffix, 1);
+ assert_lint_count("101nd", CorrectNumberSuffix, 1);
+ assert_lint_count("1012rd", CorrectNumberSuffix, 1);
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index f1dd1a4e0..5825c109b 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -2,6 +2,7 @@ use paste::paste;
use serde::{Deserialize, Serialize};
use super::an_a::AnA;
+use super::correct_number_suffix::CorrectNumberSuffix;
use super::long_sentences::LongSentences;
use super::matcher::Matcher;
use super::number_suffix_capitalization::NumberSuffixCapitalization;
@@ -99,6 +100,7 @@ create_lint_group_config!(
RepeatedWords => true,
Spaces => true,
Matcher => true,
+ CorrectNumberSuffix => true,
NumberSuffixCapitalization => true
);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 370ed64ee..50ddd9ad3 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -1,4 +1,5 @@
mod an_a;
+mod correct_number_suffix;
mod lint;
mod lint_group;
mod long_sentences;
diff --git a/harper-core/src/token.rs b/harper-core/src/token.rs
index 83cf3b19d..424b4d47c 100644
--- a/harper-core/src/token.rs
+++ b/harper-core/src/token.rs
@@ -61,6 +61,41 @@ pub enum NumberSuffix {
}
impl NumberSuffix {
+ pub fn correct_suffix_for(number: f64) -> Option<Self> {
+ if number < 0.0 || number - number.floor() > f64::EPSILON || number > u64::MAX as f64 {
+ return None;
+ }
+
+ let integer = number as u64;
+
+ if let 11..=13 = integer % 100 {
+ return Some(Self::Th);
+ };
+
+ match integer % 10 {
+ 0 => Some(Self::Th),
+ 1 => Some(Self::St),
+ 2 => Some(Self::Nd),
+ 3 => Some(Self::Rd),
+ 4 => Some(Self::Th),
+ 5 => Some(Self::Th),
+ 6 => Some(Self::Th),
+ 7 => Some(Self::Th),
+ 8 => Some(Self::Th),
+ 9 => Some(Self::Th),
+ _ => None
+ }
+ }
+
+ pub fn to_chars(self) -> Vec<char> {
+ match self {
+ NumberSuffix::Th => vec!['t', 'h'],
+ NumberSuffix::St => vec!['s', 't'],
+ NumberSuffix::Nd => vec!['n', 'd'],
+ NumberSuffix::Rd => vec!['r', 'd']
+ }
+ }
+
/// Check the first several characters in a buffer to see if it matches a
/// number suffix.
pub fn from_chars(chars: &[char]) -> Option<Self> {