Skip to content

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

feat(core): created new rule for #414

Elijah Potter9d6a90794be8
diff --git a/harper-core/src/linting/lint.rs b/harper-core/src/linting/lint.rs
index eeb0f9104..4759a625b 100644
--- a/harper-core/src/linting/lint.rs
+++ b/harper-core/src/linting/lint.rs
@@ -69,6 +69,11 @@ pub enum Suggestion {
}
impl Suggestion {
+ /// Variant of [`Self::replace_with_match_case`] that accepts a static string.
+ pub fn replace_with_match_case_str(value: &'static str, template: &[char]) -> Self {
+ Self::replace_with_match_case(value.chars().collect(), template)
+ }
+
/// Construct an instance of [`Self::ReplaceWith`], but make the content match the case of the
/// provided template.
///
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index fdbaa5c2f..be7ef925a 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -22,6 +22,7 @@ use super::proper_noun_capitalization_linters::{
};
use super::repeated_words::RepeatedWords;
use super::sentence_capitalization::SentenceCapitalization;
+use super::somewhat_something::SomewhatSomething;
use super::spaces::Spaces;
use super::spell_check::SpellCheck;
use super::spelled_numbers::SpelledNumbers;
@@ -187,7 +188,8 @@ create_lint_group_config!(
PluralConjugate => false,
OxfordComma => true,
PronounContraction => true,
- CurrencyPlacement => true
+ CurrencyPlacement => true,
+ SomewhatSomething => true
);
impl<T: Dictionary + Default> Default for LintGroup<T> {
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 6eae137e3..47a799697 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -23,6 +23,7 @@ mod pronoun_contraction;
mod proper_noun_capitalization_linters;
mod repeated_words;
mod sentence_capitalization;
+mod somewhat_something;
mod spaces;
mod spell_check;
mod spelled_numbers;
@@ -58,6 +59,7 @@ pub use proper_noun_capitalization_linters::{
};
pub use repeated_words::RepeatedWords;
pub use sentence_capitalization::SentenceCapitalization;
+pub use somewhat_something::SomewhatSomething;
pub use spaces::Spaces;
pub use spell_check::SpellCheck;
pub use spelled_numbers::SpelledNumbers;
diff --git a/harper-core/src/linting/somewhat_something.rs b/harper-core/src/linting/somewhat_something.rs
new file mode 100644
index 000000000..9ee17a152
--- /dev/null
+++ b/harper-core/src/linting/somewhat_something.rs
@@ -0,0 +1,63 @@
+use crate::{
+ patterns::{Pattern, SequencePattern},
+ Token,
+};
+
+use super::{Lint, LintKind, PatternLinter, Suggestion};
+
+pub struct SomewhatSomething {
+ pattern: Box<dyn Pattern>,
+}
+
+impl Default for SomewhatSomething {
+ fn default() -> Self {
+ let pattern = SequencePattern::aco("somewhat")
+ .then_whitespace()
+ .t_aco("of")
+ .then_whitespace()
+ .t_aco("a");
+
+ Self {
+ pattern: Box::new(pattern),
+ }
+ }
+}
+
+impl PatternLinter for SomewhatSomething {
+ fn pattern(&self) -> &dyn Pattern {
+ self.pattern.as_ref()
+ }
+
+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {
+ let span = matched_tokens.first().unwrap().span;
+ let og = span.get_content(source);
+
+ Lint {
+ span,
+ lint_kind: LintKind::Style,
+ suggestions: vec![Suggestion::replace_with_match_case_str("something", og)],
+ message: "Use the traditional form.".to_owned(),
+ priority: 63,
+ }
+ }
+
+ fn description(&self) -> &'static str {
+ "When describing a single instance of a noun, use `something` rather than `somewhat`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::tests::assert_suggestion_result;
+
+ use super::SomewhatSomething;
+
+ #[test]
+ fn issue_414() {
+ assert_suggestion_result(
+ "This may be somewhat of a surprise.",
+ SomewhatSomething::default(),
+ "This may be something of a surprise.",
+ );
+ }
+}