diff --git a/crates/oxc_linter/src/rules/eslint/max_nested_callbacks.rs b/crates/oxc_linter/src/rules/eslint/max_nested_callbacks.rs index d49e86b422a9f..f6bcc46342bb1 100644 --- a/crates/oxc_linter/src/rules/eslint/max_nested_callbacks.rs +++ b/crates/oxc_linter/src/rules/eslint/max_nested_callbacks.rs @@ -18,11 +18,19 @@ fn max_nested_callbacks_diagnostic(num: usize, max: usize, span: Span) -> OxcDia .with_label(span) } -#[derive(Debug, Default, Clone)] +#[derive(Debug, Clone)] pub struct MaxNestedCallbacks { max: usize, } +const DEFAULT_MAX_NESTED_CALLBACKS: usize = 10; + +impl Default for MaxNestedCallbacks { + fn default() -> Self { + Self { max: DEFAULT_MAX_NESTED_CALLBACKS } + } +} + declare_oxc_lint!( /// ### What it does /// @@ -126,7 +134,9 @@ impl Rule for MaxNestedCallbacks { .and_then(|config| config.get("max")) .and_then(Value::as_number) .and_then(serde_json::Number::as_u64) - .map_or(10, |v| usize::try_from(v).unwrap_or(10)) + .map_or(DEFAULT_MAX_NESTED_CALLBACKS, |v| { + usize::try_from(v).unwrap_or(DEFAULT_MAX_NESTED_CALLBACKS) + }) }; Self { max } } @@ -140,10 +150,14 @@ fn is_callback<'a>(node: &AstNode<'a>, semantic: &Semantic<'a>) -> bool { #[test] fn test() { use crate::tester::Tester; + fn nested_functions(d: usize) -> String { ["foo(function() {".repeat(d), "});".repeat(d)].concat() } + let defaults = MaxNestedCallbacks::default(); + assert_eq!(defaults.max, 10); + let nested_10 = nested_functions(10); let nested_11 = nested_functions(11);