-
Notifications
You must be signed in to change notification settings - Fork 173
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
elijah-potter
merged 2 commits into
Automattic:master
from
hippietrail:bad_verb_after_to_993
Mar 31, 2025
Merged
feat: implement #993 #994
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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, | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
out.add("SpellCheck", Box::new(SpellCheck::new(dictionary, dialect))); | ||
out.config.set_rule_enabled("SpellCheck", true); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 consumeFstDictionary::curated
from a linter, but it's a pattern I'd like to avoid moving forward.There was a problem hiding this comment.
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.