Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: publish some linter priorities that other linters refer to (#4255)
Andrew Dunbar6d8b2a5ef49c
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex cc43eb01d..334d7e889 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -921,13 +921,15 @@ pub mod tests {let lints = linter.lint(&test);// Just check the first lint for now - TODO- if let Some(lint) = lints.first()- && lint.message != expected_message- {- panic!(- "Expected lint message \"{expected_message}\", but got \"{}\"",- lint.message- );+ match lints.first() {+ Some(lint) => {+ assert_eq!(+ lint.message, expected_message,+ "Expected lint message \"{expected_message}\", but got \"{}\"",+ lint.message+ );+ }+ None => panic!("Expected lint message \"{expected_message}\", but no lints were found"),}}}diff --git a/harper-core/src/linting/out_of_the_window.rs b/harper-core/src/linting/out_of_the_window.rsindex 4ba5498a5..6efabf6f7 100644--- a/harper-core/src/linting/out_of_the_window.rs+++ b/harper-core/src/linting/out_of_the_window.rs@@ -109,7 +109,7 @@ mod tests {#[test]fn gives_the_right_message_for_canadian_english() {assert_lint_message(- "This all works great unless I use middlware and then it all goes out the window.",+ "This all works great unless I use middlware and then it all goes out of the window.",OutOfTheWindow::new(Dialect::Canadian),"If this is the idiom about abandoning a plan, it's more usual to leave out `of` in Canadian English",);diff --git a/harper-core/src/linting/regionalisms.rs b/harper-core/src/linting/regionalisms.rsindex cae9608ab..3a99434f9 100644--- a/harper-core/src/linting/regionalisms.rs+++ b/harper-core/src/linting/regionalisms.rs@@ -2,7 +2,7 @@ use crate::{Dialect::{self, American, Australian, British, Canadian, Indian},Token, TokenStringExt,expr::{Expr, FirstMatchOf, FixedPhrase},- linting::{Lint, LintKind, Suggestion},+ linting::{Lint, LintKind, Suggestion, spell_check},};use super::ExprLinter;@@ -746,7 +746,9 @@ impl ExprLinter for Regionalisms {lint_kind: LintKind::Regionalism,suggestions,message,- priority: 64,+ // TODO is the priority below confusing the higher priority and higher number?+ // TODO was 64 here vs 63 in spellcheck+ priority: spell_check::SPELL_CHECK_PRIORITY + 1, // higher priority = lower number})}diff --git a/harper-core/src/linting/repeated_words.rs b/harper-core/src/linting/repeated_words.rsindex 167bf41fc..4943bf06f 100644--- a/harper-core/src/linting/repeated_words.rs+++ b/harper-core/src/linting/repeated_words.rs@@ -4,6 +4,8 @@ use crate::char_string::char_string;use crate::linting::informal_laughter::is_informal_laughter;use crate::{CharString, CharStringExt, Document, Span};+pub const REPEATED_WORDS_PRIORITY: u8 = 128;+#[derive(Debug, Clone)]pub struct RepeatedWords {/// Words that we need to make sure are detected.@@ -77,7 +79,7 @@ impl Linter for RepeatedWords {document.get_span_content(&tok_a.span).to_vec(),)],message: "Did you mean to repeat this word?".to_owned(),- priority: 128, // Lower priority than `TheTheToThatThe`+ priority: REPEATED_WORDS_PRIORITY, // Lower priority (higher number) than `TheTheToThatThe`})}}diff --git a/harper-core/src/linting/spell_check.rs b/harper-core/src/linting/spell_check.rsindex 7999d37d2..1d6ec70ec 100644--- a/harper-core/src/linting/spell_check.rs+++ b/harper-core/src/linting/spell_check.rs@@ -10,6 +10,8 @@ use crate::expr::{Filter, SequenceExpr};use crate::spell::{Dictionary, suggest_correct_spelling};use crate::{CharString, CharStringExt, Dialect, TokenStringExt, remove_lints_overlapping_expr};+pub const SPELL_CHECK_PRIORITY: u8 = 63;+pub struct SpellCheck<T>whereT: Dictionary,@@ -147,7 +149,7 @@ impl<T: Dictionary> Linter for SpellCheck<T> {lint_kind: LintKind::Spelling,suggestions,message,- priority: 63,+ priority: SPELL_CHECK_PRIORITY,})}diff --git a/harper-core/src/linting/split_words.rs b/harper-core/src/linting/split_words.rsindex 1ee6caf39..185bbedd5 100644--- a/harper-core/src/linting/split_words.rs+++ b/harper-core/src/linting/split_words.rs@@ -346,15 +346,6 @@ mod tests {);}- #[test]- fn not_confident_proc_should_be_pro_c() {- assert_lint_message(- "proc",- SplitWords::default(),- "`proc` should possibly be written as `pro c`.",- );- }-#[test]fn confident_thankyou_should_be_thank_you() {assert_lint_message(@@ -384,7 +375,7 @@ mod tests {fn never_corrects_to_invalid_single_letter_words() {let triggers = ["comitted", "testc", "testh", "testb", "testq", "testx", "testg", "teste", "testj",- "shes",+ "shes", "proc",];let relevant_letters = ['c', 'd', 't', 'h', 'b', 'x', 'e', 'j', 's'];diff --git a/harper-core/src/linting/the_the_to_that_the.rs b/harper-core/src/linting/the_the_to_that_the.rsindex 2509861f9..ff0e24012 100644--- a/harper-core/src/linting/the_the_to_that_the.rs+++ b/harper-core/src/linting/the_the_to_that_the.rs@@ -1,7 +1,7 @@use crate::{CharStringExt, Lint, Token, TokenStringExt,expr::{Expr, SequenceExpr},- linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk, repeated_words},};pub struct TheTheToThatThe {@@ -40,7 +40,7 @@ impl ExprLinter for TheTheToThatThe {.map(|s| Suggestion::replace_with_match_case_str(s, ch)).collect(),message: "Did you mean `that the` or just `the`?".to_owned(),- priority: 126, // Higher priority than `RepeatedWords`+ priority: repeated_words::REPEATED_WORDS_PRIORITY - 2, // Higher priority (lower number) than `RepeatedWords`})}diff --git a/harper-core/src/linting/wrong_negative.rs b/harper-core/src/linting/wrong_negative.rsindex 995870de1..e6f86dd8f 100644--- a/harper-core/src/linting/wrong_negative.rs+++ b/harper-core/src/linting/wrong_negative.rs@@ -1,7 +1,7 @@use crate::{CharStringExt, Lint, Token,expr::Expr,- linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk, spell_check},spell::Dictionary,};@@ -77,7 +77,7 @@ impl<D: Dictionary + 'static> ExprLinter for WrongNegative<D> {}.to_string(),suggestions,- ..Default::default()+ priority: spell_check::SPELL_CHECK_PRIORITY - 1, // higher priority (lower number) than spell check})}@@ -92,7 +92,13 @@ impl<D: Dictionary + 'static> ExprLinter for WrongNegative<D> {#[cfg(test)]mod tests {- use crate::{linting::tests::assert_suggestion_result, spell::FstDictionary};+ use crate::{+ Dialect,+ document::Document,+ linting::{LintGroup, Linter, spell_check, tests::assert_suggestion_result},+ remove_overlaps,+ spell::FstDictionary,+ };use super::WrongNegative;@@ -122,4 +128,43 @@ mod tests {"This System makes a individual design impossible.",);}++ #[test]+ fn fix_unvisible() {+ assert_suggestion_result(+ "Sure, I'm using \"deleted\" here to mean a third state more unvisible than \"yanked\"",+ WrongNegative::new(FstDictionary::curated()),+ "Sure, I'm using \"deleted\" here to mean a third state more invisible than \"yanked\"",+ );+ }++ #[test]+ fn wrong_negative_wins_over_spell_check() {+ let dict = FstDictionary::curated();+ let mut lint_group = LintGroup::empty();++ // Add both linters+ lint_group.add(+ "SpellCheck",+ spell_check::SpellCheck::new(dict.clone(), Dialect::American),+ );+ lint_group.add_chunk_expr_linter("WrongNegative", WrongNegative::new(dict));++ // Enable both linters in the config+ lint_group.config.set_rule_enabled("SpellCheck", true);+ lint_group.config.set_rule_enabled("WrongNegative", true);++ let document = Document::new_plain_english_curated("unvisible");+ let mut lints = lint_group.lint(&document);++ // Remove overlapping lints - this should keep WrongNegative due to higher priority+ remove_overlaps(&mut lints);++ // Should only get one lint from WrongNegative, not SpellCheck+ assert_eq!(lints.len(), 1);+ assert_eq!(+ lints[0].message,+ "Could this be the negative word you intended?"+ );+ }}