Skip to content

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

feat(core): hyphenate work weeks

Elijah Potterb88098ab3c69
diff --git a/harper-core/src/linting/hyphenate_number_work_week.rs b/harper-core/src/linting/hyphenate_number_work_week.rs
new file mode 100644
index 000000000..0964926b6
--- /dev/null
+++ b/harper-core/src/linting/hyphenate_number_work_week.rs
@@ -0,0 +1,77 @@
+use crate::{
+ patterns::{Pattern, SequencePattern, WordSet},
+ Token,
+};
+
+use super::{Lint, LintKind, PatternLinter, Suggestion};
+
+pub struct HyphenateNumberWorkWeek {
+ pattern: Box<dyn Pattern>,
+}
+
+impl Default for HyphenateNumberWorkWeek {
+ fn default() -> Self {
+ let pattern = SequencePattern::default()
+ .then_number()
+ .then_whitespace()
+ .t_aco("day")
+ .then_whitespace()
+ .then_word_set(WordSet::all(&["work", "working"]))
+ .then_whitespace()
+ .then_word_set(WordSet::all(&["week", "weeks"]));
+
+ Self {
+ pattern: Box::new(pattern),
+ }
+ }
+}
+
+impl PatternLinter for HyphenateNumberWorkWeek {
+ fn pattern(&self) -> &dyn Pattern {
+ self.pattern.as_ref()
+ }
+
+ fn match_to_lint(&self, matched_tokens: &[Token], _source: &[char]) -> Lint {
+ let number = matched_tokens[0].kind.expect_number();
+ let space = matched_tokens[1];
+
+ Lint {
+ span: space.span,
+ lint_kind: LintKind::Miscellaneous,
+ suggestions: vec![Suggestion::ReplaceWith(vec!['-'])],
+ message: format!(
+ "Use a hyphen in `{}-day` when forming an adjectival compound.",
+ number
+ ),
+ priority: 31,
+ }
+ }
+
+ fn description(&self) -> &'static str {
+ "Ensures a hyphen is used in `X-day` when it is part of a compound adjective, such as `4-day work week`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::HyphenateNumberWorkWeek;
+ use crate::linting::tests::assert_suggestion_result;
+
+ #[test]
+ fn corrects_four_day_work_week() {
+ assert_suggestion_result(
+ "Sign the contract and get 4 day work weeks.",
+ HyphenateNumberWorkWeek::default(),
+ "Sign the contract and get 4-day work weeks.",
+ );
+ }
+
+ #[test]
+ fn does_not_correct_unrelated_usage() {
+ assert_suggestion_result(
+ "The trip lasted 4 days, including travel.",
+ HyphenateNumberWorkWeek::default(),
+ "The trip lasted 4 days, including travel.",
+ );
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 97833dd85..e04921e83 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -21,6 +21,7 @@ use super::dot_initialisms::DotInitialisms;
use super::ellipsis_length::EllipsisLength;
use super::hereby::Hereby;
use super::hop_hope::HopHope;
+use super::hyphenate_number_work_week::HyphenateNumberWorkWeek;
use super::left_right_hand::LeftRightHand;
use super::lets_confusion::LetsConfusion;
use super::likewise::Likewise;
@@ -181,6 +182,7 @@ macro_rules! create_lint_group_config {
}
create_lint_group_config!(
+ HyphenateNumberWorkWeek => true,
FaceFirst => true,
LeftRightHand => true,
FastPaste => true,
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index f6f5056f1..20d23acd9 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -16,6 +16,7 @@ mod dot_initialisms;
mod ellipsis_length;
mod hereby;
mod hop_hope;
+mod hyphenate_number_work_week;
mod left_right_hand;
mod lets_confusion;
mod likewise;
@@ -73,6 +74,7 @@ pub use dot_initialisms::DotInitialisms;
pub use ellipsis_length::EllipsisLength;
pub use hereby::Hereby;
pub use hop_hope::HopHope;
+pub use hyphenate_number_work_week::HyphenateNumberWorkWeek;
pub use left_right_hand::LeftRightHand;
pub use lets_confusion::LetsConfusion;
pub use likewise::Likewise;