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

[Fix #6701] Trailing comma detection regression with HEREDOC #6702

Merged
merged 1 commit into from Feb 7, 2019

Conversation

abrom
Copy link
Contributor

@abrom abrom commented Jan 23, 2019

The TrailingCommaInArguments and TrailingCommaInArrayLiteral cops have a false positive when the parent node contains HEREDOC with commas in them. The issue appears to be due to lax comma detection.

Instead of only matching the last item for heredoc, match all items and search for optional whitespace followed by trailing commas, excluding newlines in the match when heredoc is present.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@abrom abrom force-pushed the fix-trailing-comma-in-heredoc branch from db2fd10 to ff70b91 Compare January 23, 2019 15:13
@abrom
Copy link
Contributor Author

abrom commented Jan 24, 2019

I've tried to solve this problem a different way. Instead of skipping the trailing comma check if the last item is heredoc, we look to see if any item in the node is heredoc and if so we check for a trailing comma directly preceding the last item source (with only horizontal whitespace). If there is no heredoc then we allow horizontal and vertical whitespace before the comma.

This did raise some potential issues with the existing specs, specifically those titled

accepts missing comma after a heredoc

The comment at the top of these specs is:

# A heredoc that's the last item in a literal or parameter list can not
# have a trailing comma. It's a syntax error.

Although this isn't true??

some_method(
  <<-FOO,
    ...
  FOO
)

is valid syntax?!

As is

[
  <<-FOO,
    ...
  FOO
]

@abrom
Copy link
Contributor Author

abrom commented Jan 24, 2019

...as such, my belief is that the specs failing on this branch are actually incorrect. If others agree, I'll modify those specs as needed

@abrom abrom force-pushed the fix-trailing-comma-in-heredoc branch from c0d6f68 to 2f4d94b Compare January 24, 2019 01:45
Copy link
Collaborator

@Drenmi Drenmi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @abrom! 🙇

@abrom abrom force-pushed the fix-trailing-comma-in-heredoc branch from 2f4d94b to d5c0a5d Compare January 29, 2019 15:10
@Drenmi
Copy link
Collaborator

Drenmi commented Feb 7, 2019

@abrom Since there have been no objections to this, I'm ready to merge it in. Please rebase on latest master, and make sure the CHANGELOG is merged correctly, then I will accept the PR. 🙇

@abrom abrom closed this Feb 7, 2019
@abrom abrom force-pushed the fix-trailing-comma-in-heredoc branch from d5c0a5d to 648085e Compare February 7, 2019 02:10
The TrailingCommaInArguments and TrailingCommaInArrayLiteral cops
have a false positive when the parent node contains HEREDOC with
commas in them. The issue appears to be due to lax comma detection.

Instead of only matching the last item for heredoc, match all items
and search for trailing commas, excluding newlines in match when
heredoc is present
@abrom abrom reopened this Feb 7, 2019
@abrom
Copy link
Contributor Author

abrom commented Feb 7, 2019

Done 👍

@Drenmi Drenmi merged commit b675f26 into rubocop:master Feb 7, 2019
@Drenmi Drenmi mentioned this pull request Feb 10, 2019
@wilg
Copy link

wilg commented Feb 28, 2019

It seems like after this PR if I have:

Style/TrailingCommaInArguments:
  Enabled: true
  EnforcedStyleForMultiline: comma

(or the other trailing comma cops), it forces a trailing comma in the heredoc, like in the test cases:

[
  <<-FOO,
    ...
  FOO
]

This ends up looking really confusing and bizarre to me. It's particularly bizarre when a one-argument function that takes a multiline heredoc is required to have a comma. Is this intended behavior? Would it make sense to have an option to disable it for heredocs?

@abrom
Copy link
Contributor Author

abrom commented Feb 28, 2019

But that's the expected result with EnforcedStyleForMultiline: comma and your array being defined across multiple lines?!

@wilg
Copy link

wilg commented Mar 1, 2019

I think my example was poor, it makes more sense for array/hash literals, but less for:

my_func(
 <<-FOO,
    ...
  FOO
)

@abrom
Copy link
Contributor Author

abrom commented Mar 1, 2019

But that's exactly what EnforcedStyleForMultiline: comma is expected to do there too. If you wanted to have the cop configured as such, but not have the trailing comma you'd have something like:

my_func(<<-FOO)  # <-- the method definition is now on one line
  ...
FOO

@wilg
Copy link

wilg commented Mar 1, 2019

Yep, I think you're right. The codebase I'm looking at just uses heredocs and multiline comma enforcement in a way that makes things look and feel weird after this change. I'll open an issue if I can figure out a way to have my cake and eat it too. Thanks!

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

Successfully merging this pull request may close these issues.

None yet

3 participants