oxc-project/oxc · warning · OxcDiagnostic
Export statements should appear at the end of the file
Error message
Export statements should appear at the end of the file
What it means
oxlint's `import/exports-last` rule: within a module, every export declaration must appear after all non-export statements. The implementation scans the Program body, finds the position of the last non-export statement, and reports any module declaration sitting before it.
Source
Thrown at crates/oxc_linter/src/rules/import/exports_last.rs:10
use itertools::Itertools;
use oxc_ast::ast::{ModuleDeclaration, Statement};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use crate::{context::LintContext, rule::Rule};
fn exports_last_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Export statements should appear at the end of the file")
.with_help("Move this export to the end of the file, after all other statements.")
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct ExportsLast;
declare_oxc_lint!(
/// ### What it does
///
/// This rule enforces that all exports are declared at the bottom of the file.
/// This rule will report any export declarations that comes before any non-export statements.
///
/// ### Why is this bad?
///
/// Exports scattered throughout the file can lead to poor code readability
/// and increase the cost of locating the export quickly
///View on GitHub (pinned to e1e7af627c)
Solutions
- Move export declarations below the last non-export statement
- Or switch to bottom exports: define everything first, then one `export { a, b, c };` block at the end
- Disable the rule if top-of-file exports are the accepted team style
Example fix
// before — export appears before a non-export statement export const helper = () => 1; const other = compute(); // after const other = compute(); export const helper = () => 1;
Defensive patterns
Strategy: validation
Validate before calling
// .oxlintrc.json
{
"plugins": ["import"],
"rules": { "import/exports-last": "warn" }
}
// CI gate: npx oxlint src/ Prevention
- Adopt bottom-of-file export blocks (define everything, then export { ... }) so the rule is satisfied by construction
- Watch codemods that insert statements after existing exports
- If the team prefers top-of-file exports, turn the rule off rather than fighting it
- Run oxlint in lint-staged so misplaced exports are flagged at commit time
When it happens
Trigger: `export const helper = () => 1;` followed later in the same file by any non-export statement — a plain const, function declaration, or side-effect call positioned after the export.
Common situations: Files exporting constants at the top for readability while helpers grow beneath; codemods inserting statements after existing exports; teams whose convention keeps the public API at the top of the file.
Related errors
- Prefer using inline type specifiers instead of a top-level t
- Prefer using a top-level type-only import instead of inline
- Prefer using a top-level type-only import instead of inline
- Type imports from declaration files must use top-level `impo
- No named exports found in module '{module_name}'
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/2baec7c551ff36a8.
Report an issue: GitHub.