{"record":{"id":"f7b7c8ea22db3ede","repo":"oxc-project/oxc","slug":"function-has-too-many-statements-count-maximu","errorCode":null,"errorMessage":"function has too many statements ({count}). Maximum allowed is {max}.","messagePattern":"function has too many statements \\((.+?)\\)\\. Maximum allowed is (.+?)\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/max_statements.rs","lineNumber":31,"sourceCode":"use schemars::JsonSchema;\nuse serde::{Deserialize, Serialize};\nuse serde_json::Value;\n\nuse crate::{context::LintContext, rule::Rule};\n\nfn max_statements_diagnostic(\n    name: Option<&str>,\n    count: u32,\n    max: u32,\n    span: Span,\n) -> OxcDiagnostic {\n    let message = if let Some(name) = name {\n        format!(\"function `{name}` has too many statements ({count}). Maximum allowed is {max}.\")\n    } else {\n        format!(\"function has too many statements ({count}). Maximum allowed is {max}.\")\n    };\n\n    OxcDiagnostic::warn(message)\n        .with_help(\"Consider splitting it into smaller functions.\")\n        .with_label(span)\n}\n\nconst DEFAULT_MAX_STATEMENTS: u32 = 10;\n\n#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\npub struct MaxStatementsConfig {\n    /// Maximum number of statements allowed per function.\n    max: u32,\n    /// Whether to ignore top-level functions.\n    ignore_top_level_functions: bool,\n}\n\nimpl Default for MaxStatementsConfig {\n    fn default() -> Self {\n        Self { max: DEFAULT_MAX_STATEMENTS, ignore_top_level_functions: false }","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/max_statements.rs#L13-L49","documentation":"The anonymous variant of `max-statements`: the function body exceeds the statement budget (default 10) and the diagnostic cannot resolve a name, so the message reads \"function has too many statements\". Built by the same `max_statements_diagnostic` at crates/oxc_linter/src/rules/eslint/max_statements.rs:31 choosing the `None`-name branch. Configuration (`max`, `ignoreTopLevelFunctions`) is shared with the named variant.","triggerScenarios":"Anonymous function expressions and IIFEs with more than 10 direct statements: `router.get('/x', function (req, res) { /* 12 statements */ })`, module-level IIFE setup blocks, callback bodies in event emitters.","commonSituations":"Route handlers and middleware bodies in Express-style apps; IIFE bootstrap code; converting such anonymous bodies to named functions (message then switches to the named variant); enabling the rule in CI and getting dozens of reports in legacy routes files.","solutions":["Extract the anonymous body into a named, testable function with fewer statements per unit.","Raise `max` or set `ignoreTopLevelFunctions: true` in the rule configuration for wiring-style code.","Simplify the body itself: early returns, table-driven dispatch, and helper extraction reduce raw statement counts."],"exampleFix":"// before\napp.get('/report', function (req, res) { /* 13 statements: auth, parse, query, format, ... */ });\n\n// after\napp.get('/report', (req, res) => {\n  reportHandler(req, res).catch(next);\n});","handlingStrategy":"validation","validationCode":"// Gate anonymous bodies too (same rule/config): oxlint --rule max-statements='{\"max\":10}' src/\n// Route handlers: keep the anonymous wrapper 1-3 statements and delegate.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Make callback bodies thin adapters that delegate to named handlers — this also makes the logic unit-testable.","Avoid IIFEs for module setup; hoisted named functions read better and report with useful names.","When a middleware grows statements, extract stages (parse, authorize, handle) as separate middleware or helpers."],"tags":["eslint","oxlint","complexity","function-length","statements","anonymous-function"],"backgroundTag":"too-many-statements","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}