Skip to content

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

feat: add `match_to_lint_with_context` to `ExprLinter` (#2173)

Andrew Dunbar42b005d25634
diff --git a/harper-core/src/expr/sequence_expr.rs b/harper-core/src/expr/sequence_expr.rs
index eca33279d..da057fade 100644
--- a/harper-core/src/expr/sequence_expr.rs
+++ b/harper-core/src/expr/sequence_expr.rs
@@ -474,6 +474,12 @@ impl SequenceExpr {
gen_then_from_is!(conjunction);
gen_then_from_is!(preposition);
+ // Numbers
+
+ gen_then_from_is!(number);
+ gen_then_from_is!(cardinal_number);
+ gen_then_from_is!(ordinal_number);
+
// Punctuation
gen_then_from_is!(punctuation);
@@ -486,7 +492,6 @@ impl SequenceExpr {
// Other
- gen_then_from_is!(number);
gen_then_from_is!(case_separator);
gen_then_from_is!(likely_homograph);
}
diff --git a/harper-core/src/linting/expr_linter.rs b/harper-core/src/linting/expr_linter.rs
index 0352daa2a..8bef30e91 100644
--- a/harper-core/src/linting/expr_linter.rs
+++ b/harper-core/src/linting/expr_linter.rs
@@ -13,11 +13,34 @@ use super::{Lint, Linter};
pub trait ExprLinter: LSend {
/// A simple getter for the expression you want Harper to search for.
fn expr(&self) -> &dyn Expr;
- /// If any portions of a [`Document`] match [`Self::expr`], they are passed through [`ExprLinter::match_to_lint`] to be
- /// transformed into a [`Lint`] for editor consumption.
+ /// If any portions of a [`Document`] match [`Self::expr`], they are passed through [`ExprLinter::match_to_lint`]
+ /// or [`ExprLinter::match_to_lint_with_context`] to be transformed into a [`Lint`] for editor consumption.
///
- /// This function may return `None` to elect _not_ to produce a lint.
- fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint>;
+ /// Transform matched tokens into a [`Lint`] for editor consumption.
+ ///
+ /// This is the simple version that only sees the matched tokens. For context-aware linting,
+ /// implement `match_to_lint_with_context` instead.
+ ///
+ /// Return `None` to skip producing a lint for this match.
+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
+ self.match_to_lint_with_context(matched_tokens, source, None)
+ }
+
+ /// Transform matched tokens into a [`Lint`] with access to surrounding context.
+ ///
+ /// The context provides access to tokens before and after the match. When implementing
+ /// this method, you can call `self.match_to_lint()` as a fallback if the context isn't needed.
+ ///
+ /// Return `None` to skip producing a lint for this match.
+ fn match_to_lint_with_context(
+ &self,
+ matched_tokens: &[Token],
+ source: &[char],
+ _context: Option<(&[Token], &[Token])>,
+ ) -> Option<Lint> {
+ // Default implementation falls back to the simple version
+ self.match_to_lint(matched_tokens, source)
+ }
/// A user-facing description of what kinds of grammatical errors this rule looks for.
/// It is usually shown in settings menus.
fn description(&self) -> &str;
@@ -71,6 +94,129 @@ pub fn run_on_chunk<'a>(
.expr()
.iter_matches(chunk, source)
.filter_map(|match_span| {
- linter.match_to_lint(&chunk[match_span.start..match_span.end], source)
+ linter.match_to_lint_with_context(
+ &chunk[match_span.start..match_span.end],
+ source,
+ Some((&chunk[..match_span.start], &chunk[match_span.end..])),
+ )
})
}
+
+#[cfg(test)]
+mod tests {
+ use crate::expr::{Expr, FixedPhrase};
+ use crate::linting::tests::assert_suggestion_result;
+ use crate::linting::{ExprLinter, Suggestion};
+ use crate::token_string_ext::TokenStringExt;
+ use crate::{Lint, Token};
+
+ pub struct TestSimpleLinter {
+ expr: Box<dyn Expr>,
+ }
+
+ impl Default for TestSimpleLinter {
+ fn default() -> Self {
+ Self {
+ expr: Box::new(FixedPhrase::from_phrase("two")),
+ }
+ }
+ }
+
+ impl ExprLinter for TestSimpleLinter {
+ fn expr(&self) -> &dyn Expr {
+ &*self.expr
+ }
+
+ fn match_to_lint(&self, toks: &[Token], _src: &[char]) -> Option<Lint> {
+ Some(Lint {
+ span: toks.span()?,
+ message: "simple".to_string(),
+ suggestions: vec![Suggestion::ReplaceWith(vec!['2'])],
+ ..Default::default()
+ })
+ }
+
+ fn description(&self) -> &str {
+ "test linter"
+ }
+ }
+
+ pub struct TestContextLinter {
+ expr: Box<dyn Expr>,
+ }
+
+ impl Default for TestContextLinter {
+ fn default() -> Self {
+ Self {
+ expr: Box::new(FixedPhrase::from_phrase("two")),
+ }
+ }
+ }
+
+ impl ExprLinter for TestContextLinter {
+ fn expr(&self) -> &dyn Expr {
+ &*self.expr
+ }
+
+ fn match_to_lint_with_context(
+ &self,
+ toks: &[Token],
+ src: &[char],
+ context: Option<(&[Token], &[Token])>,
+ ) -> Option<Lint> {
+ if let Some((before, after)) = context {
+ let before = before.span()?.get_content_string(src);
+ let after = after.span()?.get_content_string(src);
+
+ let (message, suggestions) = if before.eq_ignore_ascii_case("one ")
+ && after.eq_ignore_ascii_case(" three")
+ {
+ (
+ "ascending".to_string(),
+ vec![Suggestion::ReplaceWith(vec!['>'])],
+ )
+ } else if before.eq_ignore_ascii_case("three ")
+ && after.eq_ignore_ascii_case(" one")
+ {
+ (
+ "descending".to_string(),
+ vec![Suggestion::ReplaceWith(vec!['<'])],
+ )
+ } else {
+ (
+ "dunno".to_string(),
+ vec![Suggestion::ReplaceWith(vec!['?'])],
+ )
+ };
+
+ return Some(Lint {
+ span: toks.span()?,
+ message,
+ suggestions,
+ ..Default::default()
+ });
+ } else {
+ None
+ }
+ }
+
+ fn description(&self) -> &str {
+ "context linter"
+ }
+ }
+
+ #[test]
+ fn simple_test_123() {
+ assert_suggestion_result("one two three", TestSimpleLinter::default(), "one 2 three");
+ }
+
+ #[test]
+ fn context_test_123() {
+ assert_suggestion_result("one two three", TestContextLinter::default(), "one > three");
+ }
+
+ #[test]
+ fn context_test_321() {
+ assert_suggestion_result("three two one", TestContextLinter::default(), "three < one");
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 336367e9f..cd1bf850e 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -126,6 +126,7 @@ use super::pronoun_inflection_be::PronounInflectionBe;
use super::pronoun_knew::PronounKnew;
use super::proper_noun_capitalization_linters;
use super::quantifier_needs_of::QuantifierNeedsOf;
+use super::quantifier_numeral_conflict::QuantifierNumeralConflict;
use super::quite_quiet::QuiteQuiet;
use super::quote_spacing::QuoteSpacing;
use super::redundant_additive_adverbs::RedundantAdditiveAdverbs;
@@ -571,6 +572,7 @@ impl LintGroup {
insert_expr_rule!(PronounInflectionBe, true);
insert_struct_rule!(PronounKnew, true);
insert_expr_rule!(QuantifierNeedsOf, true);
+ insert_expr_rule!(QuantifierNumeralConflict, true);
insert_expr_rule!(QuiteQuiet, true);
insert_struct_rule!(QuoteSpacing, true);
insert_expr_rule!(RedundantAdditiveAdverbs, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 35351c56a..c132e33d0 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -141,6 +141,7 @@ mod pronoun_inflection_be;
mod pronoun_knew;
mod proper_noun_capitalization_linters;
mod quantifier_needs_of;
+mod quantifier_numeral_conflict;
mod quite_quiet;
mod quote_spacing;
mod redundant_additive_adverbs;
@@ -315,6 +316,7 @@ pub use pronoun_are::PronounAre;
pub use pronoun_contraction::PronounContraction;
pub use pronoun_inflection_be::PronounInflectionBe;
pub use quantifier_needs_of::QuantifierNeedsOf;
+pub use quantifier_numeral_conflict::QuantifierNumeralConflict;
pub use quite_quiet::QuiteQuiet;
pub use quote_spacing::QuoteSpacing;
pub use redundant_additive_adverbs::RedundantAdditiveAdverbs;
diff --git a/harper-core/src/linting/quantifier_numeral_conflict.rs b/harper-core/src/linting/quantifier_numeral_conflict.rs
new file mode 100644
index 000000000..a198b700a
--- /dev/null
+++ b/harper-core/src/linting/quantifier_numeral_conflict.rs
@@ -0,0 +1,176 @@
+use crate::expr::{All, Expr, SequenceExpr, SpelledNumberExpr};
+use crate::linting::{ExprLinter, LintKind, Suggestion};
+use crate::patterns::{NominalPhrase, WordSet};
+use crate::token_string_ext::TokenStringExt;
+use crate::{CharStringExt, Lint, Token};
+
+pub struct QuantifierNumeralConflict {
+ expr: Box<dyn Expr>,
+}
+
+impl Default for QuantifierNumeralConflict {
+ fn default() -> Self {
+ Self {
+ expr: Box::new(All::new(vec![
+ Box::new(
+ SequenceExpr::default()
+ .then_quantifier()
+ .t_ws()
+ .then_longest_of(vec![
+ Box::new(SpelledNumberExpr),
+ Box::new(SequenceExpr::default().then_cardinal_number()),
+ ]),
+ ),
+ Box::new(
+ SequenceExpr::default().then_unless(SequenceExpr::any_of(vec![
+ Box::new(WordSet::new(&["all", "any", "every"])),
+ Box::new(
+ SequenceExpr::word_set(&["each", "no", "some"])
+ .t_ws()
+ .t_aco("one"),
+ ),
+ ])),
+ ),
+ ])),
+ }
+ }
+}
+
+impl ExprLinter for QuantifierNumeralConflict {
+ 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 there's a hyphen straight after the number it's probably part of a compound
+ if let Some((_, [next_tok, ..])) = ctx
+ && next_tok.kind.is_hyphen()
+ {
+ return None;
+ }
+
+ let qtok = toks.first().unwrap();
+ let quant = qtok.span.get_content_string(src);
+
+ // Handle special cases for "least", "most", "each", and "both"
+ match quant.to_ascii_lowercase().as_str() {
+ "least" | "most" => {
+ if let Some((previous, _)) = ctx
+ && let [.., prev_word, prev_space] = previous
+ && prev_space.kind.is_whitespace()
+ && prev_word.kind.is_word()
+ && prev_word
+ .span
+ .get_content(src)
+ .eq_ignore_ascii_case_chars(&['a', 't'])
+ {
+ return None;
+ }
+ }
+
+ "each" => {
+ return Some(Lint {
+ span: qtok.span,
+ lint_kind: LintKind::Usage,
+ suggestions: vec![Suggestion::replace_with_match_case(
+ "every".chars().collect(),
+ qtok.span.get_content(src),
+ )],
+ message: "Use 'every' instead of 'each' before a number.".to_owned(),
+ ..Default::default()
+ });
+ }
+
+ "both" => {
+ if let Some((_, following)) = ctx
+ && let Some(noun_phrase_span) = NominalPhrase.run(1, following, src)
+ && let [ws, conj, ..] = following.get(noun_phrase_span.end..).unwrap_or(&[])
+ && ws.kind.is_whitespace()
+ && conj.kind.is_conjunction()
+ && conj
+ .span
+ .get_content_string(src)
+ .eq_ignore_ascii_case("and")
+ {
+ return None;
+ }
+ }
+
+ _ => {} // Continue with the default case
+ }
+
+ Some(Lint {
+ span: toks.span()?,
+ lint_kind: LintKind::Grammar,
+ suggestions: vec![],
+ message: format!("The word '{quant}' should not be used before a number."),
+ ..Default::default()
+ })
+ }
+
+ fn description(&self) -> &'static str {
+ "Detects quantifier-numeral conflicts"
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::tests::{assert_lint_count, assert_no_lints, assert_suggestion_result};
+
+ use super::QuantifierNumeralConflict;
+
+ #[test]
+ fn flag_several_two() {
+ assert_lint_count(
+ "A few minutes ago, there was an outage due to several two hosts being down at the same time.",
+ QuantifierNumeralConflict::default(),
+ 1,
+ );
+ }
+
+ #[test]
+ fn dont_flag_at_least() {
+ assert_no_lints(
+ "Serving a company that encourages the \"996\" work schedule usually means working for at least 60 hours per week.",
+ QuantifierNumeralConflict::default(),
+ );
+ }
+
+ #[test]
+ fn dont_flag_at_most() {
+ assert_no_lints(
+ "But don't worry, the second machine takes at most 3 years.",
+ QuantifierNumeralConflict::default(),
+ );
+ }
+
+ #[test]
+ fn dont_flag_both_32_bit_and_64_bit() {
+ assert_no_lints(
+ "Both 32 bit and 64 bit architectures are supported.",
+ QuantifierNumeralConflict::default(),
+ );
+ }
+
+ #[test]
+ fn dont_flag_more_1_click() {
+ assert_no_lints(
+ "For more 1-click cloud deployments, see [Cloud Deployment",
+ QuantifierNumeralConflict::default(),
+ );
+ }
+
+ #[test]
+ fn correct_each_2() {
+ assert_suggestion_result(
+ "OSSEC by default run rootkit check each 2 hours.",
+ QuantifierNumeralConflict::default(),
+ "OSSEC by default run rootkit check every 2 hours.",
+ );
+ }
+}
diff --git a/harper-core/src/token_kind.rs b/harper-core/src/token_kind.rs
index ee1e4958f..a4e4dceec 100644
--- a/harper-core/src/token_kind.rs
+++ b/harper-core/src/token_kind.rs
@@ -170,6 +170,22 @@ impl TokenKind {
matches!(self, TokenKind::Word(None))
}
+ // Number is-methods
+
+ pub fn is_cardinal_number(&self) -> bool {
+ matches!(self, TokenKind::Number(Number { suffix: None, .. }))
+ }
+
+ pub fn is_ordinal_number(&self) -> bool {
+ matches!(
+ self,
+ TokenKind::Number(Number {
+ suffix: Some(_),
+ ..
+ })
+ )
+ }
+
// Punctuation and symbol is-methods
pub fn is_open_square(&self) -> bool {