Skip to content

Commit cc21812

Browse files
Merge pull request #455 from Automattic/somewhat-something
feat(core): created a new rule that resolves #414
2 parents 825cc53 + 9d6a907 commit cc21812

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

harper-core/src/linting/lint.rs

+5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ pub enum Suggestion {
6969
}
7070

7171
impl Suggestion {
72+
/// Variant of [`Self::replace_with_match_case`] that accepts a static string.
73+
pub fn replace_with_match_case_str(value: &'static str, template: &[char]) -> Self {
74+
Self::replace_with_match_case(value.chars().collect(), template)
75+
}
76+
7277
/// Construct an instance of [`Self::ReplaceWith`], but make the content match the case of the
7378
/// provided template.
7479
///

harper-core/src/linting/lint_group.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use super::proper_noun_capitalization_linters::{
2222
};
2323
use super::repeated_words::RepeatedWords;
2424
use super::sentence_capitalization::SentenceCapitalization;
25+
use super::somewhat_something::SomewhatSomething;
2526
use super::spaces::Spaces;
2627
use super::spell_check::SpellCheck;
2728
use super::spelled_numbers::SpelledNumbers;
@@ -187,7 +188,8 @@ create_lint_group_config!(
187188
PluralConjugate => false,
188189
OxfordComma => true,
189190
PronounContraction => true,
190-
CurrencyPlacement => true
191+
CurrencyPlacement => true,
192+
SomewhatSomething => true
191193
);
192194

193195
impl<T: Dictionary + Default> Default for LintGroup<T> {

harper-core/src/linting/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ mod pronoun_contraction;
2323
mod proper_noun_capitalization_linters;
2424
mod repeated_words;
2525
mod sentence_capitalization;
26+
mod somewhat_something;
2627
mod spaces;
2728
mod spell_check;
2829
mod spelled_numbers;
@@ -58,6 +59,7 @@ pub use proper_noun_capitalization_linters::{
5859
};
5960
pub use repeated_words::RepeatedWords;
6061
pub use sentence_capitalization::SentenceCapitalization;
62+
pub use somewhat_something::SomewhatSomething;
6163
pub use spaces::Spaces;
6264
pub use spell_check::SpellCheck;
6365
pub use spelled_numbers::SpelledNumbers;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use crate::{
2+
patterns::{Pattern, SequencePattern},
3+
Token,
4+
};
5+
6+
use super::{Lint, LintKind, PatternLinter, Suggestion};
7+
8+
pub struct SomewhatSomething {
9+
pattern: Box<dyn Pattern>,
10+
}
11+
12+
impl Default for SomewhatSomething {
13+
fn default() -> Self {
14+
let pattern = SequencePattern::aco("somewhat")
15+
.then_whitespace()
16+
.t_aco("of")
17+
.then_whitespace()
18+
.t_aco("a");
19+
20+
Self {
21+
pattern: Box::new(pattern),
22+
}
23+
}
24+
}
25+
26+
impl PatternLinter for SomewhatSomething {
27+
fn pattern(&self) -> &dyn Pattern {
28+
self.pattern.as_ref()
29+
}
30+
31+
fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {
32+
let span = matched_tokens.first().unwrap().span;
33+
let og = span.get_content(source);
34+
35+
Lint {
36+
span,
37+
lint_kind: LintKind::Style,
38+
suggestions: vec![Suggestion::replace_with_match_case_str("something", og)],
39+
message: "Use the traditional form.".to_owned(),
40+
priority: 63,
41+
}
42+
}
43+
44+
fn description(&self) -> &'static str {
45+
"When describing a single instance of a noun, use `something` rather than `somewhat`."
46+
}
47+
}
48+
49+
#[cfg(test)]
50+
mod tests {
51+
use crate::linting::tests::assert_suggestion_result;
52+
53+
use super::SomewhatSomething;
54+
55+
#[test]
56+
fn issue_414() {
57+
assert_suggestion_result(
58+
"This may be somewhat of a surprise.",
59+
SomewhatSomething::default(),
60+
"This may be something of a surprise.",
61+
);
62+
}
63+
}

0 commit comments

Comments
 (0)