Skip to content

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

refactor(core): delete macro calls and make `Likewise` stricter

Elijah Potter4ff1245cc6fd
diff --git a/harper-core/src/linting/closed_compounds.rs b/harper-core/src/linting/closed_compounds.rs
index 507f8a8d7..fd7e652f5 100644
--- a/harper-core/src/linting/closed_compounds.rs
+++ b/harper-core/src/linting/closed_compounds.rs
@@ -54,7 +54,6 @@ macro_rules! create_closed_compound_linter {
}
create_closed_compound_linter!(Itself, "it self", "itself");
-create_closed_compound_linter!(Tonight, "to night", "tonight");
create_closed_compound_linter!(Myself, "my self", "myself");
create_closed_compound_linter!(Therefore, "there fore", "therefore");
create_closed_compound_linter!(Misunderstand, "miss understand", "misunderstand");
@@ -64,10 +63,7 @@ create_closed_compound_linter!(Misused, "miss used", "misused");
create_closed_compound_linter!(Postpone, "post pone", "postpone");
create_closed_compound_linter!(Worldwide, "world wide", "worldwide");
create_closed_compound_linter!(Overall, "over all", "overall");
-create_closed_compound_linter!(Likewise, "like wise", "likewise");
create_closed_compound_linter!(However, "how ever", "however");
-create_closed_compound_linter!(Altogether, "all together", "altogether");
-create_closed_compound_linter!(Asleep, "a sleep", "asleep");
create_closed_compound_linter!(Upset, "up set", "upset");
create_closed_compound_linter!(Intact, "in tact", "intact");
create_closed_compound_linter!(Somehow, "some how", "somehow");
@@ -89,22 +85,15 @@ create_closed_compound_linter!(Multithreading, "multi threading", "multithreadin
create_closed_compound_linter!(Everywhere, "every where", "everywhere");
create_closed_compound_linter!(Multicore, "multi core", "multicore");
create_closed_compound_linter!(Multimedia, "multi media", "multimedia");
-create_closed_compound_linter!(Ecommerce, "e commerce", "ecommerce");
create_closed_compound_linter!(Widespread, "wide spread", "widespread");
create_closed_compound_linter!(Notwithstanding, "not with standing", "notwithstanding");
create_closed_compound_linter!(Anyhow, "any how", "anyhow");
create_closed_compound_linter!(Nonetheless, "none the less", "nonetheless");
create_closed_compound_linter!(Hereafter, "here after", "hereafter");
-create_closed_compound_linter!(Otherwise, "other wise", "otherwise");
-create_closed_compound_linter!(Therein, "there in", "therein");
create_closed_compound_linter!(Thereupon, "there upon", "thereupon");
-create_closed_compound_linter!(Hereby, "here by", "hereby");
-create_closed_compound_linter!(Hereunder, "here under", "hereunder");
create_closed_compound_linter!(Forthwith, "forth with", "forthwith");
create_closed_compound_linter!(Insofar, "in so far", "insofar");
create_closed_compound_linter!(Whereupon, "where upon", "whereupon");
-create_closed_compound_linter!(Thereafter, "there after", "thereafter");
-create_closed_compound_linter!(Downright, "down right", "downright");
create_closed_compound_linter!(Upward, "up ward", "upward");
create_closed_compound_linter!(Henceforth, "hence forth", "henceforth");
create_closed_compound_linter!(Regardless, "regard less", "regardless");
@@ -112,13 +101,11 @@ create_closed_compound_linter!(Regardless, "regard less", "regardless");
#[cfg(test)]
mod tests {
use super::{
- Altogether, Asleep, However, Itself, Likewise, Misunderstood, Misuse, Misused, Myself,
- Overall, Therefore, Tonight, Worldwide,
+ Anyhow, Forthwith, Henceforth, Hereafter, Insofar, Nonetheless, Notwithstanding,
+ Regardless, Thereupon, Upward, Whereupon, Widespread,
};
use super::{
- Anyhow, Downright, Forthwith, Henceforth, Hereafter, Hereby, Hereunder, Insofar,
- Nonetheless, Notwithstanding, Otherwise, Regardless, Thereafter, Therein, Thereupon,
- Upward, Whereupon, Widespread,
+ However, Itself, Misunderstood, Misuse, Misused, Myself, Overall, Therefore, Worldwide,
};
use crate::linting::tests::assert_suggestion_result;
@@ -129,13 +116,6 @@ mod tests {
assert_suggestion_result(test_sentence, Itself::default(), expected);
}
- #[test]
- fn to_night() {
- let test_sentence = "She spent the night to night.";
- let expected = "She spent the night tonight.";
- assert_suggestion_result(test_sentence, Tonight::default(), expected);
- }
-
#[test]
fn my_self() {
let test_sentence = "He treated my self with respect.";
@@ -185,13 +165,6 @@ mod tests {
assert_suggestion_result(test_sentence, Overall::default(), expected);
}
- #[test]
- fn like_wise() {
- let test_sentence = "He acted, like wise, without hesitation.";
- let expected = "He acted, likewise, without hesitation.";
- assert_suggestion_result(test_sentence, Likewise::default(), expected);
- }
-
#[test]
fn how_ever() {
let test_sentence = "This is true, how ever, details matter.";
@@ -199,20 +172,6 @@ mod tests {
assert_suggestion_result(test_sentence, However::default(), expected);
}
- #[test]
- fn all_together() {
- let test_sentence = "They did it all together.";
- let expected = "They did it altogether.";
- assert_suggestion_result(test_sentence, Altogether::default(), expected);
- }
-
- #[test]
- fn a_sleep() {
- let test_sentence = "She fell a sleep.";
- let expected = "She fell asleep.";
- assert_suggestion_result(test_sentence, Asleep::default(), expected);
- }
-
#[test]
fn wide_spread() {
let test_sentence = "The news was wide spread throughout the region.";
@@ -248,20 +207,6 @@ mod tests {
assert_suggestion_result(test_sentence, Hereafter::default(), expected);
}
- #[test]
- fn other_wise() {
- let test_sentence = "Review the guidelines, other wise you might miss an important detail.";
- let expected = "Review the guidelines, otherwise you might miss an important detail.";
- assert_suggestion_result(test_sentence, Otherwise::default(), expected);
- }
-
- #[test]
- fn there_in() {
- let test_sentence = "All the answers can be found there in the document.";
- let expected = "All the answers can be found therein the document.";
- assert_suggestion_result(test_sentence, Therein::default(), expected);
- }
-
#[test]
fn there_upon() {
let test_sentence = "A decision was made there upon reviewing the data.";
@@ -269,20 +214,6 @@ mod tests {
assert_suggestion_result(test_sentence, Thereupon::default(), expected);
}
- #[test]
- fn here_by() {
- let test_sentence = "The contract is here by declared null and void.";
- let expected = "The contract is hereby declared null and void.";
- assert_suggestion_result(test_sentence, Hereby::default(), expected);
- }
-
- #[test]
- fn here_under() {
- let test_sentence = "All terms are set forth here under.";
- let expected = "All terms are set forth hereunder.";
- assert_suggestion_result(test_sentence, Hereunder::default(), expected);
- }
-
#[test]
fn forth_with() {
let test_sentence = "Please reply forth with to our previous inquiry.";
@@ -304,20 +235,6 @@ mod tests {
assert_suggestion_result(test_sentence, Whereupon::default(), expected);
}
- #[test]
- fn there_after() {
- let test_sentence = "The system shutdown occurred there after the update.";
- let expected = "The system shutdown occurred thereafter the update.";
- assert_suggestion_result(test_sentence, Thereafter::default(), expected);
- }
-
- #[test]
- fn down_right() {
- let test_sentence = "His comment was down right insulting to everyone present.";
- let expected = "His comment was downright insulting to everyone present.";
- assert_suggestion_result(test_sentence, Downright::default(), expected);
- }
-
#[test]
fn up_ward() {
let test_sentence = "The temperature moved up ward during the afternoon.";
diff --git a/harper-core/src/linting/likewise.rs b/harper-core/src/linting/likewise.rs
new file mode 100644
index 000000000..aa4225fee
--- /dev/null
+++ b/harper-core/src/linting/likewise.rs
@@ -0,0 +1,79 @@
+use crate::{
+ patterns::{All, Invert, Pattern, SequencePattern},
+ Token, TokenStringExt,
+};
+
+use super::{Lint, LintKind, PatternLinter, Suggestion};
+
+pub struct Likewise {
+ pattern: Box<dyn Pattern>,
+}
+impl Default for Likewise {
+ fn default() -> Self {
+ let mut pattern = All::default();
+
+ pattern.add(Box::new(
+ SequencePattern::aco("like").then_whitespace().t_aco("wise"),
+ ));
+
+ pattern.add(Box::new(Invert::new(Box::new(
+ SequencePattern::default()
+ .then_anything()
+ .then_whitespace()
+ .then_anything()
+ .then_whitespace()
+ .then_noun(),
+ ))));
+
+ Self {
+ pattern: Box::new(pattern),
+ }
+ }
+}
+impl PatternLinter for Likewise {
+ 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(
+ "likewise".chars().collect(),
+ orig_chars,
+ )],
+ message: format!("Did you mean the closed compound `{}`?", "likewise"),
+ ..Default::default()
+ }
+ }
+ fn description(&self) -> &'static str {
+ "Looks for incorrect spacing inside the closed compound `likewise`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::tests::assert_suggestion_result;
+
+ use super::Likewise;
+
+ #[test]
+ fn wise_men() {
+ assert_suggestion_result(
+ "Like wise men, we waited.",
+ Likewise::default(),
+ "Like wise men, we waited.",
+ );
+ }
+
+ #[test]
+ fn like_wise() {
+ assert_suggestion_result(
+ "He acted, like wise, without hesitation.",
+ Likewise::default(),
+ "He acted, likewise, without hesitation.",
+ );
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 2a15a6f4a..e7e8c0462 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -18,6 +18,7 @@ use super::despite_of::DespiteOf;
use super::dot_initialisms::DotInitialisms;
use super::ellipsis_length::EllipsisLength;
use super::lets_confusion::LetsConfusion;
+use super::likewise::Likewise;
use super::linking_verbs::LinkingVerbs;
use super::long_sentences::LongSentences;
use super::matcher::Matcher;
@@ -170,6 +171,7 @@ macro_rules! create_lint_group_config {
}
create_lint_group_config!(
+ Likewise => true,
CompoundNouns => true,
Regardless => true,
Henceforth => true,
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 323e08cc0..131e6d09e 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -15,6 +15,7 @@ mod despite_of;
mod dot_initialisms;
mod ellipsis_length;
mod lets_confusion;
+mod likewise;
mod linking_verbs;
mod lint;
mod lint_group;
@@ -65,6 +66,7 @@ pub use despite_of::DespiteOf;
pub use dot_initialisms::DotInitialisms;
pub use ellipsis_length::EllipsisLength;
pub use lets_confusion::LetsConfusion;
+pub use likewise::Likewise;
pub use linking_verbs::LinkingVerbs;
pub use lint::Lint;
pub use lint_group::{LintGroup, LintGroupConfig};
diff --git a/harper-core/src/patterns/either_pattern.rs b/harper-core/src/patterns/either_pattern.rs
index c0ed7e1a7..984751b8d 100644
--- a/harper-core/src/patterns/either_pattern.rs
+++ b/harper-core/src/patterns/either_pattern.rs
@@ -12,6 +12,10 @@ impl EitherPattern {
pub fn new(patterns: Vec<Box<dyn Pattern>>) -> Self {
Self { patterns }
}
+
+ pub fn add(&mut self, pattern: Box<dyn Pattern>) {
+ self.patterns.push(pattern);
+ }
}
impl Pattern for EitherPattern {
diff --git a/harper-core/src/patterns/sequence_pattern.rs b/harper-core/src/patterns/sequence_pattern.rs
index 4b8aff3f1..39aa4a98f 100644
--- a/harper-core/src/patterns/sequence_pattern.rs
+++ b/harper-core/src/patterns/sequence_pattern.rs
@@ -2,7 +2,7 @@ use hashbrown::HashSet;
use paste::paste;
use super::whitespace_pattern::WhitespacePattern;
-use super::{AnyCapitalization, NounPhrase, Pattern, RepeatingPattern, WordSet};
+use super::{AnyCapitalization, AnyPattern, NounPhrase, Pattern, RepeatingPattern, WordSet};
use crate::Lrc;
use crate::{CharStringExt, Token, TokenKind};
@@ -208,6 +208,11 @@ impl SequencePattern {
self
}
+ pub fn then_anything(mut self) -> Self {
+ self.token_patterns.push(Box::new(AnyPattern));
+ self
+ }
+
pub fn then(mut self, pat: Box<dyn Pattern>) -> Self {
self.token_patterns.push(pat);
self