Add heuristic to format functional composition more nicely#4431
Add heuristic to format functional composition more nicely#4431suchipi merged 5 commits intoprettier:masterfrom
Conversation
src/language-js/printer-estree.js
Outdated
| // composeP | ||
| // composeK | ||
| // flow | ||
| // flowRight |
There was a problem hiding this comment.
Since you listed out all the possibilities anyway, perhaps we should make an array with all names and use that instead of a regex?
|
|
||
| const maybeTrailingComma = shouldPrintComma(options, "all") ? "," : ""; | ||
|
|
||
| function allArgsBrokenOut() { |
There was a problem hiding this comment.
Don't create a function, just inline the content in the if
There was a problem hiding this comment.
Ohh nevermind, you're using it below as well
src/language-js/printer-estree.js
Outdated
| "composeK", | ||
| "flow", | ||
| "flowRight" | ||
| ]; |
There was a problem hiding this comment.
If you are inlining it like this, then please use an object so it doesn't have to do a linear lookup every time.
{
pipe: true,
pipeP: true,
...
}There was a problem hiding this comment.
What about a Set?
(Btw, as far as I remember we have lots of such arrays where we do indexOf.)
|
|
||
|
|
||
|
|
||
|
|
There was a problem hiding this comment.
Because my editor was prettifying on save so I pasted these into nano 😅 I can remove them
|
|
||
| foo(6); | ||
|
|
||
|
|
|
|
||
| foo(6); | ||
|
|
||
|
|
|
This issue has more examples. |
src/language-js/printer-estree.js
Outdated
| composeP: true, | ||
| composeK: true, | ||
| flow: true, | ||
| flowRight: true |
There was a problem hiding this comment.
Maybe add as comments which is coming from which library, so we know in the future where it came from
Also add comments for libraries
vjeux
left a comment
There was a problem hiding this comment.
Looks good to me, thanks! It's going to make a lot of people happy :)
|
Thanks for coming up with the good heuristic @vjeux :) |
|
Awesome work all-around. Thanks y'all. |
What about the specified names occurring in different contexts? socket.connect for example. |
|
I avoided Prettier 1.13.4 --parser babylonInput: socket.connect(2000, () => {})Output: socket.connect(
2000,
() => {}
);We could probably refine this to bail if any of the arguments are number, string, or object literals. Thoughts? |
|
@suchipi makes sense. Or whitelist for call expressions, identifiers, (arrow) functions |
|
I'd rather do an omit list than an allow list because you could have functions as object properties and access them with a member expression. |
|
@suchipi Is there any GH issue on that matter? this.compose(
d1,
d2
); |
|
@princed I haven't made one yet, if you could make one, I would appreciate it |
|
This is |
|
I agree with @lxe. Hate to be a downer, but this change is awful. Adding a hardcoded list of function names to treat specially has just made |
None of these changes cause the React APIs to be printed differently. Also, we already do some of this special-casing (for example, never breaking |
|
Honestly, I think you'll be better off just doing what clang format did and using a trailing comma as the heuristic to decide if you should inline them all or put them each on their own lines: someThing.whatever(foo, bar, baz, qux);
//vs
somthing.whatever(
foo,
bar,
baz,
qux,
)
🤷♂️ |
|
Not all runtimes support trailing commas in functions, though. |
This is a negative, from my perspective. For me, a big value of prettier is that I can let it "do it's thing" and be hands-off about formatting... at least, mostly. We rely on hinting for multilining object literals and I have to break out of my flow to adjust formatting from time to time because of it. I would rather it didn't let me influence the formatting at all- then I could focus on writing the code and let the formatting be what it is. But the ability to influence the formatting wedges itself into my workflow... |
Based on discussion in #4172, this makes CallExpressions whose callee appears to be a functional composition function always break their arguments.
I added examples for ramda, rxjs, lodash, redux, and a generic "compose/composeFlipped".
Fixes #4172
Fixes #1420