Skip to content

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

feat: the Microsoft's Security Graph→Microsoft's Security Graph (#2331)

Andrew Dunbar92d7f4bc4371
diff --git a/harper-core/src/char_string.rs b/harper-core/src/char_string.rs
index d3e956c07..98841a926 100644
--- a/harper-core/src/char_string.rs
+++ b/harper-core/src/char_string.rs
@@ -98,11 +98,21 @@ impl CharStringExt for [char] {
}
fn eq_ignore_ascii_case_str(&self, other: &str) -> bool {
- self.len() == other.len()
- && self
- .iter()
- .zip(other.chars())
- .all(|(a, b)| a.to_ascii_lowercase() == b)
+ let mut chit = self.iter();
+ let mut strit = other.chars();
+
+ loop {
+ let (c, s) = (chit.next(), strit.next());
+ match (c, s) {
+ (Some(c), Some(s)) => {
+ if c.to_ascii_lowercase() != s {
+ return false;
+ }
+ }
+ (None, None) => return true,
+ _ => return false,
+ }
+ }
}
fn eq_ignore_ascii_case_chars(&self, other: &[char]) -> bool {
@@ -124,7 +134,7 @@ impl CharStringExt for [char] {
}
fn starts_with_ignore_ascii_case_str(&self, prefix: &str) -> bool {
- let prefix_len = prefix.len();
+ let prefix_len = prefix.chars().count();
if self.len() < prefix_len {
return false;
}
@@ -141,7 +151,7 @@ impl CharStringExt for [char] {
}
fn ends_with_ignore_ascii_case_str(&self, suffix: &str) -> bool {
- let suffix_len = suffix.len();
+ let suffix_len = suffix.chars().count();
if self.len() < suffix_len {
return false;
}
@@ -233,4 +243,14 @@ mod tests {
fn ends_with_ignore_ascii_case_str_does_not_match_different_suffix() {
assert!(!['H', 'e', 'l', 'l', 'o'].ends_with_ignore_ascii_case_str("world"));
}
+
+ #[test]
+ fn differs_only_by_length_1() {
+ assert!(!['b', 'b'].eq_ignore_ascii_case_str("b"));
+ }
+
+ #[test]
+ fn differs_only_by_length_2() {
+ assert!(!['c'].eq_ignore_ascii_case_str("cc"));
+ }
}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index a3f235002..0f26ce1e1 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -181,6 +181,7 @@ use super::that_than::ThatThan;
use super::that_which::ThatWhich;
use super::the_how_why::TheHowWhy;
use super::the_my::TheMy;
+use super::the_proper_noun_possessive::TheProperNounPossessive;
use super::then_than::ThenThan;
use super::theres::Theres;
use super::theses_these::ThesesThese;
@@ -652,6 +653,7 @@ impl LintGroup {
insert_expr_rule!(ThatWhich, true);
insert_expr_rule!(TheHowWhy, true);
insert_expr_rule!(TheMy, true);
+ insert_expr_rule!(TheProperNounPossessive, true);
insert_expr_rule!(ThenThan, true);
insert_expr_rule!(Theres, true);
insert_expr_rule!(ThesesThese, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 0b737acaf..77fe3f674 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -193,6 +193,7 @@ mod that_than;
mod that_which;
mod the_how_why;
mod the_my;
+mod the_proper_noun_possessive;
mod then_than;
mod theres;
mod theses_these;
diff --git a/harper-core/src/linting/the_proper_noun_possessive.rs b/harper-core/src/linting/the_proper_noun_possessive.rs
new file mode 100644
index 000000000..62bcddb44
--- /dev/null
+++ b/harper-core/src/linting/the_proper_noun_possessive.rs
@@ -0,0 +1,126 @@
+use crate::{
+ CharStringExt, Lint, Token, TokenStringExt,
+ expr::{Expr, SequenceExpr},
+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},
+};
+
+pub struct TheProperNounPossessive {
+ expr: Box<dyn Expr>,
+}
+
+impl Default for TheProperNounPossessive {
+ fn default() -> Self {
+ Self {
+ expr: Box::new(
+ SequenceExpr::aco("the")
+ .t_ws()
+ .then(|t: &Token, s: &[char]| {
+ // TODO: should use `k.is_proper_noun()` when #2327 is fixed
+ // TODO: should use `k.is_common_noun()` which doesn't exist yet
+ t.kind.is_possessive_noun()
+ && t.kind.is_titlecase()
+ && !t.kind.is_lowercase()
+ && !t
+ .span
+ .get_content(s)
+ .eq_any_ignore_ascii_case_str(&["internet's", "internet’s"])
+ }),
+ ),
+ }
+ }
+}
+
+impl ExprLinter for TheProperNounPossessive {
+ type Unit = Chunk;
+
+ fn expr(&self) -> &dyn Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint(&self, toks: &[Token], _: &[char]) -> Option<Lint> {
+ Some(Lint {
+ span: toks[..2].span()?,
+ lint_kind: LintKind::Redundancy,
+ suggestions: vec![Suggestion::Remove],
+ message:
+ "The definite article `the` is redundant before a proper noun in the possessive."
+ .to_string(),
+ ..Default::default()
+ })
+ }
+
+ fn description(&self) -> &str {
+ "Checks for redundant `the` before possessive proper noun such as `The London's population`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::{
+ tests::{assert_no_lints, assert_suggestion_result},
+ the_proper_noun_possessive::TheProperNounPossessive,
+ };
+
+ #[test]
+ fn fix_the_putins_war() {
+ assert_suggestion_result(
+ "The Putin's war",
+ TheProperNounPossessive::default(),
+ "Putin's war",
+ );
+ }
+
+ #[test]
+ fn fix_the_londons_population() {
+ assert_suggestion_result(
+ "The London's population.",
+ TheProperNounPossessive::default(),
+ "London's population.",
+ )
+ }
+
+ #[test]
+ fn dont_flag_common_noun_in_titlecase() {
+ assert_no_lints("The Dog's Dinner", TheProperNounPossessive::default())
+ }
+
+ #[test]
+ #[ignore = "Can't currently do this due to issue #???"]
+ fn fix_proper_noun_stylized_to_begin_lowercase() {
+ assert_suggestion_result(
+ "The macOS's Finder",
+ TheProperNounPossessive::default(),
+ "macOS's Finder",
+ );
+ }
+
+ #[test]
+ fn fix_even_when_capitalisation_omitted() {
+ assert_suggestion_result(
+ "the egypt's pyramids",
+ TheProperNounPossessive::default(),
+ "egypt's pyramids",
+ )
+ }
+
+ #[test]
+ fn dont_flag_proper_noun_thats_also_common_noun() {
+ assert_no_lints("the china's broken", TheProperNounPossessive::default());
+ }
+
+ #[test]
+ fn dont_flag_the_internets() {
+ assert_no_lints(
+ "The internet's most popular icon toolkit has been redesigned",
+ TheProperNounPossessive::default(),
+ );
+ }
+
+ #[test]
+ fn dont_flag_the_internets_curly_apostrophe() {
+ assert_no_lints(
+ "The internet’s most popular icon toolkit has been redesigned",
+ TheProperNounPossessive::default(),
+ );
+ }
+}