Skip to content

sort-named-exports

💼 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 exports.

Maintaining a consistent and sorted order of named exports can improve code readability.

💡 Examples

js
// ❌ Incorrect
export {
  defineConfig,
  validateConfig,
  merge,
  baseConfig,
} from './config'

// ✅ Correct
export {
  baseConfig,
  defineConfig,
  merge,
  validateConfig,
} from './config'
js
// ❌ Incorrect
export {
  defineConfig,
  validateConfig,
  merge,
  baseConfig,
} from './config'

// ✅ Correct
export {
  validateConfig,
  defineConfig,
  baseConfig,
  merge,
} from './config'

🔧 Options

This rule accepts an options object with the following properties:

ts
interface Options {
  type?: 'alphabetical' | 'natural' | 'line-length'
  order?: 'asc' | 'desc'
  'group-kind'?: 'mixed' | 'values-first' | 'types-first'
  'ignore-case'?: 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.

group-kind

(default: 'mixed')

Allows to group named exports by their kind, with value exports coming either before or after type exports.

  • mixed - does not group named exports by their kind
  • values-first - groups all value exports before type exports
  • types-first - groups all type exports before value exports

ignore-case

(default: false)

Only affects alphabetical and natural sorting. When true the rule ignores the case-sensitivity of the order.

⚙️ Usage

json
// .eslintrc
{
  "plugins": ["perfectionist"],
  "rules": {
    "perfectionist/sort-named-exports": [
      "error",
      {
        "type": "natural",
        "order": "asc"
      }
    ]
  }
}
js
// eslint.config.js
import perfectionist from 'eslint-plugin-perfectionist'

export default [
  {
    plugins: {
      perfectionist,
    },
    rules: {
      'perfectionist/sort-named-exports': [
        'error',
        {
          type: 'natural',
          order: 'asc',
        },
      ],
    },
  },
]

🚀 Version

This rule was introduced in v0.4.0.

📚 Resources

Released under the MIT License