Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): criterion (#3698)
Elijah Potter623c608807c3
diff --git a/AGENTS.md b/AGENTS.mdindex 7c65f18a0..ed8e3bc85 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -223,7 +223,7 @@ When asked to write a new rule, keep these guidelines in mind:- The user is almost always expecting you to write it to a file. Which file and where is up to you to find out.- You should include at least 15 total tests, covering a wide variety of cases. Cover false-positives, false negatives, true positives, and if relevant, true negatives.-- You should run any and all tests to ensure that you do no break existing behavior and that your new rule runs the way you expect.+- You should run any and all tests to ensure that you do no break existing behavior and that your new rule runs the way you expect. At minimum, that means `just test-rust`. Make sure to check the snapshots for changes.- After adding a new rule, update `harper-core/default_config.json` so it is enabled in the curated default config.- Built-in Weir rules can be grouped by putting related `.weir` files in a directory under `harper-core/src/linting/weir_rules`; the directory name becomes the single public rule name.- If the rule is related to a closed compound noun, see if you can just add an entry to the existing closed compound linter.diff --git a/harper-core/src/linting/criteria_phenomena/mod.rs b/harper-core/src/linting/criteria_phenomena/mod.rsnew file mode 100644index 000000000..feaece88b--- /dev/null+++ b/harper-core/src/linting/criteria_phenomena/mod.rs@@ -0,0 +1,8 @@+mod plural_criterion_phenomenon;+mod singular_criteria_phenomena;++use super::merge_linters::merge_linters;+use plural_criterion_phenomenon::PluralCriterionPhenomenon;+use singular_criteria_phenomena::SingularCriteriaPhenomena;++merge_linters!(CriteriaPhenomena => SingularCriteriaPhenomena, PluralCriterionPhenomenon => "The words “criteria” and “phenomena” are the plurals of “criterion” and “phenomenon”, respectively. They are often incorrectly used with the wrong number.");diff --git a/harper-core/src/linting/criteria_phenomena/plural_criterion_phenomenon.rs b/harper-core/src/linting/criteria_phenomena/plural_criterion_phenomenon.rsnew file mode 100644index 000000000..4c3e44ef9--- /dev/null+++ b/harper-core/src/linting/criteria_phenomena/plural_criterion_phenomenon.rs@@ -0,0 +1,323 @@+use super::super::{Lint, LintKind, Linter, Suggestion};+use crate::{Document, Number, Punctuation, Span, Token, TokenStringExt};+use harper_brill::UPOS;++/// Linter that checks if `criterion` or `phenomenon` should be plural after a+/// nearby plural modifier.+#[derive(Default)]+pub struct PluralCriterionPhenomenon;++impl Linter for PluralCriterionPhenomenon {+ fn lint(&mut self, document: &Document) -> Vec<Lint> {+ let mut lints = Vec::new();++ for chunk in document.iter_chunks() {+ for (noun_index, token) in chunk.iter().enumerate() {+ let Some(noun) = lower_word(token, document) else {+ continue;+ };++ if matches!(noun.as_str(), "criterion" | "phenomenon")+ && has_plural_modifier_before(chunk, noun_index, document)+ {+ lints.push(make_lint(token.span, &noun));+ }+ }+ }++ lints+ }++ fn description(&self) -> &str {+ "The words “criteria” and “phenomena” are the plurals of “criterion” and “phenomenon”, respectively. This rule checks for singular forms used after plural modifiers."+ }+}++fn make_lint(span: Span<char>, noun: &str) -> Lint {+ let replacement = match noun {+ "criterion" => "criteria",+ "phenomenon" => "phenomena",+ _ => unreachable!("unexpected criterion/phenomenon noun: {noun}"),+ };++ Lint {+ span,+ lint_kind: LintKind::Repetition,+ message: "Use the plural form after a plural modifier.".to_owned(),+ priority: 63,+ suggestions: vec![Suggestion::ReplaceWith(replacement.chars().collect())],+ }+}++fn has_plural_modifier_before(chunk: &[Token], noun_index: usize, document: &Document) -> bool {+ let mut word_count = 0;+ let mut index = noun_index;++ while let Some(previous_index) = previous_significant_token(chunk, index) {+ let token = &chunk[previous_index];++ match modifier_number(token, document) {+ ModifierNumber::Plural => return true,+ ModifierNumber::Singular => return false,+ ModifierNumber::Unknown => {}+ }++ if token.kind.is_word() {+ word_count += 1;+ if word_count > 4 {+ return false;+ }+ } else if !is_parenthetical_punctuation(token) {+ return false;+ }++ index = previous_index;+ }++ false+}++fn previous_significant_token(chunk: &[Token], before: usize) -> Option<usize> {+ (0..before)+ .rev()+ .find(|index| !chunk[*index].kind.is_whitespace())+}++fn lower_word(token: &Token, document: &Document) -> Option<String> {+ token.kind.is_word().then(|| {+ let mut word: String = token+ .span+ .get_content(document.get_source())+ .iter()+ .copied()+ .collect();+ word.make_ascii_lowercase();+ word+ })+}++enum ModifierNumber {+ Plural,+ Singular,+ Unknown,+}++fn modifier_number(token: &Token, document: &Document) -> ModifierNumber {+ if let Some(Number {+ value,+ suffix: None,+ ..+ }) = token.kind.as_number()+ {+ return if (value.0 - 1.0).abs() > f64::EPSILON {+ ModifierNumber::Plural+ } else {+ ModifierNumber::Singular+ };+ }++ let Some(word) = lower_word(token, document) else {+ return ModifierNumber::Unknown;+ };++ if token.kind.is_upos(UPOS::NUM) {+ return if word == "one" {+ ModifierNumber::Singular+ } else {+ ModifierNumber::Plural+ };+ }++ if token.kind.is_quantifier() {+ return match word.as_str() {+ "each" | "every" => ModifierNumber::Singular,+ "both" | "few" | "fewer" | "many" | "multiple" | "several" => ModifierNumber::Plural,+ _ => ModifierNumber::Unknown,+ };+ }++ if token.kind.is_demonstrative_determiner() {+ return match word.as_str() {+ "these" | "those" => ModifierNumber::Plural,+ "this" | "that" => ModifierNumber::Singular,+ _ => ModifierNumber::Unknown,+ };+ }++ if matches!(word.as_str(), "a" | "an") {+ return ModifierNumber::Singular;+ }++ ModifierNumber::Unknown+}++fn is_parenthetical_punctuation(token: &Token) -> bool {+ matches!(+ token.kind.as_punctuation(),+ Some(+ Punctuation::OpenRound+ | Punctuation::CloseRound+ | Punctuation::OpenSquare+ | Punctuation::CloseSquare+ | Punctuation::OpenCurly+ | Punctuation::CloseCurly+ | Punctuation::Hyphen+ )+ )+}++#[cfg(test)]+mod tests {+ use super::PluralCriterionPhenomenon;+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};++ #[test]+ fn detects_reported_namesake_example() {+ assert_suggestion_result(+ "To embody this original goal, I wanted to make the project a namesake of an author known for their literary devices and prose. I also had three other (practical) criterion:",+ PluralCriterionPhenomenon,+ "To embody this original goal, I wanted to make the project a namesake of an author known for their literary devices and prose. I also had three other (practical) criteria:",+ );+ }++ #[test]+ fn detects_plural_number_with_singular_criterion() {+ assert_suggestion_result(+ "I also had three other (practical) criterion.",+ PluralCriterionPhenomenon,+ "I also had three other (practical) criteria.",+ );+ }++ #[test]+ fn detects_digit_with_singular_criterion() {+ assert_suggestion_result(+ "We used 3 criterion.",+ PluralCriterionPhenomenon,+ "We used 3 criteria.",+ );+ }++ #[test]+ fn detects_these_criterion() {+ assert_suggestion_result(+ "These criterion matter.",+ PluralCriterionPhenomenon,+ "These criteria matter.",+ );+ }++ #[test]+ fn detects_those_criterion() {+ assert_suggestion_result(+ "Those criterion failed.",+ PluralCriterionPhenomenon,+ "Those criteria failed.",+ );+ }++ #[test]+ fn detects_several_phenomenon() {+ assert_suggestion_result(+ "Several strange phenomenon appeared.",+ PluralCriterionPhenomenon,+ "Several strange phenomena appeared.",+ );+ }++ #[test]+ fn detects_multiple_adjectives_before_singular_noun() {+ assert_suggestion_result(+ "Two other practical review criterion failed.",+ PluralCriterionPhenomenon,+ "Two other practical review criteria failed.",+ );+ }++ #[test]+ fn detects_parenthetical_phenomenon() {+ assert_suggestion_result(+ "Many other (rare) phenomenon appeared.",+ PluralCriterionPhenomenon,+ "Many other (rare) phenomena appeared.",+ );+ }++ #[test]+ fn detects_hyphenated_intervening_words() {+ assert_suggestion_result(+ "Both long-term criterion changed.",+ PluralCriterionPhenomenon,+ "Both long-term criteria changed.",+ );+ }++ #[test]+ fn allows_correct_plural_criteria() {+ assert_lint_count(+ "Three other practical criteria were considered.",+ PluralCriterionPhenomenon,+ 0,+ )+ }++ #[test]+ fn allows_correct_plural_phenomena() {+ assert_lint_count(+ "Many rare phenomena were on display.",+ PluralCriterionPhenomenon,+ 0,+ )+ }++ #[test]+ fn allows_correct_singular_criterion() {+ assert_lint_count("That criterion is important.", PluralCriterionPhenomenon, 0)+ }++ #[test]+ fn allows_correct_singular_phenomenon() {+ assert_lint_count(+ "One strange phenomenon appeared.",+ PluralCriterionPhenomenon,+ 0,+ )+ }++ #[test]+ fn allows_the_other_criterion() {+ assert_lint_count(+ "The other criterion was practical.",+ PluralCriterionPhenomenon,+ 0,+ )+ }++ #[test]+ fn allows_singular_number_with_singular_criterion() {+ assert_lint_count("1 criterion was enough.", PluralCriterionPhenomenon, 0)+ }++ #[test]+ fn allows_comparative_modifier_with_singular_phenomenon() {+ assert_lint_count(+ "To the wingless a more interesting phenomenon is their dissimilarity.",+ PluralCriterionPhenomenon,+ 0,+ )+ }++ #[test]+ fn allows_ambiguous_quantifier_with_singular_criterion() {+ assert_lint_count("No criterion was enough.", PluralCriterionPhenomenon, 0)+ }++ #[test]+ fn allows_plural_modifier_too_far_away() {+ assert_lint_count(+ "Three teams reviewed the policy before the criterion changed.",+ PluralCriterionPhenomenon,+ 0,+ )+ }+}diff --git a/harper-core/src/linting/criteria_phenomena.rs b/harper-core/src/linting/criteria_phenomena/singular_criteria_phenomena.rssimilarity index 74%rename from harper-core/src/linting/criteria_phenomena.rsrename to harper-core/src/linting/criteria_phenomena/singular_criteria_phenomena.rsindex 37424314c..d27975422 100644--- a/harper-core/src/linting/criteria_phenomena.rs+++ b/harper-core/src/linting/criteria_phenomena/singular_criteria_phenomena.rs@@ -1,20 +1,20 @@-use super::Suggestion;-use super::expr_linter::ExprLinter;+use super::super::Suggestion;+use super::super::expr_linter::ExprLinter;use crate::expr::Expr;use crate::expr::SequenceExpr;use crate::linting::LintKind;use crate::linting::expr_linter::Chunk;use crate::patterns::WordSet;-use crate::{Lint, Lrc, Token, TokenStringExt};+use crate::{Lint, Lrc, Token};-/// Linter that checks if 'criteria' or 'phenomena' is used as singular.-pub struct CriteriaPhenomena {+/// Linter that checks if `criteria` or `phenomena` is used as singular.+pub struct SingularCriteriaPhenomena {expr: SequenceExpr,plural_words: Lrc<WordSet>,singular_modifiers: Lrc<WordSet>,}-impl CriteriaPhenomena {+impl SingularCriteriaPhenomena {fn new() -> Self {let plural_words = Lrc::new(WordSet::new(&["criteria", "phenomena"]));@@ -30,7 +30,7 @@ impl CriteriaPhenomena {}}-impl ExprLinter for CriteriaPhenomena {+impl ExprLinter for SingularCriteriaPhenomena {type Unit = Chunk;fn expr(&self) -> &dyn Expr {@@ -53,7 +53,7 @@ impl ExprLinter for CriteriaPhenomena {};Some(Lint {- span: matched_tokens.span()?,+ span: matched_tokens[2].span,lint_kind: LintKind::Repetition,message: "You used a plural noun as singular.".to_owned(),priority: 63,@@ -66,7 +66,7 @@ impl ExprLinter for CriteriaPhenomena {}}-impl Default for CriteriaPhenomena {+impl Default for SingularCriteriaPhenomena {fn default() -> Self {Self::new()}@@ -74,24 +74,24 @@ impl Default for CriteriaPhenomena {#[cfg(test)]mod tests {- use super::CriteriaPhenomena;- use crate::linting::tests::assert_lint_count;+ use super::SingularCriteriaPhenomena;+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};#[test]fn can_detect_incorrect_criteria() {- assert_lint_count(+ assert_suggestion_result("...One criteria is essential...",- CriteriaPhenomena::new(),- 1,+ SingularCriteriaPhenomena::default(),+ "...One criterion is essential...",)}#[test]fn can_detect_incorrect_phenomena() {- assert_lint_count(+ assert_suggestion_result("...I would like to see that phenomena.",- CriteriaPhenomena::new(),- 1,+ SingularCriteriaPhenomena::default(),+ "...I would like to see that phenomenon.",)}@@ -99,7 +99,7 @@ mod tests {fn allows_correct_criteria() {assert_lint_count("...She disagrees with those criteria.",- CriteriaPhenomena::new(),+ SingularCriteriaPhenomena::default(),0,)}@@ -108,7 +108,7 @@ mod tests {fn allows_correct_phenomena() {assert_lint_count("...Many phenomena were on display.",- CriteriaPhenomena::new(),+ SingularCriteriaPhenomena::default(),0,)}diff --git a/harper-core/src/linting/lint_group/mod.rs b/harper-core/src/linting/lint_group/mod.rsindex d2d3ad738..04300f876 100644--- a/harper-core/src/linting/lint_group/mod.rs+++ b/harper-core/src/linting/lint_group/mod.rs@@ -638,7 +638,7 @@ impl LintGroup {insert_expr_rule!(Confident, true);insert_struct_rule!(CorrectNumberSuffix, true);insert_expr_rule!(CraveFor, true);- insert_expr_rule!(CriteriaPhenomena, true);+ insert_struct_rule!(CriteriaPhenomena, true);insert_expr_rule!(CureFor, true);insert_struct_rule!(CurrencyPlacement, true);insert_expr_rule!(Dashes, true);