Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: created linter for offensive language
Elijah Potter8189f740d87e
diff --git a/harper-core/affixes.json b/harper-core/affixes.jsonindex efab51053..fc87f8b94 100644--- a/harper-core/affixes.json+++ b/harper-core/affixes.json@@ -525,6 +525,17 @@"gifts_metadata": {"adjective": {}}+ },+ "6": {+ "suffix": true,+ "cross_product": true,+ "replacements": [],+ "adds_metadata": {+ "swear": true+ },+ "gifts_metadata": {+ "swear": true+ }}}}diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dictindex 3de2b6a4c..841a644f4 100644--- a/harper-core/dictionary.dict+++ b/harper-core/dictionary.dict@@ -12705,7 +12705,7 @@ aspiration/M1aspirator/SM1aspire/GDS4aspirin/MS1-ass/MS15+ass/MS156assail/GBDS4assailable/U5assailant/SM15@@ -12736,7 +12736,7 @@ assessor/MS1asset/MS1asseverate/DSGN4asseveration/M1-asshole/MS!1+asshole/MS!16assiduity/M1assiduous/PY5assiduousness/M1@@ -17202,7 +17202,7 @@ cockney/SM152cockpit/SM1cockroach/MS1cockscomb/SM1-cocksucker/MS!1+cocksucker/MS!16cocksure/5cocktail/MS154cocky/RTP514@@ -19072,7 +19072,7 @@ cumulus/M1cuneiform/M51cunnilingus/M1cunning/MRYT51-cunt/MS!145+cunt/MS!1456cup/SM14cupboard/SM14cupcake/MS14@@ -20159,7 +20159,7 @@ dichotomous/5dichotomy/SM1dicier/5diciest/5-dick/MRXZS14+dick/MRXZS146dicker/DG41dickey/SM15dickhead/S1@@ -23103,7 +23103,7 @@ farrow/SMDG145farseeing/5farsighted/P5farsightedness/M1-fart/MDGS41+fart/MDGS416farther/54farthermost/5farthest/5@@ -24534,9 +24534,9 @@ fryer/SM1ft/1ftp/ZGS5124fuchsia/MS15-fuck/SMGDRZ!41-fucker/M!1-fuckhead/S!1+fuck/SMGDRZ!416+fucker/M!16+fuckhead/S!16fuddle/DSMG41fudge/DSMG14fuehrer/MS1@@ -27132,7 +27132,7 @@ horsemen/1horseplay/M14horsepower/M1horseradish/MS1-horseshit/!1+horseshit/!16horseshoe/DSM14horseshoeing/4horsetail/SM1@@ -32731,7 +32731,7 @@ moth/M14mothball/GMDS14mother/MDYSG14motherboard/SM1-motherfucker/MS!1+motherfucker/MS!16motherfucking/!5motherhood/M1motherland/MS1@@ -36620,7 +36620,7 @@ piroshki/M1pirouette/DSMG14piscatorial/5pismire/SM1-piss/ZGMDRS14+piss/ZGMDRS146pissoir/S1pistachio/SM1piste/S1@@ -41661,13 +41661,13 @@ shirtsleeve/SM1shirttail/SM1shirtwaist/MS1shirty/5-shit/SM!154-shitfaced/!5-shithead/S!1-shitload/!1-shitted/!4-shitting/!145-shitty/RT!5+shit/SM!1546+shitfaced/!56+shithead/S!16+shitload/!16+shitted/!46+shitting/!1456+shitty/RT!56shiv/ZMRS14shiver/MDG41shivery/5@@ -46774,7 +46774,7 @@ turboprop/SM1turbot/SM1turbulence/M1turbulent/Y5-turd/MS14+turd/MS146turducken/SM1tureen/SM1turf/MDSG14@@ -46831,7 +46831,7 @@ twain/M514twang/SMDG14twangy/RT5twas/-twat/S14+twat/S146tweak/SMDG41twee/51tweed/SM1@@ -48830,7 +48830,7 @@ whop/S41whopped/4whopper/SM1whopping/541-whore/SMG14+whore/SMG146whorehouse/MS1whoreish/5whorish/5diff --git a/harper-core/src/linting/avoid_curses.rs b/harper-core/src/linting/avoid_curses.rsnew file mode 100644index 000000000..ee2e961e4--- /dev/null+++ b/harper-core/src/linting/avoid_curses.rs@@ -0,0 +1,32 @@+use super::{Lint, LintKind, Linter};+use crate::{Document, TokenStringExt};++#[derive(Debug, Default)]+pub struct AvoidCurses;++impl Linter for AvoidCurses {+ fn lint(&mut self, document: &Document) -> Vec<Lint> {+ document+ .iter_words()+ .filter(|t| t.kind.is_swear())+ .map(|t| Lint {+ span: t.span,+ lint_kind: LintKind::Miscellaneous,+ suggestions: vec![],+ message: "Try to avoid offensive language.".to_string(),+ priority: 63+ })+ .collect()+ }+}++#[cfg(test)]+mod tests {+ use super::AvoidCurses;+ use crate::linting::tests::assert_lint_count;++ #[test]+ fn detects_shit() {+ assert_lint_count("He ate shit when he fell off the bike.", AvoidCurses, 1);+ }+}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex cc7bdcdb1..d07669a4e 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -2,6 +2,7 @@ use paste::paste;use serde::{Deserialize, Serialize};use super::an_a::AnA;+use super::avoid_curses::AvoidCurses;use super::correct_number_suffix::CorrectNumberSuffix;use super::linking_verbs::LinkingVerbs;use super::long_sentences::LongSentences;@@ -107,7 +108,8 @@ create_lint_group_config!(CorrectNumberSuffix => true,NumberSuffixCapitalization => true,MultipleSequentialPronouns => true,- LinkingVerbs => false+ LinkingVerbs => false,+ AvoidCurses => true);impl<T: Dictionary + Default> Default for LintGroup<T> {diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 65f09639b..49d88ecab 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -1,4 +1,5 @@mod an_a;+mod avoid_curses;mod correct_number_suffix;mod linking_verbs;mod lint;diff --git a/harper-core/src/token.rs b/harper-core/src/token.rsindex 835deead7..030686ef1 100644--- a/harper-core/src/token.rs+++ b/harper-core/src/token.rs@@ -73,6 +73,16 @@ impl TokenKind {matches!(self, TokenKind::Punctuation(Punctuation::Pipe))}+ pub fn is_swear(&self) -> bool {+ matches!(+ self,+ TokenKind::Word(WordMetadata {+ swear: Some(true),+ ..+ })+ )+ }+/// Checks that `self` is the same enum variant as `other`, regardless of/// whether the inner metadata is also equal.pub fn matches_variant_of(&self, other: &Self) -> bool {diff --git a/harper-core/src/word_metadata.rs b/harper-core/src/word_metadata.rsindex 8891dc8bd..68e38a711 100644--- a/harper-core/src/word_metadata.rs+++ b/harper-core/src/word_metadata.rs@@ -3,10 +3,11 @@ use serde::{Deserialize, Serialize};#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Hash)]pub struct WordMetadata {- noun: Option<NounData>,- verb: Option<VerbData>,- adjective: Option<AdjectiveData>,- adverb: Option<AdverbData>+ pub noun: Option<NounData>,+ pub verb: Option<VerbData>,+ pub adjective: Option<AdjectiveData>,+ pub adverb: Option<AdverbData>,+ pub swear: Option<bool>}impl WordMetadata {@@ -16,7 +17,8 @@ impl WordMetadata {noun: self.noun.or(other.noun),verb: self.verb.or(other.verb),adjective: self.adjective.or(other.adjective),- adverb: self.adverb.or(other.adverb)+ adverb: self.adverb.or(other.adverb),+ swear: self.swear.or(other.swear)}}@@ -76,6 +78,11 @@ impl WordMetadata {)}+ /// Checks whether a word is _definitely_ a swear.+ pub fn is_swear(&self) -> bool {+ matches!(self.swear, Some(true))+ }+/// Same thing as [`Self::or`], except in-place rather than a copy.pub fn append(&mut self, other: &Self) -> &mut Self {*self = self.or(other);diff --git a/harper-ls/README.md b/harper-ls/README.mdindex 5764a1c3e..cafd096cc 100644--- a/harper-ls/README.md+++ b/harper-ls/README.md@@ -75,7 +75,8 @@ lspconfig.harper_ls.setup {correct_number_suffix = true,number_suffix_capitalization = true,multiple_sequential_pronouns = true,- linking_verbs = false+ linking_verbs = false,+ avoid_curses = true,}}},diff --git a/packages/vscode-plugin/package.json b/packages/vscode-plugin/package.jsonindex 6603ccacf..0e3972c2e 100644--- a/packages/vscode-plugin/package.json+++ b/packages/vscode-plugin/package.json@@ -137,6 +137,12 @@"default": true,"description": "Detect improper use of linking verbs."},+ "harper-ls.linters.avoid_curses": {+ "scope": "resource",+ "type": "boolean",+ "default": true,+ "description": "Catch use of curse/swear words."+ },"harper-ls.diagnosticSeverity": {"scope": "resource","type": "string",