Skip to content

sort-maps

💼 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 elements within JavaScript Map object.

Sorting Map elements provides a clear and predictable structure to the codebase, making it easier for developers to locate and understand the key-value pairs defined within a Map.

This rule detects instances where Map elements are not sorted in a specified order and raises a linting error. It encourages developers to rearrange the elements in the desired order, ensuring a consistent structure across Map objects.

💡 Examples

js
// ❌ Incorrect
let burritoRecipeMap = new Map([
  ['tomatoes', 300],
  ['bell pepper', 100],
  ['corn', 150],
  ['cheese', 200],
  ['chicken fillet', 300],
  ['beans', 150],
])

// ✅ Correct
let burritoRecipeMap = new Map([
  ['beans', 150],
  ['bell pepper', 100],
  ['cheese', 200],
  ['chicken fillet', 300],
  ['corn', 150],
  ['tomatoes', 300],
])
js
// ❌ Incorrect
let burritoRecipeMap = new Map([
  ['tomatoes', 300],
  ['bell pepper', 100],
  ['corn', 150],
  ['cheese', 200],
  ['chicken fillet', 300],
  ['beans', 150],
])

// ✅ Correct
let burritoRecipeMap = new Map([
  ['chicken fillet', 300],
  ['bell pepper', 100],
  ['tomatoes', 300],
  ['cheese', 200],
  ['beans', 150],
  ['corn', 150],
])

🔧 Options

This rule accepts an options object with the following properties:

ts
interface Options {
  type?: 'alphabetical' | 'natural' | 'line-length'
  order?: 'asc' | 'desc'
  '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.

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-maps": [
      "error",
      {
        "type": "natural",
        "order": "asc"
      }
    ]
  }
}
js
// eslint.config.js
import perfectionist from 'eslint-plugin-perfectionist'

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

🚀 Version

This rule was introduced in v0.5.0.

📚 Resources

Released under the MIT License