Skip to content

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

fix: false positive from #1492 and new one with proper noun (#1846)

Andrew Dunbar71be7f61619b
diff --git a/harper-core/src/expr/sequence_expr.rs b/harper-core/src/expr/sequence_expr.rs
index c38b5c6ce..85add585b 100644
--- a/harper-core/src/expr/sequence_expr.rs
+++ b/harper-core/src/expr/sequence_expr.rs
@@ -231,6 +231,8 @@ impl SequenceExpr {
})
}
+ // Predicate matching methods
+
/// Match a token of a given kind which is not in the list of words.
pub fn then_kind_except<F>(self, pred: F, words: &'static [&'static str]) -> Self
where
@@ -270,6 +272,23 @@ impl SequenceExpr {
self.then(move |tok: &Token, _source: &[char]| preds.iter().any(|pred| pred(&tok.kind)))
}
+ pub fn then_kind_any_or_words<F>(
+ self,
+ preds: &'static [F],
+ words: &'static [&'static str],
+ ) -> Self
+ where
+ F: Fn(&TokenKind) -> bool + Send + Sync + 'static,
+ {
+ self.then(move |tok: &Token, src: &[char]| {
+ preds.iter().any(|pred| pred(&tok.kind))
+ // && !words
+ || words
+ .iter()
+ .any(|&word| tok.span.get_content(src).eq_ignore_ascii_case_str(word))
+ })
+ }
+
/// Adds a step matching a token where the first token kind predicate returns true and the second returns false.
pub fn then_kind_is_but_is_not<F1, F2>(self, pred1: F1, pred2: F2) -> Self
where
diff --git a/harper-core/src/linting/how_to.rs b/harper-core/src/linting/how_to.rs
index 5304dbf59..5ec6285b3 100644
--- a/harper-core/src/linting/how_to.rs
+++ b/harper-core/src/linting/how_to.rs
@@ -1,14 +1,10 @@
use harper_brill::UPOS;
-use crate::expr::All;
-use crate::expr::Expr;
-use crate::expr::OwnedExprExt;
-use crate::expr::SequenceExpr;
-use crate::patterns::UPOSSet;
use crate::{
- Token, TokenStringExt,
+ CharStringExt, Token, TokenKind, TokenStringExt,
+ expr::{All, Expr, OwnedExprExt, SequenceExpr},
linting::{ExprLinter, Lint, LintKind, Suggestion},
- patterns::InflectionOfBe,
+ patterns::{InflectionOfBe, UPOSSet},
};
pub struct HowTo {
@@ -24,9 +20,10 @@ impl Default for HowTo {
.then_anything()
.t_aco("how")
.then_whitespace()
- .then(|tok: &Token, _: &[char]| {
- tok.kind.is_upos(UPOS::VERB)
- || (tok.kind.is_verb() && tok.kind.is_likely_homograph())
+ .then(|tok: &Token, src: &[char]| {
+ // TODO this is a temporary hack until PR #1730 is merged
+ // TODO we should use then_verb_lemma() instead
+ is_verb_lemma(tok, src)
});
pattern.add(pos_pattern);
@@ -36,18 +33,15 @@ impl Default for HowTo {
.then_anything()
.then_anything()
.then_unless(
- InflectionOfBe::new().or(Box::new(|tok: &Token, src: &[char]| {
- if tok.kind.is_auxiliary_verb()
- || tok.kind.is_adjective()
- || tok.kind.is_verb_progressive_form()
- || tok.kind.is_conjunction()
- {
- true
- } else {
- let normed = tok.span.get_content_string(src).to_ascii_lowercase();
- normed == "did" || normed == "come" || normed == "does"
- }
- })),
+ InflectionOfBe::new().or(SequenceExpr::default().then_kind_any_or_words(
+ &[
+ TokenKind::is_auxiliary_verb,
+ TokenKind::is_adjective,
+ TokenKind::is_conjunction,
+ TokenKind::is_proper_noun,
+ ] as &[_],
+ &["did", "come", "does"],
+ )),
);
pattern.add(SequenceExpr::default().then(exceptions));
@@ -58,6 +52,16 @@ impl Default for HowTo {
}
}
+// TODO this is a temporary hack until PR #1730 is merged
+fn is_verb_lemma(tok: &Token, src: &[char]) -> bool {
+ tok.kind.is_verb() && {
+ let verb = tok.span.get_content(src);
+ !(verb.ends_with_ignore_ascii_case_str("s")
+ || verb.ends_with_ignore_ascii_case_str("ed")
+ || verb.ends_with_ignore_ascii_case_str("ing"))
+ }
+}
+
impl ExprLinter for HowTo {
fn expr(&self) -> &dyn Expr {
self.expr.as_ref()
@@ -290,4 +294,20 @@ mod tests {
HowTo::default(),
);
}
+
+ #[test]
+ fn dont_flag_false_positive_pr_1846() {
+ assert_no_lints(
+ "About how Microsoft, Google, and others are training people in Rust.",
+ HowTo::default(),
+ )
+ }
+
+ #[test]
+ fn dont_flag_false_positives_1492_how_indexes() {
+ assert_no_lints(
+ "controls how indexes will be added to unwrapped keys of flat array-like objects",
+ HowTo::default(),
+ );
+ }
}