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

  1. Move export declarations below the last non-export statement
  2. Or switch to bottom exports: define everything first, then one `export { a, b, c };` block at the end
  3. 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

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


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/2baec7c551ff36a8. Report an issue: GitHub.