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
Style::HashTransformValues - incorrect autocorrection when wrapped in a block #10171
Style::HashTransformValues - incorrect autocorrection when wrapped in a block #10171
Conversation
I hope it's okay to have only submitted a failing test. I would love to help with the actual implementation, but would need some guidance there. What I did was generate the ASTs with the help of Without block:
With block:
|
@franzliedke are you intending on fixing this or is it meant more as a proof of concept? |
I would love to fix it, but would need some pointers (to documentation or specific lines of code - I can dig further myself, but am unsure how to tackle this). 🙏🏼 |
Yeah unfortunately rubocop/lib/rubocop/cop/mixin/hash_transform_method.rb Lines 17 to 36 in 4874caa
It looks like the matchers are working properly (as they should; the block form has the same AST within it as the non-block form), but the autocorrection is not completing properly (possibly because the right matcher isn't being used or it's not capturing the right thing?). With the wrapping block, => #<struct RuboCop::Cop::HashTransformMethod::Autocorrection
match=[:v, s(:lvar, :k), s(:send,
s(:lvar, :v), :to_s)],
block_node=s(:block,
s(:send,
s(:send, nil, :x), :map),
s(:args,
s(:arg, :k),
s(:arg, :v)),
s(:array,
s(:lvar, :k),
s(:send,
s(:lvar, :v), :to_s))),
leading=0,
trailing=0> but without it it's returning => #<struct RuboCop::Cop::HashTransformMethod::Autocorrection
match=[:v, s(:lvar, :k), s(:send,
s(:lvar, :v), :to_s)],
block_node=s(:block,
s(:send,
s(:send, nil, :x), :map),
s(:args,
s(:arg, :k),
s(:arg, :v)),
s(:array,
s(:lvar, :k),
s(:send,
s(:lvar, :v), :to_s))),
leading=0,
trailing=5> ( |
cb2e166
to
0897653
Compare
@dvandersluis Thanks so much for these first pointers! Did some step-debugging myself and found these places as well. Here's what I understood:
Therefore, I extended the condition a bit and this now has all the tests passing. 😊 What do you think? Is this the right way to fix this problem? |
I added a commit message that may or may not explain what I understood to be the fix. 😬 Now I only need to add a changelog file, I suppose? |
I'll take a look at your code in a little bit, thanks for working on this! You can run |
Changes look pretty good. Seems like a sensible fix 👍 Given that that module affects multiple cops can you please make sure to add tests for the other cops as well? |
0897653
to
54c0505
Compare
Voila. Rebased on latest master, added the changelog file and tests for Thanks for your help - this was a great experience! 🙌🏼 |
54c0505
to
2f3c3a7
Compare
… body The special case was checking for a block *being passed to* our matched `node` (i.e. the `to_h` call), without excluding the case of the `node` being *wrapped by* a block.
2f3c3a7
to
4c5f485
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing this! ❤️
I caught this incorrect auto-correction when running Rubocop in our project:
After some trial and error, I managed to create the failing test case. The failure seems to be related to the wrapping block - when not wrapped, the transformation is applied correctly.
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.