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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trailing commas #713

Closed
bbatsov opened this issue Jan 2, 2014 · 5 comments
Closed

Trailing commas #713

bbatsov opened this issue Jan 2, 2014 · 5 comments
Assignees

Comments

@bbatsov
Copy link
Collaborator

bbatsov commented Jan 2, 2014

I'd like us to add a cop for trailing commas. Code like this would be of interest to such a cop:

arr = [1, 2, 3,]

method(1, 2, 3,)

hash = { a: 1, b: 2, }

Obviously a trailing , in a method invocation is always a bad idea, while some people like to add trailing commas to array and hash literals to avoid having to add them in the future (or more precisely - to avoid forgetting to add them). Currently, however, the space after comma check we employ conflicts with a trailing comma check.

@jonas054 How about altering the existing cop and adding a new cop TrailingComma with two supported styles - Require and Forbid? Does this sound sensible to you?

@jonas054
Copy link
Collaborator

jonas054 commented Jan 3, 2014

To me the difference is between multi line and single line. Whether it's a method parameter list or a literal; I can see how adding a comma after the last item makes sense. It's easier to add another item, and the diff (in git or similar) is smaller. I would still not make it the default choice, though.

Example:

  VERSION_NUMBERS = [
    VERSION_MAJOR = 3,
    VERSION_MINOR = 0,
    VERSION_BUILD = 3,
  ]

Can we always forbid trailing comma for single line, and make it configurable for multi line?

@bbatsov
Copy link
Collaborator Author

bbatsov commented Jan 3, 2014

Good point about the multi-line literals. I guess if we're looking at a factory method with varargs trailing commas make sense there as well. Let's go with your suggestion - commas are always forbidden on a single line and configurable for multi-line (forbidden by default). We should also add this to the style guide itself.

jonas054 added a commit to jonas054/ruby-style-guide that referenced this issue Jan 11, 2014
Can occur in literals and method definitions/calls. These rules were discussed in rubocop/rubocop#713
@ghost ghost assigned jonas054 Jan 11, 2014
@jonas054
Copy link
Collaborator

We've talked about the very clear cases of one line vs separate lines, but there are some in-between cases I need to discuss.

# 1. Opening bracket together with first value
VALUES = [1001,
          2020,
          3333,
         ]

# 2. Closing bracket together with last value
VALUES = [
          1001,
          2020,
          3333, ]

# 3. Values not on separate lines
VALUES = [
          1001, 2020,
          3333,
         ]

... and then various combinations of them.

The last value needs to be alone on its line for a trailing comma to make any sense, I think. Should we put any other restrictions on when a trailing comma is enforced?

@bbatsov
Copy link
Collaborator Author

bbatsov commented Jan 14, 2014

I think a simpler approach would be check if the literal spans more than a line. If it spans more than a line it might make some sense to have a trailing comma (although the enforced style would be the inverse by default). In a single-line literal it's obviously pointless to have a trailing comma.

@jonas054
Copy link
Collaborator

OK, I'll make it simple then. Thanks.

Tsagadai pushed a commit to Tsagadai/ruby-style-guide that referenced this issue Oct 13, 2014
Can occur in literals and method definitions/calls. These rules were discussed in rubocop/rubocop#713
marocchino pushed a commit to marocchino/ruby-style-guide that referenced this issue Aug 11, 2015
Can occur in literals and method definitions/calls. These rules were discussed in rubocop/rubocop#713
shyouhei pushed a commit to shyouhei/ruby-style-guide that referenced this issue Nov 11, 2015
Can occur in literals and method definitions/calls. These rules were discussed in rubocop/rubocop#713
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants