Skip to content

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

feat: except of→except for / exception of (#2997)

Andrew Dunbar0e31eb336761
diff --git a/harper-core/src/linting/except_of.rs b/harper-core/src/linting/except_of.rs
new file mode 100644
index 000000000..1e4cc552d
--- /dev/null
+++ b/harper-core/src/linting/except_of.rs
@@ -0,0 +1,125 @@
+use crate::{
+ CharStringExt, Lint, Token,
+ expr::{Expr, FixedPhrase},
+ linting::{
+ ExprLinter, LintKind, Suggestion,
+ expr_linter::{Chunk, followed_by_word, preceded_by_word},
+ },
+};
+
+pub struct ExceptOf {
+ expr: FixedPhrase,
+}
+
+impl Default for ExceptOf {
+ fn default() -> Self {
+ Self {
+ expr: FixedPhrase::from_phrase("except of"),
+ }
+ }
+}
+
+impl ExprLinter for ExceptOf {
+ type Unit = Chunk;
+
+ fn match_to_lint_with_context(
+ &self,
+ toks: &[Token],
+ src: &[char],
+ ctx: Option<(&[Token], &[Token])>,
+ ) -> Option<Lint> {
+ if followed_by_word(ctx, |t| {
+ t.span.get_content(src).eq_ignore_ascii_case_str("course")
+ }) {
+ return None;
+ }
+
+ let (span, replacement, msg) = if preceded_by_word(ctx, |t| {
+ t.span
+ .get_content(src)
+ .eq_any_ignore_ascii_case_str(&["the", "notable", "possible"])
+ }) {
+ (
+ toks[0].span,
+ "exception",
+ "This usage requires the noun 'exception'. Use 'exception of' instead of 'except of'",
+ )
+ } else {
+ (
+ toks[2].span,
+ "for",
+ "Use `except for` instead of `except of`",
+ )
+ };
+
+ Some(Lint {
+ span,
+ lint_kind: LintKind::Usage,
+ suggestions: vec![Suggestion::replace_with_match_case_str(
+ replacement,
+ span.get_content(src),
+ )],
+ message: msg.to_string(),
+ ..Default::default()
+ })
+ }
+
+ fn description(&self) -> &str {
+ "Corrects `except of` to `except for` or `exception of`."
+ }
+
+ fn expr(&self) -> &dyn Expr {
+ &self.expr
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::tests::{assert_no_lints, assert_suggestion_result};
+
+ use super::ExceptOf;
+
+ #[test]
+ fn fix_the_except_of() {
+ assert_suggestion_result(
+ "With the except of one particular sender, all the other senders are sending correctly to the configured smarthost.",
+ ExceptOf::default(),
+ "With the exception of one particular sender, all the other senders are sending correctly to the configured smarthost.",
+ );
+ }
+
+ #[test]
+ fn fix_possible_except_of() {
+ assert_suggestion_result(
+ "With the possible except of two vertexes with one having one more in-degree than out-degree",
+ ExceptOf::default(),
+ "With the possible exception of two vertexes with one having one more in-degree than out-degree",
+ );
+ }
+
+ #[test]
+ fn fix_notable_except_of() {
+ assert_suggestion_result(
+ "identical at the end of the day to never logging into it again (with the notable except of potentially identifying info you've left in your profile, of course)",
+ ExceptOf::default(),
+ "identical at the end of the day to never logging into it again (with the notable exception of potentially identifying info you've left in your profile, of course)",
+ );
+ }
+
+ #[test]
+ fn ignore_except_of_course() {
+ assert_no_lints(
+ "much like the existing config option access_log_format, except of course for the error log (option errorlog) output format.",
+ ExceptOf::default(),
+ );
+ }
+
+ #[test]
+ fn fix_except_of() {
+ assert_suggestion_result(
+ "There are no usable way to restart worker except of supervisorctl restart workername",
+ ExceptOf::default(),
+ "There are no usable way to restart worker except for supervisorctl restart workername",
+ );
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index fc62e6d7f..80cf1ae41 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -68,6 +68,7 @@ use super::ellipsis_length::EllipsisLength;
use super::else_possessive::ElsePossessive;
use super::ever_every::EverEvery;
use super::everyday::Everyday;
+use super::except_of::ExceptOf;
use super::expand_memory_shorthands::ExpandMemoryShorthands;
use super::expand_time_shorthands::ExpandTimeShorthands;
use super::expr_linter::run_on_chunk;
@@ -658,6 +659,7 @@ impl LintGroup {
insert_expr_rule!(ElsePossessive, true);
insert_expr_rule!(EverEvery, true);
insert_expr_rule!(Everyday, true);
+ insert_expr_rule!(ExceptOf, true);
insert_expr_rule!(ExpandMemoryShorthands, true);
insert_expr_rule!(ExpandTimeShorthands, true);
insert_expr_rule!(FarBeIt, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index b899681be..b281ced90 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -63,6 +63,7 @@ mod ellipsis_length;
mod else_possessive;
mod ever_every;
mod everyday;
+mod except_of;
mod expand_memory_shorthands;
mod expand_time_shorthands;
mod expr_linter;