Skip to content

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

feat(core): rule for typo of `safe` as `save`

Elijah Potter4cce6b46b09e
diff --git a/harper-core/src/linting/else_possessive.rs b/harper-core/src/linting/else_possessive.rs
index 475b2354d..1b86b7e4c 100644
--- a/harper-core/src/linting/else_possessive.rs
+++ b/harper-core/src/linting/else_possessive.rs
@@ -38,7 +38,7 @@ impl PatternLinter for ElsePossessive {
}
fn match_to_lint(&self, toks: &[Token], _src: &[char]) -> Option<Lint> {
- let offender = toks.last()?;
+ let offender = toks.get(2)?;
Some(Lint {
span: offender.span,
lint_kind: LintKind::Miscellaneous,
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 668c20995..80dc0248c 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -60,6 +60,7 @@ use super::pronoun_contraction::PronounContraction;
use super::pronoun_knew::PronounKnew;
use super::proper_noun_capitalization_linters;
use super::repeated_words::RepeatedWords;
+use super::save_to_safe::SaveToSafe;
use super::sentence_capitalization::SentenceCapitalization;
use super::somewhat_something::SomewhatSomething;
use super::spaces::Spaces;
@@ -315,6 +316,7 @@ impl LintGroup {
insert_pattern_rule!(BoringWords, false);
insert_struct_rule!(CapitalizePersonalPronouns, true);
insert_pattern_rule!(ChockFull, true);
+ insert_struct_rule!(SaveToSafe, true);
insert_struct_rule!(NailOnTheHead, true);
insert_struct_rule!(NominalWants, true);
insert_struct_rule!(ElsePossessive, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 5caca75a6..560a3ff45 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -64,6 +64,7 @@ mod pronoun_contraction;
mod pronoun_knew;
mod proper_noun_capitalization_linters;
mod repeated_words;
+mod save_to_safe;
mod sentence_capitalization;
mod somewhat_something;
mod spaces;
@@ -134,6 +135,7 @@ pub use pique_interest::PiqueInterest;
pub use possessive_your::PossessiveYour;
pub use pronoun_contraction::PronounContraction;
pub use repeated_words::RepeatedWords;
+pub use save_to_safe::SaveToSafe;
pub use sentence_capitalization::SentenceCapitalization;
pub use somewhat_something::SomewhatSomething;
pub use spaces::Spaces;
@@ -267,8 +269,6 @@ mod tests {
let mut iter_count = 0;
loop {
- iter_count += 1;
-
let test = Document::new_from_vec(
text_chars.clone().into(),
&PlainEnglish,
@@ -289,6 +289,8 @@ mod tests {
break;
}
+ iter_count += 1;
+
if iter_count == 100 {
break;
}
diff --git a/harper-core/src/linting/save_to_safe.rs b/harper-core/src/linting/save_to_safe.rs
new file mode 100644
index 000000000..2da392635
--- /dev/null
+++ b/harper-core/src/linting/save_to_safe.rs
@@ -0,0 +1,77 @@
+use crate::{
+ Token,
+ linting::{Lint, LintKind, PatternLinter, Suggestion},
+ patterns::{InflectionOfBe, OwnedPatternExt, Pattern, SequencePattern, Word},
+};
+
+pub struct SaveToSafe {
+ pattern: Box<dyn Pattern>,
+}
+
+impl Default for SaveToSafe {
+ fn default() -> Self {
+ let pattern = SequencePattern::default()
+ .then(InflectionOfBe::new().or(Box::new(Word::new("it"))))
+ .then_whitespace()
+ .t_aco("save")
+ .then_whitespace()
+ .t_aco("to")
+ .then_whitespace()
+ .then_verb();
+ Self {
+ pattern: Box::new(pattern),
+ }
+ }
+}
+
+impl PatternLinter for SaveToSafe {
+ fn pattern(&self) -> &dyn Pattern {
+ self.pattern.as_ref()
+ }
+
+ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
+ let save_tok = &toks.get(2)?;
+ let verb_tok = &toks.get(4)?;
+ let verb = verb_tok.span.get_content_string(src).to_lowercase();
+ Some(Lint {
+ span: save_tok.span,
+ lint_kind: LintKind::WordChoice,
+ suggestions: vec![Suggestion::ReplaceWith("safe".chars().collect())],
+ message: format!("Did you mean `safe to {verb}`?"),
+ priority: 57,
+ })
+ }
+
+ fn description(&self) -> &str {
+ "Corrects `save to <verb>` to `safe to <verb>` after a form of `be`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::SaveToSafe;
+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+
+ #[test]
+ fn fix_ignore() {
+ assert_suggestion_result(
+ "It is save to ignore trivial code.",
+ SaveToSafe::default(),
+ "It is safe to ignore trivial code.",
+ );
+ }
+
+ #[test]
+ fn fix_travel() {
+ assert_suggestion_result(
+ "Is it save to travel abroad now?",
+ SaveToSafe::default(),
+ "Is it safe to travel abroad now?",
+ );
+ }
+
+ #[test]
+ fn ignore_correct() {
+ assert_lint_count("It is safe to assume nothing.", SaveToSafe::default(), 0);
+ }
+}