Skip to content

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

Criteria phenomena (#2028)

Thomas van Nellen5f4f66d6f984
diff --git a/harper-core/src/linting/criteria_phenomena.rs b/harper-core/src/linting/criteria_phenomena.rs
new file mode 100644
index 000000000..acc3dee10
--- /dev/null
+++ b/harper-core/src/linting/criteria_phenomena.rs
@@ -0,0 +1,115 @@
+use super::Suggestion;
+use super::expr_linter::ExprLinter;
+use crate::expr::Expr;
+use crate::expr::SequenceExpr;
+use crate::linting::LintKind;
+use crate::patterns::WordSet;
+use crate::{Lint, Lrc, Token, TokenStringExt};
+
+/// Linter that checks if 'criteria' or 'phenomena' is used as singular.
+pub struct CriteriaPhenomena {
+ expr: Box<dyn Expr>,
+ plural_words: Lrc<WordSet>,
+ singular_modifiers: Lrc<WordSet>,
+}
+
+impl CriteriaPhenomena {
+ fn new() -> Self {
+ let plural_words = Lrc::new(WordSet::new(&["criteria", "phenomena"]));
+
+ let singular_modifiers = Lrc::new(WordSet::new(&["this", "that", "a", "one"]));
+
+ Self {
+ expr: Box::new(
+ SequenceExpr::default()
+ .then(singular_modifiers.clone())
+ .then_whitespace()
+ .then(plural_words.clone()),
+ ),
+ plural_words,
+ singular_modifiers,
+ }
+ }
+}
+
+impl ExprLinter for CriteriaPhenomena {
+ fn expr(&self) -> &dyn Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
+ let mut second_word: String = matched_tokens[2]
+ .span
+ .get_content(source)
+ .iter()
+ .copied()
+ .collect();
+ second_word.make_ascii_lowercase();
+
+ let suggestions = match second_word.as_str() {
+ "criteria" => vec![Suggestion::ReplaceWith("criterion".chars().collect())],
+ "phenomena" => vec![Suggestion::ReplaceWith("phenomenon".chars().collect())],
+ _ => panic!("Don't know what to say about '{second_word}'!"),
+ };
+
+ Some(Lint {
+ span: matched_tokens.span()?,
+ lint_kind: LintKind::Repetition,
+ message: "You used a plural noun as singular.".to_owned(),
+ priority: 63,
+ suggestions,
+ })
+ }
+
+ fn description(&self) -> &'static str {
+ "The words “criteria” and “phenomena” are the plurals of “criterion” and “phenomenon”, respectively. They are often incorrectly used as singular."
+ }
+}
+
+impl Default for CriteriaPhenomena {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::CriteriaPhenomena;
+ use crate::linting::tests::assert_lint_count;
+
+ #[test]
+ fn can_detect_incorrect_criteria() {
+ assert_lint_count(
+ "...One criteria is essential...",
+ CriteriaPhenomena::new(),
+ 1,
+ )
+ }
+
+ #[test]
+ fn can_detect_incorrect_phenomena() {
+ assert_lint_count(
+ "...I would like to see that phenomena.",
+ CriteriaPhenomena::new(),
+ 1,
+ )
+ }
+
+ #[test]
+ fn allows_correct_criteria() {
+ assert_lint_count(
+ "...She disagrees with those criteria.",
+ CriteriaPhenomena::new(),
+ 0,
+ )
+ }
+
+ #[test]
+ fn allows_correct_phenomena() {
+ assert_lint_count(
+ "...Many phenomena were on display.",
+ CriteriaPhenomena::new(),
+ 0,
+ )
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index c5b152e40..c33b57096 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -33,6 +33,7 @@ use super::comma_fixes::CommaFixes;
use super::compound_nouns::CompoundNouns;
use super::confident::Confident;
use super::correct_number_suffix::CorrectNumberSuffix;
+use super::criteria_phenomena::CriteriaPhenomena;
use super::despite_of::DespiteOf;
use super::discourse_markers::DiscourseMarkers;
use super::dot_initialisms::DotInitialisms;
@@ -441,6 +442,7 @@ impl LintGroup {
insert_struct_rule!(CompoundNouns, true);
insert_expr_rule!(Confident, true);
insert_struct_rule!(CorrectNumberSuffix, true);
+ insert_expr_rule!(CriteriaPhenomena, true);
insert_struct_rule!(CurrencyPlacement, true);
insert_expr_rule!(Dashes, true);
insert_expr_rule!(DespiteOf, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index cb9858241..3a82eda31 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -25,6 +25,7 @@ mod comma_fixes;
mod compound_nouns;
mod confident;
mod correct_number_suffix;
+mod criteria_phenomena;
mod currency_placement;
mod dashes;
mod despite_of;
@@ -175,6 +176,7 @@ pub use comma_fixes::CommaFixes;
pub use compound_nouns::CompoundNouns;
pub use confident::Confident;
pub use correct_number_suffix::CorrectNumberSuffix;
+pub use criteria_phenomena::CriteriaPhenomena;
pub use currency_placement::CurrencyPlacement;
pub use dashes::Dashes;
pub use despite_of::DespiteOf;
diff --git a/packages/vscode-plugin/package.json b/packages/vscode-plugin/package.json
index df6874bcf..afcd50fce 100644
--- a/packages/vscode-plugin/package.json
+++ b/packages/vscode-plugin/package.json
@@ -643,6 +643,12 @@
"default": true,
"description": "In English idioms, “to course” means to flow rapidly—so avoid the eggcorn `cursing through veins.`"
},
+ "harper.linters.CriteriaPhenomena": {
+ "scope": "resource",
+ "type": "boolean",
+ "default": true,
+ "description": "The words “criteria” and “phenomena” are the plurals of “criterion” and “phenomenon”, respectively. They are often incorrectly used as singular."
+ },
"harper.linters.CurrencyPlacement": {
"scope": "resource",
"type": "boolean",