Skip to content

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

feat(core): added linters for common uses of hyphens

Elijah Potterda85de8ee72d
diff --git a/harper-core/src/linting/left_right_hand.rs b/harper-core/src/linting/left_right_hand.rs
new file mode 100644
index 000000000..1bb0aba99
--- /dev/null
+++ b/harper-core/src/linting/left_right_hand.rs
@@ -0,0 +1,81 @@
+use crate::{
+ patterns::{Pattern, SequencePattern, WordSet},
+ Token,
+};
+
+use super::{Lint, LintKind, PatternLinter, Suggestion};
+
+pub struct LeftRightHand {
+ pattern: Box<dyn Pattern>,
+}
+
+impl Default for LeftRightHand {
+ fn default() -> Self {
+ let pattern = SequencePattern::default()
+ .then_word_set(WordSet::all(&["left", "right"]))
+ .then_whitespace()
+ .t_aco("hand")
+ .then_whitespace()
+ .then_noun();
+
+ Self {
+ pattern: Box::new(pattern),
+ }
+ }
+}
+
+impl PatternLinter for LeftRightHand {
+ fn pattern(&self) -> &dyn Pattern {
+ self.pattern.as_ref()
+ }
+
+ fn match_to_lint(&self, matched_tokens: &[Token], _source: &[char]) -> Lint {
+ let space = matched_tokens[1];
+
+ Lint {
+ span: space.span,
+ lint_kind: LintKind::Miscellaneous,
+ suggestions: vec![Suggestion::ReplaceWith(vec!['-'])],
+ message: "Use a hyphen in `left-hand` or `right-hand` when modifying a noun."
+ .to_owned(),
+ priority: 31,
+ }
+ }
+
+ fn description(&self) -> &'static str {
+ "Ensures `left hand` and `right hand` are hyphenated when used as adjectives before a noun, such as in `left-hand side` or `right-hand corner`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::LeftRightHand;
+ use crate::linting::tests::assert_suggestion_result;
+
+ #[test]
+ fn corrects_left_hand_side() {
+ assert_suggestion_result(
+ "You'll see it on the left hand side.",
+ LeftRightHand::default(),
+ "You'll see it on the left-hand side.",
+ );
+ }
+
+ #[test]
+ fn corrects_right_hand_corner() {
+ assert_suggestion_result(
+ "It's in the right hand corner.",
+ LeftRightHand::default(),
+ "It's in the right-hand corner.",
+ );
+ }
+
+ #[test]
+ fn does_not_correct_noun_usage() {
+ assert_suggestion_result(
+ "She raised her right hand.",
+ LeftRightHand::default(),
+ "She raised her right hand.",
+ );
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index ae33ca708..97833dd85 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::left_right_hand::LeftRightHand;
use super::lets_confusion::LetsConfusion;
use super::likewise::Likewise;
use super::linking_verbs::LinkingVerbs;
@@ -30,6 +31,7 @@ use super::merge_words::MergeWords;
use super::multiple_sequential_pronouns::MultipleSequentialPronouns;
use super::nobody::Nobody;
use super::number_suffix_capitalization::NumberSuffixCapitalization;
+use super::phrase_corrections::FaceFirst;
use super::phrase_corrections::FastPaste;
use super::phrase_corrections::StateOfTheArt;
use super::phrase_corrections::WantBe;
@@ -179,6 +181,8 @@ macro_rules! create_lint_group_config {
}
create_lint_group_config!(
+ FaceFirst => true,
+ LeftRightHand => true,
FastPaste => true,
StateOfTheArt => true,
WantBe => true,
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 8ece4f1e0..f6f5056f1 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 left_right_hand;
mod lets_confusion;
mod likewise;
mod linking_verbs;
@@ -72,6 +73,7 @@ pub use dot_initialisms::DotInitialisms;
pub use ellipsis_length::EllipsisLength;
pub use hereby::Hereby;
pub use hop_hope::HopHope;
+pub use left_right_hand::LeftRightHand;
pub use lets_confusion::LetsConfusion;
pub use likewise::Likewise;
pub use linking_verbs::LinkingVerbs;
@@ -87,6 +89,7 @@ pub use nobody::Nobody;
pub use number_suffix_capitalization::NumberSuffixCapitalization;
pub use oxford_comma::OxfordComma;
pub use pattern_linter::PatternLinter;
+pub use phrase_corrections::FaceFirst;
pub use phrase_corrections::FastPaste;
pub use phrase_corrections::StateOfTheArt;
pub use phrase_corrections::WantBe;
diff --git a/harper-core/src/linting/phrase_corrections.rs b/harper-core/src/linting/phrase_corrections.rs
index 0351cb043..65b4af10d 100644
--- a/harper-core/src/linting/phrase_corrections.rs
+++ b/harper-core/src/linting/phrase_corrections.rs
@@ -102,6 +102,14 @@ create_linter_map_phrase!(WantBe, ExactPhrase::from_phrase("want be"),"won't be"
create_linter_map_phrase!(StateOfTheArt, ExactPhrase::from_phrase("state of art"), "state of the art", "Did you mean `state of the art`?", "Detects incorrect usage of `state of art` and suggests `state of the art` as the correct phrase.");
create_linter_map_phrase!(FastPaste, ExactPhrase::from_phrase("fast paste").or(Box::new(ExactPhrase::from_phrase("fast-paste"))), "fast-paced", "Did you mean `fast-paced`?", "Detects incorrect usage of `fast paste` or `fast-paste` and suggests `fast-paced` as the correct phrase.");
+create_linter_map_phrase!(
+ FaceFirst,
+ ExactPhrase::from_phrase("face first into"),
+ "Should this be `face-first`?",
+ "face-first into",
+ "Ensures `face first` is correctly hyphenated as `face-first` when used before `into`."
+);
+
#[cfg(test)]
mod tests {
use crate::linting::tests::{assert_lint_count, assert_suggestion_result};