Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat(core): added linter for capitalization of `WordPress.com`

Elijah Pottera099610e5347
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rs
index e6809f677..b83615e7f 100644
--- a/harper-core/src/document.rs
+++ b/harper-core/src/document.rs
@@ -538,6 +538,7 @@ macro_rules! create_fns_on_doc {
impl TokenStringExt for Document {
create_fns_on_doc!(word);
+ create_fns_on_doc!(hostname);
create_fns_on_doc!(word_like);
create_fns_on_doc!(conjunction);
create_fns_on_doc!(space);
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 32e847dc2..6ad75df09 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -76,6 +76,7 @@ use super::unclosed_quotes::UnclosedQuotes;
use super::use_genitive::UseGenitive;
use super::was_aloud::WasAloud;
use super::whereas::Whereas;
+use super::wordpress_dotcom::WordPressDotcom;
use super::wrong_quotes::WrongQuotes;
use super::{CurrencyPlacement, Lint, Linter, NoOxfordComma, OxfordComma};
use crate::{Dictionary, Document};
@@ -199,6 +200,7 @@ macro_rules! create_lint_group_config {
}
create_lint_group_config!(
+ WordPressDotcom => true,
DayOneNames => true,
PocketCastsNames => true,
TumblrNames => true,
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 4c25f1d82..c74df0e5f 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -56,6 +56,7 @@ mod unclosed_quotes;
mod use_genitive;
mod was_aloud;
mod whereas;
+mod wordpress_dotcom;
mod wrong_quotes;
pub use an_a::AnA;
@@ -139,6 +140,7 @@ pub use unclosed_quotes::UnclosedQuotes;
pub use use_genitive::UseGenitive;
pub use was_aloud::WasAloud;
pub use whereas::Whereas;
+pub use wordpress_dotcom::WordPressDotcom;
pub use wrong_quotes::WrongQuotes;
use crate::Document;
diff --git a/harper-core/src/linting/wordpress_dotcom.rs b/harper-core/src/linting/wordpress_dotcom.rs
new file mode 100644
index 000000000..33ab889a9
--- /dev/null
+++ b/harper-core/src/linting/wordpress_dotcom.rs
@@ -0,0 +1,57 @@
+use crate::{CharString, CharStringExt, TokenStringExt};
+
+use super::{Lint, LintKind, Linter, Suggestion};
+
+/// Make sure you properly capitalize `WordPress.com`.
+#[derive(Default)]
+pub struct WordPressDotcom;
+
+impl Linter for WordPressDotcom {
+ fn lint(&mut self, document: &crate::Document) -> Vec<Lint> {
+ let correct: CharString = "WordPress.com".chars().collect();
+ let correct_lower = correct.to_lower();
+ let mut lints = Vec::new();
+
+ for hostname in document.iter_hostnames() {
+ let text = document.get_span_content(hostname.span);
+
+ if correct.as_slice() != text && text.to_lower() == correct_lower {
+ lints.push(Lint {
+ span: hostname.span,
+ lint_kind: LintKind::Style,
+ suggestions: vec![Suggestion::ReplaceWith(correct.to_vec())],
+ message: "The WordPress hosting provider should be stylized as `WordPress.com`"
+ .to_owned(),
+ priority: 31,
+ });
+ }
+ }
+
+ lints
+ }
+
+ fn description(&self) -> &str {
+ "Ensures correct capitalization of WordPress.com. This rule verifies that the official stylization of WordPress.com is used when referring to the hosting provider."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::tests::assert_suggestion_result;
+
+ use super::WordPressDotcom;
+
+ #[test]
+ fn simple() {
+ assert_suggestion_result("wordpress.com", WordPressDotcom, "WordPress.com");
+ }
+
+ #[test]
+ fn sentence() {
+ assert_suggestion_result(
+ "wordpress.com is a great hosting provider",
+ WordPressDotcom,
+ "WordPress.com is a great hosting provider",
+ );
+ }
+}
diff --git a/harper-core/src/title_case.rs b/harper-core/src/title_case.rs
index 6819a314e..75101ddd0 100644
--- a/harper-core/src/title_case.rs
+++ b/harper-core/src/title_case.rs
@@ -35,23 +35,21 @@ pub fn make_title_case(toks: &[Token], source: &[char], dict: &impl Dictionary)
let mut output = toks.span().unwrap().get_content(source).to_vec();
while let Some((index, word)) = word_likes.next() {
- let Some(metadata) = word.kind.as_word() else {
- continue;
- };
-
- if metadata.is_proper_noun() {
- // Replace it with the dictionary entry verbatim.
- let orig_text = word.span.get_content(source);
-
- if let Some(correct_caps) = dict.get_correct_capitalization_of(orig_text) {
- // It should match the dictionary verbatim
- output[word.span.start - start_index..word.span.end - start_index]
- .iter_mut()
- .enumerate()
- .for_each(|(idx, c)| *c = correct_caps[idx]);
- continue;
+ if let Some(metadata) = word.kind.as_word() {
+ if metadata.is_proper_noun() {
+ // Replace it with the dictionary entry verbatim.
+ let orig_text = word.span.get_content(source);
+
+ if let Some(correct_caps) = dict.get_correct_capitalization_of(orig_text) {
+ // It should match the dictionary verbatim
+ output[word.span.start - start_index..word.span.end - start_index]
+ .iter_mut()
+ .enumerate()
+ .for_each(|(idx, c)| *c = correct_caps[idx]);
+ continue;
+ }
}
- }
+ };
let should_capitalize = should_capitalize_token(&word, source, dict)
|| index == 0
diff --git a/harper-core/src/token_string_ext.rs b/harper-core/src/token_string_ext.rs
index 89458fc1a..c2ca1edcf 100644
--- a/harper-core/src/token_string_ext.rs
+++ b/harper-core/src/token_string_ext.rs
@@ -65,6 +65,7 @@ pub trait TokenStringExt {
create_decl_for!(number);
create_decl_for!(at);
create_decl_for!(ellipsis);
+ create_decl_for!(hostname);
create_decl_for!(unlintable);
create_decl_for!(sentence_terminator);
create_decl_for!(paragraph_break);
@@ -99,6 +100,7 @@ pub trait TokenStringExt {
impl TokenStringExt for [Token] {
create_fns_for!(word);
create_fns_for!(word_like);
+ create_fns_for!(hostname);
create_fns_for!(conjunction);
create_fns_for!(space);
create_fns_for!(apostrophe);