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 #10883] Fix Style/RedundantParentheses to be able to detect offenses and properly correct when the end parentheses and comma are on their own line #10885

Merged
merged 1 commit into from Aug 9, 2022

Conversation

dvandersluis
Copy link
Member

Previously in #2874 / #3078, Style/RedundantParentheses was updated to ignore parentheses in arrays when the comma is on a separate line with the end parenthesis, as that would leave a comma on a line by itself, which is a syntax error.

Additionally, this trailing comma problem shows up in method argument lists, hashes, etc.

This change removes the false negative for this situation, and instead updates ParenthesesCorrector to be able to handle commas that are orphaned onto their own line after removing parentheses. Note that this corrector is also used by Style/ParenthesesAroundCondition and Style/RescueModifier but all tests are passing without change.

Fixes #10883.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • 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.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

config/default.yml Outdated Show resolved Hide resolved
…tect offenses and properly correct when the end parentheses and comma are on their own line.
@koic koic merged commit 67f3055 into rubocop:master Aug 9, 2022
@koic
Copy link
Member

koic commented Aug 9, 2022

Awesome! Thanks!

@dvandersluis dvandersluis deleted the issue/10883 branch August 9, 2022 03:00
@justisb
Copy link

justisb commented Aug 9, 2022

FYI this update causes invalid syntax for a heredoc string used in an array or hash, as the autocorrect adds the comma to the end of the heredoc end marker, breaking the marker. In that scenario we'll want to leave the dangling parentheses + comma.

Example:

    [
      (
        <<-'HEREDOC'
          Foo
        HEREDOC
      ),
      (
        <<-'HEREDOC'
          Bar
        HEREDOC
      ),
    ]

Is improperly autocorrected to:

    [
      
        <<-'HEREDOC'
          Foo
        HEREDOC,
      
        <<-'HEREDOC'
          Bar
        HEREDOC,
    ]

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.

Style/RedundantParentheses multiline list detection problem
3 participants