Skip to content

Commit f111abe

Browse files
authored
refactor: improvements to ProgressiveNeedsBe (#2232)
1 parent 3b0a502 commit f111abe

File tree

3 files changed

+68
-16
lines changed

3 files changed

+68
-16
lines changed

harper-core/src/expr/sequence_expr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ impl SequenceExpr {
443443
gen_then_from_is!(verb_lemma);
444444
gen_then_from_is!(verb_simple_past_form);
445445
gen_then_from_is!(verb_past_participle_form);
446+
gen_then_from_is!(verb_progressive_form);
446447

447448
// Adjectives
448449

harper-core/src/linting/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,7 @@ pub mod tests {
481481
let transformed_str = transform_nth_str(text, &mut linter, n);
482482

483483
if transformed_str.as_str() != expected_result {
484-
panic!(
485-
"Expected \"{transformed_str}\" to be \"{expected_result}\" after applying the computed suggestions."
486-
);
484+
panic!("Expected \"{expected_result}\"\n But got \"{transformed_str}\"");
487485
}
488486

489487
// Applying the suggestions should fix all the lints.

harper-core/src/linting/progressive_needs_be.rs

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use crate::expr::Expr;
2-
use crate::expr::SequenceExpr;
3-
use crate::{Token, TokenKind};
1+
use crate::Token;
2+
use crate::expr::{Expr, SequenceExpr};
43

54
use super::{ExprLinter, Lint, LintKind, Suggestion};
65

@@ -12,15 +11,17 @@ impl Default for ProgressiveNeedsBe {
1211
fn default() -> Self {
1312
// Support both contracted (I've/We've/You've/They've) and non-contracted
1413
// (I have/We have/You have/They have) forms before a progressive verb.
15-
let contracted = SequenceExpr::word_set(&["I've", "We've", "You've", "They've"])
16-
.t_ws()
17-
.then_kind_both(TokenKind::is_verb, TokenKind::is_verb_progressive_form);
14+
let contracted = SequenceExpr::word_set(&[
15+
"I've", "We've", "You've", "They've", "Ive", "Weve", "Youve", "Theyve",
16+
])
17+
.t_ws()
18+
.then_verb_progressive_form();
1819

1920
let non_contracted = SequenceExpr::word_set(&["I", "We", "You", "They"])
2021
.t_ws()
2122
.then_any_capitalization_of("have")
2223
.t_ws()
23-
.then_kind_both(TokenKind::is_verb, TokenKind::is_verb_progressive_form);
24+
.then_verb_progressive_form();
2425

2526
let expr = SequenceExpr::any_of(vec![Box::new(contracted), Box::new(non_contracted)]);
2627

@@ -55,15 +56,14 @@ impl ExprLinter for ProgressiveNeedsBe {
5556
};
5657

5758
// Choose the correct "be" contraction based on the pronoun
58-
let pronoun_str: String = first_word.span.get_content(src).iter().copied().collect();
59-
let lower = pronoun_str.to_lowercase();
60-
let progressive_replacement = if lower.starts_with("i") {
59+
let pronoun = first_word.span.get_content(src);
60+
let progressive_replacement = if pronoun.starts_with_ignore_ascii_case_str("i") {
6161
"I'm"
62-
} else if lower.starts_with("we") {
62+
} else if pronoun.starts_with_ignore_ascii_case_str("we") {
6363
"We're"
64-
} else if lower.starts_with("you") {
64+
} else if pronoun.starts_with_ignore_ascii_case_str("you") {
6565
"You're"
66-
} else if lower.starts_with("they") {
66+
} else if pronoun.starts_with_ignore_ascii_case_str("they") {
6767
"They're"
6868
} else {
6969
"I'm"
@@ -854,4 +854,57 @@ mod tests {
854854
"We're\nworking on it today.",
855855
);
856856
}
857+
858+
//////////
859+
860+
#[test]
861+
#[ignore = "Handling the progressive `being` will need a special case"]
862+
fn test_ive_being() {
863+
assert_good_and_bad_suggestions(
864+
"I've being playing with languages.toml",
865+
ProgressiveNeedsBe::default(),
866+
&[
867+
"I've been playing with languages.toml",
868+
"I'm playing with languages.toml",
869+
],
870+
&[],
871+
);
872+
}
873+
874+
#[test]
875+
fn test_ive_doing_no_apostrophe() {
876+
assert_suggestion_result(
877+
"Ive always seen the variables and debug into it, and thats what ive doing.",
878+
ProgressiveNeedsBe::default(),
879+
"Ive always seen the variables and debug into it, and thats what i'm doing.",
880+
);
881+
}
882+
883+
#[test]
884+
fn test_ive_looking_no_apostrophe() {
885+
assert_suggestion_result(
886+
"Ive looking for a way to get temperature and humidity for all of our rooms within for a reasonable price in Germany.",
887+
ProgressiveNeedsBe::default(),
888+
"I'm looking for a way to get temperature and humidity for all of our rooms within for a reasonable price in Germany.",
889+
);
890+
}
891+
892+
#[test]
893+
#[ignore = "Handling the progressive `being` will need a special case"]
894+
fn test_youve_being() {
895+
assert_suggestion_result(
896+
"Thanks for all the work you've being doing for this project btw!",
897+
ProgressiveNeedsBe::default(),
898+
"Thanks for all the work you're doing for this project btw!",
899+
);
900+
}
901+
902+
#[test]
903+
fn test_theyve_doing() {
904+
assert_suggestion_result(
905+
"it’s also kind of implied users read the documentation or generally have a sense of what they’ve doing and what could go wrong",
906+
ProgressiveNeedsBe::default(),
907+
"it’s also kind of implied users read the documentation or generally have a sense of what they're doing and what could go wrong",
908+
);
909+
}
857910
}

0 commit comments

Comments
 (0)