Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): wrote linter for #702
Elijah Potter4d46df761db3
diff --git a/harper-core/src/linting/back_in_the_day.rs b/harper-core/src/linting/back_in_the_day.rsnew file mode 100644index 000000000..c925acfaf--- /dev/null+++ b/harper-core/src/linting/back_in_the_day.rs@@ -0,0 +1,139 @@+use itertools::Itertools;++use crate::{+ patterns::{ExactPhrase, OwnedPatternExt, Pattern, SequencePattern, WordSet},+ Lrc, Token, TokenStringExt,+};++use super::{Lint, LintKind, PatternLinter, Suggestion};++pub struct BackInTheDay {+ pattern: Box<dyn Pattern>,+ // The trailing words that should tell us to ignore the rule.+ exceptions: Lrc<WordSet>,+}++impl Default for BackInTheDay {+ fn default() -> Self {+ let exceptions = Lrc::new(WordSet::all(&["of", "when"]));+ let phrase = Lrc::new(ExactPhrase::from_phrase("back in the days"));++ let pattern = SequencePattern::default()+ .then(Box::new(phrase.clone()))+ .then_whitespace()+ .then(Box::new(exceptions.clone()))+ .or(Box::new(phrase));++ Self {+ pattern: Box::new(pattern),+ exceptions,+ }+ }+}++impl PatternLinter for BackInTheDay {+ fn pattern(&self) -> &dyn Pattern {+ self.pattern.as_ref()+ }++ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {+ dbg!(matched_tokens+ .iter()+ .map(|t| t.to_fat(source))+ .enumerate()+ .collect_vec());+ if let Some(tail) = matched_tokens.get(8..) {+ dbg!(tail.iter().map(|t| t.to_fat(source)).collect_vec());+ if self.exceptions.matches(tail, source) != 0 {+ return None;+ }+ }++ let span = matched_tokens.span()?;+ let chars = span.get_content(source);++ Some(Lint {+ span,+ lint_kind: LintKind::WordChoice,+ suggestions: vec![Suggestion::replace_with_match_case(+ "back in the day".chars().collect(),+ chars,+ )],+ message: "Use the more idiomatic version of this phrase.".to_owned(),+ priority: 127,+ })+ }++ fn description(&self) -> &'static str {+ "This linter flags instances of the nonstandard phrase `back in the days`. The correct, more accepted form is `back in the day`"+ }+}++#[cfg(test)]+mod tests {+ use super::BackInTheDay;+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};++ #[test]+ fn detects_gem_update_case() {+ assert_suggestion_result(+ "... has been resolved through a gem update back in the days",+ BackInTheDay::default(),+ "... has been resolved through a gem update back in the day",+ );+ }++ #[test]+ fn detects_install_case() {+ assert_suggestion_result(+ "Back in the days we're used to install it directly from ...",+ BackInTheDay::default(),+ "Back in the day we're used to install it directly from ...",+ );+ }++ #[test]+ fn detects_composer_json_case() {+ assert_suggestion_result(+ "Back in the days there was only composer.json and ...",+ BackInTheDay::default(),+ "Back in the day there was only composer.json and ...",+ );+ }++ #[test]+ fn detects_version_release_case() {+ assert_suggestion_result(+ "... should have been released back in the days in a version 11",+ BackInTheDay::default(),+ "... should have been released back in the day in a version 11",+ );+ }++ #[test]+ fn avoids_false_positive_springfox() {+ assert_lint_count(+ "Back in the days of SpringFox, there were several requests to ...",+ BackInTheDay::default(),+ 0,+ );+ }++ #[test]+ fn avoids_false_positive_ie() {+ assert_lint_count(+ "Back in the days of IE, Powershell used to ...",+ BackInTheDay::default(),+ 0,+ );+ }++ #[test]+ fn avoids_false_positive_code_usage() {+ assert_lint_count(+ "Back in the days when I had 100% of my code in ...",+ BackInTheDay::default(),+ 0,+ );+ }+}diff --git a/harper-core/src/linting/dashes.rs b/harper-core/src/linting/dashes.rsindex 6098890a6..27f70737f 100644--- a/harper-core/src/linting/dashes.rs+++ b/harper-core/src/linting/dashes.rs@@ -49,7 +49,7 @@ impl PatternLinter for Dashes {message: "A sequence of hyphens is not an em dash.".to_owned(),priority: 63,}),- _ => Some(panic!("Received unexpected number of tokens.")),+ _ => panic!("Received unexpected number of tokens."),}}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex fda9d1b2e..92b41466b 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};use super::an_a::AnA;use super::avoid_curses::AvoidCurses;+use super::back_in_the_day::BackInTheDay;use super::boring_words::BoringWords;use super::capitalize_personal_pronouns::CapitalizePersonalPronouns;use super::chock_full::ChockFull;@@ -200,6 +201,7 @@ macro_rules! create_lint_group_config {}create_lint_group_config!(+ BackInTheDay => true,WordPressDotcom => true,DayOneNames => true,PocketCastsNames => true,diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 196ce0a77..25b3aeef7 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -4,6 +4,7 @@mod an_a;mod avoid_curses;+mod back_in_the_day;mod boring_words;mod capitalize_personal_pronouns;mod chock_full;@@ -61,6 +62,7 @@ mod wrong_quotes;pub use an_a::AnA;pub use avoid_curses::AvoidCurses;+pub use back_in_the_day::BackInTheDay;pub use boring_words::BoringWords;pub use capitalize_personal_pronouns::CapitalizePersonalPronouns;pub use chock_full::ChockFull;diff --git a/harper-core/src/linting/pattern_linter.rs b/harper-core/src/linting/pattern_linter.rsindex 202a750d9..0113df548 100644--- a/harper-core/src/linting/pattern_linter.rs+++ b/harper-core/src/linting/pattern_linter.rs@@ -12,7 +12,9 @@ pub trait PatternLinter {fn pattern(&self) -> &dyn Pattern;/// If any portions of a [`Document`](crate::Document) match [`Self::pattern`], they are passed through [`PatternLinter::match_to_lint`] to be/// transformed into a [`Lint`] for editor consumption.- fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint;+ ///+ /// This function may return `None` to elect _not_ to produce a lint.+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint>;/// A user-facing description of what kinds of grammatical errors this rule looks for./// It is usually shown in settings menus.fn description(&self) -> &str;