{"record":{"id":"08a0a00ede8c49c1","repo":"oxc-project/oxc","slug":"variable-or-function-declarations-are-not-allowe","errorCode":null,"errorMessage":"Variable or `function` declarations are not allowed in nested blocks","messagePattern":"Variable or `function` declarations are not allowed in nested blocks","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/eslint/no_inner_declarations.rs","lineNumber":11,"sourceCode":"use oxc_ast::AstKind;\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\nuse schemars::JsonSchema;\nuse serde::{Deserialize, Serialize};\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn no_inner_declarations_diagnostic(decl_type: &str, body: &str, span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Variable or `function` declarations are not allowed in nested blocks\")\n        .with_help(format!(\"Move {decl_type} declaration to {body} root\"))\n        .with_label(span)\n}\n\n/// Determines what type of declarations to check.\n#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Serialize, Deserialize, JsonSchema)]\n#[serde(rename_all = \"lowercase\")]\nenum NoInnerDeclarationsConfig {\n    /// Disallows function declarations in nested blocks.\n    #[default]\n    Functions,\n    /// Disallows function and var declarations in nested blocks.\n    Both,\n}\n\n#[derive(Debug, Default, Clone, Deserialize, JsonSchema)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\nstruct NoInnerDeclarationsOptions {","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_inner_declarations.rs#L1-L29","documentation":"Diagnostic from oxlint's no-inner-declarations rule (ESLint port, eslint:recommended). Function declarations inside nested blocks have implementation-defined hoisting in sloppy mode, and var inside blocks hoists to the function root while looking block-scoped. The rule reports function declarations (default 'functions' mode) or functions plus var (option 'both') declared anywhere except the program root or the top level of a function body, with help text 'Move {decl_type} declaration to {body} root'.","triggerScenarios":"if (x) { function handle() {} } in a script; for (...) { var item = ...; } with the 'both' option; function declarations inside plain blocks or switch cases; declarations in catch blocks.","commonSituations":"Sloppy-mode scripts where block-level function declarations behave differently across engines; var-in-loop refactors; enabling the 'both' option on an older codebase and hitting hundreds of var reports.","solutions":["Move the function declaration to the top of the enclosing function or program.","Replace it with a function expression or arrow function assigned to const inside the block.","Keep the rule in default 'functions' mode, or switch to 'both' only after fixing var-in-block findings.","Extract the block body into a named function so the declaration lands at a valid root."],"exampleFix":"// before\nif (enabled) {\n  function run() {\n    start();\n  }\n  run();\n}\n\n// after\nfunction run() {\n  start();\n}\nif (enabled) {\n  run();\n}","handlingStrategy":"validation","validationCode":"npx oxlint -A all -D no-inner-declarations src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Declare functions at the top level of the enclosing function or program.","Use const arrow or function expressions for block-local helpers.","Adopt the 'both' option only after fixing var-in-block findings."],"tags":["eslint","oxlint","lint","hoisting","scope","functions"],"backgroundTag":"declaration-in-nested-block","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"}