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 #9939] Fix/hash as last array item #9943

Merged
merged 2 commits into from Aug 19, 2021

Conversation

martinjaimem
Copy link

@martinjaimem martinjaimem commented Jul 20, 2021

Fix autocorrect bug with HashAsLastArrayItem

Fixes #9939

When running autocorrect with:

Style/TrailingCommaInHashLiteral:
  EnforcedStyleForMultiline: comma

Style/TrailingCommaInArrayLiteral:
  EnforcedStyleForMultiline: comma

Style/HashAsLastArrayItem:
  EnforcedStyle: no_braces

there was an edge case where it created incorrect code:

before autocorrect

x = [
  1,
  {
    a: 1,
    b: 2,
  },
]

after autocorrect:

x = [
  1,
  
    a: 1,
    b: 2,
  ,
]

With this PR, the result of applying autocorrect makes executable code =>

x = [
  1,
  
    a: 1,
    b: 2,

]

Notes:

  • The PR contains commits that are not squashed, that's because I specifically wanted to receive feedback before and then I can squash the two commits (this way I can show the two approaches)
  • The first commit represents the original approach that only corrected the code within the node, it worked fine with a one-line array but generated the wrong code with multi-line arrays. Maybe it was okay to leave it that way but It was better to mark it as unsafe, some feedback would be awesome.
  • The second commit corrects the code within the node and its parent (I don't know if that's a good practice, I'm really open to some feedback)

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.

@martinjaimem martinjaimem changed the title Fix/hash as last array item [Fixes #9942] Fix/hash as last array item Jul 20, 2021
@martinjaimem martinjaimem changed the title [Fixes #9942] Fix/hash as last array item [Fix #9942] Fix/hash as last array item Jul 20, 2021
one: 1,
two: 2,
},
]
Copy link
Contributor

Choose a reason for hiding this comment

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

Should there be a testcase for this scenario as well?

 [
    1,
    2,
    {
      one: 1,
      two: 2,
    },
    3,
    4
]

p.s I am not an official maintainer. Randomly came across your PR so I thought I could take a look to understand the codebase better..

Copy link
Author

Choose a reason for hiding this comment

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

I added that scenario 👍 good call

@martinjaimem martinjaimem changed the title [Fix #9942] Fix/hash as last array item [Fix #9939] Fix/hash as last array item Aug 3, 2021
@bbatsov bbatsov merged commit d9c2330 into rubocop:master Aug 19, 2021
@bbatsov
Copy link
Collaborator

bbatsov commented Aug 19, 2021

The proposed fix looks good to me. 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.

Style/TrailingCommaInArrayLiteral vs Style/HashAsLastArrayItem
4 participants