Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): create new linter for dollar sign placement
Elijah Potter49e2404a34c6
diff --git a/harper-core/src/linting/dollar_placement.rs b/harper-core/src/linting/dollar_placement.rsnew file mode 100644index 000000000..ccbe1477a--- /dev/null+++ b/harper-core/src/linting/dollar_placement.rs@@ -0,0 +1,92 @@+use crate::{+ patterns::{Pattern, SequencePattern},+ Punctuation, Token, TokenKind, TokenStringExt,+};++use super::{Lint, LintKind, PatternLinter, Suggestion};++static DESCRIPTION: &str = "Dollar signs should always come before the quantity being described.";++pub struct DollarPlacement {+ pattern: Box<dyn Pattern>,+}++impl Default for DollarPlacement {+ fn default() -> Self {+ let pattern = SequencePattern::default().then_number().then(Box::new(+ |t: &Token, _source: &[char]| {+ matches!(t.kind, TokenKind::Punctuation(Punctuation::Dollar))+ },+ ));++ Self {+ pattern: Box::new(pattern),+ }+ }+}++impl PatternLinter for DollarPlacement {+ fn pattern(&self) -> &dyn Pattern {+ self.pattern.as_ref()+ }++ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {+ let _ = source;+ // We can unwrap like this thanks to the pattern.+ let (value, suffix) = matched_tokens.first().unwrap().kind.as_number().unwrap();++ let mut fix = vec!['$'];++ fix.extend(value.to_string().chars());++ if let Some(suffix) = suffix {+ fix.extend(suffix.to_chars());+ }++ Lint {+ span: matched_tokens[0..2].span().unwrap(),+ lint_kind: LintKind::Formatting,+ suggestions: vec![Suggestion::ReplaceWith(fix)],+ message: DESCRIPTION.to_string(),+ priority: 63,+ }+ }++ fn description(&self) -> &'static str {+ DESCRIPTION+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::assert_suggestion_result;++ use super::DollarPlacement;++ #[test]+ fn eof() {+ assert_suggestion_result(+ "It was my last bill worth more than 4$.",+ DollarPlacement::default(),+ "It was my last bill worth more than $4.",+ );+ }++ #[test]+ fn blog_title() {+ assert_suggestion_result(+ "The Best 25$ I Ever Spent",+ DollarPlacement::default(),+ "The Best $25 I Ever Spent",+ );+ }++ #[test]+ fn suffix() {+ assert_suggestion_result(+ "It was my 20th$.",+ DollarPlacement::default(),+ "It was my $20th.",+ );+ }+}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 2c1fc5ea9..8d311f40b 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -30,7 +30,7 @@ use super::that_which::ThatWhich;use super::unclosed_quotes::UnclosedQuotes;use super::use_genitive::UseGenitive;use super::wrong_quotes::WrongQuotes;-use super::{Lint, Linter, OxfordComma};+use super::{DollarPlacement, Lint, Linter, OxfordComma};use crate::{Dictionary, Document};macro_rules! create_lint_group_config {@@ -186,7 +186,8 @@ create_lint_group_config!(MergeWords => true,PluralConjugate => false,OxfordComma => true,- PronounContraction => true+ PronounContraction => true,+ DollarPlacement => true);impl<T: Dictionary + Default> Default for LintGroup<T> {diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 94fad8d54..8dc32f5b3 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 correct_number_suffix;mod dashes;+mod dollar_placement;mod dot_initialisms;mod ellipsis_length;mod linking_verbs;@@ -36,6 +37,7 @@ pub use avoid_curses::AvoidCurses;pub use boring_words::BoringWords;pub use capitalize_personal_pronouns::CapitalizePersonalPronouns;pub use correct_number_suffix::CorrectNumberSuffix;+pub use dollar_placement::DollarPlacement;pub use dot_initialisms::DotInitialisms;pub use ellipsis_length::EllipsisLength;pub use linking_verbs::LinkingVerbs;diff --git a/harper-core/src/patterns/sequence_pattern.rs b/harper-core/src/patterns/sequence_pattern.rsindex e581c1801..e3b21b9d0 100644--- a/harper-core/src/patterns/sequence_pattern.rs+++ b/harper-core/src/patterns/sequence_pattern.rs@@ -53,6 +53,7 @@ impl SequencePattern {gen_then_from_is!(conjunction);gen_then_from_is!(comma);gen_then_from_is!(period);+ gen_then_from_is!(number);gen_then_from_is!(case_separator);gen_then_from_is!(adverb);gen_then_from_is!(adjective);