Skip to content

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

feat: very unique→unique / very special (#1561)

Andrew Dunbar82f20e9d838c
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 5b8569aca..32d749625 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -97,6 +97,7 @@ use super::throw_rubbish::ThrowRubbish;
use super::touristic::Touristic;
use super::unclosed_quotes::UnclosedQuotes;
use super::use_genitive::UseGenitive;
+use super::very_unique::VeryUnique;
use super::was_aloud::WasAloud;
use super::way_too_adjective::WayTooAdjective;
use super::whereas::Whereas;
@@ -458,6 +459,7 @@ impl LintGroup {
insert_expr_rule!(Touristic, true);
insert_struct_rule!(UnclosedQuotes, true);
insert_expr_rule!(UseGenitive, false);
+ insert_expr_rule!(VeryUnique, true);
insert_expr_rule!(WasAloud, true);
insert_expr_rule!(Whereas, true);
insert_expr_rule!(WidelyAccepted, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 74f095513..af42155f3 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -109,6 +109,7 @@ mod throw_rubbish;
mod touristic;
mod unclosed_quotes;
mod use_genitive;
+mod very_unique;
mod was_aloud;
mod way_too_adjective;
mod whereas;
@@ -209,6 +210,7 @@ pub use throw_rubbish::ThrowRubbish;
pub use touristic::Touristic;
pub use unclosed_quotes::UnclosedQuotes;
pub use use_genitive::UseGenitive;
+pub use very_unique::VeryUnique;
pub use was_aloud::WasAloud;
pub use way_too_adjective::WayTooAdjective;
pub use whereas::Whereas;
diff --git a/harper-core/src/linting/very_unique.rs b/harper-core/src/linting/very_unique.rs
new file mode 100644
index 000000000..1937de662
--- /dev/null
+++ b/harper-core/src/linting/very_unique.rs
@@ -0,0 +1,137 @@
+use crate::{
+ Token, TokenStringExt,
+ expr::{Expr, SequenceExpr},
+ linting::{ExprLinter, Lint, LintKind, Suggestion},
+ patterns::WordSet,
+};
+
+pub struct VeryUnique {
+ expr: Box<dyn Expr>,
+}
+
+impl Default for VeryUnique {
+ fn default() -> Self {
+ Self {
+ expr: Box::new(
+ SequenceExpr::default()
+ .then(WordSet::new(&[
+ "fairly", "pretty", "rather", "quite", "somewhat", "very",
+ ]))
+ .t_ws()
+ .t_aco("unique"),
+ ),
+ }
+ }
+}
+
+impl ExprLinter for VeryUnique {
+ fn expr(&self) -> &dyn Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
+ let very_unique_span = toks.span()?;
+ let very_unique_chars = very_unique_span.get_content(src);
+ let qualifier_tok = &toks.first()?;
+ let qualifier_str = qualifier_tok.span.get_content_string(src);
+
+ let adjectives = ["special", "rare", "unusual"];
+
+ let suggestions = adjectives
+ .iter()
+ .map(|adj| {
+ Suggestion::replace_with_match_case(
+ format!("{qualifier_str} {adj}").chars().collect(),
+ very_unique_chars,
+ )
+ })
+ .chain(std::iter::once(Suggestion::replace_with_match_case(
+ "unique".chars().collect(),
+ very_unique_chars,
+ )))
+ .collect::<Vec<_>>();
+
+ Some(Lint {
+ span: very_unique_span,
+ lint_kind: LintKind::WordChoice,
+ suggestions,
+ message: "`Unique` is an absolute, so consider using `unique` alone or a more precise adjective such as `special`, `rare`, or `unusual`.".to_string(),
+ priority: 57,
+ })
+ }
+
+ fn description(&self) -> &str {
+ "Flags phrases like `very unique`, `pretty unique`, etc., and suggests using `unique` alone or a more precise adjective such as `special`, `rare`, or `unusual`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::{
+ VeryUnique,
+ tests::{assert_good_and_bad_suggestions, assert_top3_suggestion_result},
+ };
+
+ #[test]
+ fn fix_very_unique() {
+ assert_good_and_bad_suggestions(
+ "I'm not sure whether Llama Stack or ollama are generating the chat completion ids, but they are not very unique.",
+ VeryUnique::default(),
+ &[
+ "I'm not sure whether Llama Stack or ollama are generating the chat completion ids, but they are not unique.",
+ ],
+ &[],
+ );
+ }
+
+ #[test]
+ fn fix_pretty_unique() {
+ assert_top3_suggestion_result(
+ "Numerous accounts with my exact full name/surname (which is pretty unique) has been created (most recently).",
+ VeryUnique::default(),
+ "Numerous accounts with my exact full name/surname (which is pretty rare) has been created (most recently).",
+ );
+ }
+
+ #[test]
+ fn fix_fairly_unique() {
+ assert_good_and_bad_suggestions(
+ "In browsers, the first chars are obtained from the user agent string (which is fairly unique), and the supported mimeTypes",
+ VeryUnique::default(),
+ &[
+ "In browsers, the first chars are obtained from the user agent string (which is unique), and the supported mimeTypes",
+ ],
+ &[],
+ );
+ }
+
+ #[test]
+ fn fix_somewhat_unique() {
+ assert_top3_suggestion_result(
+ "A new pack of somewhat unique upgrades for R.E.P.O.!",
+ VeryUnique::default(),
+ "A new pack of somewhat unusual upgrades for R.E.P.O.!",
+ );
+ }
+
+ #[test]
+ fn fix_quite_unique() {
+ assert_good_and_bad_suggestions(
+ "Now I understand that this is quite unique to insta and if it's not useful I am also going to investigate alternatives",
+ VeryUnique::default(),
+ &[
+ "Now I understand that this is unique to insta and if it's not useful I am also going to investigate alternatives",
+ ],
+ &[],
+ );
+ }
+
+ #[test]
+ fn fix_rather_unique() {
+ assert_top3_suggestion_result(
+ "I regret using the Vue compiler because the resulting AST is rather unique.",
+ VeryUnique::default(),
+ "I regret using the Vue compiler because the resulting AST is rather unusual.",
+ );
+ }
+}