Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix(core): Weir options should resolve independent of order (#3523)
Elijah Potter242ba270ffe1
diff --git a/harper-core/src/expr/longest_match_of.rs b/harper-core/src/expr/longest_match_of.rsindex f2e2bf10d..d7961b98e 100644--- a/harper-core/src/expr/longest_match_of.rs+++ b/harper-core/src/expr/longest_match_of.rs@@ -14,6 +14,10 @@ impl LongestMatchOf {pub fn add(&mut self, expr: impl Expr + 'static) {self.exprs.push(Box::new(expr));}++ pub fn add_boxed(&mut self, expr: Box<dyn Expr>) {+ self.exprs.push(expr);+ }}impl Expr for LongestMatchOf {diff --git a/harper-core/src/weir/ast.rs b/harper-core/src/weir/ast.rsindex d22028c74..74e1268b3 100644--- a/harper-core/src/weir/ast.rs+++ b/harper-core/src/weir/ast.rs@@ -3,7 +3,7 @@ use hashbrown::HashMap;use is_macro::Is;use itertools::Itertools;-use crate::expr::{Expr, Filter, FirstMatchOf, SequenceExpr, UnlessStep};+use crate::expr::{Expr, Filter, LongestMatchOf, SequenceExpr, UnlessStep};use crate::patterns::{AnyPattern, DerivedFrom, UPOSSet, WhitespacePattern, Word};use crate::{CharString, CharStringExt, Lrc, Punctuation, Token};@@ -128,7 +128,7 @@ impl AstExprNode {Ok(Box::new(expr))}AstExprNode::Arr(children) => {- let mut expr = FirstMatchOf::default();+ let mut expr = LongestMatchOf::default();for node in children {expr.add_boxed(node.to_expr(ctx_exprs)?);diff --git a/harper-core/src/weir/mod.rs b/harper-core/src/weir/mod.rsindex fc9939eab..c7d5ccea1 100644--- a/harper-core/src/weir/mod.rs+++ b/harper-core/src/weir/mod.rs@@ -463,6 +463,30 @@ pub mod tests {assert_eq!(5, linter.count_tests());}+ #[test]+ fn array_prefers_longest_match_over_first_match() {+ for main in [+ "[(capitalized off of), (capitalized off)]",+ "[(capitalized off), (capitalized off of)]",+ ] {+ let source = format!(+ r#"+ expr main {main}+ let message "Use the replacement."+ let description "Regression test for overlapping Weir array options."+ let kind "Miscellaneous"+ let becomes "replacement"+ let strategy "Exact"++ test "capitalized off of" "replacement"+ "#+ );++ let mut linter = WeirLinter::new(&source).unwrap();+ assert_passes_all(&mut linter);+ }+ }+#[test]fn g_suite_with_refs() {let source = r#"