{"record":{"id":"b1be9c42996d13cb","repo":"oxc-project/oxc","slug":"hoisted-api-cannot-be-used-in-a-runtime-location-i","errorCode":null,"errorMessage":"Hoisted API cannot be used in a runtime location in this file.","messagePattern":"Hoisted API cannot be used in a runtime location in this file\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/vitest/hoisted_apis_on_top.rs","lineNumber":18,"sourceCode":"use oxc_ast::AstKind;\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{\n    context::LintContext,\n    fixer::RuleFixer,\n    rule::Rule,\n    utils::{\n        JestFnKind, JestGeneralFnKind, KnownMemberExpressionProperty, PossibleJestNode,\n        parse_general_jest_fn_call,\n    },\n};\n\nfn hoisted_apis_on_top_diagnostic(span: Span) -> OxcDiagnostic {\n    // TODO: Only mention the doMock alternative if the API in question is `vi.mock()`.\n    OxcDiagnostic::warn(\"Hoisted API cannot be used in a runtime location in this file.\")\n        .with_help(\"Move this hoisted API to the top of the file to better reflect its behavior.\\nYou may alternatively replace `vi.mock()` with `vi.doMock`, which is not hoisted.\")\n        .with_label(span)\n}\n\nconst HOISTED_APIS: [&str; 3] = [\"mock\", \"hoisted\", \"unmock\"];\n\n#[derive(Debug, Default, Clone)]\npub struct HoistedApisOnTop;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Requires [hoisted](https://developer.mozilla.org/en-US/docs/Glossary/Hoisting) Vitest APIs\n    /// (`vi.mock`, `vi.unmock`, and `vi.hoisted`) to appear at the top level of the file.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Vitest hoists certain APIs to the top of the file during transformation, so they always","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/vitest/hoisted_apis_on_top.rs#L1-L36","documentation":"Emitted by the vitest/hoisted-apis-on-top rule when a hoisted vitest API (`vi.mock`, `vi.unmock`, `vi.doMock`, `vi.doUnmock`) is called in a runtime location — i.e. inside another function, hook, or non-top-level scope. These APIs are hoisted by vitest to the top of the file, so calling them from runtime code is invalid or misleading.","triggerScenarios":"Thrown at crates/oxc_linter/src/rules/vitest/hoisted_apis_on_top.rs:18 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Move the `vi.mock()`/`vi.doMock()` call to the top level of the test file","Use `vi.doMock` semantics deliberately, or restructure so mocking happens at module top level"],"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"}