Skip to content

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

feat(core): cover the simple cases too

Elijah Pottera510f1c60728
diff --git a/harper-core/src/linting/multiple_sequential_pronouns.rs b/harper-core/src/linting/multiple_sequential_pronouns.rs
index 310668bc9..0c24809d7 100644
--- a/harper-core/src/linting/multiple_sequential_pronouns.rs
+++ b/harper-core/src/linting/multiple_sequential_pronouns.rs
@@ -20,11 +20,11 @@ impl MultipleSequentialPronouns {
pattern: Box::new(
SequencePattern::default()
.then(pronouns.clone())
- .then_one_or_more(Box::new(
+ .then_one_or_more(
SequencePattern::default()
.then_whitespace()
.then(pronouns.clone()),
- )),
+ ),
),
}
}
diff --git a/harper-core/src/linting/oxford_comma.rs b/harper-core/src/linting/oxford_comma.rs
deleted file mode 100644
index 578483fa4..000000000
--- a/harper-core/src/linting/oxford_comma.rs
+++ /dev/null
@@ -1,187 +0,0 @@
-use crate::{
- Document, Token, TokenStringExt,
- patterns::{NounPhrase, Pattern, SequencePattern, WordSet},
-};
-
-use super::{Lint, LintKind, Linter, Suggestion};
-
-pub struct OxfordComma {
- pattern: SequencePattern,
-}
-
-impl OxfordComma {
- pub fn new() -> Self {
- let pattern = SequencePattern::default()
- .then_one_or_more(Box::new(
- SequencePattern::default()
- .then(NounPhrase)
- .then_comma()
- .then_whitespace(),
- ))
- .then(NounPhrase)
- .then_whitespace()
- .then(WordSet::new(&["and", "or", "nor"]))
- .then_whitespace()
- .then(NounPhrase);
-
- Self { pattern }
- }
-
- fn match_to_lint(&self, matched_toks: &[Token], _source: &[char]) -> Option<Lint> {
- let conj_index = matched_toks.last_conjunction_index()?;
- let offender = matched_toks[conj_index - 2];
-
- Some(Lint {
- span: offender.span,
- lint_kind: LintKind::Style,
- suggestions: vec![Suggestion::InsertAfter(vec![','])],
- message: "An Oxford comma is necessary here.".to_owned(),
- priority: 31,
- })
- }
-}
-
-impl Default for OxfordComma {
- fn default() -> Self {
- Self::new()
- }
-}
-
-impl Linter for OxfordComma {
- fn lint(&mut self, document: &Document) -> Vec<Lint> {
- let mut lints = Vec::new();
-
- for sentence in document.iter_sentences() {
- let mut tok_cursor = 0;
-
- if let Some(first) = sentence
- .first()
- .and_then(|t| t.kind.as_word().cloned())
- .flatten()
- {
- if first.preposition {
- tok_cursor = sentence
- .iter()
- .position(|t| t.kind.is_comma())
- .unwrap_or(sentence.iter().len())
- }
- }
-
- loop {
- if tok_cursor >= sentence.len() {
- break;
- }
-
- let match_len = self
- .pattern
- .matches(&sentence[tok_cursor..], document.get_source());
-
- if match_len != 0 {
- let lint = self.match_to_lint(
- &sentence[tok_cursor..tok_cursor + match_len],
- document.get_source(),
- );
-
- lints.extend(lint);
- tok_cursor += match_len;
- } else {
- tok_cursor += 1;
- }
- }
- }
-
- lints
- }
-
- fn description(&self) -> &str {
- "The Oxford comma is one of the more controversial rules in common use today. Enabling this lint checks that there is a comma before `and`, `or`, or `nor` when listing out more than two ideas."
- }
-}
-
-#[cfg(test)]
-mod tests {
- use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
-
- use super::OxfordComma;
-
- #[test]
- fn people() {
- assert_suggestion_result(
- "Nancy, Steve and Carl are going to the coffee shop.",
- OxfordComma::default(),
- "Nancy, Steve, and Carl are going to the coffee shop.",
- );
- }
-
- #[test]
- fn places() {
- assert_suggestion_result(
- "I've always wanted to visit Paris, Tokyo and Rome.",
- OxfordComma::default(),
- "I've always wanted to visit Paris, Tokyo, and Rome.",
- );
- }
-
- #[test]
- fn foods() {
- assert_suggestion_result(
- "My favorite foods are pizza, sushi, tacos and burgers.",
- OxfordComma::default(),
- "My favorite foods are pizza, sushi, tacos, and burgers.",
- );
- }
-
- #[test]
- fn allows_clean_music() {
- assert_lint_count(
- "I enjoy listening to pop music, rock, hip-hop, electronic dance, and classical music.",
- OxfordComma::default(),
- 0,
- );
- }
-
- #[test]
- fn allows_clean_nations() {
- assert_lint_count(
- "The team consists of players from different countries: France, Germany, Italy, and Spain.",
- OxfordComma::default(),
- 0,
- );
- }
-
- #[test]
- fn or_writing() {
- assert_suggestion_result(
- "Harper can be a lifesaver when writing technical documents, emails or other formal forms of communication.",
- OxfordComma::default(),
- "Harper can be a lifesaver when writing technical documents, emails, or other formal forms of communication.",
- );
- }
-
- #[test]
- fn sports() {
- assert_suggestion_result(
- "They enjoy playing soccer, basketball or tennis.",
- OxfordComma::default(),
- "They enjoy playing soccer, basketball, or tennis.",
- );
- }
-
- #[test]
- fn nor_vegetables() {
- assert_suggestion_result(
- "I like carrots, kale nor broccoli.",
- OxfordComma::default(),
- "I like carrots, kale, nor broccoli.",
- );
- }
-
- #[test]
- fn allow_non_list_transportation() {
- assert_lint_count(
- "In transportation, autonomous vehicles and smart traffic management systems promise to reduce accidents and optimize travel routes.",
- OxfordComma::default(),
- 0,
- );
- }
-}
diff --git a/harper-core/src/linting/oxford_comma/general.rs b/harper-core/src/linting/oxford_comma/general.rs
new file mode 100644
index 000000000..06af2494e
--- /dev/null
+++ b/harper-core/src/linting/oxford_comma/general.rs
@@ -0,0 +1,99 @@
+use crate::{
+ Document, Token, TokenStringExt,
+ patterns::{NounPhrase, Pattern, SequencePattern, WordSet},
+};
+
+use super::super::{Lint, LintKind, Linter, Suggestion};
+
+pub struct General {
+ pattern: SequencePattern,
+}
+
+impl General {
+ pub fn new() -> Self {
+ let pattern = SequencePattern::default()
+ .then_one_or_more(
+ SequencePattern::default()
+ .then(NounPhrase)
+ .then_comma()
+ .then_whitespace(),
+ )
+ .then(NounPhrase)
+ .then_whitespace()
+ .then(WordSet::new(&["and", "or", "nor"]))
+ .then_whitespace()
+ .then(NounPhrase);
+
+ Self { pattern }
+ }
+
+ fn match_to_lint(&self, matched_toks: &[Token], _source: &[char]) -> Option<Lint> {
+ let conj_index = matched_toks.last_conjunction_index()?;
+ let offender = matched_toks[conj_index - 2];
+
+ Some(Lint {
+ span: offender.span,
+ lint_kind: LintKind::Style,
+ suggestions: vec![Suggestion::InsertAfter(vec![','])],
+ message: "An Oxford comma is necessary here.".to_owned(),
+ priority: 31,
+ })
+ }
+}
+
+impl Default for General {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl Linter for General {
+ fn lint(&mut self, document: &Document) -> Vec<Lint> {
+ let mut lints = Vec::new();
+
+ for sentence in document.iter_sentences() {
+ let mut tok_cursor = 0;
+
+ if let Some(first) = sentence
+ .first()
+ .and_then(|t| t.kind.as_word().cloned())
+ .flatten()
+ {
+ if first.preposition {
+ tok_cursor = sentence
+ .iter()
+ .position(|t| t.kind.is_comma())
+ .unwrap_or(sentence.iter().len())
+ }
+ }
+
+ loop {
+ if tok_cursor >= sentence.len() {
+ break;
+ }
+
+ let match_len = self
+ .pattern
+ .matches(&sentence[tok_cursor..], document.get_source());
+
+ if match_len != 0 {
+ let lint = self.match_to_lint(
+ &sentence[tok_cursor..tok_cursor + match_len],
+ document.get_source(),
+ );
+
+ lints.extend(lint);
+ tok_cursor += match_len;
+ } else {
+ tok_cursor += 1;
+ }
+ }
+ }
+
+ lints
+ }
+
+ fn description(&self) -> &str {
+ "Covers the general cases of missing Oxford commas."
+ }
+}
diff --git a/harper-core/src/linting/oxford_comma/mod.rs b/harper-core/src/linting/oxford_comma/mod.rs
new file mode 100644
index 000000000..f8e0c2696
--- /dev/null
+++ b/harper-core/src/linting/oxford_comma/mod.rs
@@ -0,0 +1,105 @@
+use super::merge_linters::merge_linters;
+
+mod general;
+mod simple;
+use general::General;
+use simple::Simple;
+
+merge_linters!(OxfordComma => General, Simple => "The Oxford comma is one of the more controversial rules in common use today. Enabling this lint checks that there is a comma before `and`, `or`, or `nor` when listing out more than two ideas.");
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+
+ use super::OxfordComma;
+
+ #[test]
+ fn fruits() {
+ assert_lint_count(
+ "An apple, a banana and a pear walk into a bar.",
+ OxfordComma::default(),
+ 1,
+ );
+ }
+
+ #[test]
+ fn people() {
+ assert_suggestion_result(
+ "Nancy, Steve and Carl are going to the coffee shop.",
+ OxfordComma::default(),
+ "Nancy, Steve, and Carl are going to the coffee shop.",
+ );
+ }
+
+ #[test]
+ fn places() {
+ assert_suggestion_result(
+ "I've always wanted to visit Paris, Tokyo and Rome.",
+ OxfordComma::default(),
+ "I've always wanted to visit Paris, Tokyo, and Rome.",
+ );
+ }
+
+ #[test]
+ fn foods() {
+ assert_suggestion_result(
+ "My favorite foods are pizza, sushi, tacos and burgers.",
+ OxfordComma::default(),
+ "My favorite foods are pizza, sushi, tacos, and burgers.",
+ );
+ }
+
+ #[test]
+ fn allows_clean_music() {
+ assert_lint_count(
+ "I enjoy listening to pop music, rock, hip-hop, electronic dance, and classical music.",
+ OxfordComma::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn allows_clean_nations() {
+ assert_lint_count(
+ "The team consists of players from different countries: France, Germany, Italy, and Spain.",
+ OxfordComma::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn or_writing() {
+ assert_suggestion_result(
+ "Harper can be a lifesaver when writing technical documents, emails or other formal forms of communication.",
+ OxfordComma::default(),
+ "Harper can be a lifesaver when writing technical documents, emails, or other formal forms of communication.",
+ );
+ }
+
+ #[test]
+ fn sports() {
+ assert_suggestion_result(
+ "They enjoy playing soccer, basketball or tennis.",
+ OxfordComma::default(),
+ "They enjoy playing soccer, basketball, or tennis.",
+ );
+ }
+
+ #[test]
+ fn nor_vegetables() {
+ assert_suggestion_result(
+ "I like carrots, kale nor broccoli.",
+ OxfordComma::default(),
+ "I like carrots, kale, nor broccoli.",
+ );
+ }
+
+ #[test]
+ fn allow_non_list_transportation() {
+ assert_lint_count(
+ "In transportation, autonomous vehicles and smart traffic management systems promise to reduce accidents and optimize travel routes.",
+ OxfordComma::default(),
+ 0,
+ );
+ }
+}
diff --git a/harper-core/src/linting/oxford_comma/simple.rs b/harper-core/src/linting/oxford_comma/simple.rs
new file mode 100644
index 000000000..c3980ed24
--- /dev/null
+++ b/harper-core/src/linting/oxford_comma/simple.rs
@@ -0,0 +1,91 @@
+use crate::{
+ Lrc, Token, TokenStringExt,
+ linting::Linter,
+ patterns::{OwnedPatternExt, Pattern, SequencePattern, WordSet},
+};
+
+use super::super::{Lint, LintKind, Suggestion};
+
+pub struct Simple {
+ pattern: Box<dyn Pattern>,
+}
+
+impl Default for Simple {
+ fn default() -> Self {
+ let item = Lrc::new(
+ SequencePattern::default()
+ .then_article()
+ .then_whitespace()
+ .then_noun()
+ .or(Box::new(SequencePattern::default().then_noun())),
+ );
+ let item_chunk = SequencePattern::default()
+ .then(item.clone())
+ .then_comma()
+ .then_whitespace();
+
+ let pattern = SequencePattern::default()
+ .then_one_or_more(item_chunk)
+ .then(item.clone())
+ .then_whitespace()
+ .then(WordSet::new(&["and", "or", "nor"]))
+ .then_whitespace()
+ .then(item.clone());
+
+ Self {
+ pattern: Box::new(pattern),
+ }
+ }
+}
+
+impl Simple {
+ fn match_to_lint(&self, matched_toks: &[Token], _source: &[char]) -> Option<Lint> {
+ let conj_index = matched_toks.last_conjunction_index()?;
+ let offender = matched_toks[conj_index - 2];
+
+ Some(Lint {
+ span: offender.span,
+ lint_kind: LintKind::Style,
+ suggestions: vec![Suggestion::InsertAfter(vec![','])],
+ message: "An Oxford comma is necessary here.".to_owned(),
+ priority: 31,
+ })
+ }
+}
+
+impl Linter for Simple {
+ fn lint(&mut self, document: &crate::Document) -> Vec<crate::linting::Lint> {
+ let mut lints = Vec::new();
+ for sentence in document.iter_sentences() {
+ let mut tok_cursor = 0;
+
+ loop {
+ if tok_cursor >= sentence.len() {
+ break;
+ }
+
+ let match_len = self
+ .pattern
+ .matches(&sentence[tok_cursor..], document.get_source());
+
+ if match_len != 0 {
+ let lint = self.match_to_lint(
+ &sentence[tok_cursor..tok_cursor + match_len],
+ document.get_source(),
+ );
+
+ lints.extend(lint);
+ tok_cursor += match_len;
+ } else {
+ tok_cursor += 1;
+ }
+ }
+ }
+
+ lints
+ }
+
+ fn description(&self) -> &str {
+ "Covers the simple cases."
+ }
+}
diff --git a/harper-core/src/parsers/collapse_identifiers.rs b/harper-core/src/parsers/collapse_identifiers.rs
index 7a793c8e0..c13ee0fa9 100644
--- a/harper-core/src/parsers/collapse_identifiers.rs
+++ b/harper-core/src/parsers/collapse_identifiers.rs
@@ -26,9 +26,9 @@ impl CollapseIdentifiers {
thread_local! {
static WORD_OR_NUMBER: Lrc<SequencePattern> = Lrc::new(SequencePattern::default()
.then_any_word()
- .then_one_or_more(Box::new(SequencePattern::default()
+ .then_one_or_more(SequencePattern::default()
.then_case_separator()
- .then_any_word())));
+ .then_any_word()));
}
impl Parser for CollapseIdentifiers {
diff --git a/harper-core/src/patterns/sequence_pattern.rs b/harper-core/src/patterns/sequence_pattern.rs
index 3e976fee4..654d1e1ec 100644
--- a/harper-core/src/patterns/sequence_pattern.rs
+++ b/harper-core/src/patterns/sequence_pattern.rs
@@ -160,9 +160,9 @@ impl SequencePattern {
self
}
- pub fn then_one_or_more(mut self, pat: Box<dyn Pattern>) -> Self {
+ pub fn then_one_or_more(mut self, pat: impl Pattern + 'static) -> Self {
self.token_patterns
- .push(Box::new(RepeatingPattern::new(pat, 0)));
+ .push(Box::new(RepeatingPattern::new(Box::new(pat), 0)));
self
}