sort-objects
Enforce sorted objects.
By adhering to this rule, developers can ensure that object keys are consistently sorted, leading to cleaner and more maintainable code. This rule promotes a standardized key ordering across objects, making it easier to navigate and understand the structure of objects within the codebase.
It’s safe. The rule considers spread elements in objects and does not break component functionality.
Important
If you use the sort-keys
rule, it is highly recommended to disable it to avoid conflicts.
Try it out
Options
This rule accepts an options object with the following properties:
type
default:'alphabetical'
Specifies the sorting method.
'alphabetical'
— Sort items alphabetically (e.g., “a” < “b” < “c”) using localeCompare.'natural'
— Sort items in a natural order (e.g., “item2” < “item10”).'line-length'
— Sort items by the length of the code line (shorter lines first).
order
default:'asc'
Determines whether the sorted items should be in ascending or descending order.
'asc'
— Sort items in ascending order (A to Z, 1 to 9).'desc'
— Sort items in descending order (Z to A, 9 to 1).
ignoreCase
default:true
Controls whether sorting should be case-sensitive or not.
true
— Ignore case when sorting alphabetically or naturally (e.g., “A” and “a” are the same).false
— Consider case when sorting (e.g., “a” comes before “A”).
specialCharacters
default:keep
Controls whether special characters should be trimmed, removed or kept before sorting.
'keep'
— Keep special characters when sorting (e.g., “_a” comes before “a”).'trim'
— Trim special characters when sorting alphabetically or naturally (e.g., “_a” and “a” are the same).'remove'
— Remove special characters when sorting (e.g., “/a/b” and “ab” are the same).
locales
default:'en-US'
Specifies the sorting locales. See String.prototype.localeCompare() - locales.
string
— A BCP 47 language tag (e.g.'en'
,'en-US'
,'zh-CN'
).string[]
— An array of BCP 47 language tags.
partitionByComment
default:false
Allows you to use comments to separate the keys of objects into logical groups. This can help in organizing and maintaining large objects by creating partitions based on comments.
true
— All comments will be treated as delimiters, creating partitions.false
— Comments will not be used as delimiters.string
— A regexp pattern to specify which comments should act as delimiters.string[]
— An array of regexp patterns to specify which comments should act as delimiters.
partitionByNewLine
default:false
When true
, the rule will not sort the object’s keys if there is an empty line between them. This can be useful for keeping logically separated groups of keys in their defined order.
const user = {
// Group 1
firstName: 'John',
lastName: 'Doe',
// Group 2
age: 30,
birthDate: '1990-01-01',
// Group 3
email: 'john.doe@example.com',
phone: '555-555-5555'
};
Each group of keys (separated by empty lines) is treated independently, and the order within each group is preserved.
newlinesBetween
default:'ignore'
Specifies how new lines should be handled between object groups.
ignore
— Do not report errors related to new lines between object groups.always
— Enforce one new line between each group, and forbid new lines inside a group.never
— No new lines are allowed in objects.
This options is only applicable when partitionByNewLine
is false
.
styledComponents
default:true
Determines whether this rule should be applied to styled-components like libraries or style
JSX attribute.
true
— Apply the rule to styled-components.false
— Disable the rule for styled-components.
ignorePattern
default:[]
Allows you to specify names or patterns for object types that should be ignored by this rule. This can be useful if you have specific objects that you do not want to sort.
You can specify their names or a regexp pattern to ignore, for example: '^User.+'
to ignore all object types whose names begin with the word “User”.
[DEPRECATED] destructureOnly
default:false
Use the objectDeclarations and destructuredObjects options instead.
Allows you to only sort objects that are part of a destructuring pattern. When set to true
, the rule will apply sorting exclusively to destructured objects, leaving other object declarations unchanged.
objectDeclarations
default:true
Allows you to choose whether to sort standard object declarations.
destructuredObjects
type: boolean | { groups: boolean }
true
Allows you to choose whether to sort destructured objects. The groups
attribute allows you to specify whether to use groups to sort destructured objects.
groups
type: Array<string | string[]>
[]
Allows you to specify a list of object keys groups for sorting. Groups help organize object keys into categories, making your objects more readable and maintainable.
Predefined groups:
'multiline'
— Properties with multiline definitions, such as methods or complex type declarations.'method'
- Members that are methods.'unknown'
— Properties that don’t fit into any group specified in thegroups
option.
If the unknown
group is not specified in the groups
option, it will automatically be added to the end of the list.
Each object member will be assigned a single group specified in the groups
option (or the unknown
group if no match is found). The order of items in the groups
option determines how groups are ordered.
Within a given group, members will be sorted according to the type
, order
, ignoreCase
, etc. options.
Individual groups can be combined together by placing them in an array. The order of groups in that array does not matter. All members of the groups in the array will be sorted together as if they were part of a single group.
customGroups
type: { [groupName: string]: string | string[] }
{}
You can define your own groups and use regexp pattern to match specific object keys.
Each key of customGroups
represents a group name which you can then use in the groups
option. The value for each key can either be of type:
string
— An object attribute’s name matching the value will be marked as part of the group referenced by the key.string[]
— An object attribute’s name matching any of the values of the array will be marked as part of the group referenced by the key. The order of values in the array does not matter.
Custom group matching takes precedence over predefined group matching.
Example
Put all properties starting with id
and name
at the top, put metadata at the bottom. Regroup multiline and in the middle, above unknown-matched properties.
const user = {
id: 'id', // top
name: 'John', // top
getEmail: () => null, // method
localization: { // multiline
// Stuff about localization
},
age: 40, // unknown
isAdmin: false, // unknown
lastUpdated_metadata: null, // bottom
version_metadata: '1' // bottom
}
groups
and customGroups
configuration:
{
groups: [
+ 'top',
['multiline', 'method'],
['unknown'],
'bottom'
],
+ customGroups: {
+ top: ['^id$', '^name$']
+ bottom: '.+_metadata$'
+ }
}
Usage
Version
This rule was introduced in v0.6.0.