Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: added descriptions to each individual lint rule
Elijah Potterd8227828e672
diff --git a/harper-core/src/linting/an_a.rs b/harper-core/src/linting/an_a.rsindex c7c008ba4..fe1b0bb37 100644--- a/harper-core/src/linting/an_a.rs+++ b/harper-core/src/linting/an_a.rs@@ -62,6 +62,10 @@ impl Linter for AnA {lints}++ fn description(&self) -> &'static str {+ "A rule that looks for incorrect indefinite articles. For example, \"this is an mule\" would be flagged as incorrect."+ }}fn to_lower_word(word: &[char]) -> Cow<'_, [char]> {diff --git a/harper-core/src/linting/avoid_curses.rs b/harper-core/src/linting/avoid_curses.rsindex ec7e85e64..717327c8c 100644--- a/harper-core/src/linting/avoid_curses.rs+++ b/harper-core/src/linting/avoid_curses.rs@@ -18,6 +18,10 @@ impl Linter for AvoidCurses {}).collect()}++ fn description(&self) -> &'static str {+ "A rule that looks for common offensive language."+ }}#[cfg(test)]diff --git a/harper-core/src/linting/boring_words.rs b/harper-core/src/linting/boring_words.rsindex b56900be8..1e30b3eb1 100644--- a/harper-core/src/linting/boring_words.rs+++ b/harper-core/src/linting/boring_words.rs@@ -44,4 +44,8 @@ impl PatternLinter for BoringWords {priority: 127,}}++ fn description(&self) -> &'static str {+ "This rule looks for particularly boring or overused words. Using varied language is an easy way to keep a reader's attention."+ }}diff --git a/harper-core/src/linting/capitalize_personal_pronouns.rs b/harper-core/src/linting/capitalize_personal_pronouns.rsindex 849d0d814..04a884d68 100644--- a/harper-core/src/linting/capitalize_personal_pronouns.rs+++ b/harper-core/src/linting/capitalize_personal_pronouns.rs@@ -34,6 +34,10 @@ impl Linter for CapitalizePersonalPronouns {lints}++ fn description(&self) -> &'static str {+ "Forgetting to capitalize personal pronouns, like \"I\" or \"I'm\" is one of the most common errors. This rule helps with that."+ }}#[cfg(test)]diff --git a/harper-core/src/linting/correct_number_suffix.rs b/harper-core/src/linting/correct_number_suffix.rsindex bc06b7004..0c53a786e 100644--- a/harper-core/src/linting/correct_number_suffix.rs+++ b/harper-core/src/linting/correct_number_suffix.rs@@ -31,6 +31,10 @@ impl Linter for CorrectNumberSuffix {output}++ fn description(&self) -> &'static str {+ "When making quick edits, it is common for authors to change the value of a number without changing its suffix. This rule looks for these cases, for example: \"2st\"."+ }}#[cfg(test)]diff --git a/harper-core/src/linting/dot_initialisms.rs b/harper-core/src/linting/dot_initialisms.rsindex 05099f3bc..5f01de428 100644--- a/harper-core/src/linting/dot_initialisms.rs+++ b/harper-core/src/linting/dot_initialisms.rs@@ -51,6 +51,10 @@ impl PatternLinter for DotInitialisms {priority: 63,}}++ fn description(&self) -> &'static str {+ "Ensures common initialisms (like \"i.e.\") are properly dot-separated."+ }}#[cfg(test)]diff --git a/harper-core/src/linting/ellipsis_length.rs b/harper-core/src/linting/ellipsis_length.rsindex 493e0ad30..c91757e9c 100644--- a/harper-core/src/linting/ellipsis_length.rs+++ b/harper-core/src/linting/ellipsis_length.rs@@ -35,6 +35,10 @@ impl Linter for EllipsisLength {lints}++ fn description(&self) -> &'static str {+ "Make sure you have the correct number of dots in your ellipsis."+ }}#[cfg(test)]diff --git a/harper-core/src/linting/linking_verbs.rs b/harper-core/src/linting/linking_verbs.rsindex dadb4fded..55ddeb21e 100644--- a/harper-core/src/linting/linking_verbs.rs+++ b/harper-core/src/linting/linking_verbs.rs@@ -34,6 +34,10 @@ impl Linter for LinkingVerbs {output}++ fn description(&self) -> &'static str {+ "Linking verbs connect nouns to other ideas. Make sure you do not accidentaly link words that aren't nouns."+ }}#[cfg(test)]diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 728679e9b..d2ecd7ca7 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -29,6 +29,16 @@ use crate::{Dictionary, Document};macro_rules! create_lint_group_config {($($linter:ident => $default:expr),*) => {paste! {+ /// A collection of all the descriptions from the composing linters.+ #[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]+ pub struct LintGroupDescriptions<'a> {+ $(+ #[doc = "The description for the [`" $linter "`] linter."]+ pub [<$linter:snake>]: &'a str,+ )*+ pub spell_check: &'a str+ }+#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]pub struct LintGroupConfig {$(@@ -85,10 +95,19 @@ macro_rules! create_lint_group_config {config,}}++ pub fn all_descriptions(&self) -> LintGroupDescriptions<'_> {+ LintGroupDescriptions {+ $(+ [<$linter:snake>]: self.[<$linter:snake>].description(),+ )*+ spell_check: self.spell_check.description(),+ }+ }}impl<T: Dictionary> Linter for LintGroup<T> {- fn lint(&mut self, document: &Document) -> Vec<Lint>{+ fn lint(&mut self, document: &Document) -> Vec<Lint> {let mut lints = Vec::new();let mut config = self.config.clone();@@ -107,6 +126,10 @@ macro_rules! create_lint_group_config {lints}++ fn description(&self) -> &'static str {+ "A collection of linters that can be run as one."+ }}}};@@ -141,3 +164,16 @@ impl<T: Dictionary + Default> Default for LintGroup<T> {Self::new(LintGroupConfig::default(), T::default())}}++#[cfg(test)]+mod tests {+ use crate::FullDictionary;++ use super::LintGroup;++ #[test]+ fn can_get_all_descriptions() {+ let group = LintGroup::<FullDictionary>::default();+ group.all_descriptions();+ }+}diff --git a/harper-core/src/linting/long_sentences.rs b/harper-core/src/linting/long_sentences.rsindex 005a842f0..e877f2591 100644--- a/harper-core/src/linting/long_sentences.rs+++ b/harper-core/src/linting/long_sentences.rs@@ -25,4 +25,9 @@ impl Linter for LongSentences {output}++ fn description(&self) -> &'static str {+ "This rule looks for run-on sentences, which can make your work harder to grok.+"+ }}diff --git a/harper-core/src/linting/matcher.rs b/harper-core/src/linting/matcher.rsindex 827d22d43..2a0a109b1 100644--- a/harper-core/src/linting/matcher.rs+++ b/harper-core/src/linting/matcher.rs@@ -364,6 +364,10 @@ impl Linter for Matcher {lints}++ fn description(&self) -> &'static str {+ "A collection of curated rules. A catch-all that will be removed in the future."+ }}#[cfg(test)]diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 70d6d237e..37aca7458 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -55,10 +55,12 @@ use crate::Document;#[cfg(not(feature = "concurrent"))]pub trait Linter {fn lint(&mut self, document: &Document) -> Vec<Lint>;+ fn description(&self) -> &str;}#[cfg(feature = "concurrent")]pub trait Linter: Send + Sync {fn lint(&mut self, document: &Document) -> Vec<Lint>;+ fn description(&self) -> &str;}#[cfg(test)]diff --git a/harper-core/src/linting/multiple_sequential_pronouns.rs b/harper-core/src/linting/multiple_sequential_pronouns.rsindex 03282cb5c..1221ecef4 100644--- a/harper-core/src/linting/multiple_sequential_pronouns.rs+++ b/harper-core/src/linting/multiple_sequential_pronouns.rs@@ -61,6 +61,10 @@ impl PatternLinter for MultipleSequentialPronouns {suggestions,}}++ fn description(&self) -> &'static str {+ "When editing work to change point of view (i.e. first-person or third-person) it is common to add pronouns while neglecting to remove old ones. This rule catches cases where you have multiple disparate pronouns in sequence."+ }}impl Default for MultipleSequentialPronouns {diff --git a/harper-core/src/linting/number_suffix_capitalization.rs b/harper-core/src/linting/number_suffix_capitalization.rsindex 4d617b4f9..350b198be 100644--- a/harper-core/src/linting/number_suffix_capitalization.rs+++ b/harper-core/src/linting/number_suffix_capitalization.rs@@ -33,6 +33,10 @@ impl Linter for NumberSuffixCapitalization {output}++ fn description(&self) -> &'static str {+ "You should never capitalize number suffixes."+ }}#[cfg(test)]diff --git a/harper-core/src/linting/pattern_linter.rs b/harper-core/src/linting/pattern_linter.rsindex fc2cb26fe..4fed20727 100644--- a/harper-core/src/linting/pattern_linter.rs+++ b/harper-core/src/linting/pattern_linter.rs@@ -7,6 +7,7 @@ pub trait PatternLinter {/// A simple getter for the pattern to be searched for.fn pattern(&self) -> &dyn Pattern;fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint;+ fn description<'a>(&'a self) -> &'a str;}#[cfg(feature = "concurrent")]@@ -14,6 +15,7 @@ pub trait PatternLinter: Send + Sync {/// A simple getter for the pattern to be searched for.fn pattern(&self) -> &dyn Pattern;fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint;+ fn description(&self) -> &str;}impl<L> Linter for L@@ -48,4 +50,8 @@ wherelints}++ fn description(&self) -> &str {+ self.description()+ }}diff --git a/harper-core/src/linting/repeated_words.rs b/harper-core/src/linting/repeated_words.rsindex 8ccd7ffe0..068e6053d 100644--- a/harper-core/src/linting/repeated_words.rs+++ b/harper-core/src/linting/repeated_words.rs@@ -63,6 +63,10 @@ impl Linter for RepeatedWords {lints}++ fn description(&self) -> &'static str {+ "This rule looks for repetitions of words that are not homographs."+ }}#[cfg(test)]diff --git a/harper-core/src/linting/sentence_capitalization.rs b/harper-core/src/linting/sentence_capitalization.rsindex 33d8371b8..449cb331e 100644--- a/harper-core/src/linting/sentence_capitalization.rs+++ b/harper-core/src/linting/sentence_capitalization.rs@@ -60,6 +60,10 @@ impl Linter for SentenceCapitalization {lints}++ fn description(&self) -> &'static str {+ "The opening word of a sentence should almost always be capitalized."+ }}fn is_full_sentence(toks: &[Token]) -> bool {diff --git a/harper-core/src/linting/spaces.rs b/harper-core/src/linting/spaces.rsindex 5da5eac11..f9faa0872 100644--- a/harper-core/src/linting/spaces.rs+++ b/harper-core/src/linting/spaces.rs@@ -61,6 +61,10 @@ impl Linter for Spaces {output}++ fn description(&self) -> &'static str {+ "Words should be separated by at most one space."+ }}#[cfg(test)]diff --git a/harper-core/src/linting/spell_check.rs b/harper-core/src/linting/spell_check.rsindex f19d99bbc..3f5f7c6a7 100644--- a/harper-core/src/linting/spell_check.rs+++ b/harper-core/src/linting/spell_check.rs@@ -93,4 +93,8 @@ impl<T: Dictionary> Linter for SpellCheck<T> {lints}++ fn description(&self) -> &'static str {+ "Looks and provides corrections for misspelled words."+ }}diff --git a/harper-core/src/linting/spelled_numbers.rs b/harper-core/src/linting/spelled_numbers.rsindex e9fc5fd70..8824d78fb 100644--- a/harper-core/src/linting/spelled_numbers.rs+++ b/harper-core/src/linting/spelled_numbers.rs@@ -29,6 +29,10 @@ impl Linter for SpelledNumbers {lints}++ fn description(&self) -> &'static str {+ "Most style guides recommend that you spell out numbers less than ten."+ }}/// Converts a number to it's spelled-out variant.diff --git a/harper-core/src/linting/terminating_conjunctions.rs b/harper-core/src/linting/terminating_conjunctions.rsindex dac69bf0c..9b0bb5c2b 100644--- a/harper-core/src/linting/terminating_conjunctions.rs+++ b/harper-core/src/linting/terminating_conjunctions.rs@@ -68,6 +68,10 @@ impl PatternLinter for TerminatingConjunctions {priority: 63,}}++ fn description(&self) -> &'static str {+ "Subordinating conjunctions are words that create a grammatical space for another idea or clause. As such, they should never appear at the end of a clause."+ }}#[cfg(test)]diff --git a/harper-core/src/linting/that_which.rs b/harper-core/src/linting/that_which.rsindex 65357a922..ac152ad08 100644--- a/harper-core/src/linting/that_which.rs+++ b/harper-core/src/linting/that_which.rs@@ -56,6 +56,10 @@ impl PatternLinter for ThatWhich {priority: 126,}}++ fn description(&self) -> &'static str {+ "Repeating the word \"that\" twice is often redundent. \"That which\" is easier to read."+ }}#[cfg(test)]diff --git a/harper-core/src/linting/unclosed_quotes.rs b/harper-core/src/linting/unclosed_quotes.rsindex 0b1087540..ac5504312 100644--- a/harper-core/src/linting/unclosed_quotes.rs+++ b/harper-core/src/linting/unclosed_quotes.rs@@ -25,4 +25,8 @@ impl Linter for UnclosedQuotes {lints}++ fn description(&self) -> &'static str {+ "Quotation marks should always be closed. Unpaired quotation marks are a hallmark of sloppy work."+ }}diff --git a/harper-core/src/linting/use_genitive.rs b/harper-core/src/linting/use_genitive.rsindex 7c7abf996..c6d61b521 100644--- a/harper-core/src/linting/use_genitive.rs+++ b/harper-core/src/linting/use_genitive.rs@@ -67,6 +67,10 @@ impl PatternLinter for UseGenitive {priority: 31,}}++ fn description(&self) -> &'static str {+ "Looks situations where the genitive case of \"there\" should be used."+ }}impl Default for UseGenitive {diff --git a/harper-core/src/linting/wrong_quotes.rs b/harper-core/src/linting/wrong_quotes.rsindex e22e5848f..50ae3a43b 100644--- a/harper-core/src/linting/wrong_quotes.rs+++ b/harper-core/src/linting/wrong_quotes.rs@@ -13,6 +13,10 @@ impl Linter for WrongQuotes {.filter_map(|(quote_idx, quote_token)| lint_quote(document, quote_idx, quote_token)).collect()}++ fn description(&self) -> &'static str {+ "The key on the keyboard often used as a quotation mark is actually a double-apostrophe. Use the correct character."+ }}fn lint_quote(document: &Document, quote_idx: usize, quote_token: Token) -> Option<Lint> {diff --git a/harper-wasm/src/lib.rs b/harper-wasm/src/lib.rsindex aeea7fbf2..563e92937 100644--- a/harper-wasm/src/lib.rs+++ b/harper-wasm/src/lib.rs@@ -79,6 +79,11 @@ impl Linter {document.to_string()}+ /// Get a JSON map containing the descriptions of all the linting rules.+ pub fn get_lint_descriptions_as_json(&self) -> String {+ serde_json::to_string(&self.lint_group.all_descriptions()).unwrap()+ }+pub fn get_lint_config_as_json(&self) -> String {serde_json::to_string(&self.lint_group.config).unwrap()}@@ -88,6 +93,11 @@ impl Linter {Ok(())}+ /// Get a Record containing the descriptions of all the linting rules.+ pub fn get_lint_descriptions_as_object(&self) -> JsValue {+ serde_wasm_bindgen::to_value(&self.lint_group.all_descriptions()).unwrap()+ }+pub fn get_lint_config_as_object(&self) -> JsValue {// Important for downstream JSON serializationlet serializer = serde_wasm_bindgen::Serializer::json_compatible();diff --git a/packages/harper.js/src/Linter.test.ts b/packages/harper.js/src/Linter.test.tsindex 571f1754b..ef7086616 100644--- a/packages/harper.js/src/Linter.test.ts+++ b/packages/harper.js/src/Linter.test.ts@@ -102,6 +102,25 @@ for (const [linterName, Linter] of Object.entries(linters)) {expect(titleCase).toBe('This Is a Test for Making Titles');});++ test(`${linterName} can get rule descriptions`, async () => {+ const linter = new Linter();++ const descriptions = await linter.getLintDescriptions();++ expect(descriptions).toBeTypeOf('object');+ });++ test(`${linterName} rule descriptions are not empty`, async () => {+ const linter = new Linter();++ const descriptions = await linter.getLintDescriptions();++ for (const value of Object.values(descriptions)) {+ expect(value).toBeTypeOf('string');+ expect(value).not.toHaveLength(0);+ }+ });}test('Linters have the same config format', async () => {diff --git a/packages/harper.js/src/Linter.ts b/packages/harper.js/src/Linter.tsindex 2d4894142..6febf738f 100644--- a/packages/harper.js/src/Linter.ts+++ b/packages/harper.js/src/Linter.ts@@ -34,6 +34,12 @@ export default interface Linter {/** Set the linter's current configuration from JSON. */setLintConfigWithJSON(config: string): Promise<void>;+ /** Get the linting rule descriptions as a JSON map. */+ getLintDescriptionsAsJSON(): Promise<string>;++ /** Get the linting rule descriptions as an object */+ getLintDescriptions(): Promise<Record<string, string>>;+/** Convert a string to Chicago-style title case. */toTitleCase(text: string): Promise<string>;}diff --git a/packages/harper.js/src/LocalLinter.ts b/packages/harper.js/src/LocalLinter.tsindex a6a28ae38..8f9031e0c 100644--- a/packages/harper.js/src/LocalLinter.ts+++ b/packages/harper.js/src/LocalLinter.ts@@ -74,4 +74,14 @@ export default class LocalLinter implements Linter {const wasm = await loadWasm();return wasm.to_title_case(text);}++ async getLintDescriptions(): Promise<Record<string, string>> {+ await this.initialize();+ return this.inner!.get_lint_descriptions_as_object();+ }++ async getLintDescriptionsAsJSON(): Promise<string> {+ await this.initialize();+ return this.inner!.get_lint_descriptions_as_json();+ }}diff --git a/packages/harper.js/src/WorkerLinter/index.ts b/packages/harper.js/src/WorkerLinter/index.tsindex bbfd63e35..7710140c7 100644--- a/packages/harper.js/src/WorkerLinter/index.ts+++ b/packages/harper.js/src/WorkerLinter/index.ts@@ -98,6 +98,14 @@ export default class WorkerLinter implements Linter {return this.rpc('toTitleCase', [text]);}+ getLintDescriptionsAsJSON(): Promise<string> {+ return this.rpc('getLintDescriptionsAsJSON', []);+ }++ async getLintDescriptions(): Promise<Record<string, string>> {+ return JSON.parse(await this.getLintDescriptionsAsJSON()) as Record<string, string>;+ }+/** Run a procedure on the remote worker. */private async rpc(procName: string, args: any[]): Promise<any> {const promise = new Promise((resolve, reject) => {