Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
test: make sure linter descriptions are grammatically correct
Elijah Potter1e0b5b08e05b
diff --git a/demo.md b/demo.mdindex 12a015d84..5f20a7aff 100644--- a/demo.md+++ b/demo.md@@ -4,7 +4,7 @@ checkers don't cut it. That s where Harper comes in handy.Harper is an language checker for developers. it can detectimproper capitalization and misspellled words,as well as a number of other issues.-Like if you break up words you shouldn't.+Like if you break up words you shoul dn't.Harper works everywhere, even offline. Since you r datanever leaves your device, you don't ned to worry aout usdiff --git a/harper-comments/tests/language_support_sources/javadoc_complex.java b/harper-comments/tests/language_support_sources/javadoc_complex.javaindex a3ae0b532..054f4c464 100644--- a/harper-comments/tests/language_support_sources/javadoc_complex.java+++ b/harper-comments/tests/language_support_sources/javadoc_complex.java@@ -19,7 +19,7 @@ class TestClass {/*** This doc has a link in it: {@link this sould b ignor} but not tis*- * @param name this is an other test.+ * @param name this is anoher test.*/public static void greet(String name) {System.out.println("Hello " + name + ".");diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dictindex ed3210606..d3753e0b3 100644--- a/harper-core/dictionary.dict+++ b/harper-core/dictionary.dict@@ -28344,7 +28344,7 @@ inimitably/iniquitous/5Yiniquity/1SMinitial/514SGMDY-initialism/1+initialism/1MSinitialization/1initialize/4DSGinitialized/4AU@@ -49636,7 +49636,6 @@ scatterplot/14SMGWikilink/MS1stacktrace/SM1scrollbar/1SM-break-up/1SMsweetgrass/1SMPowerShell/SMWebSocket/SMdiff --git a/harper-core/src/lexing/hostname.rs b/harper-core/src/lexing/hostname.rsindex 6a130726f..5534fbe5f 100644--- a/harper-core/src/lexing/hostname.rs+++ b/harper-core/src/lexing/hostname.rs@@ -1,3 +1,30 @@+use crate::TokenKind;++use super::FoundToken;++/// Lex a hostname token.+pub fn lex_hostname_token(source: &[char]) -> Option<FoundToken> {+ let len = lex_hostname(source)?;++ // Might be word, just skip it.+ if len <= 1 {+ return None;+ }++ if !source.get(1..len - 1)?.contains(&'.') {+ return None;+ }++ if source.get(len - 1) == Some(&'.') {+ return None;+ }++ Some(FoundToken {+ next_index: len,+ token: TokenKind::Hostname,+ })+}+pub fn lex_hostname(source: &[char]) -> Option<usize> {let mut passed_chars = 0;diff --git a/harper-core/src/lexing/mod.rs b/harper-core/src/lexing/mod.rsindex a44919ed0..a2d03ece0 100644--- a/harper-core/src/lexing/mod.rs+++ b/harper-core/src/lexing/mod.rs@@ -2,6 +2,7 @@ mod email_address;mod hostname;mod url;+use hostname::lex_hostname_token;use url::lex_url;use self::email_address::lex_email_address;@@ -27,6 +28,7 @@ pub fn lex_token(source: &[char]) -> Option<FoundToken> {lex_number,lex_url,lex_email_address,+ lex_hostname_token,lex_word,lex_catch,];@@ -161,11 +163,25 @@ fn lex_catch(_source: &[char]) -> Option<FoundToken> {#[cfg(test)]mod tests {+ use super::lex_token;use super::lex_word;+ use super::{FoundToken, TokenKind};#[test]fn lexes_cjk_as_unlintable() {let source: Vec<_> = "世".chars().collect();assert!(lex_word(&source).is_none());}++ #[test]+ fn lexes_youtube_as_hostname() {+ let source: Vec<_> = "YouTube.com".chars().collect();+ assert!(matches!(+ lex_token(&source),+ Some(FoundToken {+ token: TokenKind::Hostname,+ ..+ })+ ));+ }}diff --git a/harper-core/src/linting/an_a.rs b/harper-core/src/linting/an_a.rsindex fe1b0bb37..a76432b37 100644--- a/harper-core/src/linting/an_a.rs+++ b/harper-core/src/linting/an_a.rs@@ -64,7 +64,7 @@ impl Linter for AnA {}fn description(&self) -> &'static str {- "A rule that looks for incorrect indefinite articles. For example, \"this is an mule\" would be flagged as incorrect."+ "A rule that looks for incorrect indefinite articles. For example, `this is an mule` would be flagged as incorrect."}}diff --git a/harper-core/src/linting/compound_words.rs b/harper-core/src/linting/compound_words.rsindex b7d254913..c644f5f63 100644--- a/harper-core/src/linting/compound_words.rs+++ b/harper-core/src/linting/compound_words.rs@@ -51,18 +51,10 @@ impl Linter for CompoundWords {merged_word.extend_from_slice(b_chars);// Check for closed compound words- if self.dict.contains_word(&merged_word) {- potential_compounds.push(merged_word.clone());- }-- // Check for hyphenated compound words- merged_word.clear();- merged_word.extend_from_slice(a_chars);- merged_word.push('-');- merged_word.extend_from_slice(b_chars);-- // Check for closed compound words- if self.dict.contains_word(&merged_word) {+ if self.dict.contains_word(&merged_word)+ && !a.kind.is_common_word()+ && !b.kind.is_common_word()+ {potential_compounds.push(merged_word.clone());}@@ -134,15 +126,6 @@ mod tests {);}- #[test]- fn makeup() {- assert_lint_count(- "She spent a lot of time doing her make up this morning.",- CompoundWords::default(),- 1,- );- }-#[test]fn birthday() {assert_lint_count(@@ -175,7 +158,7 @@ mod tests {assert_suggestion_count("Like if you break up words you shouldn't.",CompoundWords::default(),- 2,+ 0,);}}diff --git a/harper-core/src/linting/correct_number_suffix.rs b/harper-core/src/linting/correct_number_suffix.rsindex 0c53a786e..8d0951560 100644--- a/harper-core/src/linting/correct_number_suffix.rs+++ b/harper-core/src/linting/correct_number_suffix.rs@@ -33,7 +33,7 @@ impl Linter for CorrectNumberSuffix {}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\"."+ "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`."}}diff --git a/harper-core/src/linting/linking_verbs.rs b/harper-core/src/linting/linking_verbs.rsindex 55ddeb21e..60fea3cad 100644--- a/harper-core/src/linting/linking_verbs.rs+++ b/harper-core/src/linting/linking_verbs.rs@@ -36,7 +36,7 @@ impl Linter for LinkingVerbs {}fn description(&self) -> &'static str {- "Linking verbs connect nouns to other ideas. Make sure you do not accidentaly link words that aren't nouns."+ "Linking verbs connect nouns to other ideas. Make sure you do not accidentally link words that aren't nouns."}}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 175e2d040..753df32dc 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -45,6 +45,14 @@ macro_rules! create_lint_group_config {pub spell_check: &'a str}++ impl<'a> LintGroupDescriptions<'a> {+ /// Create a [`Vec`] containing the key-value pairs of this struct.+ pub fn to_vec_pairs(self) -> Vec<(&'static str, &'a str)>{+ vec![$((stringify!([<$linter:snake>]), self.[<$linter:snake>],),)* ("spell_check", self.spell_check)]+ }+ }+#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]pub struct LintGroupConfig {$(@@ -175,7 +183,7 @@ create_lint_group_config!(AppleNames => true,AzureNames => true,CompoundWords => true,- PluralConjugate => true+ PluralConjugate => false);impl<T: Dictionary + Default> Default for LintGroup<T> {@@ -186,13 +194,30 @@ impl<T: Dictionary + Default> Default for LintGroup<T> {#[cfg(test)]mod tests {- use crate::FullDictionary;+ use crate::{linting::Linter, Document, FstDictionary, FullDictionary};- use super::LintGroup;+ use super::{LintGroup, LintGroupConfig};#[test]fn can_get_all_descriptions() {let group = LintGroup::<FullDictionary>::default();group.all_descriptions();}++ #[test]+ fn lint_descriptions_are_clean() {+ let mut group = LintGroup::new(LintGroupConfig::default(), FstDictionary::curated());+ let pairs: Vec<_> = group+ .all_descriptions()+ .to_vec_pairs()+ .into_iter()+ .map(|(a, b)| (a.to_string(), b.to_string()))+ .collect();++ for (key, value) in pairs {+ let doc = Document::new_markdown_curated(&value);+ eprintln!("{key}: {value}");+ assert!(group.lint(&doc).is_empty())+ }+ }}diff --git a/harper-core/src/linting/long_sentences.rs b/harper-core/src/linting/long_sentences.rsindex e877f2591..666d018e0 100644--- a/harper-core/src/linting/long_sentences.rs+++ b/harper-core/src/linting/long_sentences.rs@@ -27,7 +27,6 @@ impl Linter for LongSentences {}fn description(&self) -> &'static str {- "This rule looks for run-on sentences, which can make your work harder to grok.-"+ "This rule looks for run-on sentences, which can make your work harder to grok."}}diff --git a/harper-core/src/linting/that_which.rs b/harper-core/src/linting/that_which.rsindex ac152ad08..dfd1eb6ab 100644--- a/harper-core/src/linting/that_which.rs+++ b/harper-core/src/linting/that_which.rs@@ -58,7 +58,7 @@ impl PatternLinter for ThatWhich {}fn description(&self) -> &'static str {- "Repeating the word \"that\" twice is often redundent. \"That which\" is easier to read."+ "Repeating the word \"that\" twice is often redundant. `That which` is easier to read."}}diff --git a/harper-core/src/token.rs b/harper-core/src/token.rsindex ef82c703f..b8ef580e0 100644--- a/harper-core/src/token.rs+++ b/harper-core/src/token.rs@@ -327,6 +327,14 @@ impl TokenKind {metadata.is_not_plural_noun()}+ pub fn is_common_word(&self) -> bool {+ let TokenKind::Word(metadata) = self else {+ return true;+ };++ metadata.common+ }+pub fn is_plural_noun(&self) -> bool {let TokenKind::Word(metadata) = self else {return false;diff --git a/harper-core/tests/run_tests.rs b/harper-core/tests/run_tests.rsindex eece70c26..72bd58dd6 100644--- a/harper-core/tests/run_tests.rs+++ b/harper-core/tests/run_tests.rs@@ -44,3 +44,4 @@ create_test!(chinese_lorem_ipsum.md, 2);create_test!(obsidian_links.md, 2);create_test!(issue_267.md, 0);create_test!(proper_noun_capitalization.md, 2);+create_test!(amazon_hostname.md, 0);diff --git a/harper-core/tests/test_sources/amazon_hostname.md b/harper-core/tests/test_sources/amazon_hostname.mdnew file mode 100644index 000000000..48d4be50f--- /dev/null+++ b/harper-core/tests/test_sources/amazon_hostname.md@@ -0,0 +1 @@+This is a test of whether Amazon.com is considered a URI.diff --git a/packages/harper.js/package.json b/packages/harper.js/package.jsonindex 50b393123..0133dd7d8 100644--- a/packages/harper.js/package.json+++ b/packages/harper.js/package.json@@ -17,7 +17,7 @@"scripts": {"dev": "vite","build": "tsc && vite build",- "test": "vitest run --browser firefox && vitest run --browser chromium"+ "test": "vitest run --browser chromium && vitest run --browser firefox"},"devDependencies": {"wasm": "link:../../harper-wasm/pkg",