{"record":{"id":"67f6dda24775919b","repo":"oxc-project/oxc","slug":"return-should-be-used-inside-a-function","errorCode":null,"errorMessage":"\"return\" should be used inside a function.","messagePattern":"\"return\" should be used inside a function\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_module.rs","lineNumber":21,"sourceCode":"use oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\n\nuse crate::{\n    AstNode,\n    context::{ContextHost, LintContext},\n    rule::Rule,\n};\n\nconst COMMON_JS_GLOBALS: [&str; 5] = [\"exports\", \"require\", \"module\", \"__filename\", \"__dirname\"];\n\nfn use_strict_directive_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(r#\"Do not use \"use strict\" directive.\"#)\n        .with_help(\"ES modules are always strict mode, so this directive is redundant.\")\n        .with_label(span)\n}\n\nfn global_return_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(r#\"\"return\" should be used inside a function.\"#).with_label(span)\n}\n\nfn common_js_global_diagnostic(span: Span, name: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(r#\"Do not use \"{name}\".\"#))\n        .with_help(\"Prefer ES modules over CommonJS globals.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferModule;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Prefer JavaScript modules (ESM) over CommonJS.\n    ///\n    /// ### Why is this bad?\n    ///","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_module.rs#L3-L39","documentation":"Diagnostic from the unicorn/prefer-module lint rule. It fires when a top-level `return` statement is found outside any function — a CommonJS idiom used to stop module execution that is illegal or meaningless in ES modules. The flagged input is the top-level return statement. It is a lint error for module code, not a runtime exception.","triggerScenarios":"Thrown at crates/oxc_linter/src/rules/unicorn/prefer_module.rs:21 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Wrap the early-exit logic in a function, or invert the condition with an `if`","Replace the top-level `return` with an `if` guard around the remaining code","Restructure the module body so no early top-level exit is needed"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"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"}