Skip to content

Commit 66eb97f

Browse files
committed
feat: add named import sort option to order rule
Matching the functionality in eslint-plugin-import. Resolves #225
1 parent d2512df commit 66eb97f

9 files changed

+1404
-16
lines changed

docs/rules/order.md

+69
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,75 @@ import React, { PureComponent } from 'react'
338338
import { compose, apply } from 'xcompose'
339339
```
340340

341+
### `named: [boolean | object]`
342+
343+
Enforce ordering of names within imports and exports.
344+
345+
- Default: `false`
346+
347+
If set to `true`, it enforces alphabetical ordering of named imports according to the `alphabetize` settings.
348+
If set to an object, you can configure specific aspects of named import ordering:
349+
350+
```ts
351+
{
352+
"import-x/order": [
353+
"error",
354+
{
355+
"named": {
356+
"enabled": true, // Master switch (default: true if object is provided)
357+
"import": true, // Order names in import statements (default: true)
358+
"export": true, // Order names in export statements (default: true)
359+
"require": true, // Order names in require destructuring (default: true)
360+
"cjsExports": true, // Order names in CommonJS exports (default: true)
361+
"types": "mixed" // How to order type imports: "mixed", "types-first", or "types-last" (default: "mixed")
362+
},
363+
"alphabetize": {
364+
"order": "asc",
365+
"caseInsensitive": true
366+
}
367+
}
368+
]
369+
}
370+
```
371+
372+
This will fail the rule check:
373+
374+
```ts
375+
/* eslint import-x/order: ["error", {"named": true, "alphabetize": {"order": "asc"}}] */
376+
import { D, C } from './Z'
377+
export { B, A } from './Z'
378+
const { b, a } = require('./Z')
379+
```
380+
381+
While this will pass:
382+
383+
```ts
384+
/* eslint import-x/order: ["error", {"named": true, "alphabetize": {"order": "asc"}}] */
385+
import { C, D } from './Z'
386+
export { A, B } from './Z'
387+
const { a, b } = require('./Z')
388+
```
389+
390+
The `types` option allows controlling how type imports are ordered:
391+
392+
- `"mixed"`: (default) - Sort all identifiers alphabetically regardless of type status
393+
- `"types-first"`: Type imports appear before non-type imports
394+
- `"types-last"`: Type imports appear after non-type imports
395+
396+
This will fail the rule check with `"types-first"`:
397+
398+
```ts
399+
/* eslint import-x/order: ["error", {"named": { "types": "types-first" }, "alphabetize": {"order": "asc"}}] */
400+
import { type User, Profile, type Account, Settings } from './models'
401+
```
402+
403+
While this will pass:
404+
405+
```ts
406+
/* eslint import-x/order: ["error", {"named": { "types": "types-first" }, "alphabetize": {"order": "asc"}}] */
407+
import { type Account, type User, Profile, Settings } from './models'
408+
```
409+
341410
### `warnOnUnassignedImports: true|false`
342411

343412
- default: `false`

0 commit comments

Comments
 (0)