mirror of
https://github.com/Automattic/harper.git
synced 2025-07-07 13:05:01 +00:00
feat(core): added Whereas
rule
This commit is contained in:
parent
73dc67748c
commit
8836400131
5 changed files with 83 additions and 1 deletions
|
@ -37,6 +37,7 @@ use super::terminating_conjunctions::TerminatingConjunctions;
|
|||
use super::that_which::ThatWhich;
|
||||
use super::unclosed_quotes::UnclosedQuotes;
|
||||
use super::use_genitive::UseGenitive;
|
||||
use super::whereas::Whereas;
|
||||
use super::wrong_quotes::WrongQuotes;
|
||||
use super::{CurrencyPlacement, Lint, Linter, NoOxfordComma, OxfordComma};
|
||||
use crate::{Dictionary, Document};
|
||||
|
@ -160,6 +161,7 @@ macro_rules! create_lint_group_config {
|
|||
}
|
||||
|
||||
create_lint_group_config!(
|
||||
Whereas => true,
|
||||
SpelledNumbers => false,
|
||||
AnA => true,
|
||||
SentenceCapitalization => true,
|
||||
|
@ -219,7 +221,7 @@ create_lint_group_config!(
|
|||
SneakingSuspicion => true,
|
||||
SupposeTo => true,
|
||||
SpecialAttention => true,
|
||||
ThanOthers => true
|
||||
ThanOthers => true,
|
||||
);
|
||||
|
||||
impl<T: Dictionary + Default> Default for LintGroup<T> {
|
||||
|
|
|
@ -167,3 +167,5 @@ mod tests {
|
|||
assert_lint_count(&transformed_str, linter, 0);
|
||||
}
|
||||
}
|
||||
pub use whereas::Whereas;
|
||||
mod whereas;
|
||||
|
|
65
harper-core/src/linting/whereas.rs
Normal file
65
harper-core/src/linting/whereas.rs
Normal file
|
@ -0,0 +1,65 @@
|
|||
use crate::{
|
||||
patterns::{Pattern, SequencePattern},
|
||||
Token, TokenStringExt,
|
||||
};
|
||||
|
||||
use super::{Lint, LintKind, PatternLinter, Suggestion};
|
||||
|
||||
pub struct Whereas {
|
||||
pattern: Box<dyn Pattern>,
|
||||
}
|
||||
|
||||
impl Default for Whereas {
|
||||
fn default() -> Self {
|
||||
let pattern = SequencePattern::default()
|
||||
.t_aco("where")
|
||||
.then_whitespace()
|
||||
.t_aco("as");
|
||||
|
||||
Self {
|
||||
pattern: Box::new(pattern),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PatternLinter for Whereas {
|
||||
fn pattern(&self) -> &dyn Pattern {
|
||||
self.pattern.as_ref()
|
||||
}
|
||||
|
||||
fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {
|
||||
let span = matched_tokens.span().unwrap();
|
||||
let orig_chars = span.get_content(source);
|
||||
|
||||
Lint {
|
||||
span,
|
||||
lint_kind: LintKind::WordChoice,
|
||||
suggestions: vec![Suggestion::replace_with_match_case(
|
||||
vec!['w', 'h', 'e', 'r', 'e', 'a', 's'],
|
||||
orig_chars,
|
||||
)],
|
||||
message: "`Whereas` is commonly mistaken for `where as`.".to_owned(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn description(&self) -> &'static str {
|
||||
"The Whereas rule is designed to identify instances where the phrase `where as` is used in text and suggests replacing it with the single word `whereas`."
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::linting::tests::assert_suggestion_result;
|
||||
|
||||
use super::Whereas;
|
||||
|
||||
#[test]
|
||||
fn where_as() {
|
||||
assert_suggestion_result(
|
||||
"Dogs love playing fetch, where as cats are more independent creatures.",
|
||||
Whereas::default(),
|
||||
"Dogs love playing fetch, whereas cats are more independent creatures.",
|
||||
);
|
||||
}
|
||||
}
|
12
justfile
12
justfile
|
@ -290,3 +290,15 @@ fuzz:
|
|||
exit $?
|
||||
fi
|
||||
done
|
||||
|
||||
registerlinter module name:
|
||||
#! /bin/bash
|
||||
|
||||
D="{{justfile_directory()}}/harper-core/src/linting"
|
||||
|
||||
echo "pub use {{module}}::{{name}};" >> "$D/mod.rs"
|
||||
echo "use super::{{module}}::{{name}};" >> "$D/lint_group.rs"
|
||||
echo "mod {{module}};" >> "$D/mod.rs"
|
||||
|
||||
sed -i "/create_lint_group_config\!/a {{name}} => true," "$D/lint_group.rs"
|
||||
just format
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
newline_style = "Unix"
|
||||
use_field_init_shorthand = true
|
||||
reorder_imports = true
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue