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

False positive for Style/TrailingComma cop #1955

Closed
sferik opened this issue Jun 7, 2015 · 8 comments
Closed

False positive for Style/TrailingComma cop #1955

sferik opened this issue Jun 7, 2015 · 8 comments
Labels

Comments

@sferik
Copy link
Contributor

sferik commented Jun 7, 2015

In RuboCop version 0.32.0, given the following configuration:

Style/TrailingComma:
  EnforcedStyleForMultiline: 'comma'

And the following code:

Foo.new({})

RuboCop generates the following offense:

Put a comma after the last parameter of a multiline method call.
Foo.new({})
        ^^

With autocorrect enabled, the code is changed to:

Foo.new({},)

Something is clearly wrong. This was not a problem in earlier versions of RuboCop.

@mattjmcnaughton
Copy link
Contributor

I made a pull request with a possible fix.

Here's what I think is going on (although I might be wrong). Basically the cop interpreted the expression, correctly, as a method call. As can be seen in the code, because the last argument, the {} has no comma, and comma is the setting, an offense was reported.

I simply added a check to not report an offense if the first argument of the function is an empty hash, as can be seen here.

My only concern, as mentioned on the pull request is that my solution would create false negatives. For example, it would not report an offense for Foo.new({}, ... \n /// 5), when perhaps it should.

Thoughts? Let me know if there are any changes I can make to make it better!

@sferik
Copy link
Contributor Author

sferik commented Jun 8, 2015

The error message says:

Put a comma after the last parameter of a multiline method call.

IMHO, it should only apply to multiline method calls. For example:

Foo.new(
  "first arg",
  "second arg",
)

@mattjmcnaughton
Copy link
Contributor

Hmm so maybe the problem is that Foo.new({}) should never have been called multiline in the first place. I'm guessing that has something to do with the new definition of multiline (seen here), although I may be totally wrong about that.

@sferik
Copy link
Contributor Author

sferik commented Jun 8, 2015

@philoserf This is not the default. I am overwriting the default, as I say in the first line of the description with the configuration:

Style/TrailingComma:
  EnforcedStyleForMultiline: 'comma'

This is just a bug. 🐛

Not a bloody nose. 🚫 💉 👃

@philoserf
Copy link

My miss. Comment retracted.

@mattjmcnaughton
Copy link
Contributor

I updated my pull request off of @sferik's observation that this method should not have been labelled multiline? in the first place. Let me know if I can make any changes to improve it!

@jfelchner
Copy link
Contributor

My setup is exactly like @sferik and I'm also getting this problem.

@denisdefreyne
Copy link

I’m running into the same exact issue.

Here’s another case where the cop gives incorrect output:

Foo.new(
  'blah blah',
  { owner: 'me', author: 'myself' },
  'more blah',
)

RuboCop reports:

stuff.rb:6:14: C: Avoid comma after the last parameter of a method call, unless each item is on its own line.
  'more blah',
             ^

This is the RuboCop configuration:

TrailingComma:
  EnforcedStyleForMultiline: comma

ahorner added a commit to tablexi/restforce-db that referenced this issue Jun 12, 2015
We're locking rubocop at 0.31.0 until a trailing comma issue is resolved
rubocop/rubocop#1955
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants