Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat: add lint for on the floor phrase (#1555)

thuan141213cd2afd5655
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 0cd9d0424..098f5b108 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -68,6 +68,7 @@ use super::noun_countability::NounCountability;
use super::noun_instead_of_verb::NounInsteadOfVerb;
use super::number_suffix_capitalization::NumberSuffixCapitalization;
use super::of_course::OfCourse;
+use super::on_floor::OnFloor;
use super::one_and_the_same::OneAndTheSame;
use super::open_the_light::OpenTheLight;
use super::out_of_date::OutOfDate;
@@ -443,6 +444,7 @@ impl LintGroup {
insert_expr_rule!(NounInsteadOfVerb, true);
insert_struct_rule!(NumberSuffixCapitalization, true);
insert_struct_rule!(OfCourse, true);
+ insert_expr_rule!(OnFloor, true);
insert_expr_rule!(OneAndTheSame, true);
insert_expr_rule!(OpenTheLight, true);
insert_expr_rule!(OutOfDate, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 99dbc7066..ff9c595dc 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -74,6 +74,7 @@ mod noun_countability;
mod noun_instead_of_verb;
mod number_suffix_capitalization;
mod of_course;
+pub mod on_floor;
mod one_and_the_same;
mod open_compounds;
mod open_the_light;
@@ -184,6 +185,7 @@ pub use noun_countability::NounCountability;
pub use noun_instead_of_verb::NounInsteadOfVerb;
pub use number_suffix_capitalization::NumberSuffixCapitalization;
pub use of_course::OfCourse;
+pub use on_floor::OnFloor;
pub use one_and_the_same::OneAndTheSame;
pub use open_the_light::OpenTheLight;
pub use out_of_date::OutOfDate;
diff --git a/harper-core/src/linting/on_floor.rs b/harper-core/src/linting/on_floor.rs
new file mode 100644
index 000000000..bc3a8dbb6
--- /dev/null
+++ b/harper-core/src/linting/on_floor.rs
@@ -0,0 +1,165 @@
+use crate::CharStringExt;
+use crate::expr::Expr;
+use crate::expr::LongestMatchOf;
+use crate::expr::SequenceExpr;
+use crate::patterns::WordSet;
+use crate::{
+ Lrc, Token, TokenStringExt,
+ linting::{ExprLinter, Lint, LintKind, Suggestion},
+};
+
+pub struct OnFloor {
+ expr: Box<dyn Expr>,
+}
+
+impl Default for OnFloor {
+ fn default() -> Self {
+ let preposition = WordSet::new(&["in", "at"]);
+
+ let on_the_floor = Lrc::new(
+ SequenceExpr::default()
+ .then(preposition)
+ .t_ws()
+ .t_aco("the")
+ .t_ws()
+ .t_any()
+ .t_ws()
+ .t_aco("floor"),
+ );
+
+ let look_up_phrase = Lrc::new(
+ SequenceExpr::default()
+ .then(WordSet::new(&["look", "looking", "looks", "looked"]))
+ .t_ws()
+ .t_aco("up"),
+ );
+
+ let stop = Lrc::new(WordSet::new(&["stop", "stopping", "stops", "stopped"]));
+ let exceptions = Lrc::new(LongestMatchOf::new(vec![
+ Box::new(SequenceExpr::default().then(look_up_phrase.clone())),
+ Box::new(SequenceExpr::default().then(stop.clone())),
+ ]));
+
+ let pattern = LongestMatchOf::new(vec![
+ Box::new(on_the_floor.clone()),
+ Box::new(
+ SequenceExpr::default()
+ .then(exceptions.clone())
+ .t_ws()
+ .then(on_the_floor.clone()),
+ ),
+ ]);
+
+ Self {
+ expr: Box::new(pattern),
+ }
+ }
+}
+
+impl ExprLinter for OnFloor {
+ fn expr(&self) -> &dyn Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
+ let incorrect_preposition = matched_tokens[0..1].span()?.get_content(source).to_string();
+ // if the first token is not "in" or "at", means that the match is belong to the exceptions
+ // so we don't need to lint it
+ if !["in", "at"].contains(&incorrect_preposition.to_lowercase().as_str()) {
+ return None;
+ }
+ let span = matched_tokens[0..1].span()?;
+
+ Some(Lint {
+ lint_kind: LintKind::WordChoice,
+ span,
+ suggestions: vec![Suggestion::replace_with_match_case_str(
+ "on",
+ span.get_content(source),
+ )],
+ message: format!(
+ "Corrects `{incorrect_preposition}` to `on` when talking about position inside a building",
+ )
+ .to_string(),
+ priority: 63,
+ })
+ }
+
+ fn description(&self) -> &'static str {
+ "This rule identifies incorrect uses of the prepositions `in` or `at` when referring to locations inside a building and recommends using `on the floor` instead."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::OnFloor;
+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+
+ #[test]
+ fn not_lint_with_correct_phrase() {
+ assert_lint_count(
+ "I'm living on the 3rd floor of a building.",
+ OnFloor::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn lint_with_in() {
+ assert_suggestion_result(
+ "I'm living in the 3rd floor of a building.",
+ OnFloor::default(),
+ "I'm living on the 3rd floor of a building.",
+ );
+ }
+
+ #[test]
+ fn lint_with_at() {
+ assert_suggestion_result(
+ "I'm living at the second floor of a building.",
+ OnFloor::default(),
+ "I'm living on the second floor of a building.",
+ );
+ }
+
+ #[test]
+ fn in_the_start_of_sentence() {
+ assert_suggestion_result(
+ "In the 3rd floor of a building.",
+ OnFloor::default(),
+ "On the 3rd floor of a building.",
+ );
+ }
+
+ #[test]
+ fn at_the_start_of_sentence() {
+ assert_suggestion_result(
+ "At the second floor of a building.",
+ OnFloor::default(),
+ "On the second floor of a building.",
+ );
+ }
+
+ #[test]
+ fn no_lint_with_look_up_at() {
+ assert_lint_count("She looked up at the third floor.", OnFloor::default(), 0);
+ }
+
+ #[test]
+ fn no_lint_with_stop_at() {
+ assert_lint_count(
+ "The elevator stops at the 3rd floor of a building.",
+ OnFloor::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn no_lint_with_looking_up_at() {
+ assert_lint_count(
+ "The workers are looking up at the 3rd floor of a building.",
+ OnFloor::default(),
+ 0,
+ );
+ }
+}