Skip to content

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

feat(core): created rule for `it self`

Elijah Potterf2ec21a78699
diff --git a/harper-core/src/linting/itself.rs b/harper-core/src/linting/itself.rs
new file mode 100644
index 000000000..dfe36c911
--- /dev/null
+++ b/harper-core/src/linting/itself.rs
@@ -0,0 +1,62 @@
+use crate::{
+ patterns::{Pattern, SequencePattern},
+ Token, TokenStringExt,
+};
+
+use super::{Lint, LintKind, PatternLinter, Suggestion};
+
+pub struct Itself {
+ pattern: Box<dyn Pattern>,
+}
+
+impl Default for Itself {
+ fn default() -> Self {
+ let pattern = SequencePattern::default()
+ .t_aco("it")
+ .then_whitespace()
+ .t_aco("self");
+
+ Self {
+ pattern: Box::new(pattern),
+ }
+ }
+}
+
+impl PatternLinter for Itself {
+ 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!['i', 't', 's', 'e', 'l', 'f'],
+ orig_chars,
+ )],
+ message: "`Itself` is commonly mistaken for `it self`.".to_owned(),
+ ..Default::default()
+ }
+ }
+
+ fn description(&self) -> &'static str {
+ "Lint for condensing `it self` to `itself`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::Itself;
+ use crate::linting::tests::assert_suggestion_result;
+
+ #[test]
+ fn it_self() {
+ let test_sentence = "The project, it self, was quite challenging.";
+ let expected = "The project, itself, was quite challenging.";
+ assert_suggestion_result(test_sentence, Itself::default(), expected);
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index ce9608014..6a98b0c92 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -9,6 +9,7 @@ use super::correct_number_suffix::CorrectNumberSuffix;
use super::despite_of::DespiteOf;
use super::dot_initialisms::DotInitialisms;
use super::ellipsis_length::EllipsisLength;
+use super::itself::Itself;
use super::lets_confusion::LetsConfusion;
use super::linking_verbs::LinkingVerbs;
use super::long_sentences::LongSentences;
@@ -161,6 +162,7 @@ macro_rules! create_lint_group_config {
}
create_lint_group_config!(
+ Itself => true,
Whereas => true,
SpelledNumbers => false,
AnA => true,
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index d42d6d454..528bbdcd9 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -12,6 +12,7 @@ mod dashes;
mod despite_of;
mod dot_initialisms;
mod ellipsis_length;
+mod itself;
mod lets_confusion;
mod linking_verbs;
mod lint;
@@ -52,6 +53,7 @@ pub use currency_placement::CurrencyPlacement;
pub use despite_of::DespiteOf;
pub use dot_initialisms::DotInitialisms;
pub use ellipsis_length::EllipsisLength;
+pub use itself::Itself;
pub use lets_confusion::LetsConfusion;
pub use linking_verbs::LinkingVerbs;
pub use lint::Lint;
diff --git a/justfile b/justfile
index 66a59edda..08b64b4d7 100644
--- a/justfile
+++ b/justfile
@@ -296,9 +296,8 @@ registerlinter module name:
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"
+ sed -i "/mod an_a;/a mod {{module}};" "$D/mod.rs"
+ sed -i "/pub use an_a::AnA;/a pub use {{module}}::{{name}};" "$D/mod.rs"
+ sed -i "/use super::an_a::AnA;/a use super::{{module}}::{{name}};" "$D/lint_group.rs"
+ sed -i "/create_lint_group_config\!/a \ \ \ \ {{name}} => true," "$D/lint_group.rs"
just format