Skip to content

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

fix(core): address problems with prepositional phrases (#2022)

Elijah Pottera6174e8441c9
diff --git a/harper-core/src/linting/to_two_too/mod.rs b/harper-core/src/linting/to_two_too/mod.rs
index 1763a6b15..56cb73747 100644
--- a/harper-core/src/linting/to_two_too/mod.rs
+++ b/harper-core/src/linting/to_two_too/mod.rs
@@ -409,4 +409,30 @@ mod tests {
ToTwoToo::default(),
);
}
+
+ #[test]
+ fn no_lint_all_the_way_to_advanced_usage() {
+ assert_no_lints(
+ "All the way to advanced usage, like an expert.",
+ ToTwoToo::default(),
+ );
+ }
+
+ #[test]
+ fn no_lint_access_to_over_400_integrations() {
+ assert_no_lints(
+ "You'll have access to over 400 integrations.",
+ ToTwoToo::default(),
+ );
+ }
+
+ #[test]
+ fn no_lint_accustomed_to_precision() {
+ assert_no_lints("I’m rather accustomed to precision.", ToTwoToo::default());
+ }
+
+ #[test]
+ fn no_lint_prone_to_melancholy() {
+ assert_no_lints("He wasn’t a man prone to melancholy.", ToTwoToo::default());
+ }
}
diff --git a/harper-core/src/linting/to_two_too/to_too_adjective_end.rs b/harper-core/src/linting/to_two_too/to_too_adjective_end.rs
index 327f78135..590ffefab 100644
--- a/harper-core/src/linting/to_two_too/to_too_adjective_end.rs
+++ b/harper-core/src/linting/to_two_too/to_too_adjective_end.rs
@@ -2,7 +2,7 @@ use crate::{
Token, TokenKind,
char_string::CharStringExt,
expr::{Expr, SequenceExpr},
- patterns::WhitespacePattern,
+ patterns::{SingleTokenPattern, WhitespacePattern, prepositional_preceder},
};
use super::{ExprLinter, Lint, LintKind, Suggestion};
@@ -14,6 +14,11 @@ pub struct ToTooAdjectiveEnd {
impl Default for ToTooAdjectiveEnd {
fn default() -> Self {
let expr = SequenceExpr::default()
+ .then_optional(
+ SequenceExpr::default()
+ .then_any_word()
+ .then(WhitespacePattern),
+ )
.t_aco("to")
.t_ws()
.then_kind_is_but_is_not_except(
@@ -53,6 +58,16 @@ impl ExprLinter for ToTooAdjectiveEnd {
if idx >= tokens.len() || !tokens[idx].kind.is_adjective() {
return None;
}
+ let prev_non_ws = tokens[..to_index].iter().rfind(|t| !t.kind.is_whitespace());
+ if tokens[idx].kind.is_preposition() {
+ return None;
+ }
+
+ if let Some(prev_token) = prev_non_ws
+ && prepositional_preceder().matches_token(prev_token, source)
+ {
+ return None;
+ }
// Find the next non-whitespace after the adjective
let mut j = idx + 1;
diff --git a/harper-core/src/linting/to_two_too/to_too_adjective_punct.rs b/harper-core/src/linting/to_two_too/to_too_adjective_punct.rs
index 5f81806de..4a1c626f5 100644
--- a/harper-core/src/linting/to_two_too/to_too_adjective_punct.rs
+++ b/harper-core/src/linting/to_two_too/to_too_adjective_punct.rs
@@ -1,8 +1,8 @@
-use crate::patterns::WhitespacePattern;
use crate::{
Token, TokenKind,
char_string::CharStringExt,
expr::{Expr, SequenceExpr},
+ patterns::{SingleTokenPattern, WhitespacePattern, prepositional_preceder},
};
use super::{ExprLinter, Lint, LintKind, Suggestion};
@@ -14,6 +14,11 @@ pub struct ToTooAdjectivePunct {
impl Default for ToTooAdjectivePunct {
fn default() -> Self {
let expr = SequenceExpr::default()
+ .then_optional(
+ SequenceExpr::default()
+ .then_any_word()
+ .then(WhitespacePattern),
+ )
.t_aco("to")
.t_ws()
.then_kind_is_but_is_not_except(
@@ -36,12 +41,36 @@ impl ExprLinter for ToTooAdjectivePunct {
}
fn match_to_lint(&self, tokens: &[Token], source: &[char]) -> Option<Lint> {
- let to_tok = tokens.iter().find(|t| {
+ let to_index = tokens.iter().position(|t| {
t.span
.get_content(source)
.eq_ignore_ascii_case_chars(&['t', 'o'])
})?;
+ let mut idx = to_index + 1;
+ while idx < tokens.len() && tokens[idx].kind.is_whitespace() {
+ idx += 1;
+ }
+ if idx >= tokens.len() {
+ return None;
+ }
+ let adjective = &tokens[idx];
+ if !adjective.kind.is_adjective() {
+ return None;
+ }
+ if adjective.kind.is_preposition() {
+ return None;
+ }
+
+ let prev_non_ws = tokens[..to_index].iter().rfind(|t| !t.kind.is_whitespace());
+ if let Some(prev_token) = prev_non_ws
+ && prepositional_preceder().matches_token(prev_token, source)
+ {
+ return None;
+ }
+
+ let to_tok = &tokens[to_index];
+
Some(Lint {
span: to_tok.span,
lint_kind: LintKind::WordChoice,
diff --git a/harper-core/src/patterns/mod.rs b/harper-core/src/patterns/mod.rs
index 851488851..d9cf0d7ac 100644
--- a/harper-core/src/patterns/mod.rs
+++ b/harper-core/src/patterns/mod.rs
@@ -14,6 +14,7 @@ mod inflection_of_be;
mod invert;
mod modal_verb;
mod nominal_phrase;
+mod prepositional_preceder;
mod upos_set;
mod whitespace_pattern;
mod within_edit_distance;
@@ -27,6 +28,7 @@ pub use inflection_of_be::InflectionOfBe;
pub use invert::Invert;
pub use modal_verb::ModalVerb;
pub use nominal_phrase::NominalPhrase;
+pub use prepositional_preceder::{PrepositionalPrecederPattern, prepositional_preceder};
pub use upos_set::UPOSSet;
pub use whitespace_pattern::WhitespacePattern;
pub use within_edit_distance::WithinEditDistance;
diff --git a/harper-core/src/patterns/prepositional_preceder.rs b/harper-core/src/patterns/prepositional_preceder.rs
new file mode 100644
index 000000000..7c0a9aca1
--- /dev/null
+++ b/harper-core/src/patterns/prepositional_preceder.rs
@@ -0,0 +1,61 @@
+use lazy_static::lazy_static;
+
+use super::{SingleTokenPattern, WordSet};
+use crate::Token;
+
+/// Matches adjectives that routinely introduce a `… to …` prepositional phrase, such as
+/// `accustomed`, `prone`, or `used`.
+///
+/// Several `ToTwoToo` branches need this guard so they only flag cases where `to` is meant as
+/// `too`, not when it participates in idioms like `accustomed to precision`.
+#[derive(Debug, Clone)]
+pub struct PrepositionalPrecederPattern {
+ word_set: WordSet,
+}
+
+impl Default for PrepositionalPrecederPattern {
+ fn default() -> Self {
+ Self {
+ word_set: WordSet::new(&[
+ "accustomed",
+ "addicted",
+ "adjacent",
+ "allergic",
+ "attached",
+ "attuned",
+ "committed",
+ "connected",
+ "dedicated",
+ "devoted",
+ "immune",
+ "oblivious",
+ "opposed",
+ "partial",
+ "prone",
+ "receptive",
+ "related",
+ "resistant",
+ "sensitive",
+ "subject",
+ "susceptible",
+ "used",
+ ]),
+ }
+ }
+}
+
+impl SingleTokenPattern for PrepositionalPrecederPattern {
+ fn matches_token(&self, token: &Token, source: &[char]) -> bool {
+ self.word_set.matches_token(token, source)
+ }
+}
+
+lazy_static! {
+ static ref PREPOSITIONAL_PRECEDER_PATTERN: PrepositionalPrecederPattern =
+ PrepositionalPrecederPattern::default();
+}
+
+/// Shared accessor for the lazily-initialized [`PrepositionalPrecederPattern`].
+pub fn prepositional_preceder() -> &'static PrepositionalPrecederPattern {
+ &PREPOSITIONAL_PRECEDER_PATTERN
+}