Skip to content

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

fix(core): ignore the letter `s` when found in a parenthesis after a word (#3596)

Elijah Potterbecc25fe36dc
diff --git a/harper-core/src/lib.rs b/harper-core/src/lib.rs
index d3e0a4a3e..cbcf4b501 100644
--- a/harper-core/src/lib.rs
+++ b/harper-core/src/lib.rs
@@ -99,6 +99,45 @@ pub fn remove_overlaps(lints: &mut Vec<Lint>) {
lints.remove_indices(remove_indices);
}
+/// Remove lints whose character spans overlap any nonempty match of an expression.
+///
+/// This is useful for letting higher-level token patterns mark text ranges where otherwise valid
+/// lower-level lints should be suppressed. Expression matches are checked from every token index,
+/// including overlapping matches, and zero-width expression matches are ignored.
+pub fn remove_lints_overlapping_expr<E: expr::Expr + ?Sized>(
+ expr: &E,
+ document: &Document,
+ lints: &mut Vec<Lint>,
+) {
+ if lints.is_empty() {
+ return;
+ }
+
+ let tokens = document.get_tokens();
+ let source = document.get_source();
+ let matched_spans: Vec<Span<char>> = (0..tokens.len())
+ .filter_map(|cursor| {
+ let token_span = expr.run(cursor, tokens, source)?;
+
+ if token_span.is_empty() {
+ None
+ } else {
+ Some(token_span.to_char_span(tokens))
+ }
+ })
+ .collect();
+
+ if matched_spans.is_empty() {
+ return;
+ }
+
+ lints.retain(|lint| {
+ !matched_spans
+ .iter()
+ .any(|matched_span| lint.span.overlaps_with(*matched_span))
+ });
+}
+
/// Remove overlapping lints from a map keyed by rule name, similar to [`remove_overlaps`].
///
/// The map is treated as if all contained lints were in a single flat collection, ensuring the
@@ -173,9 +212,10 @@ mod tests {
use crate::remove_overlaps_map;
use crate::spell::FstDictionary;
use crate::{
- Dialect, Document,
+ Dialect, Document, Span,
+ expr::{AnchorStart, SequenceExpr},
linting::{LintGroup, Linter},
- remove_overlaps,
+ remove_lints_overlapping_expr, remove_overlaps,
};
#[test]
@@ -193,6 +233,45 @@ mod tests {
assert_eq!(lints.len(), 3);
}
+ #[test]
+ fn remove_lints_overlapping_expr_removes_overlapping_lints() {
+ let doc = Document::new_plain_english_curated("keep bad keep");
+ let mut lints = vec![Lint {
+ span: Span::new(5, 8),
+ ..Default::default()
+ }];
+
+ remove_lints_overlapping_expr(&SequenceExpr::aco("bad"), &doc, &mut lints);
+
+ assert!(lints.is_empty());
+ }
+
+ #[test]
+ fn remove_lints_overlapping_expr_keeps_non_overlapping_lints() {
+ let doc = Document::new_plain_english_curated("keep bad keep");
+ let mut lints = vec![Lint {
+ span: Span::new(0, 4),
+ ..Default::default()
+ }];
+
+ remove_lints_overlapping_expr(&SequenceExpr::aco("bad"), &doc, &mut lints);
+
+ assert_eq!(lints.len(), 1);
+ }
+
+ #[test]
+ fn remove_lints_overlapping_expr_ignores_zero_width_matches() {
+ let doc = Document::new_plain_english_curated("bad");
+ let mut lints = vec![Lint {
+ span: Span::new(0, 3),
+ ..Default::default()
+ }];
+
+ remove_lints_overlapping_expr(&AnchorStart, &doc, &mut lints);
+
+ assert_eq!(lints.len(), 1);
+ }
+
#[quickcheck]
fn overlap_removals_have_equivalent_behavior(s: String) {
let doc = Document::new_plain_english_curated(&s);
diff --git a/harper-core/src/linting/spell_check.rs b/harper-core/src/linting/spell_check.rs
index ad272c2d8..8c7449dd3 100644
--- a/harper-core/src/linting/spell_check.rs
+++ b/harper-core/src/linting/spell_check.rs
@@ -6,8 +6,9 @@ use smallvec::ToSmallVec;
use super::{Lint, LintKind, Linter};
use super::{Suggestion, informal_laughter::is_informal_laughter};
use crate::document::Document;
+use crate::expr::{Filter, SequenceExpr};
use crate::spell::{Dictionary, suggest_correct_spelling};
-use crate::{CharString, CharStringExt, Dialect, TokenStringExt};
+use crate::{CharString, CharStringExt, Dialect, TokenStringExt, remove_lints_overlapping_expr};
pub struct SpellCheck<T>
where
@@ -66,6 +67,25 @@ impl<T: Dictionary> SpellCheck<T> {
}
}
+fn parenthetical_plural_s_expr() -> Filter {
+ Filter::new(vec![
+ Box::new(
+ SequenceExpr::default()
+ .then_any_word()
+ .then_kind_where(|kind| kind.is_open_round())
+ .t_aco("s")
+ .then_kind_where(|kind| kind.is_close_round()),
+ ),
+ Box::new(
+ SequenceExpr::default()
+ .then_kind_where(|kind| kind.is_open_round())
+ .t_aco("s")
+ .then_kind_where(|kind| kind.is_close_round()),
+ ),
+ Box::new(SequenceExpr::aco("s")),
+ ])
+}
+
impl<T: Dictionary> Linter for SpellCheck<T> {
fn lint(&mut self, document: &Document) -> Vec<Lint> {
let mut lints = Vec::new();
@@ -131,6 +151,8 @@ impl<T: Dictionary> Linter for SpellCheck<T> {
})
}
+ remove_lints_overlapping_expr(&parenthetical_plural_s_expr(), document, &mut lints);
+
lints
}
@@ -534,6 +556,66 @@ mod tests {
);
}
+ #[test]
+ fn allows_parenthetical_plural_s() {
+ assert_no_lints(
+ "Please ask each person(s) to sign.",
+ SpellCheck::new(FstDictionary::curated(), Dialect::American),
+ );
+ }
+
+ #[test]
+ fn allows_multiple_parenthetical_plural_s_markers() {
+ assert_no_lints(
+ "Review the person(s) and document(s).",
+ SpellCheck::new(FstDictionary::curated(), Dialect::American),
+ );
+ }
+
+ #[test]
+ fn allows_uppercase_parenthetical_plural_s() {
+ assert_no_lints(
+ "CONTACT THE PERSON(S).",
+ SpellCheck::new(FstDictionary::curated(), Dialect::American),
+ );
+ }
+
+ #[test]
+ fn still_flags_misspelled_word_before_parenthetical_plural_s() {
+ assert_lint_count(
+ "Please ask each persson(s) to sign.",
+ SpellCheck::new(FstDictionary::curated(), Dialect::American),
+ 1,
+ );
+ }
+
+ #[test]
+ fn still_flags_parenthetical_s_without_preceding_word() {
+ assert_lint_count(
+ "Please mark (s) on the form.",
+ SpellCheck::new(FstDictionary::curated(), Dialect::American),
+ 1,
+ );
+ }
+
+ #[test]
+ fn still_flags_spaced_parenthetical_s() {
+ assert_lint_count(
+ "Please ask each person (s) to sign.",
+ SpellCheck::new(FstDictionary::curated(), Dialect::American),
+ 1,
+ );
+ }
+
+ #[test]
+ fn still_flags_longer_parenthetical_marker() {
+ assert_lint_count(
+ "Please ask each person(ss) to sign.",
+ SpellCheck::new(FstDictionary::curated(), Dialect::American),
+ 1,
+ );
+ }
+
// Tests that were previously in `spell/mod.rs`
// Tests for dialect-specific misspelling patterns