Skip to content

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

feat: upgrade `AsSoonAsPossible` from initialisms linter to its own (#4565)

Andrew Dunbaraca99a6f9b22
diff --git a/harper-core/src/linting/asap.rs b/harper-core/src/linting/asap.rs
new file mode 100644
index 000000000..54e0b48d4
--- /dev/null
+++ b/harper-core/src/linting/asap.rs
@@ -0,0 +1,176 @@
+use crate::{
+ CharStringExt, Lint, Span, Token, TokenStringExt,
+ expr::{Expr, SequenceExpr},
+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},
+};
+
+pub struct AsSoonAsPossible {
+ expr: SequenceExpr,
+}
+
+impl Default for AsSoonAsPossible {
+ fn default() -> Self {
+ Self {
+ expr: SequenceExpr::any_capitalization_of("asap")
+ .then_optional(SequenceExpr::whitespace().then_word_seq(&["as", "possible"])),
+ }
+ }
+}
+
+impl ExprLinter for AsSoonAsPossible {
+ type Unit = Chunk;
+
+ fn match_to_lint_with_context(
+ &self,
+ toks: &[Token],
+ src: &[char],
+ ctx: Option<(&[Token], &[Token])>,
+ ) -> Option<Lint> {
+ let opt_as_before = if let Some(([.., word, ws], _)) = ctx {
+ (ws.kind.is_whitespace() && word.kind.is_word() && word.get_ch(src).eq_ch(&['a', 's']))
+ .then_some(word)
+ } else {
+ None
+ };
+
+ let (span, offer_asap) = match toks.len() {
+ 1 => (toks.span()?, false),
+ 5 => {
+ let span = opt_as_before
+ .and_then(|as_tok| toks.span().map(|s| Span::new(as_tok.span.start, s.end)))
+ .or_else(|| toks.span())?;
+ (span, true)
+ }
+ _ => return None,
+ };
+
+ // Always all-caps, so not `replace_with_match_case`.
+ let offer_asap = offer_asap.then(|| Suggestion::ReplaceWith(vec!['A', 'S', 'A', 'P']));
+
+ // We can't use `replace_with_match_case` because it's by character index
+ // and will cause unexpected characters to be uppercased if ASAP was uppercase.
+ // Instead, if "ASAP" is capitalized and followed by all-caps "AS", we suggest an all-caps expansion.
+ let offer_to_expand = Suggestion::ReplaceWith(
+ if toks[0].get_ch(src) == ['A', 'S', 'A', 'P']
+ && toks.get(2).map(|t| t.get_ch(src)) == Some(&['A', 'S'])
+ {
+ "AS SOON AS POSSIBLE".chars().collect()
+ } else {
+ "as soon as possible".chars().collect()
+ },
+ );
+
+ let suggestions: Vec<_> = offer_asap
+ .into_iter()
+ .chain(std::iter::once(offer_to_expand))
+ .collect();
+
+ let (lint_kind, message) = if suggestions.len() == 1 {
+ (
+ LintKind::Miscellaneous,
+ // As in `initialism_linter.rs`
+ "Try expanding this initialism.".to_owned(),
+ )
+ } else {
+ (
+ LintKind::Redundancy,
+ "You can avoid redundancy since `ASAP` already means `as soon as possible`."
+ .to_owned(),
+ )
+ };
+
+ Some(Lint {
+ span,
+ lint_kind,
+ suggestions,
+ message,
+ ..Default::default()
+ })
+ }
+
+ fn expr(&self) -> &dyn Expr {
+ &self.expr
+ }
+
+ fn description(&self) -> &str {
+ "Looks for redundant use of the initialism `ASAP` and offers to expand when not redundant."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::tests::{assert_good_and_bad_suggestions, assert_suggestion_result};
+
+ use super::AsSoonAsPossible;
+
+ #[test]
+ fn corrects_asap() {
+ assert_suggestion_result(
+ "Please respond asap.",
+ AsSoonAsPossible::default(),
+ "Please respond as soon as possible.",
+ );
+ }
+
+ #[test]
+ fn corrects_asap_caps_as_possible() {
+ assert_good_and_bad_suggestions(
+ "Regardless, let's get the deprecation in ASAP as possible to signal to users to stop using it.",
+ AsSoonAsPossible::default(),
+ &[
+ "Regardless, let's get the deprecation in as soon as possible to signal to users to stop using it.",
+ "Regardless, let's get the deprecation in ASAP to signal to users to stop using it.",
+ ],
+ &[],
+ );
+ }
+
+ #[test]
+ fn corrects_asap_lower_as_possible() {
+ assert_good_and_bad_suggestions(
+ "I need $1000 asap as possible",
+ AsSoonAsPossible::default(),
+ &["I need $1000 as soon as possible", "I need $1000 ASAP"],
+ &[],
+ );
+ }
+
+ #[test]
+ fn corrects_asap_as_possible_all_caps() {
+ assert_good_and_bad_suggestions(
+ "WELL JUST TELL HIM TO CALL ME ASAP AS POSSIBLE.",
+ AsSoonAsPossible::default(),
+ &[
+ "WELL JUST TELL HIM TO CALL ME AS SOON AS POSSIBLE.",
+ "WELL JUST TELL HIM TO CALL ME ASAP.",
+ ],
+ &[],
+ );
+ }
+
+ #[test]
+ fn corrects_as_asap_caps_as_possible() {
+ assert_good_and_bad_suggestions(
+ "Please feel free just to send one of us a brief, descriptive email with your question, and we'll do our best to get back to you as ASAP as possible.",
+ AsSoonAsPossible::default(),
+ &[
+ "Please feel free just to send one of us a brief, descriptive email with your question, and we'll do our best to get back to you as soon as possible.",
+ "Please feel free just to send one of us a brief, descriptive email with your question, and we'll do our best to get back to you ASAP.",
+ ],
+ &[],
+ );
+ }
+
+ #[test]
+ fn corrects_as_asap_lower_as_possible() {
+ assert_good_and_bad_suggestions(
+ "security updates should still be created as asap as possible.",
+ AsSoonAsPossible::default(),
+ &[
+ "security updates should still be created as soon as possible.",
+ "security updates should still be created ASAP.",
+ ],
+ &[],
+ );
+ }
+}
diff --git a/harper-core/src/linting/initialisms.rs b/harper-core/src/linting/initialisms.rs
index b039e90a1..318fcf780 100644
--- a/harper-core/src/linting/initialisms.rs
+++ b/harper-core/src/linting/initialisms.rs
@@ -20,7 +20,6 @@ pub fn lint_group() -> LintGroup {
"AsFarAsICanTell" => ("afaict", &["as far as I can tell"]),
"AsFarAsIKnow" => ("afaik", &["as far as I know"]),
"AsIUnderstandIt" => ("aiui", &["as I understand it"]),
- "AsSoonAsPossible" => ("asap", &["as soon as possible"]),
"BeRightBack" => ("brb", &["be right back"]),
"ByTheWay" => ("btw", &["by the way"]),
"ExplainLikeImFive" => ("eli5", &["explain like i'm five"]),
@@ -84,15 +83,6 @@ mod tests {
);
}
- #[test]
- fn corrects_asap() {
- assert_suggestion_result(
- "Please respond asap.",
- lint_group(),
- "Please respond as soon as possible.",
- );
- }
-
#[test]
fn corrects_imo() {
assert_suggestion_result(
diff --git a/harper-core/src/linting/lint_group/mod.rs b/harper-core/src/linting/lint_group/mod.rs
index c5cac2f9c..957827c09 100644
--- a/harper-core/src/linting/lint_group/mod.rs
+++ b/harper-core/src/linting/lint_group/mod.rs
@@ -31,6 +31,7 @@ use super::apart_from::ApartFrom;
use super::arrive_to::ArriveTo;
use super::as_how::AsHow;
use super::as_to_interrogative::AsToInterrogative;
+use super::asap::AsSoonAsPossible;
use super::ask_no_preposition::AskNoPreposition;
use super::aspire_to::AspireTo;
use super::avoid_contractions::AvoidContractions;
@@ -661,6 +662,7 @@ impl LintGroup {
insert_expr_rule!(ApartFrom);
insert_expr_rule!(ArriveTo);
insert_expr_rule!(AsHow);
+ insert_expr_rule!(AsSoonAsPossible);
insert_expr_rule!(AsToInterrogative);
insert_expr_rule!(AskNoPreposition);
insert_expr_rule!(AvoidContractions);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 36604ede8..91d5bb6ac 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -27,6 +27,7 @@ mod apart_from;
mod arrive_to;
mod as_how;
mod as_to_interrogative;
+mod asap;
mod ask_no_preposition;
mod aspire_to;
mod avoid_contractions;