Skip to content

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

fix: false positive for "since N years old" (#2342)

Andrew Dunbarc9f4b7c49cc4
diff --git a/harper-core/src/linting/since_duration.rs b/harper-core/src/linting/since_duration.rs
index b14ba9544..1fbc6937d 100644
--- a/harper-core/src/linting/since_duration.rs
+++ b/harper-core/src/linting/since_duration.rs
@@ -1,5 +1,5 @@
-use crate::expr::{DurationExpr, Expr, LongestMatchOf, SequenceExpr};
-use crate::{Lrc, Token, TokenStringExt};
+use crate::expr::{DurationExpr, Expr, SequenceExpr};
+use crate::{CharStringExt, Token, TokenStringExt};
use super::{ExprLinter, Lint, LintKind, Suggestion};
use crate::linting::expr_linter::Chunk;
@@ -25,23 +25,18 @@ pub struct SinceDuration {
impl Default for SinceDuration {
fn default() -> Self {
- let pattern_without_ago = Lrc::new(
- SequenceExpr::default()
- .then_any_capitalization_of("since")
- .then_whitespace()
- .then(DurationExpr),
- );
-
- let pattern_with_ago = SequenceExpr::default()
- .then(pattern_without_ago.clone())
- .then_whitespace()
- .then_any_capitalization_of("ago");
-
Self {
- expr: Box::new(LongestMatchOf::new(vec![
- Box::new(pattern_without_ago),
- Box::new(pattern_with_ago),
- ])),
+ expr: Box::new(
+ SequenceExpr::default()
+ .then_any_capitalization_of("since")
+ .then_whitespace()
+ .then(DurationExpr)
+ .then_optional(
+ SequenceExpr::default()
+ .t_ws()
+ .then_word_set(&["ago", "old"]),
+ ),
+ ),
}
}
}
@@ -55,7 +50,11 @@ impl ExprLinter for SinceDuration {
fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
let last = toks.last()?;
- if last.span.get_content_string(src).to_lowercase() == "ago" {
+ if last
+ .span
+ .get_content(src)
+ .eq_any_ignore_ascii_case_chars(&[&['a', 'g', 'o'], &['o', 'l', 'd']])
+ {
return None;
}
@@ -94,7 +93,9 @@ impl ExprLinter for SinceDuration {
#[cfg(test)]
mod tests {
use super::SinceDuration;
- use crate::linting::tests::{assert_lint_count, assert_top3_suggestion_result};
+ use crate::linting::tests::{
+ assert_lint_count, assert_no_lints, assert_top3_suggestion_result,
+ };
#[test]
fn catches_spelled() {
@@ -107,10 +108,9 @@ mod tests {
#[test]
fn permits_spelled_with_ago() {
- assert_lint_count(
+ assert_no_lints(
"I have been waiting since two hours ago.",
SinceDuration::default(),
- 0,
);
}
@@ -125,10 +125,9 @@ mod tests {
#[test]
fn permits_numerals_with_ago() {
- assert_lint_count(
+ assert_no_lints(
"I have been waiting since 2 hours ago.",
SinceDuration::default(),
- 0,
);
}
@@ -287,4 +286,12 @@ mod tests {
"I use a Wacom Cintiq 27QHDT for several years on Linux",
);
}
+
+ #[test]
+ fn ignore_since_years_old() {
+ assert_no_lints(
+ "I've been coding since 11 years old and I'm now 57",
+ SinceDuration::default(),
+ );
+ }
}