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 an incorrect autocorrect for RSpec/PredicateMatcher when multiline expect and predicate method with heredoc #1555

Conversation

ydah
Copy link
Member

@ydah ydah commented Jan 14, 2023

This PR is fix an incorrect autocorrect for RSpec/PredicateMatcher when multiline expect and predicate method with heredoc.

The following code:

expect(foo)
  .to be_something(<<~TEXT)
    bar
  TEXT

This would be corrected as follows:

expect(foo.something?(<<~TEXT))
  .to be(true) <<----------- this would be included in heredoc
    bar
TEXT

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).

@ydah ydah force-pushed the fix-incorrect-autocorrect-rspec-PredicateMatcher branch from eb88035 to d51a350 Compare January 14, 2023 07:08
@bquorning
Copy link
Collaborator

FYI I intend to do a v2.17.1 release when this and #1554 is merged. Followed by a v2.18.0 release with the extraction of Capybara cops (#1519).

corrector_explicit(corrector, node, actual, matcher, matcher)
end
end
end

def uncorrectable_matcher?(node, matcher)
str_args = matcher.arguments.select(&:str_type?)
str_args.any?(&:heredoc?) && !same_line?(node, matcher)
Copy link
Member

Choose a reason for hiding this comment

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

what if the heredoc contains variables? wouldn't it be dstr_type and still contain heredoc nodes? I suggest adding dstr and xstr here

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for catching this. I updated this PR.

…ine expect and predicate method with heredoc

This PR is fix an incorrect autocorrect for `RSpec/PredicateMatcher` when multiline expect and predicate method with heredoc.

The following code:
```ruby
expect(foo)
  .to be_something(<<~TEXT)
    bar
  TEXT
```

This would be corrected as follows:
```ruby
expect(foo.something?(<<~TEXT))
  .to be(true)
    bar
TEXT
```
@ydah ydah force-pushed the fix-incorrect-autocorrect-rspec-PredicateMatcher branch from c5414b2 to 3799e63 Compare January 14, 2023 13:08
@ydah ydah requested a review from Darhazer January 14, 2023 13:34
Copy link
Member

@pirj pirj 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!

@bquorning
Copy link
Collaborator

@Darhazer Is this PR ok to merge?

@bquorning bquorning merged commit 3a31ab1 into rubocop:master Jan 15, 2023
@ydah ydah deleted the fix-incorrect-autocorrect-rspec-PredicateMatcher branch January 16, 2023 05:03
bquorning added a commit that referenced this pull request Jan 16, 2023
…edicateMatcher

Fix an incorrect autocorrect for `RSpec/PredicateMatcher` when multiline expect and predicate method with 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.

None yet

4 participants