{"record":{"id":"3f1ccead396a7c00","repo":"oxc-project/oxc","slug":"unexpected-kind-declaration-in-the-global-scope","errorCode":null,"errorMessage":"Unexpected {kind} declaration in the global scope.","messagePattern":"Unexpected (.+?) declaration in the global scope\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/no_implicit_globals.rs","lineNumber":17,"sourceCode":"use javascript_globals::{GLOBALS, GLOBALS_BUILTIN};\nuse oxc_ast::AstKind;\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\nuse oxc_syntax::{reference::Reference, symbol::SymbolFlags};\nuse schemars::JsonSchema;\nuse serde::{Deserialize, Serialize};\n\nuse crate::{\n    config::GlobalValue,\n    context::LintContext,\n    rule::{DefaultRuleConfig, Rule},\n};\n\nfn global_non_lexical_binding_diagnostic(kind: &'static str, span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"Unexpected {kind} declaration in the global scope.\"))\n        .with_help(\n            \"Wrap it in an IIFE for a local variable, or assign it as a global property for a global variable.\",\n        )\n        .with_label(span)\n}\n\nfn global_lexical_binding_diagnostic(kind: &'static str, span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"Unexpected {kind} declaration in the global scope.\"))\n        .with_help(\"Wrap it in a block or in an IIFE.\")\n        .with_label(span)\n}\n\nfn global_variable_leak_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Global variable leak.\")\n        .with_help(\"Declare the variable if it is intended to be local.\")\n        .with_label(span)\n}\n","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_implicit_globals.rs#L1-L35","documentation":"Diagnostic from oxlint's no-implicit-globals rule. In classic scripts (non-module files) a top-level var or function declaration does not create a file-local binding; it writes properties onto the global object shared by every script in the runtime. The rule reports 'Unexpected {kind} declaration in the global scope.' with kind 'var' or 'function' and suggests wrapping in an IIFE or assigning an explicit global property.","triggerScenarios":"A .js file loaded via a script tag containing top-level var config = {...} or function boot() {}; bundler-less deployments where several scripts share the page; script files with no import/export statements.","commonSituations":"Legacy browser apps and widgets; missing \"type\": \"module\" in package.json so files are treated as scripts; ads/analytics snippets polluting the page; migrating an old codebase toward modules.","solutions":["Wrap the file body in an IIFE: (() => { ... })();.","Convert the file to an ES module (add import/export or set \"type\": \"module\").","If a real global is intended, assign it explicitly: globalThis.config = {...} or window.config = {...}.","Introduce a bundler so each file gets its own scope."],"exampleFix":"// before (script.js, loaded via <script>)\nvar config = { debug: false };\nfunction boot() {\n  /* ... */\n}\n\n// after\n(() => {\n  const config = { debug: false };\n  function boot() {\n    /* ... */\n  }\n})();","handlingStrategy":"validation","validationCode":"const isScript = !/^\\s*(?:import|export)\\b/m.test(source);\nconst leaks = isScript && /^\\s*(?:var\\b|function\\b)/m.test(source);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Wrap script-file bodies in IIFEs to obtain local scope.","Prefer ES modules; set \"type\": \"module\" where possible.","Assign deliberate globals explicitly on globalThis/window."],"tags":["oxlint","lint","globals","scope","scripts"],"backgroundTag":"implicit-global-declaration","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"}