Skip to content

yml/sort-keys

require mapping keys to be sorted

  • 🔧 The --fix option on the command line can automatically fix some of the problems reported by this rule.

📖 Rule Details

This rule checks all pair definitions of mapping and verifies that all keys are sorted alphabetically or specified order.

Now loading...

🔧 Options

yaml
yml/sort-keys:
  - error
  # For example, a definition for eslintrc
  - pathPattern: ^$ # Hits the root properties
    order: 
      - root
      - plugins
      - extends
      - env
      - rules
      - overrides
      # ...
  - pathPattern: ^env$
    order: { type: asc }
  - pathPattern: ^rules$
    order: { type: asc }
  # ...

The option receives multiple objects with the following properties:

  • pathPattern (Required) ... Defines the regular expression pattern of paths to which you want to enforce the order. If you want to apply to the top level, define "^$".
  • hasProperties ... Defines an array of property names. Checks only objects that have the defined properties.
  • order (Required) ... Defines how to enforce the order. You can use an object or an array.
    • Array ... Defines an array of properties to enforce the order.
      • String ... Defines the property name.
      • Object ... The object has the following properties:
        • keyPattern ... Defines a pattern to match the property name. Default is to match all.
        • order ... The object has the following properties:
          • type:
            • "asc" ... Enforce properties to be in ascending order. This is default.
            • "desc" ... Enforce properties to be in descending order.
          • caseSensitive ... If true, enforce properties to be in case-sensitive order. Default is true.
          • natural ... If true, enforce properties to be in natural order. Default is false.
    • Object ... The object has the following properties:
      • type:
        • "asc" ... Enforce properties to be in ascending order. This is default.
        • "desc" ... Enforce properties to be in descending order.
      • caseSensitive ... If true, enforce properties to be in case-sensitive order. Default is true.
      • natural ... If true, enforce properties to be in natural order. Default is false.
  • minKeys ... Specifies the minimum number of keys that an object should have in order for the object's unsorted keys to produce an error. Default is 2, which means by default all objects with unsorted keys will result in lint errors.
  • allowLineSeparatedGroups ... If true, the rule allows to group object keys through line breaks. In other words, a blank line after a property will reset the sorting of keys. Default is false.

You can also define options in the same format as the sort-keys rule.

yaml
yml/sort-keys:
  - error
  - asc
  - caseSensitive: true
    natural: false
    minKeys: 2
    allowLineSeparatedGroups: false

See here for details.

🚀 Version

This rule was introduced in eslint-plugin-yml v0.3.0

🔍 Implementation