# sort-imports

> Enforce sorted `import` declarations and sorted names inside them

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 `* as` imports, 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.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"sort-imports" = "error" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"sort-imports" = ["error", {
  ignoreCase = false,
  ignoreDeclarationSort = false,
  ignoreMemberSort = false,
  memberSyntaxSortOrder = ["none", "all", "multiple", "single"],
  allowSeparatedGroups = false,
}]
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad */}
```js
import { writeFile, readFile } from "node:fs/promises";
```

`fs` sorts before `path`:

{/* bad */}
```js
import path from "node:path";
import fs from "node:fs";
```

An import of several names comes before an import of one name:

{/* bad */}
```js
import path from "node:path";
import { readFile, writeFile } from "node:fs/promises";
```

Not reported:

{/* good */}
```js
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:

{/* good {"options": [{"ignoreDeclarationSort": true}]} */}
```js
import path from "node:path";
import { readFile, writeFile } from "node:fs/promises";
```

With `allowSeparatedGroups`, the blank line starts a new block:

{/* good {"options": [{"allowSeparatedGroups": true}]} */}
```js
import path from "node:path";

import fs from "node:fs";
```

With `ignoreCase`:

{/* good {"options": [{"ignoreCase": true}]} */}
```js
import { createElement, Fragment, useState } from "react";
```

With `memberSyntaxSortOrder` set to put single imports first:

{/* good {"options": [{"memberSyntaxSortOrder": ["single", "multiple", "all", "none"]}]} */}
```js
import path from "node:path";
import { readFile, writeFile } from "node:fs/promises";
import "./setup.js";
```
