Skip to content

feat: implement #993 #994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions harper-core/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,8 @@ impl TokenStringExt for Document {
create_fns_on_doc!(likely_homograph);
create_fns_on_doc!(comma);
create_fns_on_doc!(adjective);
create_fns_on_doc!(verb);
create_fns_on_doc!(preposition);

fn first_sentence_word(&self) -> Option<&Token> {
self.tokens.first_sentence_word()
Expand Down
153 changes: 153 additions & 0 deletions harper-core/src/linting/inflected_verb_after_to.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
use std::sync::Arc;

use crate::{Dictionary, Document, FstDictionary, Span, TokenStringExt};

use super::{Lint, LintKind, Linter, Suggestion};

pub struct InflectedVerbAfterTo {
pub dictionary: Arc<FstDictionary>,
}

impl InflectedVerbAfterTo {
pub fn new() -> Self {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind refactoring this to take a generic dictionary as an argument and passing it from the LintGroup constructor? There are other places in the code where we directly consume FstDictionary::curated from a linter, but it's a pattern I'd like to avoid moving forward.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah OK I'll look into that. So far I mostly see what other code does and copy the approaches I find.

Self {
dictionary: FstDictionary::curated(),
}
}
}

impl Default for InflectedVerbAfterTo {
fn default() -> Self {
Self::new()
}
}

impl Linter for InflectedVerbAfterTo {
fn lint(&mut self, document: &Document) -> Vec<Lint> {
let mut lints = Vec::new();
for pi in document.iter_preposition_indices() {
let Some(prep) = document.get_token(pi) else {
continue;
};
let Some(space) = document.get_token(pi + 1) else {
continue;
};
let Some(word) = document.get_token(pi + 2) else {
continue;
};
if !space.kind.is_whitespace() || !word.kind.is_word() {
continue;
}
let prep_to = document.get_span_content(&prep.span);
if prep_to != ['t', 'o'] && prep_to != ['T', 'o'] {
continue;
}

let chars = document.get_span_content(&word.span);
let (len, form) = match word.kind {
_ if word.kind.is_verb() => match chars {
// [.., 'i', 'n', 'g'] => (3, "continuous"), // breaks the Laravel test at "prior to deploying the application"
[.., 'e', 'd'] => (2, "past"),
[.., 'e', 's'] => (2, "3rd person singular present"),
[.., 's'] => (1, "3rd person singular present"),
_ => continue,
},
_ if word.kind.is_plural_noun() => match chars {
[.., 'e', 's'] => (2, "plural"),
[.., 's'] => (1, "plural"),
_ => continue,
},
_ => continue,
};
let stem = chars[..chars.len() - len].to_vec();
let Some(md) = self.dictionary.get_word_metadata(&stem) else {
continue;
};
if !md.is_verb() {
continue;
}
if word.kind.is_plural_noun() && md.is_noun() {
continue;
}

lints.push(Lint {
span: Span::new(prep.span.start, word.span.end),
lint_kind: LintKind::WordChoice,
message: format!("This verb seems to be in the {} form.", form).to_string(),
suggestions: vec![Suggestion::ReplaceWith(
prep_to
.iter()
.chain([' '].iter())
.chain(stem.iter())
.copied()
.collect(),
)],
..Default::default()
});
}
lints
}

fn description(&self) -> &str {
"This rule looks for `to verb` where `verb` is not in the infinitive form."
}
}

#[cfg(test)]
mod tests {
use super::InflectedVerbAfterTo;
use crate::linting::tests::{assert_lint_count, assert_suggestion_result};

#[test]
fn dont_flag_to_check() {
assert_lint_count("to check", InflectedVerbAfterTo::default(), 0);
}

#[test]
fn dont_flag_to_checks() {
assert_lint_count("to checks", InflectedVerbAfterTo::default(), 0);
}

#[test]
fn dont_flag_to_cheques() {
assert_lint_count("to cheques", InflectedVerbAfterTo::default(), 0);
}

// #[test]
// fn flag_to_checking() {
// assert_lint_count("to checking", InflectedVerbAfterTo::default(), 1);
// }

#[test]
fn flag_to_checked() {
assert_lint_count("to checked", InflectedVerbAfterTo::default(), 1);
}

#[test]
fn dont_flag_plural_noun() {
assert_lint_count("to beliefs", InflectedVerbAfterTo::default(), 0);
}

#[test]
fn dont_flag_plural_meats() {
assert_lint_count("to meats", InflectedVerbAfterTo::default(), 0);
}

// #[test]
// fn check_993_suggestions() {
// assert_suggestion_result(
// "A location-agnostic structure that attempts to captures the context and content that a Lint occurred.",
// InflectedVerbAfterTo::default(),
// "A location-agnostic structure that attempts to capture the context and content that a Lint occurred.",
// );
// }

#[test]
fn dont_flag_plural_embarrass() {
assert_lint_count(
"Second I'm going to embarrass you for a.",
InflectedVerbAfterTo::default(),
0,
);
}
}
2 changes: 2 additions & 0 deletions harper-core/src/linting/lint_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use super::hedging::Hedging;
use super::hereby::Hereby;
use super::hop_hope::HopHope;
use super::hyphenate_number_day::HyphenateNumberDay;
use super::inflected_verb_after_to::InflectedVerbAfterTo;
use super::left_right_hand::LeftRightHand;
use super::lets_confusion::LetsConfusion;
use super::likewise::Likewise;
Expand Down Expand Up @@ -343,6 +344,7 @@ impl LintGroup {
insert_pattern_rule!(Hedging, true);
insert_pattern_rule!(ExpandTimeShorthands, true);
insert_pattern_rule!(ModalOf, true);
insert_struct_rule!(InflectedVerbAfterTo, true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to replace the macro call with something similar to how the SpellCheck rule is added.


out.add("SpellCheck", Box::new(SpellCheck::new(dictionary, dialect)));
out.config.set_rule_enabled("SpellCheck", true);
Expand Down
2 changes: 2 additions & 0 deletions harper-core/src/linting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod hedging;
mod hereby;
mod hop_hope;
mod hyphenate_number_day;
mod inflected_verb_after_to;
mod left_right_hand;
mod lets_confusion;
mod likewise;
Expand Down Expand Up @@ -86,6 +87,7 @@ pub use hedging::Hedging;
pub use hereby::Hereby;
pub use hop_hope::HopHope;
pub use hyphenate_number_day::HyphenateNumberDay;
pub use inflected_verb_after_to::InflectedVerbAfterTo;
pub use left_right_hand::LeftRightHand;
pub use lets_confusion::LetsConfusion;
pub use likewise::Likewise;
Expand Down
4 changes: 4 additions & 0 deletions harper-core/src/token_string_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ pub trait TokenStringExt {
create_decl_for!(likely_homograph);
create_decl_for!(comma);
create_decl_for!(adjective);
create_decl_for!(verb);
create_decl_for!(preposition);

fn iter_linking_verb_indices(&self) -> impl Iterator<Item = usize> + '_;
fn iter_linking_verbs(&self) -> impl Iterator<Item = &Token> + '_;
Expand Down Expand Up @@ -119,6 +121,8 @@ impl TokenStringExt for [Token] {
create_fns_for!(likely_homograph);
create_fns_for!(comma);
create_fns_for!(adjective);
create_fns_for!(verb);
create_fns_for!(preposition);

fn first_non_whitespace(&self) -> Option<&Token> {
self.iter().find(|t| !t.kind.is_whitespace())
Expand Down