Rule: object-literal-sort-keys
Checks ordering of keys in object literals.
When using the default alphabetical ordering, additional blank lines may be used to group object properties together while keeping the elements within each group in alphabetical order. To opt out of this use ignore-blank-lines option.
Rationale
Useful in preventing merge conflicts
Config
By default, this rule checks that keys are in alphabetical order. The following may optionally be passed:
ignore-blank-lines
will enforce alphabetical ordering regardless of blank lines between each key-value pair.ignore-case
will compare keys in a case insensitive way.locale-compare
will compare keys using the expected sort order of special characters, such as accents.-
match-declaration-order
will prefer to use the key ordering of the contextual type of the object literal, as in:interface I { foo: number; bar: number; } const obj: I = { foo: 1, bar: 2 };
If a contextual type is not found, alphabetical ordering will be used instead.
-
“match-declaration-order-only” exactly like “match-declaration-order”, but don’t fall back to alphabetical if a contextual type is not found.
Note: If both match-declaration-order-only and match-declaration-order options are present, match-declaration-order-only will take precedence and alphabetical fallback will not occur.
-
shorthand-first
will enforce shorthand properties to appear first, as in:const obj = { a, c, b: true };
Config examples
"object-literal-sort-keys": true
"object-literal-sort-keys": [ true, "ignore-blank-lines", "ignore-case", "locale-compare", "match-declaration-order", "shorthand-first" ]
Schema
{ "type": "string", "enum": [ "ignore-blank-lines", "ignore-case", "locale-compare", "match-declaration-order", "match-declaration-order-only", "shorthand-first" ] }
Code examples:
Requires that an object literal's keys be sorted alphabetically.
"rules": { "object-literal-sort-keys": true }
Passes
let o = {
bar: 2,
foo: 1
};
Fails
let o = {
foo: 1,
bar: 2
};
Requires that an object literal's keys be sorted by interface-definition. If there is no interface fallback to alphabetically.
"rules": {
"object-literal-sort-keys": {
"options": "match-declaration-order"
}
}
Passes
interface I {
foo: number;
bar: number;
}
let o: I = {
foo: 1,
bar: 2
};
Fails
interface I {
foo: number;
bar: number;
}
let o: I = {
bar: 2,
foo: 1
};