Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
refactor: centralize suggestion transformation depth limit (#3614)
jdgajjar6294ee09061c
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 98bd295c5..76b7cfe82 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -314,6 +314,13 @@ pub use suggestion::{Suggestion, SuggestionCollectionExt};use crate::{Document, LSend, render_markdown};+/// Maximum number of sequential lint suggestions explored when searching for a+/// transformation path (for example in Weir rule tests or linting test helpers).+///+/// This is a compile-time limit that prevents unbounded search when suggestion+/// application cycles or deep multi-step fixes are involved.+pub const MAX_SUGGESTION_TRANSFORMATION_DEPTH: usize = 100;+/// A __stateless__ rule that searches documents for grammatical errors.////// Commonly implemented via [`ExprLinter`].@@ -601,7 +608,8 @@ pub mod tests {/// Applies suggestions iteratively until any combination produces the expected result.////// Explores all possible suggestion branches (depth-first search) until finding a path- /// that produces the expected result. Stops after 100 iterations to prevent infinite loops.+ /// that produces the expected result. Stops after+ /// [`MAX_SUGGESTION_TRANSFORMATION_DEPTH`] iterations to prevent infinite loops.////// Use this when you want to verify that *some* suggestion sequence produces the/// expected result, without caring which specific suggestions are used.@@ -637,8 +645,11 @@ pub mod tests {depth: usize,) -> bool {// Prevent infinite recursion (e.g. cycles in suggestions)- if depth > 100 {- eprintln!("⚠️ Reached depth limit (100)");+ if depth > super::MAX_SUGGESTION_TRANSFORMATION_DEPTH {+ eprintln!(+ "⚠️ Reached depth limit ({})",+ super::MAX_SUGGESTION_TRANSFORMATION_DEPTH+ );return false;}diff --git a/harper-core/src/weir/mod.rs b/harper-core/src/weir/mod.rsindex c7d5ccea1..97cea9a74 100644--- a/harper-core/src/weir/mod.rs+++ b/harper-core/src/weir/mod.rs@@ -17,7 +17,10 @@ use parsing::{parse_expr_str, parse_str};use strum_macros::{AsRefStr, EnumString};use crate::expr::{Expr, ExprExt};-use crate::linting::{Chunk, ExprLinter, Lint, LintKind, Linter, Sentence, Suggestion};+use crate::linting::{+ Chunk, ExprLinter, Lint, LintKind, Linter, MAX_SUGGESTION_TRANSFORMATION_DEPTH, Sentence,+ Suggestion,+};use crate::parsers::Markdown;use crate::spell::FstDictionary;use crate::{Document, Lrc, Token, TokenStringExt};@@ -232,7 +235,7 @@ impl WeirLinter {return Some(current);}- if depth >= 100 {+ if depth >= MAX_SUGGESTION_TRANSFORMATION_DEPTH {continue;}@@ -280,7 +283,7 @@ impl WeirLinter {}iter_count += 1;- if iter_count == 100 {+ if iter_count == MAX_SUGGESTION_TRANSFORMATION_DEPTH {break;}}