Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: flag "allow to do" (#2159)
Andrew Dunbarc50dfcdc40cf
diff --git a/harper-core/src/linting/allow_to.rs b/harper-core/src/linting/allow_to.rsnew file mode 100644index 000000000..f90729a68--- /dev/null+++ b/harper-core/src/linting/allow_to.rs@@ -0,0 +1,119 @@+use crate::expr::{Expr, SequenceExpr};+use crate::linting::{ExprLinter, Lint, LintKind};+use crate::token::Token;+use crate::token_string_ext::TokenStringExt;++pub struct AllowTo {+ exp: Box<dyn Expr>,+}++impl Default for AllowTo {+ fn default() -> Self {+ Self {+ // Note: Does not include "allowed to", which is a legitimate usage in its own right.+ exp: Box::new(+ SequenceExpr::word_set(&["allow", "allowing", "allows"])+ .t_ws()+ .t_aco("to")+ .then_optional(SequenceExpr::default().t_ws().then_adverb())+ .t_ws()+ .then_any_word(),+ ),+ }+ }+}++impl ExprLinter for AllowTo {+ fn expr(&self) -> &dyn Expr {+ self.exp.as_ref()+ }++ fn match_to_lint(&self, toks: &[Token], _src: &[char]) -> Option<Lint> {+ let span = toks.span()?;+ let first = toks.first()?;+ let allow = first.span.get_content_string(_src);++ let message = format!(+ "For correct usage, either add a subject between `{allow}` and `to` (e.g., `{allow} someone to do`) or use the present participle (e.g., `{allow} doing`)."+ );++ Some(Lint {+ span,+ lint_kind: LintKind::Grammar,+ suggestions: vec![],+ message,+ ..Default::default()+ })+ }++ fn description(&self) -> &'static str {+ "Flags erroneous usage of `allow to` without a subject."+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::{+ AllowTo,+ tests::{assert_lint_count, assert_no_lints},+ };++ #[test]+ fn flag_allow_to() {+ assert_lint_count(+ "Allow to change approval policy during running task # 4394.",+ AllowTo::default(),+ 1,+ );+ }++ #[test]+ fn flag_allowing_to() {+ assert_lint_count(+ "Allowing to have multiple views with different filtering # 952.",+ AllowTo::default(),+ 1,+ );+ }++ #[test]+ fn flag_allows_to() {+ assert_lint_count(+ "It is easily doable for classic IHostBuilder, because its extension allows to pass configure action",+ AllowTo::default(),+ 1,+ );+ }++ #[test]+ fn dont_flag_allowed_to() {+ assert_no_lints(+ "In C and C++ aliasing has to do with what expression types we are allowed to access stored values through.",+ AllowTo::default(),+ );+ }++ #[test]+ fn dont_flag_allow_pronoun_to() {+ assert_no_lints(+ "It would be really great to allow me to enter body data using multipart form",+ AllowTo::default(),+ );+ }++ #[test]+ fn dont_flag_allow_noun_to() {+ assert_no_lints(+ "Allows users to export SMART statistics from any connected hard drive",+ AllowTo::default(),+ );+ }++ #[test]+ fn dont_flag_allow_np_to() {+ assert_no_lints(+ "This vulnerability allows an authenticated attacker to infer data from the database by measuring response times",+ AllowTo::default(),+ );+ }+}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex d38af5524..be6c04910 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -17,6 +17,7 @@ use super::adjective_double_degree::AdjectiveDoubleDegree;use super::adjective_of_a::AdjectiveOfA;use super::after_later::AfterLater;use super::all_intents_and_purposes::AllIntentsAndPurposes;+use super::allow_to::AllowTo;use super::am_in_the_morning::AmInTheMorning;use super::amounts_for::AmountsFor;use super::an_a::AnA;@@ -454,6 +455,7 @@ impl LintGroup {insert_struct_rule!(AdjectiveOfA, true);insert_expr_rule!(AfterLater, true);insert_expr_rule!(AllIntentsAndPurposes, true);+ insert_expr_rule!(AllowTo, true);insert_struct_rule!(AmInTheMorning, true);insert_expr_rule!(AmountsFor, true);insert_struct_rule!(AnA, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 571a2f205..952bf909a 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -8,6 +8,7 @@ mod adjective_double_degree;mod adjective_of_a;mod after_later;mod all_intents_and_purposes;+mod allow_to;mod am_in_the_morning;mod amounts_for;mod an_a;@@ -189,6 +190,7 @@ pub use adjective_double_degree::AdjectiveDoubleDegree;pub use adjective_of_a::AdjectiveOfA;pub use after_later::AfterLater;pub use all_intents_and_purposes::AllIntentsAndPurposes;+pub use allow_to::AllowTo;pub use am_in_the_morning::AmInTheMorning;pub use amounts_for::AmountsFor;pub use an_a::AnA;