Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): create `UseEllipsisCharacter` rule (#3099)
Elijah Potter9fbdda16502a
diff --git a/harper-comments/tests/language_support_sources/basic.clj b/harper-comments/tests/language_support_sources/basic.cljindex 492cb47c8..48cc7be05 100644--- a/harper-comments/tests/language_support_sources/basic.clj+++ b/harper-comments/tests/language_support_sources/basic.clj@@ -6,9 +6,9 @@;;;; Section Comment/Heading-;;; Foo...-;;; Bar...-;;; Baz...+;;; Foo+;;; Bar+;;; Baz;; good(defn foodiff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 5d3605155..f15342350 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -231,6 +231,7 @@ use super::try_ones_hand_at::TryOnesHandAt;use super::try_ones_luck::TryOnesLuck;use super::unclosed_quotes::UnclosedQuotes;use super::update_place_names::UpdatePlaceNames;+use super::use_ellipsis_character::UseEllipsisCharacter;use super::use_title_case::UseTitleCase;use super::verb_to_adjective::VerbToAdjective;use super::very_unique::VeryUnique;@@ -658,6 +659,7 @@ impl LintGroup {insert_expr_rule!(DoubleClick, true);insert_expr_rule!(DoubleModal, true);insert_struct_rule!(EllipsisLength, true);+ insert_struct_rule!(UseEllipsisCharacter, true);insert_expr_rule!(ElsePossessive, true);insert_expr_rule!(EverEvery, true);insert_expr_rule!(Everyday, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex e64d1d27a..e83abfb16 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -243,6 +243,7 @@ mod try_ones_hand_at;mod try_ones_luck;mod unclosed_quotes;mod update_place_names;+mod use_ellipsis_character;mod use_title_case;mod verb_to_adjective;mod very_unique;diff --git a/harper-core/src/linting/use_ellipsis_character.rs b/harper-core/src/linting/use_ellipsis_character.rsnew file mode 100644index 000000000..354f571ef--- /dev/null+++ b/harper-core/src/linting/use_ellipsis_character.rs@@ -0,0 +1,117 @@+use super::{Lint, LintKind, Linter, Suggestion};+use crate::TokenStringExt;++const UNICODE_ELLIPSIS: char = '…';++/// Converts three periods used as an ellipsis into the Unicode ellipsis character.+#[derive(Debug, Default)]+pub struct UseEllipsisCharacter;++impl Linter for UseEllipsisCharacter {+ fn lint(&mut self, document: &crate::Document) -> Vec<Lint> {+ let mut lints = Vec::new();++ for tok in document.iter_ellipsiss() {+ let tok_content = document.get_span_content(&tok.span);++ if tok_content != ['.', '.', '.'] {+ continue;+ }++ lints.push(Lint {+ span: tok.span,+ lint_kind: LintKind::Formatting,+ suggestions: vec![Suggestion::ReplaceWith(vec![UNICODE_ELLIPSIS])],+ message: "Use the Unicode ellipsis character (…).".to_owned(),+ priority: 31,+ });+ }++ lints+ }++ fn description(&self) -> &'static str {+ "Replaces three-period ellipses with the single Unicode ellipsis character."+ }+}++#[cfg(test)]+mod tests {+ use super::UseEllipsisCharacter;+ use crate::linting::tests::{assert_lint_count, assert_no_lints, assert_suggestion_result};++ #[test]+ fn corrects_basic_ellipsis() {+ assert_suggestion_result("...", UseEllipsisCharacter, "…");+ }++ #[test]+ fn corrects_sentence_final_ellipsis() {+ assert_suggestion_result("Wait...", UseEllipsisCharacter, "Wait…");+ }++ #[test]+ fn corrects_ellipsis_with_trailing_space() {+ assert_suggestion_result("Wait... now", UseEllipsisCharacter, "Wait… now");+ }++ #[test]+ fn corrects_ellipsis_in_quotes() {+ assert_suggestion_result("\"...\"", UseEllipsisCharacter, "\"…\"");+ }++ #[test]+ fn corrects_ellipsis_after_word() {+ assert_suggestion_result("maybe...", UseEllipsisCharacter, "maybe…");+ }++ #[test]+ fn corrects_ellipsis_before_word() {+ assert_suggestion_result("...maybe", UseEllipsisCharacter, "…maybe");+ }++ #[test]+ fn corrects_multiple_ellipses() {+ assert_suggestion_result("... and ...", UseEllipsisCharacter, "… and …");+ }++ #[test]+ fn corrects_adjacent_to_punctuation() {+ assert_suggestion_result("Wait...!", UseEllipsisCharacter, "Wait…!");+ }++ #[test]+ fn corrects_parenthetical_ellipsis() {+ assert_suggestion_result("(...) ", UseEllipsisCharacter, "(…) ");+ }++ #[test]+ fn allows_unicode_ellipsis() {+ assert_no_lints("…", UseEllipsisCharacter);+ }++ #[test]+ fn allows_unicode_ellipsis_in_sentence() {+ assert_no_lints("Wait…", UseEllipsisCharacter);+ }++ #[test]+ fn allows_four_periods() {+ assert_no_lints("....", UseEllipsisCharacter);+ }++ #[test]+ fn allows_two_periods() {+ assert_no_lints("..", UseEllipsisCharacter);+ }++ #[test]+ fn allows_long_period_run() {+ assert_no_lints(".....", UseEllipsisCharacter);+ }++ #[test]+ fn flags_single_three_period_ellipsis_once() {+ assert_lint_count("Wait... now", UseEllipsisCharacter, 1);+ }+}diff --git a/harper-core/tests/test_sources/issue_1581.md b/harper-core/tests/test_sources/issue_1581.mdindex a8f223d26..ea9807b26 100644--- a/harper-core/tests/test_sources/issue_1581.md+++ b/harper-core/tests/test_sources/issue_1581.md@@ -1,3 +1,3 @@-Simple walkthroughs with...+Simple walkthroughs with…The above phrase supposedly threw an error in an older version of Harper.diff --git a/packages/harper.js/src/Linter.test.ts b/packages/harper.js/src/Linter.test.tsindex 9eed7dd26..380913190 100644--- a/packages/harper.js/src/Linter.test.ts+++ b/packages/harper.js/src/Linter.test.ts@@ -65,7 +65,7 @@ for (const [linterName, Linter] of Object.entries(linters)) {test(`${linterName} detects repeated words`, async () => {const linter = new Linter({ binary });- const lints = await linter.lint('The the problem is...');+ const lints = await linter.lint('The the problem is');expect(lints.length).toBe(1);@@ -87,7 +87,7 @@ for (const [linterName, Linter] of Object.entries(linters)) {flattened.push(...value);}- expect(flattened.length).toBe(1);+ expect(flattened.length).toBe(2);expect(flattened.length).toBe(normal.length);const item = flattened[0];@@ -100,9 +100,9 @@ for (const [linterName, Linter] of Object.entries(linters)) {const linter = new Linter({ binary });const promises = [- linter.lint('The problem is that that...'),- linter.lint('The problem is...'),- linter.lint('The the problem is...'),+ linter.lint('The problem is that that'),+ linter.lint('The problem is'),+ linter.lint('The the problem is'),];const results = await Promise.all(promises);@@ -119,9 +119,9 @@ for (const [linterName, Linter] of Object.entries(linters)) {const linter = new Linter({ binary });const promises = [- linter.lint('The problem is that that...'),- linter.lint('The problem is...'),- linter.lint('The the problem is...'),+ linter.lint('The problem is that that'),+ linter.lint('The problem is'),+ linter.lint('The the problem is'),];const results = await Promise.all(promises);