Skip to content

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

feat:expand memory shorhands based on expand time shorthands linter (#1810)

Andrew Dunbar0463f234ab59
diff --git a/harper-core/src/linting/expand_memory_shorthands.rs b/harper-core/src/linting/expand_memory_shorthands.rs
new file mode 100644
index 000000000..37b3bf64a
--- /dev/null
+++ b/harper-core/src/linting/expand_memory_shorthands.rs
@@ -0,0 +1,246 @@
+use std::sync::Arc;
+
+use super::{ExprLinter, Lint, LintKind};
+use crate::Token;
+use crate::expr::{Expr, SequenceExpr, SpaceOrHyphen};
+use crate::linting::Suggestion;
+use crate::patterns::{ImpliesQuantity, WordSet};
+
+pub struct ExpandMemoryShorthands {
+ expr: Box<dyn Expr>,
+}
+
+impl ExpandMemoryShorthands {
+ pub fn new() -> Self {
+ let hotwords = Arc::new(WordSet::new(&[
+ "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "RB", "QB", "KiB", "MiB", "GiB",
+ "TiB", "PiB", "EiB", "ZiB", "YiB", "RiB", "QiB",
+ ]));
+
+ Self {
+ expr: Box::new(
+ SequenceExpr::default()
+ .then(ImpliesQuantity)
+ .then_longest_of(vec![
+ Box::new(SequenceExpr::default().then(hotwords.clone())),
+ Box::new(
+ SequenceExpr::default()
+ .then(SpaceOrHyphen)
+ .then(hotwords.clone()),
+ ),
+ ]),
+ ),
+ }
+ }
+
+ fn get_replacement(abbreviation: &str, plural: Option<bool>) -> Option<&'static str> {
+ let is_plural = plural.unwrap_or(abbreviation.ends_with('s'));
+ match abbreviation {
+ "B" => Some(if is_plural { "bytes" } else { "byte" }),
+ "kB" => Some(if is_plural { "kilobytes" } else { "kilobyte" }),
+ "MB" => Some(if is_plural { "megabytes" } else { "megabyte" }),
+ "GB" => Some(if is_plural { "gigabytes" } else { "gigabyte" }),
+ "TB" => Some(if is_plural { "terabytes" } else { "terabyte" }),
+ "PB" => Some(if is_plural { "petabytes" } else { "petabyte" }),
+ "EB" => Some(if is_plural { "exabytes" } else { "exabyte" }),
+ "ZB" => Some(if is_plural { "zettabytes" } else { "zettabyte" }),
+ "YB" => Some(if is_plural { "yottabytes" } else { "yottabyte" }),
+ "RB" => Some(if is_plural { "ronnabytes" } else { "ronnabyte" }),
+ "QB" => Some(if is_plural {
+ "quettabytes"
+ } else {
+ "quettabyte"
+ }),
+ "KiB" => Some(if is_plural { "kibibytes" } else { "kibibyte" }),
+ "MiB" => Some(if is_plural { "mebibytes" } else { "mebibyte" }),
+ "GiB" => Some(if is_plural { "gibibytes" } else { "gibibyte" }),
+ "TiB" => Some(if is_plural { "tebibytes" } else { "tebibyte" }),
+ "PiB" => Some(if is_plural { "pebibytes" } else { "pebibyte" }),
+ "EiB" => Some(if is_plural { "exbibytes" } else { "exbibyte" }),
+ "ZiB" => Some(if is_plural { "zebibytes" } else { "zebibyte" }),
+ "YiB" => Some(if is_plural { "yobibytes" } else { "yobibyte" }),
+ "RiB" => Some(if is_plural { "robibytes" } else { "robibyte" }),
+ "QiB" => Some(if is_plural { "quebibytes" } else { "quebibyte" }),
+
+ _ => None,
+ }
+ }
+}
+
+impl Default for ExpandMemoryShorthands {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl ExprLinter for ExpandMemoryShorthands {
+ fn expr(&self) -> &dyn Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
+ let offending_span = matched_tokens.last()?.span;
+ let implies_plural = ImpliesQuantity::implies_plurality(matched_tokens.first()?, source);
+
+ let offending_text = offending_span.get_content(source);
+
+ let replacement =
+ Self::get_replacement(&offending_text.iter().collect::<String>(), implies_plural)?;
+
+ let mut replacement_chars = Vec::new();
+
+ // If there isn't spacing, insert a space
+ if matched_tokens.len() == 2 {
+ replacement_chars.push(' ');
+ }
+
+ replacement_chars.extend(replacement.chars());
+
+ if replacement_chars == offending_text {
+ return None;
+ }
+
+ Some(Lint {
+ span: offending_span,
+ lint_kind: LintKind::WordChoice,
+ suggestions: vec![Suggestion::ReplaceWith(replacement_chars)],
+ message: format!("Did you mean `{replacement}`?"),
+ priority: 31,
+ })
+ }
+
+ fn description(&self) -> &str {
+ "Expands memory-related abbreviations (`B`, `kB`, `MB`, `GB`, `TB`, `PB`, `KiB`, `MiB`, `GiB`, `TiB`, `PiB`, etc.) to their full forms (`byte`, `kilobyte`, `megabyte`, `gigabyte`, `terabyte`, `petabyte`, `kibibyte`, `mebibyte`, `gibibyte`, `tebibyte`, `pebibyte`, etc.)."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+
+ use super::ExpandMemoryShorthands;
+
+ #[test]
+ fn detects_bytes() {
+ assert_suggestion_result("5 B", ExpandMemoryShorthands::new(), "5 bytes");
+ }
+
+ #[test]
+ fn detects_kilobytes() {
+ assert_suggestion_result("10 kB", ExpandMemoryShorthands::new(), "10 kilobytes");
+ }
+
+ #[test]
+ fn detects_megabytes() {
+ assert_suggestion_result("30 MB", ExpandMemoryShorthands::new(), "30 megabytes");
+ }
+
+ #[test]
+ fn detects_gigabytes() {
+ assert_suggestion_result("16 GB", ExpandMemoryShorthands::new(), "16 gigabytes");
+ }
+
+ #[test]
+ fn detects_terabytes() {
+ assert_suggestion_result("2 TB", ExpandMemoryShorthands::new(), "2 terabytes");
+ }
+
+ #[test]
+ fn detects_kibibytes() {
+ assert_suggestion_result("1024 KiB", ExpandMemoryShorthands::new(), "1024 kibibytes");
+ }
+
+ #[test]
+ fn detects_mebibytes() {
+ assert_suggestion_result("2048 MiB", ExpandMemoryShorthands::new(), "2048 mebibytes");
+ }
+
+ #[test]
+ fn detects_gibibytes() {
+ assert_suggestion_result("4 GiB", ExpandMemoryShorthands::new(), "4 gibibytes");
+ }
+
+ #[test]
+ fn detects_tebibytes() {
+ assert_suggestion_result("8 TiB", ExpandMemoryShorthands::new(), "8 tebibytes");
+ }
+
+ #[test]
+ fn detects_petabytes() {
+ assert_suggestion_result("1 PB", ExpandMemoryShorthands::new(), "1 petabyte");
+ }
+
+ #[test]
+ fn detects_exabytes() {
+ assert_suggestion_result("1 EB", ExpandMemoryShorthands::new(), "1 exabyte");
+ }
+
+ #[test]
+ fn detects_zettabytes() {
+ assert_suggestion_result("1 ZB", ExpandMemoryShorthands::new(), "1 zettabyte");
+ }
+
+ #[test]
+ fn detects_yottabytes() {
+ assert_suggestion_result("1 YB", ExpandMemoryShorthands::new(), "1 yottabyte");
+ }
+
+ #[test]
+ fn detects_quettabytes() {
+ assert_suggestion_result("1 QB", ExpandMemoryShorthands::new(), "1 quettabyte");
+ }
+
+ #[test]
+ fn detects_pebibytes() {
+ assert_suggestion_result("1 PiB", ExpandMemoryShorthands::new(), "1 pebibyte");
+ }
+
+ #[test]
+ fn detects_exbibytes() {
+ assert_suggestion_result("1 EiB", ExpandMemoryShorthands::new(), "1 exbibyte");
+ }
+
+ #[test]
+ fn detects_zebibytes() {
+ assert_suggestion_result("1 ZiB", ExpandMemoryShorthands::new(), "1 zebibyte");
+ }
+
+ #[test]
+ fn detects_yobibytes() {
+ assert_suggestion_result("1 YiB", ExpandMemoryShorthands::new(), "1 yobibyte");
+ }
+
+ #[test]
+ fn detects_robibytes() {
+ assert_suggestion_result("1 RiB", ExpandMemoryShorthands::new(), "1 robibyte");
+ }
+
+ #[test]
+ fn detects_quebibytes() {
+ assert_suggestion_result("1 QiB", ExpandMemoryShorthands::new(), "1 quebibyte");
+ }
+
+ #[test]
+ fn handles_punctuation() {
+ assert_suggestion_result("8 GB.", ExpandMemoryShorthands::new(), "8 gigabytes.");
+ }
+
+ #[test]
+ fn handles_adjacent_number() {
+ assert_suggestion_result("16GB", ExpandMemoryShorthands::new(), "16 gigabytes");
+ }
+
+ #[test]
+ fn handles_hyphen_separated() {
+ assert_suggestion_result("32-GB", ExpandMemoryShorthands::new(), "32-gigabytes");
+ }
+
+ #[test]
+ fn doesnt_handle_wrong_kb_cases() {
+ assert_lint_count(
+ "48kb and 64 KB were common in the 8-bit era.",
+ ExpandMemoryShorthands::new(),
+ 0,
+ );
+ }
+}
diff --git a/harper-core/src/linting/expand_time_shorthands.rs b/harper-core/src/linting/expand_time_shorthands.rs
index 67dfa7843..86f25643e 100644
--- a/harper-core/src/linting/expand_time_shorthands.rs
+++ b/harper-core/src/linting/expand_time_shorthands.rs
@@ -1,5 +1,6 @@
use crate::expr::Expr;
use crate::expr::SequenceExpr;
+use crate::expr::SpaceOrHyphen;
use std::sync::Arc;
use super::{ExprLinter, Lint, LintKind};
@@ -25,10 +26,9 @@ impl ExpandTimeShorthands {
Box::new(SequenceExpr::default().then(hotwords.clone())),
Box::new(
SequenceExpr::default()
- .then_whitespace()
+ .then(SpaceOrHyphen)
.then(hotwords.clone()),
),
- Box::new(SequenceExpr::default().then_hyphen().then(hotwords.clone())),
]),
),
}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 641585af5..fb9f2d5e1 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -37,6 +37,7 @@ use super::double_modal::DoubleModal;
use super::ellipsis_length::EllipsisLength;
use super::else_possessive::ElsePossessive;
use super::everyday::Everyday;
+use super::expand_memory_shorthands::ExpandMemoryShorthands;
use super::expand_time_shorthands::ExpandTimeShorthands;
use super::expr_linter::run_on_chunk;
use super::few_units_of_time_ago::FewUnitsOfTimeAgo;
@@ -414,8 +415,6 @@ impl LintGroup {
insert_expr_rule!(BoringWords, false);
insert_struct_rule!(CapitalizePersonalPronouns, true);
insert_expr_rule!(ChockFull, true);
- insert_expr_rule!(QuantifierNeedsOf, true);
- insert_struct_rule!(NoFrenchSpaces, true);
insert_struct_rule!(CommaFixes, true);
insert_struct_rule!(CompoundNouns, true);
insert_expr_rule!(Confident, true);
@@ -429,6 +428,7 @@ impl LintGroup {
insert_struct_rule!(EllipsisLength, true);
insert_struct_rule!(ElsePossessive, true);
insert_struct_rule!(Everyday, true);
+ insert_expr_rule!(ExpandMemoryShorthands, true);
insert_expr_rule!(ExpandTimeShorthands, true);
insert_expr_rule!(FewUnitsOfTimeAgo, true);
insert_expr_rule!(FillerWords, true);
@@ -457,6 +457,7 @@ impl LintGroup {
insert_expr_rule!(MostNumber, true);
insert_expr_rule!(MultipleSequentialPronouns, true);
insert_struct_rule!(NailOnTheHead, true);
+ insert_struct_rule!(NoFrenchSpaces, true);
insert_expr_rule!(NoMatchFor, true);
insert_struct_rule!(NoOxfordComma, false);
insert_expr_rule!(Nobody, true);
@@ -477,6 +478,7 @@ impl LintGroup {
insert_struct_rule!(PronounContraction, true);
insert_expr_rule!(PronounInflectionBe, true);
insert_struct_rule!(PronounKnew, true);
+ insert_expr_rule!(QuantifierNeedsOf, true);
insert_expr_rule!(RedundantAdditiveAdverbs, true);
insert_struct_rule!(RepeatedWords, true);
insert_struct_rule!(SaveToSafe, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index de41f8156..16622efaf 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -31,6 +31,7 @@ mod double_modal;
mod ellipsis_length;
mod else_possessive;
mod everyday;
+mod expand_memory_shorthands;
mod expand_time_shorthands;
mod expr_linter;
mod few_units_of_time_ago;
@@ -162,6 +163,7 @@ pub use dot_initialisms::DotInitialisms;
pub use double_modal::DoubleModal;
pub use ellipsis_length::EllipsisLength;
pub use everyday::Everyday;
+pub use expand_memory_shorthands::ExpandMemoryShorthands;
pub use expand_time_shorthands::ExpandTimeShorthands;
pub use expr_linter::ExprLinter;
pub use few_units_of_time_ago::FewUnitsOfTimeAgo;