sort-object-properties
Sort object properties by name length and then by name.
- Rule ID:
tai-kun/sort-object-properties - Auto-fix: Yes
Sorts properties in object literals and object patterns by name length, then alphabetically when lengths match.
Configuration
Add the rule to lint.rules in vite.config.ts to enable it.
"tai-kun/sort-object-properties": "error",
Examples
Invalid
const value = {
zebra: 1,
a: 2,
};
Valid
const value = {
a: 2,
zebra: 1,
};
Group headers
A comment separated from the following property by a blank line acts as a group header. It stays in place, and only the properties below it are sorted.
// Before
const rules = {
// Category 1
zebra: 1,
a: 2,
// Category 2
delta: 4,
c: 3,
};
// After
const rules = {
// Category 1
a: 2,
zebra: 1,
// Category 2
c: 3,
delta: 4,
};
Notes
- Only properties inside the same blank-line group are reordered.
- Comments directly above a property move with it.
- Comments separated by a blank line remain in place as group headers.
- Objects containing getters or setters are skipped. Methods are sortable.
- Objects with a trailing comment on a property line, a comment on the
{line, or a comment before}are skipped for safety. - Computed keys,
__proto__, and duplicate keys are skipped, as are object literals containing spread elements. - Object literals are also made multiline.