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
Expand first argument if it is an inline function #889
Conversation
The examples look really good to me, cc @robarnold who wanted this as well. |
src/printer.js
Outdated
@@ -1952,6 +1952,14 @@ function shouldGroupLastArg(args) { | |||
(!penultimateArg || penultimateArg.type !== lastArg.type); | |||
} | |||
|
|||
function shouldGroupFirstArg(args) { | |||
const firstArg = args[0]; |
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.
you need to check that there is at least one argument, otherwise it's going to throw an exception Cannot read property 'type' of undefined
return (firstArg.type === 'FunctionExpression' || | ||
(firstArg.type === 'ArrowFunctionExpression' && | ||
firstArg.body.type === 'BlockStatement')) && | ||
args.slice(1).every(a => !typeIsFunction(a.type)); |
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.
👍
src/printer.js
Outdated
concat(["(", join(concat([", "]), printed), ")"]), | ||
concat([ | ||
"(", | ||
group(printed[0], { shouldBreak: true }), |
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.
it looks like this part is the only difference. Could you try to merge the two instead of copy pasting everything?
Something like that (feel free to find better variable names:
const groupFirst = shouldGroupFirst()
const groupLast = shouldGroupLast()
if (groupFirst || groupLast) {
const begin = groupFirst ? printed[0] : printed.slice(0, -1);
const end = groupFirst ? printed.slice(1) : util.getLast(printed);
// ...
}
@vjeux Thanks for the prompt feedback. I've updated the PR |
Thanks, the code looks good to me. Next step is to wait for @jlongster to decide if we want to add this behavior or not :) |
My gut tells me not to do this. It's just getting too specific. At some point we have to draw the line and say we aren't going to specialize any more patterns because it makes it too easy to become inconsistent. Once you starting adding more params or doing more things it becomes unreadable: setInterval(function() {
thing();
}, short ? 200 : 500);
func(function() {
thing();
}, { param: " foo" }); All of these are much better to expand all of the arguments. We shouldn't violate that rule too much. I think the above code looks weird to you only because you are used to see |
I see your point. Thanks for your time, James, and for a great program :) |
@Royce Implemented in arijs@8d18e1a as |
@Royce I think I might be OK with this if it's a lot more strict. Only do it if there are only 2 parameters and the second one is "primitive", meaning it's a literal or an identifier. I tend to resist adding a lot more heuristics but the fact is prettier is really nice because of these kinds of heuristics. If want to re-open a PR with those changes, I think we can merge it. |
@jlongster Please see #947 for a stricter heuristic for grouping first arg. :) |
Addresses #888
No longer produces.