sort-imports
Enforce sorted import declarations and sorted names inside them
fixable
A sorted list of imports is quick to scan, and two people who add an import put it in the same place.
The rule checks three things:
- The names inside
{ ... }are in alphabetical order. - Declarations are grouped by syntax. The default order is imports with no names, then
* asimports, then imports of several names, then imports of one name. - Inside a group, declarations are sorted alphabetically by the first name they import. The rule compares the local name, not the module path.
Capital letters sort before lowercase letters unless you set ignoreCase.
--fix sorts the names inside { ... }, unless there are comments between them. It doesn't move declarations.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"sort-imports" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"sort-imports" = ["error", {
ignoreCase = false,
ignoreDeclarationSort = false,
ignoreMemberSort = false,
memberSyntaxSortOrder = ["none", "all", "multiple", "single"],
allowSeparatedGroups = false,
}]Examples#
example.js1 problem
import { writeFile, readFileMember readFile of the import declaration should be sorted alphabetically. } from "node:fs/promises";fs sorts before path:
example.js1 problem
import path from "node:path";
import fs from "node:fs";Imports should be sorted alphabetically.An import of several names comes before an import of one name:
example.js1 problem
import path from "node:path";
import { readFile, writeFile } from "node:fs/promises";Expected multiple syntax before single syntax.example.jsNo problems
import "./setup.js";
import * as z from "zod";
import { readFile, writeFile } from "node:fs/promises";
import fs from "node:fs";
import path from "node:path";Options#
| Option | Default | What it does |
|---|---|---|
ignoreCase | false | Ignore case when comparing names. |
ignoreDeclarationSort | false | Don't check the order of declarations, only the names inside { ... }. |
ignoreMemberSort | false | Don't check the names inside { ... }. |
memberSyntaxSortOrder | ["none", "all", "multiple", "single"] | The order of the syntax groups. All four names must be present. |
allowSeparatedGroups | false | Start over after a blank line or a line that isn't an import, so each block is sorted separately. |
ignoreDeclarationSort is useful when another tool orders your imports by module path:
example.jswith
{ "ignoreDeclarationSort": true }No problemsimport path from "node:path";
import { readFile, writeFile } from "node:fs/promises";With allowSeparatedGroups, the blank line starts a new block:
example.jswith
{ "allowSeparatedGroups": true }No problemsimport path from "node:path";
import fs from "node:fs";With ignoreCase:
example.jswith
{ "ignoreCase": true }No problemsimport { createElement, Fragment, useState } from "react";With memberSyntaxSortOrder set to put single imports first:
example.jswith
{ "memberSyntaxSortOrder": ["single", "multiple", "all", "none"] }No problemsimport path from "node:path";
import { readFile, writeFile } from "node:fs/promises";
import "./setup.js";