import/first Style
What it does
Forbids any non-import statements before imports except directives.
Why is this bad?
Notably, imports are hoisted, which means the imported modules will be evaluated before any of the statements interspersed between them. Keeping all imports together at the top of the file may prevent surprises resulting from this part of the spec
Examples
Examples of incorrect code for this rule:
import { x } from "./foo";
export { x };
import { y } from "./bar";Examples of correct code for this rule:
import { y } from "./bar";
import { x } from "./foo";
export { x, y };Configuration
This rule accepts a configuration object with the following properties:
absolute_first
type: "absolute-first" | "disable-absolute-first"
Whether to enforce absolute imports before relative imports.
Examples of incorrect code for this rule with "absolute-first":
import { y } from "bar";
import { x } from "./foo";Examples of correct code for this rule with "absolute-first":
import { y } from "bar";
import { x } from "./foo";How to use
To enable this rule in the CLI or using the config file, you can use:
oxlint --deny import/first --import-plugin{
"plugins": ["import"],
"rules": {
"import/first": "error"
}
}