Rule to sort properties in object literal
See original GitHub issueSimilar to sort-vars
, how about a rule to always sort object properties alphabetically when defining an object literal?
It would be really useful for things like options objects. I realize that the property order can matter in iteration, but it’s nothing you should rely on anyway. With the new ES6 data structures, I think the use case for object literals is shrinking to be a lot about options objects, including in native APIs.
You could argue that it will cause a huge number of warnings in existing code and thus not be useful at all. I’m not completely sure, I just know that I often use vim’s sort
command, but it doesn’t work well if a property value spans several lines.
Warnings:
var obj = {
c: "foo",
a: "bar",
b: {
e: 1,
d: 2,
}
};
OK:
var obj = {
a: "bar",
b: {
d: 2,
e: 1,
},
c: "foo",
};
Issue Analytics
- State:
- Created 9 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
object-literal-sort-keys - Rule
Checks ordering of keys in object literals. When using the default alphabetical ordering, additional blank lines may be used to group object properties...
Read more >sort-keys - ESLint - Pluggable JavaScript Linter
This rule checks all property definitions of object expressions and verifies that all variables are sorted alphabetically.
Read more >How to Sort a JS Object Literal? - Stack Overflow
Object properties are in no specific order (the order is implementation dependent) and you cannot sort the properties.
Read more >Property order is predictable in JavaScript objects since ES2015
The order of properties in an object depends on the type of the included properties and their values.
Read more >Working with objects - JavaScript - MDN Web Docs - Mozilla
Each property name before colons is an identifier (either a name, a number, or a string literal), and each valueN is an expression...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
http://eslint.org/docs/rules/sort-keys from eslint 3.3.0 on 😃
@nzakas Now that https://github.com/jacobrask/eslint-plugin-sorting has somewhat matured, is it worth it to consider adding it to the core?