{"record":{"id":"322bc7444c339176","repo":"oxc-project/oxc","slug":"all-var-declarations-must-be-at-the-top-of-the-f","errorCode":null,"errorMessage":"All 'var' declarations must be at the top of the function scope.","messagePattern":"All 'var' declarations must be at the top of the function scope\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/vars_on_top.rs","lineNumber":10,"sourceCode":"use oxc_ast::AstKind;\nuse oxc_ast::ast::{Declaration, Expression, Program, Statement, VariableDeclarationKind};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{AstNode, context::LintContext, rule::Rule, utils::has_ambient_typescript_ancestor};\n\nfn vars_on_top_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"All 'var' declarations must be at the top of the function scope.\")\n        .with_help(\"Consider moving this to the top of the functions scope or using let or const to declare this variable.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct VarsOnTop;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Enforces that all `var` declarations are placed at the top of their containing scope.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// In JavaScript, `var` declarations are hoisted to the top of their containing scope. Placing `var` declarations at the top explicitly improves code readability and maintainability by making the scope of variables clear.\n    ///\n    /// ### Examples\n    ///","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/vars_on_top.rs#L1-L28","documentation":"oxlint's `vars-on-top` rule enforces the style where every `var` declaration in a function (or program) scope precedes all other statements — matching what hoisting actually does at runtime. The diagnostic fires on any var declaration that follows a non-var statement in its scope; ambient TypeScript contexts are exempted via `has_ambient_typescript_ancestor`.","triggerScenarios":"`function f() { doFirst(); var x = 1; }` — a VariableDeclaration of kind var appearing after expression or other statements within the same function scope (or top-level program scope).","commonSituations":"Legacy ES5 codebases; incremental migrations where pockets of var remain; merge conflicts that move statements above existing var declarations.","solutions":["Replace var with let/const declared where the value is first needed (preferred modern fix)","Or move the var declarations to the top of the function scope","Disable or downgrade `vars-on-top` if the team has fully adopted let/const and the rule is pure noise"],"exampleFix":"// before\nfunction process(input) {\n  validate(input);\n  var results = [];\n  for (var item of input) results.push(transform(item));\n  return results;\n}\n\n// after\nfunction process(input) {\n  validate(input);\n  const results = [];\n  for (const item of input) results.push(transform(item));\n  return results;\n}","handlingStrategy":"validation","validationCode":"// .oxlintrc.json\n{ \"rules\": { \"vars-on-top\": \"warn\" } }\n// pre-commit: npx oxlint --fix src/legacy/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer let/const at point of use; var should be rare enough that the rule stays quiet","Isolate hoisting-prone ES5 code in folders with their own lint config","Watch merge conflicts that move statements above existing var declarations","Run the fixer on legacy directories during migrations, not hand-edits"],"tags":["lint","oxlint","eslint","style","var","hoisting","scoping"],"backgroundTag":"var-declaration-placement","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}