Skip to content

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

feat:one in the same→one and the same (#1154)

Andrew Dunbar16afd91aa8e8
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index e4086b8d9..7c12af2e4 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -45,6 +45,7 @@ use super::multiple_sequential_pronouns::MultipleSequentialPronouns;
use super::nobody::Nobody;
use super::number_suffix_capitalization::NumberSuffixCapitalization;
use super::of_course::OfCourse;
+use super::one_and_the_same::OneAndTheSame;
use super::out_of_date::OutOfDate;
use super::oxymorons::Oxymorons;
use super::pattern_linter::run_on_chunk;
@@ -338,6 +339,7 @@ impl LintGroup {
insert_pattern_rule!(Nobody, true);
insert_struct_rule!(NumberSuffixCapitalization, true);
insert_struct_rule!(OfCourse, true);
+ insert_pattern_rule!(OneAndTheSame, true);
insert_pattern_rule!(OutOfDate, true);
insert_struct_rule!(OxfordComma, true);
insert_pattern_rule!(Oxymorons, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 2e60f5d96..a1ac7707e 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -47,6 +47,7 @@ mod no_oxford_comma;
mod nobody;
mod number_suffix_capitalization;
mod of_course;
+mod one_and_the_same;
mod out_of_date;
mod oxford_comma;
mod oxymorons;
@@ -116,6 +117,7 @@ pub use no_oxford_comma::NoOxfordComma;
pub use nobody::Nobody;
pub use number_suffix_capitalization::NumberSuffixCapitalization;
pub use of_course::OfCourse;
+pub use one_and_the_same::OneAndTheSame;
pub use out_of_date::OutOfDate;
pub use oxford_comma::OxfordComma;
pub use oxymorons::Oxymorons;
diff --git a/harper-core/src/linting/one_and_the_same.rs b/harper-core/src/linting/one_and_the_same.rs
new file mode 100644
index 000000000..582f521e1
--- /dev/null
+++ b/harper-core/src/linting/one_and_the_same.rs
@@ -0,0 +1,182 @@
+use crate::{
+ Lrc, Token, TokenStringExt,
+ patterns::{EitherPattern, ExactPhrase, Pattern, SequencePattern, WordSet},
+};
+
+use super::{Lint, LintKind, PatternLinter, Suggestion};
+
+pub struct OneAndTheSame {
+ pattern: Box<dyn Pattern>,
+}
+
+impl Default for OneAndTheSame {
+ fn default() -> Self {
+ let one_in_the_same = Lrc::new(ExactPhrase::from_phrase("one in the same"));
+
+ Self {
+ pattern: Box::new(EitherPattern::new(vec![
+ Box::new(
+ SequencePattern::default()
+ .then(WordSet::new(&["are", "were"]))
+ .t_ws()
+ .then(one_in_the_same.clone()),
+ ),
+ Box::new(
+ SequencePattern::default()
+ .then(one_in_the_same.clone())
+ .t_ws()
+ .t_aco("as"),
+ ),
+ ])),
+ }
+ }
+}
+
+fn ws_word(word: &'static str) -> SequencePattern {
+ SequencePattern::default().t_ws().t_aco(word)
+}
+
+impl PatternLinter for OneAndTheSame {
+ fn pattern(&self) -> &dyn Pattern {
+ self.pattern.as_ref()
+ }
+
+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
+ let phrase = if matched_tokens.last()?.span.get_content(source) == ['a', 's'] {
+ matched_tokens[0..matched_tokens.len() - 2].span()?
+ } else {
+ matched_tokens[2..].span()?
+ };
+
+ Some(Lint {
+ span: phrase,
+ lint_kind: LintKind::WordChoice,
+ suggestions: vec![Suggestion::replace_with_match_case(
+ "one and the same".chars().collect(),
+ phrase.get_content(source),
+ )],
+ message: "The actual idiom is with the word `and`.".to_owned(),
+ priority: 127,
+ })
+ }
+
+ fn description(&self) -> &'static str {
+ "This linter flags instances of the nonstandard phrase `one in the same`. The correct, more accepted form is `one and the same`"
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::OneAndTheSame;
+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+
+ #[test]
+ fn corrects_after_are_atomic() {
+ assert_suggestion_result(
+ "... are one in the same ...",
+ OneAndTheSame::default(),
+ "... are one and the same ...",
+ );
+ }
+
+ #[test]
+ fn corrects_after_were_atomic() {
+ assert_suggestion_result(
+ "... were one in the same ...",
+ OneAndTheSame::default(),
+ "... were one and the same ...",
+ );
+ }
+
+ #[test]
+ fn doesnt_flag_after_other_words_atomic() {
+ assert_lint_count(
+ "... and another one in the same place ...",
+ OneAndTheSame::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn corrects_github_are() {
+ assert_suggestion_result(
+ "Yes, I believe they are one in the same.",
+ OneAndTheSame::default(),
+ "Yes, I believe they are one and the same.",
+ );
+ }
+
+ #[test]
+ fn corrects_github_were() {
+ assert_suggestion_result(
+ "As prior to OpenShift 4.0, OAuth and Kubernetes REST API were one in the same, option (2) above should still work there.",
+ OneAndTheSame::default(),
+ "As prior to OpenShift 4.0, OAuth and Kubernetes REST API were one and the same, option (2) above should still work there.",
+ );
+ }
+
+ #[test]
+ fn corrects_before_as_atomic() {
+ assert_suggestion_result(
+ "... one in the same as ...",
+ OneAndTheSame::default(),
+ "... one and the same as ...",
+ );
+ }
+
+ #[test]
+ fn corrects_before_as_github() {
+ assert_suggestion_result(
+ "In our case the slicedState is one in the same as the featureState",
+ OneAndTheSame::default(),
+ "In our case the slicedState is one and the same as the featureState",
+ );
+ }
+
+ #[test]
+ #[ignore = "needs zero-width end-of-chunk pattern akin to regex `$`"]
+ fn corrects_at_end() {
+ assert_suggestion_result(
+ "I think this is one in the same.",
+ OneAndTheSame::default(),
+ "I think this is one and the same.",
+ );
+ }
+
+ #[test]
+ fn corrects_is_as() {
+ assert_suggestion_result(
+ "I believe this and this issue is one in the same as Next.js uses cloudflare workers for it's edge infra.",
+ OneAndTheSame::default(),
+ "I believe this and this issue is one and the same as Next.js uses cloudflare workers for it's edge infra.",
+ );
+ }
+
+ #[test]
+ fn avoids_false_positive() {
+ assert_lint_count(
+ "If there is no postgresql.pg_hba either there is one in the same section of patroni.yaml or pg_hba.conf is not managed by Patroni.",
+ OneAndTheSame::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ #[ignore = "Cannot detect unexpected ungrammatical `same of`"]
+ fn corrects_is_of() {
+ assert_suggestion_result(
+ "R3 that Stephan Buhre noted is one-in-the-same of what I posted.",
+ OneAndTheSame::default(),
+ "R3 that Stephan Buhre noted is one and the same of what I posted.",
+ );
+ }
+
+ #[test]
+ fn doesnt_flag_ambiguous_before_noun() {
+ assert_lint_count(
+ "I'm guessing this is one in the same request.",
+ OneAndTheSame::default(),
+ 0,
+ );
+ }
+}