Skip to content

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

Fixes three false positives when "A" or "B" are school test grades (#3718)

Andrew Dunbarc3dc82c158d8
diff --git a/harper-core/src/linting/an_a.rs b/harper-core/src/linting/an_a.rs
index c241208a9..365c62664 100644
--- a/harper-core/src/linting/an_a.rs
+++ b/harper-core/src/linting/an_a.rs
@@ -1,8 +1,10 @@
use itertools::Itertools;
-use crate::indefinite_article::{InitialSound, starts_with_vowel};
-use crate::linting::{Lint, LintKind, Linter, Suggestion};
-use crate::{Dialect, Document, TokenStringExt};
+use crate::{
+ CharStringExt, Dialect, Document, TokenStringExt,
+ indefinite_article::{InitialSound, starts_with_vowel},
+ linting::{Lint, LintKind, Linter, Suggestion},
+};
#[derive(Debug)]
pub struct AnA {
@@ -43,6 +45,26 @@ impl Linter for AnA {
.next()
.unwrap_or(chars_second);
+ // Capital letter 'A' might be a test grade rather than an article
+ if chars_first == ['A'] {
+ // Check for range of test grades starting with "A-"
+ if second_idx == first_idx + 2
+ && chunk[first_idx + 1].kind.is_hyphen()
+ && ('B'..='F').contains(&chars_second[0])
+ {
+ continue;
+ }
+
+ // Check for an indefinite article before: "an A" or "a A"
+ if let Some(prev) = document.get_next_word_from_offset(first_idx, -1)
+ && prev
+ .get_ch(document.get_source())
+ .eq_any_ignore_ascii_case_chars(&[&['a'], &['a', 'n']])
+ {
+ continue;
+ }
+ }
+
let is_a_an = match chars_first {
['a'] => Some(true),
['A'] => Some(true),
@@ -94,8 +116,10 @@ impl Linter for AnA {
#[cfg(test)]
mod tests {
use super::AnA;
- use crate::Dialect;
- use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+ use crate::{
+ Dialect,
+ linting::tests::{assert_lint_count, assert_no_lints, assert_suggestion_result},
+ };
#[test]
fn detects_html_as_vowel() {
@@ -350,4 +374,14 @@ mod tests {
0,
);
}
+
+ #[test]
+ fn dont_flag_a_f_grading_scale_3715() {
+ assert_no_lints("an A-F grading scale", AnA::new(Dialect::American));
+ }
+
+ #[test]
+ fn dont_flag_is_80_an_a_3715() {
+ assert_no_lints("Is 80 an A in Malaysia?", AnA::new(Dialect::American));
+ }
}
diff --git a/harper-core/src/linting/expand_memory_shorthands.rs b/harper-core/src/linting/expand_memory_shorthands.rs
index 24581e67d..cc828c81e 100644
--- a/harper-core/src/linting/expand_memory_shorthands.rs
+++ b/harper-core/src/linting/expand_memory_shorthands.rs
@@ -1,11 +1,12 @@
use std::sync::Arc;
use super::{ExprLinter, Lint, LintKind};
-use crate::Token;
-use crate::expr::{Expr, SequenceExpr};
-use crate::linting::Suggestion;
-use crate::linting::expr_linter::Chunk;
-use crate::patterns::{ImpliesQuantity, WordSet};
+use crate::{
+ expr::{Expr, SequenceExpr},
+ linting::{Suggestion, expr_linter::Chunk},
+ patterns::{ImpliesQuantity, WordSet},
+ {CharStringExt, Token},
+};
pub struct ExpandMemoryShorthands {
expr: SequenceExpr,
@@ -73,7 +74,24 @@ impl ExprLinter for ExpandMemoryShorthands {
&self.expr
}
- fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
+ // use crate::linting::debug::format_tokens;
+ fn match_to_lint_with_context(
+ &self,
+ matched_tokens: &[Token],
+ source: &[char],
+ context: Option<(&[Token], &[Token])>,
+ ) -> Option<Lint> {
+ // #3715 fix "B plus/ B minus grade:"
+ if matched_tokens
+ .last()
+ .is_some_and(|t| t.get_ch(source).eq_ch(&['b']))
+ && context
+ .and_then(|(_, after)| after.first())
+ .is_some_and(|hy| hy.kind.is_plus() || hy.kind.is_hyphen())
+ {
+ return None;
+ }
+
let offending_span = matched_tokens.last()?.span;
let implies_plural = ImpliesQuantity::implies_plurality(matched_tokens.first()?, source);
@@ -111,7 +129,7 @@ impl ExprLinter for ExpandMemoryShorthands {
#[cfg(test)]
mod tests {
- use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+ use crate::linting::tests::{assert_lint_count, assert_no_lints, assert_suggestion_result};
use super::ExpandMemoryShorthands;
@@ -238,4 +256,9 @@ mod tests {
0,
);
}
+
+ #[test]
+ fn dont_flag_is_a_b_plus_3715() {
+ assert_no_lints("Is a B+ a 3.3 GPA?", ExpandMemoryShorthands::new());
+ }
}