Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃帀 Trailing commas 馃帀 #1775

Merged
merged 5 commits into from Jan 25, 2018

Conversation

@jordwalke
Copy link
Member

commented Jan 23, 2018

Intelligent printing and parsing of trailing commas on basically everything!

Please see the stack of diffs here. Each commit accomplishes some purpose and it will be much easier to understand the entire set of changes by looking at the individual commits.

@chenglou

This comment has been minimized.

Copy link
Contributor

commented Jan 23, 2018

For reference: #1549

@@ -116,13 +116,13 @@ let res =
id(
fun
| Some(x) => x + 1
| None => 0
| None => 0,

This comment has been minimized.

Copy link
@arnihermann

arnihermann Jan 23, 2018

Is this trailing comma expected?

This comment has been minimized.

Copy link
@jordwalke

jordwalke Jan 23, 2018

Author Member

This is a trailing comma on the function argument. I鈥檓 pretty sure this is what we want. The alternative is to not print trailing commas for functions accepting only one argument.

This comment has been minimized.

Copy link
@ubsan

ubsan Jan 23, 2018

I think this would be a lot nicer to look at with different function syntax

This comment has been minimized.

Copy link
@jordwalke

jordwalke Jan 23, 2018

Author Member

Yeah, if funs had braces, it would be nicer. There was discussion about adding them. In the mean time, the comma is kind of nice in that it tells you pretty clearly where to add another argument. We could also special case it so that single argument functions whose argument is a fun | do not print the trailing comma.

This comment has been minimized.

Copy link
@hcarty

hcarty Jan 23, 2018

Contributor

In the mean time, the comma is kind of nice in that it tells you pretty clearly where to add another argument

This seems like a compelling argument for keeping the trailing comma.

This comment has been minimized.

Copy link
@arnihermann

arnihermann Jan 24, 2018

It felt a bit odd when I saw it, but it makes sense.

I really like this change and I agree it helps with paren-fatigue 馃憤

@jordwalke

This comment has been minimized.

Copy link
Member Author

commented Jan 23, 2018

I've found that trailing commas help with a bit of paren-fatigue as well, because it helps you distinguish closing parens for arguments/tuples/variants from parens whose only purpose is to resolve precedence - at least in the case where things break onto multiple lines. It's a small help, but it is a help.

"func": a => {"a": (arg1, arg2) => arg1 + arg2}
"func": a => {
"a": (arg1, arg2) => arg1 + arg2
}

This comment has been minimized.

Copy link
@chenglou

chenglou Jan 23, 2018

Contributor

Seems missing here

This comment has been minimized.

Copy link
@jordwalke

jordwalke Jan 24, 2018

Author Member

Yes JS objects were the only ones I didn鈥檛 get to. I鈥檒l try to add them before landing.

@jordwalke

This comment has been minimized.

Copy link
Member Author

commented Jan 25, 2018

Going to merge:

Suggestions for anyone trying to extend this to other features in the future:
It's very important to make sure your printer doesn't write a check that your parser can't cash.
Meaning, if you're going to print trailing commas (or anything), make sure it can be parsed. It's tough to catch all the bugs because many times trailing commas (or other features we want to add in the future) will be omitted most of the time. To catch these cases, go into the printer and disable the part that strips out these trailing commas when lines don't break. Then run the tests. Search for idempotent failures to parse. It will catch all of them. Apply that same technique to features you add in the future that are "intelligently omitted". (Such as leading bars on switch cases etc).

@jordwalke jordwalke merged commit d0cb1a6 into master Jan 25, 2018

1 of 3 checks passed

continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
continuous-integration/travis-ci/push The Travis CI build is in progress
Details
ci/circleci Your tests passed on CircleCI!
Details

@jordwalke jordwalke deleted the TrailingCommas branch Jan 25, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can鈥檛 perform that action at this time.