sort-named-imports
💼 This rule is enabled in the following configs: recommended-alphabetical
, recommended-line-length
, recommended-natural
.
🔧 This rule is automatically fixable by the --fix
CLI option.
📖 Rule Details
Enforce sorted named imports.
It promotes a standardized ordering of named imports, making it easier for developers to navigate and understand the import statements within the codebase.
Important
If you use the sort-imports
rule, it is highly recommended to disable it to avoid conflicts.
💡 Examples
// ❌ Incorrect
import {
useLayoutEffect,
useRef,
useEffect,
useId,
useState,
createContext,
useReducer,
} from 'react'
// ✅ Correct
import {
createContext,
useEffect,
useId,
useLayoutEffect,
useReducer,
useRef,
useState,
} from 'react'
// ❌ Incorrect
import {
useLayoutEffect,
useRef,
useEffect,
useId,
useState,
createContext,
useReducer,
} from 'react'
// ✅ Correct
import {
useLayoutEffect,
createContext,
useReducer,
useEffect,
useState,
useRef,
useId,
} from 'react'
🔧 Options
This rule accepts an options object with the following properties:
interface Options {
type?: 'alphabetical' | 'natural' | 'line-length'
order?: 'asc' | 'desc'
'ignore-case'?: boolean
'ignore-alias'?: boolean
}
type
(default: 'alphabetical'
)
alphabetical
- sort alphabetically.natural
- sort in natural order.line-length
- sort by code line length.
order
(default: 'asc'
)
asc
- enforce properties to be in ascending order.desc
- enforce properties to be in descending order.
ignore-case
(default: false
)
Only affects alphabetical and natural sorting. When true
the rule ignores the case-sensitivity of the order.
ignore-alias
(default: true
)
Use import alias as name instead of exported name.
⚙️ Usage
// .eslintrc
{
"plugins": ["perfectionist"],
"rules": {
"perfectionist/sort-named-imports": [
"error",
{
"type": "natural",
"order": "asc"
}
]
}
}
// eslint.config.js
import perfectionist from 'eslint-plugin-perfectionist'
export default [
{
plugins: {
perfectionist,
},
rules: {
'perfectionist/sort-named-imports': [
'error',
{
type: 'natural',
order: 'asc',
},
],
},
},
]
🚀 Version
This rule was introduced in v0.2.0.