Skip to content

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

feat: nor I would→nor would I (#2590)

Andrew Dunbara7aaac66171c
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 590bfbdaa..6af9715e1 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -125,6 +125,7 @@ use super::no_match_for::NoMatchFor;
use super::no_oxford_comma::NoOxfordComma;
use super::nobody::Nobody;
use super::nominal_wants::NominalWants;
+use super::nor_modal_pronoun::NorModalPronoun;
use super::noun_verb_confusion::NounVerbConfusion;
use super::number_suffix_capitalization::NumberSuffixCapitalization;
use super::obsess_preposition::ObsessPreposition;
@@ -517,6 +518,7 @@ impl LintGroup {
insert_struct_rule!(NoOxfordComma, false);
insert_expr_rule!(Nobody, true);
insert_expr_rule!(NominalWants, true);
+ insert_expr_rule!(NorModalPronoun, true);
insert_struct_rule!(NounVerbConfusion, true);
insert_struct_rule!(NumberSuffixCapitalization, true);
insert_expr_rule!(ObsessPreposition, true);
@@ -591,8 +593,8 @@ impl LintGroup {
insert_expr_rule!(VeryUnique, true);
insert_expr_rule!(ViceVersa, true);
insert_expr_rule!(ViciousCircle, true);
- insert_expr_rule!(ViciousCycle, false);
insert_expr_rule!(ViciousCircleOrCycle, false);
+ insert_expr_rule!(ViciousCycle, false);
insert_expr_rule!(WasAloud, true);
insert_expr_rule!(WayTooAdjective, true);
insert_expr_rule!(WellEducated, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 541c1996e..2c0c6cd21 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -132,6 +132,7 @@ mod no_match_for;
mod no_oxford_comma;
mod nobody;
mod nominal_wants;
+mod nor_modal_pronoun;
mod noun_verb_confusion;
mod number_suffix_capitalization;
mod obsess_preposition;
diff --git a/harper-core/src/linting/nor_modal_pronoun.rs b/harper-core/src/linting/nor_modal_pronoun.rs
new file mode 100644
index 000000000..88166ead7
--- /dev/null
+++ b/harper-core/src/linting/nor_modal_pronoun.rs
@@ -0,0 +1,230 @@
+use crate::{
+ Lint, Token, TokenStringExt,
+ expr::{Expr, SequenceExpr},
+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},
+ patterns::ModalVerb,
+};
+
+pub struct NorModalPronoun {
+ expr: Box<dyn Expr>,
+}
+
+impl Default for NorModalPronoun {
+ fn default() -> Self {
+ Self {
+ expr: Box::new(
+ SequenceExpr::aco("nor")
+ .t_ws()
+ .then_subject_pronoun()
+ .t_ws()
+ .then(ModalVerb::with_common_errors()),
+ ),
+ }
+ }
+}
+
+impl ExprLinter for NorModalPronoun {
+ type Unit = Chunk;
+
+ fn description(&self) -> &str {
+ "Corrects the order of the pronoun and modal verb after `nor`."
+ }
+
+ fn expr(&self) -> &dyn Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint_with_context(
+ &self,
+ toks: &[Token],
+ src: &[char],
+ ctx: Option<(&[Token], &[Token])>,
+ ) -> Option<Lint> {
+ if ctx
+ .map(|(pre, _)| {
+ // Check for pattern 1: subject pronoun [ws]
+ let is_subj_pronoun = pre
+ .get_rel(-1)
+ .filter(|t| t.kind.is_whitespace())
+ .and_then(|_| pre.get_rel(-2))
+ .is_some_and(|t| t.kind.is_subject_pronoun());
+
+ // Check for pattern 2: possessive [ws] noun [ws]
+ let is_poss_and_noun = pre
+ .get_rel(-1)
+ .filter(|t| t.kind.is_whitespace())
+ .and_then(|_| pre.get_rel(-2))
+ .filter(|t| t.kind.is_noun())
+ .and_then(|_| pre.get_rel(-3))
+ .filter(|t| t.kind.is_whitespace())
+ .and_then(|_| pre.get_rel(-4))
+ .is_some_and(|t| t.kind.is_possessive_determiner());
+
+ is_subj_pronoun || is_poss_and_noun
+ })
+ .unwrap_or(false)
+ {
+ return None;
+ }
+
+ let (pron_tok, modal_tok) = (toks.get_rel(-3)?, toks.get_rel(-1)?);
+ let pron_ws_modal_toks = toks.get_rel_slice(-3, -1)?;
+ let (pron_span, modal_span) = (pron_tok.span, modal_tok.span);
+ let pron_modal_span = pron_ws_modal_toks.span()?;
+
+ let value = format!(
+ "{} {}",
+ modal_span.get_content_string(src),
+ pron_span.get_content_string(src)
+ )
+ .chars()
+ .collect();
+
+ // Avoid capitalizing the modals verbs just because the pronoun was "I"
+ let suggestion = if pron_span.get_content(src) == ['I'] {
+ Suggestion::ReplaceWith(value)
+ } else {
+ Suggestion::replace_with_match_case(value, pron_modal_span.get_content(src))
+ };
+
+ Some(Lint {
+ span: pron_modal_span,
+ lint_kind: LintKind::Grammar,
+ suggestions: vec![suggestion],
+ message: "After `nor`, the modal verb should come before the pronoun.".to_string(),
+ ..Default::default()
+ })
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::NorModalPronoun;
+ use crate::linting::tests::{assert_no_lints, assert_suggestion_result};
+
+ #[test]
+ fn fix_nor_i_can() {
+ assert_suggestion_result(
+ "i can't see the menu nor i can see the features of your app or how it looks !",
+ NorModalPronoun::default(),
+ "i can't see the menu nor can i see the features of your app or how it looks !",
+ );
+ }
+
+ #[test]
+ fn fix_nor_i_could() {
+ assert_suggestion_result(
+ "but never saw any warnings nor I could read messages until I debugged",
+ NorModalPronoun::default(),
+ "but never saw any warnings nor could I read messages until I debugged",
+ );
+ }
+
+ #[test]
+ fn fix_nor_i_will() {
+ assert_suggestion_result(
+ "I am not the author of the plugins nor I will be updating bugged/unavailable plugins.",
+ NorModalPronoun::default(),
+ "I am not the author of the plugins nor will I be updating bugged/unavailable plugins.",
+ );
+ }
+
+ #[test]
+ fn fix_nor_i_would() {
+ assert_suggestion_result(
+ "I would not like to own a Pollock, nor I would hang one of his paintings on a wall inside my home",
+ NorModalPronoun::default(),
+ "I would not like to own a Pollock, nor would I hang one of his paintings on a wall inside my home",
+ );
+ }
+
+ #[test]
+ fn fix_nor_it_can() {
+ assert_suggestion_result(
+ "However, since several days ago FreeTube simply doesn't open ANY videos nor it can search.",
+ NorModalPronoun::default(),
+ "However, since several days ago FreeTube simply doesn't open ANY videos nor can it search.",
+ );
+ }
+
+ #[test]
+ fn fix_nor_it_should() {
+ assert_suggestion_result(
+ "Since the code doesn't guard against it (nor it should), internalModule.stripBOM is called with an undefined",
+ NorModalPronoun::default(),
+ "Since the code doesn't guard against it (nor should it), internalModule.stripBOM is called with an undefined",
+ );
+ }
+
+ #[test]
+ fn fix_nor_it_will() {
+ assert_suggestion_result(
+ "It will never \"create a table\", nor it will issue any query - it will only create the entity instance.",
+ NorModalPronoun::default(),
+ "It will never \"create a table\", nor will it issue any query - it will only create the entity instance.",
+ );
+ }
+
+ #[test]
+ fn fix_nor_it_would() {
+ assert_suggestion_result(
+ "Neither whitespace (excepting NL and CR) is special char in this sense, nor it would destroy something, if it gets \"escaped\" as variable",
+ NorModalPronoun::default(),
+ "Neither whitespace (excepting NL and CR) is special char in this sense, nor would it destroy something, if it gets \"escaped\" as variable",
+ );
+ }
+
+ #[test]
+ fn fix_nor_they_can() {
+ assert_suggestion_result(
+ "Currently these assets don't include the code provided by the submodules nor they can be disabled",
+ NorModalPronoun::default(),
+ "Currently these assets don't include the code provided by the submodules nor can they be disabled",
+ );
+ }
+
+ #[test]
+ fn fix_nor_we_can() {
+ assert_suggestion_result(
+ "The NSLayoutConstraint errors are really Apple bugs, not our fault, nor we can fix them, but they are harmless.",
+ NorModalPronoun::default(),
+ "The NSLayoutConstraint errors are really Apple bugs, not our fault, nor can we fix them, but they are harmless.",
+ );
+ }
+
+ #[test]
+ fn fix_nor_you_can() {
+ assert_suggestion_result(
+ "You cannot create a view to do it, nor you can have a function to do it",
+ NorModalPronoun::default(),
+ "You cannot create a view to do it, nor can you have a function to do it",
+ );
+ }
+
+ #[test]
+ fn fix_nor_you_should() {
+ assert_suggestion_result(
+ "I believe you cannot create two sessions through one signin, and maybe nor you should.",
+ NorModalPronoun::default(),
+ "I believe you cannot create two sessions through one signin, and maybe nor should you.",
+ );
+ }
+
+ // Potential false positives
+
+ #[test]
+ fn ignore_neither_they_nor_i_could() {
+ assert_no_lints(
+ "One of my users was unable to install tools via mise, but neither they nor I could initially figure out why.",
+ NorModalPronoun::default(),
+ );
+ }
+
+ #[test]
+ fn ignore_neither_my_tool_nor_i_shall() {
+ assert_no_lints(
+ "but neither my tool nor I shall feel disrespected",
+ NorModalPronoun::default(),
+ );
+ }
+}
diff --git a/harper-core/src/token_kind.rs b/harper-core/src/token_kind.rs
index ee7d5046c..73c12a309 100644
--- a/harper-core/src/token_kind.rs
+++ b/harper-core/src/token_kind.rs
@@ -90,6 +90,7 @@ impl TokenKind {
is_subject_pronoun,
is_object_pronoun,
is_possessive_noun,
+ // Note: possessive pronouns are: mine, ours, yours, his, hers, its, theirs
is_possessive_pronoun,
// Verb methods
diff --git a/harper-core/src/token_string_ext.rs b/harper-core/src/token_string_ext.rs
index b54df7a1f..c42cf523e 100644
--- a/harper-core/src/token_string_ext.rs
+++ b/harper-core/src/token_string_ext.rs
@@ -125,6 +125,49 @@ pub trait TokenStringExt: private::Sealed {
slice.get(idx)
}
+ /// Get a slice of tokens using relative indices.
+ ///
+ /// # Examples
+ /// ```
+ /// # use harper_core::{Token, TokenStringExt, parsers::{Parser, PlainEnglish}};
+ /// # fn main() {
+ /// let source = "The cat sat on the mat.".chars().collect::<Vec<_>>();
+ /// let tokens = PlainEnglish.parse(&source);
+ /// assert_eq!(tokens.get_rel_slice(0, 2).unwrap().span().unwrap().get_content_string(&source), "The cat");
+ /// assert_eq!(tokens.get_rel_slice(-3, -1).unwrap().span().unwrap().get_content_string(&source), " mat.");
+ /// # }
+ /// ```
+ fn get_rel_slice(&self, rel_start: isize, inclusive_end: isize) -> Option<&[Token]>
+ where
+ Self: AsRef<[Token]>,
+ {
+ let slice = self.as_ref();
+ let len = slice.len() as isize;
+
+ // Convert relative indices to absolute indices
+ let start_idx = if rel_start >= 0 {
+ rel_start
+ } else {
+ len + rel_start
+ } as usize;
+
+ let end_idx_plus_one = if inclusive_end >= 0 {
+ inclusive_end + 1 // +1 to make end exclusive
+ } else {
+ len + inclusive_end + 1
+ } as usize;
+
+ // Check bounds
+ if start_idx >= slice.len()
+ || end_idx_plus_one > slice.len()
+ || start_idx >= end_idx_plus_one
+ {
+ return None;
+ }
+
+ Some(&slice[start_idx..end_idx_plus_one])
+ }
+
fn iter_linking_verb_indices(&self) -> impl Iterator<Item = usize> + '_;
fn iter_linking_verbs(&self) -> impl Iterator<Item = &Token> + '_;