Prevent wrapping object properties with short keys#10335
Prevent wrapping object properties with short keys#10335thorn0 merged 3 commits intoprettier:mainfrom
Conversation
src/language-js/print/assignment.js
Outdated
| node.key && | ||
| !node.computed && | ||
| node.key.type === "Identifier" && | ||
| node.key.name.length <= options.tabWidth + 2 |
There was a problem hiding this comment.
It was 3 initially. :D Suggestions?
There was a problem hiding this comment.
There has to be a sufficient visual overlap for the line break to look natural. I'm having trouble with determining how long exactly it needs to be.
There was a problem hiding this comment.
Honestly, I also have no ideas, magic numbers scare me 😄 , maybe we just need to make cases with 2/3/4 values and look where it will be better
There was a problem hiding this comment.
Added that to snapshots. Have a look.
There was a problem hiding this comment.
I guess the value 2 was based on this comment #3711 (comment)
There was a problem hiding this comment.
A sample diff: prettier/prettier-regression-testing#32 (comment)
| a: "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", | ||
| ab: "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", | ||
| abc: "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", | ||
| abcd: "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", |
There was a problem hiding this comment.
'abcd': "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
Should be same?
There was a problem hiding this comment.
The overall length of the part before : is measured.
abcd → 4
'abcd' → 6
[x] → 3
| } | ||
| // TODO: a more lightweight version of cleanDoc is needed that would stop once it's | ||
| // detected that the doc can't be normalized into a string. | ||
| keyDoc = cleanDoc(keyDoc); |
There was a problem hiding this comment.
Doesn't sound like an optimization
There was a problem hiding this comment.
Doesn't, but don't relay on how the doc is build.
There was a problem hiding this comment.
printDocToString always returns a string, but I need to check that the doc consists only of strings. No line breaks, etc. How is that worse than, for example, willBreak? Doc structure checks are everywhere in Prettier's code.
There was a problem hiding this comment.
label is for complex things like member chains. We definitely don't want to consider such keys "simple".
e76f87a to
4098493
Compare
Co-authored-by: Sosuke Suzuki <[email protected]>
sosukesuzuki
left a comment
There was a problem hiding this comment.
String length causes a strange bug as below, but I think we can ignore it in the real world 😄
Prettier pr-10335
Playground link
--parser babelInput:
foo = {
// length 5
foooo: "https://prettier.io/docs/en/rationale.html#what-prettier-is-concerned-about",
// length 4
fooo: "https://prettier.io/docs/en/rationale.html#what-prettier-is-concerned-about",
// length 4? no, length 5!
"𩸽o": "https://prettier.io/docs/en/rationale.html#what-prettier-is-concerned-about",
};Output:
foo = {
// length 5
foooo:
"https://prettier.io/docs/en/rationale.html#what-prettier-is-concerned-about",
// length 4
fooo: "https://prettier.io/docs/en/rationale.html#what-prettier-is-concerned-about",
// length 4? no, length 5!
"𩸽o":
"https://prettier.io/docs/en/rationale.html#what-prettier-is-concerned-about",
};|
@sosukesuzuki We have |
|
upd: Turns out Prettier conservatively uses rules from ES5.1 to decide what can be unquoted. |
Description
Fixes #3711
It's not obvious from the changes in the snapshots, but this is able to lead to huge diffs. Still worth doing IMHO.
Checklist
changelog_unreleased/*/XXXX.mdfile followingchangelog_unreleased/TEMPLATE.md.✨Try the playground for this PR✨