Skip to content

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

feat(core): add new linter to detect `call as <pronoun>` (#2172)

Elijah Pottera6c7b34c0429
diff --git a/harper-core/src/linting/call_them.rs b/harper-core/src/linting/call_them.rs
new file mode 100644
index 000000000..16f85a282
--- /dev/null
+++ b/harper-core/src/linting/call_them.rs
@@ -0,0 +1,335 @@
+use std::{ops::Range, sync::Arc};
+
+use crate::expr::{Expr, ExprMap, SequenceExpr};
+use crate::patterns::{DerivedFrom, WordSet};
+use crate::{Token, TokenStringExt};
+
+use super::{ExprLinter, Lint, LintKind, Suggestion};
+
+pub struct CallThem {
+ expr: Box<dyn Expr>,
+ map: Arc<ExprMap<Range<usize>>>,
+}
+
+impl Default for CallThem {
+ fn default() -> Self {
+ let mut map = ExprMap::default();
+
+ let post_exception = Arc::new(
+ SequenceExpr::default()
+ .t_ws()
+ .then(WordSet::new(&["if", "it"])),
+ );
+
+ map.insert(
+ SequenceExpr::default()
+ .then(DerivedFrom::new_from_str("call"))
+ .t_ws()
+ .then_pronoun()
+ .t_ws()
+ .t_aco("as")
+ .then_unless(post_exception.clone()),
+ 3..5,
+ );
+
+ map.insert(
+ SequenceExpr::default()
+ .then(DerivedFrom::new_from_str("call"))
+ .t_ws()
+ .t_aco("as")
+ .t_ws()
+ .then_pronoun()
+ .then_unless(post_exception.clone()),
+ 1..3,
+ );
+
+ let map = Arc::new(map);
+
+ Self {
+ expr: Box::new(map.clone()),
+ map,
+ }
+ }
+}
+
+impl ExprLinter for CallThem {
+ fn expr(&self) -> &dyn Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
+ let removal_range = self.map.lookup(0, matched_tokens, source)?.clone();
+ let offending_tokens = matched_tokens.get(removal_range)?;
+
+ Some(Lint {
+ span: offending_tokens.span()?,
+ lint_kind: LintKind::Redundancy,
+ suggestions: vec![Suggestion::Remove],
+ message: "`as` is redundant in this context.".to_owned(),
+ ..Default::default()
+ })
+ }
+
+ fn description(&self) -> &'static str {
+ "Addresses the non-idiomatic phrases `call them as`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ #[allow(unused_imports)]
+ use crate::Document;
+ use crate::linting::tests::{assert_no_lints, assert_suggestion_result};
+
+ use super::CallThem;
+
+ #[test]
+ fn prefer_plug_and_receptacle() {
+ assert_suggestion_result(
+ r#"I prefer to call them as Plug (male) and Receptacle (female). Receptacles are seen in laptops, mobile phones etc.."#,
+ CallThem::default(),
+ r#"I prefer to call them Plug (male) and Receptacle (female). Receptacles are seen in laptops, mobile phones etc.."#,
+ );
+ }
+
+ #[test]
+ fn builtins_id() {
+ assert_suggestion_result(
+ r#"I’d categorically ignore *id* as a builtin, and when you do need it in a module, make it super explicit and `import builtins` and call it as `builtins.id`."#,
+ CallThem::default(),
+ r#"I’d categorically ignore *id* as a builtin, and when you do need it in a module, make it super explicit and `import builtins` and call it `builtins.id`."#,
+ );
+ }
+
+ #[test]
+ fn non_modal_dialogue() {
+ assert_suggestion_result(
+ r#"We usually call it as non-modal dialogue e.g. when hit Gmail compose button, a nonmodal dialogue opens."#,
+ CallThem::default(),
+ r#"We usually call it non-modal dialogue e.g. when hit Gmail compose button, a nonmodal dialogue opens."#,
+ );
+ }
+
+ #[test]
+ fn prefer_to_call_them() {
+ assert_suggestion_result(
+ r#"So, how do you typically prefer to call them as?"#,
+ CallThem::default(),
+ r#"So, how do you typically prefer to call them?"#,
+ );
+ }
+
+ #[test]
+ fn called_them_allies() {
+ assert_suggestion_result(
+ r#"Yes as tribes or nomads you called them as allies but you didn’t get their levies as your own."#,
+ CallThem::default(),
+ r#"Yes as tribes or nomads you called them allies but you didn’t get their levies as your own."#,
+ );
+ }
+
+ #[test]
+ fn character_development() {
+ assert_suggestion_result(
+ r#"I call this as character development."#,
+ CallThem::default(),
+ r#"I call this character development."#,
+ );
+ }
+
+ #[test]
+ fn fate_or_time() {
+ assert_suggestion_result(
+ r#"Should I Call It As Fate Or Time"#,
+ CallThem::default(),
+ r#"Should I Call It Fate Or Time"#,
+ );
+ }
+
+ #[test]
+ fn abstract_latte_art() {
+ assert_suggestion_result(
+ r#"Can we just call it as abstract latte art."#,
+ CallThem::default(),
+ r#"Can we just call it abstract latte art."#,
+ );
+ }
+
+ #[test]
+ fn sounding_boards() {
+ assert_suggestion_result(
+ r#"I call them as my ‘sounding boards’"#,
+ CallThem::default(),
+ r#"I call them my ‘sounding boards’"#,
+ );
+ }
+
+ #[test]
+ fn calling_them_disaster() {
+ assert_suggestion_result(
+ r#"I totally disagree with your point listed and calling them as disaster."#,
+ CallThem::default(),
+ r#"I totally disagree with your point listed and calling them disaster."#,
+ );
+ }
+
+ #[test]
+ fn battle_of_boxes() {
+ assert_suggestion_result(
+ r#"Windows Sandbox and VirtualBox or I would like to call this as “Battle of Boxes.”"#,
+ CallThem::default(),
+ r#"Windows Sandbox and VirtualBox or I would like to call this “Battle of Boxes.”"#,
+ );
+ }
+
+ #[test]
+ fn called_her_shinnasan() {
+ assert_suggestion_result(
+ r#"Nice meeting a follower from reddit I called her as Shinna-san, welcome again to Toram!!"#,
+ CallThem::default(),
+ r#"Nice meeting a follower from reddit I called her Shinna-san, welcome again to Toram!!"#,
+ );
+ }
+
+ #[test]
+ fn calling_it_otp() {
+ assert_suggestion_result(
+ r#"Calling it as OTP in this case misleading"#,
+ CallThem::default(),
+ r#"Calling it OTP in this case misleading"#,
+ );
+ }
+
+ #[test]
+ fn call_it_procrastination() {
+ assert_suggestion_result(
+ r#"To summarise it in just one word I would call it as procrastination."#,
+ CallThem::default(),
+ r#"To summarise it in just one word I would call it procrastination."#,
+ );
+ }
+
+ #[test]
+ fn call_her_important() {
+ assert_suggestion_result(
+ r#"Liked the article overall but to call her as important to rap as Jay or Dre is a bold overstatement."#,
+ CallThem::default(),
+ r#"Liked the article overall but to call her important to rap as Jay or Dre is a bold overstatement."#,
+ );
+ }
+
+ #[test]
+ fn call_him_kindles() {
+ assert_suggestion_result(
+ r#"The days when I had my first best friend, I would rather call him as human version of kindle audiobook, who keeps on talking about everything under the umbrella."#,
+ CallThem::default(),
+ r#"The days when I had my first best friend, I would rather call him human version of kindle audiobook, who keeps on talking about everything under the umbrella."#,
+ );
+ }
+
+ #[test]
+ fn call_them_defenders() {
+ assert_suggestion_result(
+ r#"Declaring war challenging land of a vassal should call them as defenders!"#,
+ CallThem::default(),
+ r#"Declaring war challenging land of a vassal should call them defenders!"#,
+ );
+ }
+
+ #[test]
+ fn call_it_magical() {
+ assert_suggestion_result(
+ r#"I would like to call it as magical."#,
+ CallThem::default(),
+ r#"I would like to call it magical."#,
+ );
+ }
+
+ #[test]
+ fn forward_lateral() {
+ assert_suggestion_result(
+ r#"Surprised the refs didn’t call this as a forward lateral."#,
+ CallThem::default(),
+ r#"Surprised the refs didn’t call this a forward lateral."#,
+ );
+ }
+
+ #[test]
+ fn calling_best_friend() {
+ assert_suggestion_result(
+ r#"Meet my buddy! I love calling him as my best friend, because he never failed to bring some cheer in me!"#,
+ CallThem::default(),
+ r#"Meet my buddy! I love calling him my best friend, because he never failed to bring some cheer in me!"#,
+ );
+ }
+
+ #[test]
+ fn calling_everyone_titles() {
+ assert_suggestion_result(
+ r#"Currently, I’m teaching in Asia and the students have the local custom of calling everyone as Mr. Givenname or Miss Givenname"#,
+ CallThem::default(),
+ r#"Currently, I’m teaching in Asia and the students have the local custom of calling everyone Mr. Givenname or Miss Givenname"#,
+ );
+ }
+
+ #[test]
+ fn called_as_he() {
+ assert_suggestion_result(
+ r#"I prefer to be called as he when referred in 3rd person and I’m sure that everyone would be ok to call me as he."#,
+ CallThem::default(),
+ r#"I prefer to be called he when referred in 3rd person and I’m sure that everyone would be ok to call me he."#,
+ );
+ }
+
+ #[test]
+ fn calls_him_bob() {
+ assert_suggestion_result(
+ r#"In Twelve Monkeys, Cole hears someone who calls him as “Bob”"#,
+ CallThem::default(),
+ r#"In Twelve Monkeys, Cole hears someone who calls him “Bob”"#,
+ );
+ }
+
+ #[test]
+ fn pliny_called_it() {
+ assert_suggestion_result(
+ r#"Pliny the Elder called it as lake of Gennesaret or Taricheae in his encyclopedia, Natural History."#,
+ CallThem::default(),
+ r#"Pliny the Elder called it lake of Gennesaret or Taricheae in his encyclopedia, Natural History."#,
+ );
+ }
+
+ #[test]
+ fn students_call_you() {
+ assert_suggestion_result(
+ r#"In the same way your students will call you as ~先生 even after they graduated/move to higher education."#,
+ CallThem::default(),
+ r#"In the same way your students will call you ~先生 even after they graduated/move to higher education."#,
+ );
+ }
+
+ #[test]
+ fn paradoxical_reaction() {
+ assert_suggestion_result(
+ r#"We can call it as Paradoxical Reaction which means a medicine which is used to reduce pain increases the pain when it is"#,
+ CallThem::default(),
+ r#"We can call it Paradoxical Reaction which means a medicine which is used to reduce pain increases the pain when it is"#,
+ );
+ }
+
+ #[test]
+ fn rust_module() {
+ assert_no_lints(
+ "I want to call them as if they were just another Rust module",
+ CallThem::default(),
+ );
+ }
+
+ #[test]
+ fn want_to_do() {
+ assert_no_lints(
+ "however its a design choice to not call it as it does things I don't want to do.",
+ CallThem::default(),
+ );
+ }
+}
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 952bf909a..6cacb687c 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -21,6 +21,7 @@ mod be_allowed;
mod best_of_all_time;
mod boring_words;
mod bought;
+mod call_them;
mod cant;
mod capitalize_personal_pronouns;
mod cautionary_tale;
diff --git a/harper-core/src/patterns/derived_from.rs b/harper-core/src/patterns/derived_from.rs
new file mode 100644
index 000000000..f1d818665
--- /dev/null
+++ b/harper-core/src/patterns/derived_from.rs
@@ -0,0 +1,45 @@
+use crate::spell::WordId;
+
+use super::Pattern;
+
+/// A [Pattern] that looks for Word tokens that are either derived from a given word, or the word
+/// itself.
+///
+/// For example, this will match "call" as well as "recall", "calling", etc.
+pub struct DerivedFrom {
+ word_id: WordId,
+}
+
+impl DerivedFrom {
+ pub fn new_from_str(word: &str) -> DerivedFrom {
+ Self::new(WordId::from_word_str(word))
+ }
+
+ pub fn new_from_chars(word: &[char]) -> DerivedFrom {
+ Self::new(WordId::from_word_chars(word))
+ }
+
+ pub fn new(word_id: WordId) -> Self {
+ Self { word_id }
+ }
+}
+
+impl Pattern for DerivedFrom {
+ fn matches(&self, tokens: &[crate::Token], source: &[char]) -> Option<usize> {
+ let tok = tokens.first()?;
+ let metadata = tok.kind.as_word()?.as_ref()?;
+
+ if metadata.derived_from == Some(self.word_id) {
+ return Some(1);
+ }
+
+ let chars = tok.span.get_content(source);
+ let word_id = WordId::from_word_chars(chars);
+
+ if word_id == self.word_id {
+ return Some(1);
+ }
+
+ None
+ }
+}
diff --git a/harper-core/src/patterns/mod.rs b/harper-core/src/patterns/mod.rs
index d9cf0d7ac..4346c6aba 100644
--- a/harper-core/src/patterns/mod.rs
+++ b/harper-core/src/patterns/mod.rs
@@ -8,6 +8,7 @@
use crate::{Document, LSend, Span, Token};
mod any_pattern;
+mod derived_from;
mod implies_quantity;
mod indefinite_article;
mod inflection_of_be;
@@ -22,6 +23,7 @@ mod word;
mod word_set;
pub use any_pattern::AnyPattern;
+pub use derived_from::DerivedFrom;
pub use implies_quantity::ImpliesQuantity;
pub use indefinite_article::IndefiniteArticle;
pub use inflection_of_be::InflectionOfBe;