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 subject nesting detection in RSpec/LeadingSubject #1011

Merged
merged 1 commit into from Aug 21, 2020

Conversation

pirj
Copy link
Member

@pirj pirj commented Aug 19, 2020

Fixes #1010


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • [-] Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

@pirj pirj self-assigned this Aug 19, 2020
@pirj
Copy link
Member Author

pirj commented Aug 19, 2020

@fabioxgn can you please check if this is fixing your case?

@fabioxgn
Copy link

@fabioxgn can you please check if this is fixing your case?

It does, thanks! 🎉

break if sibling.equal?(node)

yield sibling if offending?(sibling)
end
end

def parent(node)
node.each_ancestor(:block).first.body
Copy link
Collaborator

@bquorning bquorning Aug 20, 2020

Choose a reason for hiding this comment

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

@pirj Could you help me understand how this fixed the bug? (Extra points for adding the explanation to the commit message ❤️)

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure. Described what was wrong here: 87562ac

By picking the nearest outer block body, we make sure we only pick real siblings inside that block.

For the following code:

    RSpec.describe User do
      let(:foo) { 'bar' }
      it_behaves_like 'a good citizen' do
        subject { described_class.new }
      end
    end

when `on_block` is triggered for `subject`,
`node.parent.each_child_node.to_a` is:

    [
      s(:send, nil, :it_behaves_like, s(:str, "a good citizen")),
      s(:args),
      s(:block, s(:send, nil, :subject), s(:args), s(:send, s(:send, nil, :described_class), :new))
    ]

and autocorrect was putting the subject block above all "offenders"
coming before it, including the `it_behaves_like` send node.
@pirj pirj merged commit 2ea1fad into master Aug 21, 2020
@pirj pirj deleted the fix-leading-subject branch August 21, 2020 21:59
@pirj
Copy link
Member Author

pirj commented Aug 21, 2020

@bquorning Was going to say it's high time for 1.43.2 with two recent bugfixes, but it turns out we've merged a new cop, RSpec/RepeatedIncludeExample.

bquorning pushed a commit that referenced this pull request Aug 24, 2020
Fix subject nesting detection in RSpec/LeadingSubject
@bquorning
Copy link
Collaborator

bquorning commented Aug 24, 2020

Was going to say it's high time for 1.43.2 with two recent bugfixes

I’ve pushed a branch with cherry-picked commits: https://github.com/rubocop-hq/rubocop-rspec/compare/1.43-bugfixes

We can create a release from that branch, then merge it back into master afterwards. However, I cannot open a pull request, because what would the target branch be? (bundle exec rake runs and passes locally)

Good idea or bad idea?

@pirj
Copy link
Member Author

pirj commented Aug 24, 2020

@bquorning Great idea! 👏
Just merged #1020, can you please add it to the pack?

Can you please glance over #1018? We have different points of view with @koic, #1018 (comment) I'll squash and rebase in the meanwhile.

@pirj
Copy link
Member Author

pirj commented Aug 24, 2020

I cannot open a pull request, because what would the target branch be

Maybe some temp branch branched of the last release?
PS not sure if it's worth the hassle.

@pirj
Copy link
Member Author

pirj commented Aug 24, 2020

Maybe add your doc fix as well? #1020 skips manual, and changes docs.
Or, add the results of generate_cops_documentation in an extra commit. Just not to forget to remove it when merging back to master.

@bquorning
Copy link
Collaborator

I am pretty sure neither #1018 nor #1020 fix bugs caused by regressions between v1.42.0 and v1.43.0. So, I’ll leave them for the normal release cycle.

@bquorning bquorning mentioned this pull request Aug 25, 2020
3 tasks
@bquorning
Copy link
Collaborator

Opened #1022. Let’s talk there.

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.

Autocorrect for RSpec/LeadingSubject wrongly corrects subject
4 participants