Consistency in lists/objects
See original GitHub issueThanks for your work, I’m VERY interested in something that deals with formatting in my place =)
One thing that struck me upon trying it out tonight, though, was that it does not handle “siblings” consistently. That is, given a list of, say, { id, name } objects; if one of them has a name large enough to reach the line limit, then this object, and this object alone, will be printed over 4 lines, while its siblings will be printed on 1 line. The “exploded” one will have a trailing comma, too, while obviously the one-liners will not.
Original test.js
:
const myList = [
{
id: 0,
name: 'Short',
},
{
id: 1,
name: 'Short Too',
},
{
id: 3,
name: 'Too Long For Your Line Width',
},
];
Result ($ prettier --bracket-spacing --trailing-comma --print-width 40 test.js
):
const myList = [
{ id: 0, name: "Short" },
{ id: 1, name: "Short Too" },
{
id: 3,
name: "Too Long For Your Line Width",
},
];
I think that in this case, the original is the better code: it’s consistent, easily readable and parsable, at the only cost of taking a lot of vertical space (scrolling is cheap, though).
EDIT: removed JSX example and reworded the issue as per @vramana’s comment.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:5
- Comments:18 (6 by maintainers)
@vjeux Thank you very much for putting this in, it’s great!
Shouldn’t the same reasoning apply to arrays as well?
Original:
Changed to:
Expected it to stay the same.
I strongly believe that “pretty” output means “output that can communicate the most information in the most consistent way with as little effort as possible”. For me, being able to know that there are three locales in a split second without having to parse a list and make out if the commas are in, or out, of the quotes, is invaluable, and removing that is making the code less readable.
Thanks for your time and great work (on this and other stuff ^^).
Why aren’t arrays following the same reasoning applied to objects?