Skip to content

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

feat: their self / selves → themself / themselves (#4328)

Andrew Dunbar5f14d9c4b40a
diff --git a/harper-core/default_config.json b/harper-core/default_config.json
index bd64bd8e9..1b7261d02 100644
--- a/harper-core/default_config.json
+++ b/harper-core/default_config.json
@@ -5886,6 +5886,13 @@
"label": "The Last Days"
}
},
+ {
+ "Bool": {
+ "name": "Themself",
+ "state": true,
+ "label": "Themself"
+ }
+ },
{
"Bool": {
"name": "TheWhetherWeather",
diff --git a/harper-core/src/linting/lint_group/mod.rs b/harper-core/src/linting/lint_group/mod.rs
index a90c08f0e..bc28d7727 100644
--- a/harper-core/src/linting/lint_group/mod.rs
+++ b/harper-core/src/linting/lint_group/mod.rs
@@ -287,6 +287,7 @@ use super::the_my::TheMy;
use super::the_point_for::ThePointFor;
use super::the_proper_noun_possessive::TheProperNounPossessive;
use super::the_the_to_that_the::TheTheToThatThe;
+use super::themself::Themself;
use super::then_than::ThenThan;
use super::there_is_agreement::ThereIsAgreement;
use super::there_own::ThereOwn;
@@ -907,6 +908,7 @@ impl LintGroup {
insert_expr_rule!(ThePointFor);
insert_expr_rule!(TheProperNounPossessive);
insert_expr_rule!(TheTheToThatThe);
+ insert_expr_rule!(Themself);
insert_expr_rule!(ThenThan);
insert_expr_rule!(ThereOwn);
insert_expr_rule!(Theres);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 42f7f50e0..156c0debc 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -306,6 +306,7 @@ mod the_my;
mod the_point_for;
mod the_proper_noun_possessive;
mod the_the_to_that_the;
+mod themself;
mod then_than;
mod there_is_agreement;
mod there_own;
diff --git a/harper-core/src/linting/themself.rs b/harper-core/src/linting/themself.rs
new file mode 100644
index 000000000..eb9d1d6aa
--- /dev/null
+++ b/harper-core/src/linting/themself.rs
@@ -0,0 +1,125 @@
+use crate::{
+ Lint, Token, TokenStringExt,
+ expr::{All, Expr, FirstMatchOf, SequenceExpr, UnlessStep},
+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},
+};
+
+pub struct Themself {
+ expr: FirstMatchOf,
+}
+
+impl Default for Themself {
+ fn default() -> Self {
+ Self {
+ expr: FirstMatchOf::new([
+ Box::new(SequenceExpr::aco("their").t_ws().t_aco("selves")) as Box<dyn Expr>,
+ Box::new(All::new([
+ Box::new(SequenceExpr::aco("their").t_ws().t_aco("self")) as Box<dyn Expr>,
+ Box::new(UnlessStep::new(
+ SequenceExpr::anything().t_any().t_any().then_any_of([
+ Box::new(SequenceExpr::default().then_hyphen()),
+ Box::new(SequenceExpr::whitespace().then(|t: &Token, _: &[char]| {
+ t.kind.is_noun()
+ || t.kind.is_verb_past_form()
+ || t.kind.is_verb_past_participle_form()
+ })),
+ ]),
+ |_: &Token, _: &[char]| true,
+ )),
+ ])),
+ ]),
+ }
+ }
+}
+
+impl ExprLinter for Themself {
+ type Unit = Chunk;
+
+ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
+ let refl = toks.get(2)?.get_ch(src);
+
+ let span = toks.span()?;
+
+ let suggestions = vec![Suggestion::replace_with_match_case_str(
+ if refl.len() == 4 {
+ "themself"
+ } else {
+ "themselves"
+ },
+ span.get_content(src),
+ )];
+
+ Some(Lint {
+ span,
+ lint_kind: LintKind::Usage,
+ suggestions,
+ message: "This should be one word, not two.".to_owned(),
+ ..Default::default()
+ })
+ }
+
+ fn expr(&self) -> &dyn Expr {
+ &self.expr
+ }
+
+ fn description(&self) -> &str {
+ "Corrects `their self` and `their selves` to `themself` and `themselves`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::tests::{assert_no_lints, assert_suggestion_result};
+
+ use super::Themself;
+
+ #[test]
+ fn fix_respect_their_self() {
+ assert_suggestion_result(
+ "You can't respect someone who doesn't respect their self.",
+ Themself::default(),
+ "You can't respect someone who doesn't respect themself.",
+ );
+ }
+
+ #[test]
+ fn dont_flag_self_hyphen() {
+ assert_no_lints(
+ "For most renaissance artists, their self-portraits are far from photorealistic.",
+ Themself::default(),
+ );
+ }
+
+ #[test]
+ fn fix_their_selves() {
+ assert_suggestion_result(
+ "Constructedness: see \"people as forming and reforming their selves within each relationship\".",
+ Themself::default(),
+ "Constructedness: see \"people as forming and reforming themselves within each relationship\".",
+ );
+ }
+
+ #[test]
+ fn dont_flag_self_hosted() {
+ assert_no_lints(
+ "ZeroTier is certainly another viable alternative, but their self hosted option still relies on the ZeroTier root servers",
+ Themself::default(),
+ );
+ }
+
+ #[test]
+ fn dont_flag_self_interest() {
+ assert_no_lints(
+ "this state that everyone is acting in their self interest is not something we should promote",
+ Themself::default(),
+ );
+ }
+
+ #[test]
+ fn dont_flag_self_proclaimed() {
+ assert_no_lints(
+ "Which is why they feel threatened by anything that challenges their self proclaimed uniquely human trait.",
+ Themself::default(),
+ );
+ }
+}