Skip to content

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

fix(core): problems with `EffectAffect` linter (#2171)

Elijah Potter1e1bbfdebac4
diff --git a/harper-core/src/linting/noun_verb_confusion/effect_affect/affect_to_effect.rs b/harper-core/src/linting/noun_verb_confusion/effect_affect/affect_to_effect.rs
index 73e959921..914b9933d 100644
--- a/harper-core/src/linting/noun_verb_confusion/effect_affect/affect_to_effect.rs
+++ b/harper-core/src/linting/noun_verb_confusion/effect_affect/affect_to_effect.rs
@@ -85,6 +85,18 @@ impl ExprLinter for AffectToEffect {
let offending_index = *self.map.lookup(0, matched_tokens, source)?;
let target = &matched_tokens[offending_index];
+ let preceding = matched_tokens[..offending_index]
+ .iter()
+ .rfind(|tok| !tok.kind.is_whitespace());
+
+ if preceding.is_some_and(|tok| {
+ (tok.kind.is_pronoun() || tok.kind.is_upos(UPOS::PRON))
+ && !tok.kind.is_possessive_pronoun()
+ }) {
+ // Pronouns like "it" or "they" almost always introduce the verb form ("it affects").
+ return None;
+ }
+
let token_text = target.span.get_content_string(source);
let lower = token_text.to_lowercase();
let replacement = match lower.as_str() {
diff --git a/harper-core/src/linting/noun_verb_confusion/effect_affect/effect_to_affect.rs b/harper-core/src/linting/noun_verb_confusion/effect_affect/effect_to_affect.rs
index b122ae7ce..0ef120f8b 100644
--- a/harper-core/src/linting/noun_verb_confusion/effect_affect/effect_to_affect.rs
+++ b/harper-core/src/linting/noun_verb_confusion/effect_affect/effect_to_affect.rs
@@ -125,6 +125,12 @@ impl ExprLinter for EffectToAffect {
let token_text = target.span.get_content_string(source);
let lower = token_text.to_lowercase();
+ if lower.as_str() == "effects" && preceding.is_some_and(|tok| tok.kind.is_upos(UPOS::VERB))
+ {
+ // Imperative phrases like "Avoid effects" legitimately use the noun.
+ return None;
+ }
+
let replacement = match lower.as_str() {
"effect" => "affect",
"effects" => "affects",
diff --git a/harper-core/src/linting/noun_verb_confusion/mod.rs b/harper-core/src/linting/noun_verb_confusion/mod.rs
index b8a592e6a..199711e0a 100644
--- a/harper-core/src/linting/noun_verb_confusion/mod.rs
+++ b/harper-core/src/linting/noun_verb_confusion/mod.rs
@@ -30,7 +30,7 @@ merge_linters! {
#[cfg(test)]
mod tests {
use super::NounVerbConfusion;
- use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+ use crate::linting::tests::{assert_lint_count, assert_no_lints, assert_suggestion_result};
#[test]
fn corrects_good_advise() {
@@ -1337,4 +1337,20 @@ mod tests {
0,
);
}
+
+ #[test]
+ fn issue_1997() {
+ assert_no_lints(
+ "It depends on which sources it affects, what parameters it uses, etc.",
+ NounVerbConfusion::default(),
+ );
+ }
+
+ #[test]
+ fn issue_1996() {
+ assert_no_lints(
+ "Avoid effects outside of functions.",
+ NounVerbConfusion::default(),
+ );
+ }
}