oxc-project/oxc · error · OxcDiagnostic

Import statements must come first

Error message

Import statements must come first

What it means

Fires from import/first when an import declaration appears after non-import statements in the module, i.e. the imports are not at the top of the file. The diagnostic labels the offending import's span and suggests moving it to the top.

Source

Thrown at crates/oxc_linter/src/rules/import/first.rs:14

use oxc_ast::ast::{Statement, TSModuleReference};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{
    context::LintContext,
    rule::{DefaultRuleConfig, Rule},
};

fn first_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Import statements must come first")
        .with_help("Move import statement to the top of the file")
        .with_label(span)
}

fn absolute_first_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Relative imports before absolute imports are prohibited")
        .with_help("Move absolute import above relative import")
        .with_label(span)
}

#[derive(Debug, Default, Clone, Deserialize, Serialize)]
pub struct First(AbsoluteFirst);

#[derive(Debug, Default, Clone, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "kebab-case")]
enum AbsoluteFirst {
    /// Forces absolute imports to be listed before relative imports.
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Move the import statement above all other statements
  2. Move any side-effectful code that must run first into a separate module imported before it
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/import/first.rs:14 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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