-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): added simple linter for the conjugations of "to be" for #256
- Loading branch information
1 parent
f6f5784
commit 5fe070e
Showing
9 changed files
with
143 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,94 @@ | ||
use crate::{ | ||
patterns::{EitherPattern, Pattern, SequencePattern}, | ||
Token, | ||
}; | ||
|
||
use super::{Lint, LintKind, PatternLinter, Suggestion}; | ||
|
||
pub struct PluralConjugate { | ||
pattern: Box<dyn Pattern>, | ||
} | ||
|
||
impl Default for PluralConjugate { | ||
fn default() -> Self { | ||
let plural_case = SequencePattern::default() | ||
.then_plural_noun() | ||
.then_whitespace() | ||
.then_exact_word("is"); | ||
|
||
let non_plural_case = SequencePattern::default() | ||
.then(Box::new(|tok: &Token, _source: &[char]| { | ||
tok.kind.is_not_plural_noun() && tok.kind.is_noun() | ||
})) | ||
.then_whitespace() | ||
.then_exact_word("are"); | ||
|
||
let pat = EitherPattern::new(vec![Box::new(plural_case), Box::new(non_plural_case)]); | ||
|
||
Self { | ||
pattern: Box::new(pat), | ||
} | ||
} | ||
} | ||
|
||
impl PatternLinter for PluralConjugate { | ||
fn pattern(&self) -> &dyn Pattern { | ||
self.pattern.as_ref() | ||
} | ||
|
||
fn match_to_lint(&self, matched_tokens: &[Token], _source: &[char]) -> Lint { | ||
let should_be_plural = matched_tokens.first().unwrap().kind.is_plural_noun(); | ||
|
||
let sug = if should_be_plural { | ||
vec!['a', 'r', 'e'] | ||
} else { | ||
vec!['i', 's'] | ||
}; | ||
|
||
Lint { | ||
span: matched_tokens.last().unwrap().span, | ||
lint_kind: LintKind::WordChoice, | ||
suggestions: vec![Suggestion::ReplaceWith(sug)], | ||
message: "Use the alternative conjugation of this verb to be consistent with the noun's plural nature.".to_owned(), | ||
priority: 63, | ||
} | ||
} | ||
|
||
fn description(&self) -> &'static str { | ||
"Make sure you use the correct conjugation of the verb \"to be\" in plural contexts." | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::linting::tests::assert_suggestion_result; | ||
|
||
use super::PluralConjugate; | ||
|
||
#[test] | ||
fn issue_256() { | ||
assert_suggestion_result( | ||
"The bananas is tasty", | ||
PluralConjugate::default(), | ||
"The bananas are tasty", | ||
); | ||
} | ||
|
||
#[test] | ||
fn plural_students() { | ||
assert_suggestion_result( | ||
"The students is doing their homework.", | ||
PluralConjugate::default(), | ||
"The students are doing their homework.", | ||
); | ||
} | ||
|
||
#[test] | ||
fn singular_house() { | ||
assert_suggestion_result( | ||
"The house are just sitting there.", | ||
PluralConjugate::default(), | ||
"The house is just sitting there.", | ||
); | ||
} | ||
} |
This file contains 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 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 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