-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement objectWrap
config option
#16163
Implement objectWrap
config option
#16163
Conversation
f55f23d
to
f84d250
Compare
f84d250
to
7c3754c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about an option that's the opposite of the "collapse"
option? This way objects could be forced to always be expanded on multiple lines. That approach would minimise Git diffs when someone adds a new property to an object, otherwise prettier might suddenly convert a previously single line object to multiple lines if it can't fit within the max line length anymore.
I'm not pushing for more configurability; just talking out loud to see what the best approach is. I would be happy with whatever the community lands on as the default.
@jaydenseric that hypothetical option would be However, I do not believe this to be viable for short objects. Moreover, it makes more sense for object members to be consistent with array, function call, argument members. Which are placed on multiple lines only as necessary. |
Can I get some eyes on this? |
Personally, I like |
7c3754c
to
debff30
Compare
I too am 👍 on this proposal. Ideally I don't want to add the option, but it seems like a good compromise. I will review it properly later. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation looks good. However, additional tests are needed. This change also affects the type Foo = { field1: number };
syntax in TypeScript and Flow, so please add tests for it.
Also, look for existing test cases that may be relevant and enable the { multilineObjects: "collapse" }
option. And please fix CI fail.
7dd0227
to
f289756
Compare
|
commit: |
0287e17
to
dda1608
Compare
2487cc6
to
7a6a1b5
Compare
7a6a1b5
to
8bcb6d3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm beginning to think that multilineObject
is not the best name. It sounds like it takes a boolean value, which isn't what we want to convey. Instead, it would be better to use a name that suggests "determining how objects wrap across multiple lines." For example, how about objectWrapping
? It’s reminiscent of CSS’s text-wrapping, so JavaScript developers likely won’t find it off-putting.
objectWrapping
config option
@pauldraper @fisker I've updated the option name from |
I like it. Could be objectWrap, for similarity to proseWrap. (Though the option values/semantics are different.) |
|
objectWrapping
config optionobjectWrap
config option
🎉 Thanks @sosukesuzuki ! |
Description
Implement
multilineObject
config option to control how multi-line JavaScript objects are formatted.Choices
"preserve"
This is the default. Objects are kept on multiple lines if there is a newline between the opening brace and the first property key. (I.e. the existing behavior of Prettier.)
"collapse"
Objects ignore whitespace-only formatting from the source, and are collapsed to a single line when possible.
Examples
Input
"preserve"
"collapse"
Motivation
John submits a PR:
Jessica submits a PR:
Prettier fails to prevent these two contributors from quibbling over the style. The project must publish a style guide to resolve the syntax debate of where and when to use multi-line objects. Moreover, to follow that guide for the single-line object cases, reviewers must now measure characters to see if the object can fit on a single-line without violating line length rules. (Which by itself is a complex task.)
This debate can be settled by using
--multiline-object=collapse
.The pain of the above situation has been felt by many, many users who expect Prettier to solve this stylistic variation, and is a perennial source of consternation marked by complaints of "instability" and "inconsistency":
However, the original justifications for the semi-manual formatting still apply. Even though there are many who prefer to Prettier to be opinionated (including myself), it is a non-starter to impose whitespace agnostic object formatting on all Prettier users.
Thus, this change is the only path forward.
Aren't we not supposed to add options?
Normally yes, but this option fundamentally differs from the usual proposals.
1. This option makes Prettier more opinionated, not less.
Normally, options are eschewed because they contradict Prettier's purpose to be an opinionated formatter.
But today, the single-line vs multi-line is configured not project-by-project, not even file-by-file, but object-literal-by-object-literal. That's literally the most flexibility/least opinionated possible. This change allows users to reign in that flexibility.
Unlike other proposals for adding options, this does not change the variety of styles that Prettier outputs. It simply changes how those styles are configured (project-wide vs line-by-line).
2. The current behavior is "not a feature"
This option allows users to opt out of this non-feature workaround.
3. Non-reversibility is significantly undesirable
This option allows users to make Prettier a reversible formatter.
4. Prettier maintainers have invited this change
#2068 (comment)
Checklist
docs/
directory).changelog_unreleased/*/XXXX.md
file followingchangelog_unreleased/TEMPLATE.md
.✨Try the playground for this PR✨