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

Remove unnecessary early return in RSpec/ContextWordingcop #1981

Closed
wants to merge 1 commit into from

Conversation

ydah
Copy link
Member

@ydah ydah commented Oct 18, 2024

Checks if allowed_patterns is empty, but looking at the implementation of matches_allowed_pattern?, it refers to it as allowed_patterns.any?, so return false if allowed_patterns .empty? is a completely unnecessary early return.

https://github.com/rubocop/rubocop/blob/e7ccb0200d013d1c4b75b3face0dea0538620769/lib/rubocop/cop/mixin/allowed_pattern.rb#L29-L31

def matches_allowed_pattern?(line)
  allowed_patterns.any? { |pattern| Regexp.new(pattern).match?(line) }
end

also resolve: #1972

image


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 requested a review from a team as a code owner October 18, 2024 02:58
@pirj
Copy link
Member

pirj commented Oct 18, 2024

I’m quite concerned if we had a method, and it was not fully covered with specs, and now we’re refactoring it to a node pattern where coverage is not calculated, can we say that it does the same thing with certainty?
I live node patterns, they are often more expressive due to their declarative nature.
But does this one read better than the method?

Node patterns support inline comments, but we rarely use that, and it’s not always useful.
Node pattern has this @!method YARD directive, but we often miss the opportunity to add examples of what it does match and what not.

With all this, I think in this case a node pattern is inferior to a method.
Especially since this change conceals a gap in our spec coverage.

@ydah ydah force-pushed the refactor-cwc branch 2 times, most recently from be4954b to d749bb7 Compare October 19, 2024 01:50
@ydah ydah changed the title Refactor RSpec/ContextWording cop to use allowed? method for pattern validation Remove unnecessary early return in RSpec/ContextWordingcop Oct 19, 2024
@ydah
Copy link
Member Author

ydah commented Oct 19, 2024

@pirj It is certainly not a good choice to replace it with a node pattern for which coverage is not calculated.
So I have updated this PR concern as removing unnecessary early returns. What do you think?

@ydah ydah requested a review from pirj October 19, 2024 01:53
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.

Nice refactoring!
Great to see it’s getting us to 100% coverage ☺️

@pirj
Copy link
Member

pirj commented Oct 19, 2024

Another pair of eyes in case I’m missing something this early morning, @rubocop/rubocop-rspec ?

@bquorning
Copy link
Collaborator

Isn’t the reason we didn’t have complete spec coverage before that we never hit a case where allowed_patterns.empty?? Should we add a context 'when both Prefixes and AllowedPatterns are empty' to the specs before refactoring?

@corsonknowles
Copy link
Contributor

@bquorning That spec is available here:

Checks if `allowed_patterns` is empty, but looking at the implementation of `matches_allowed_pattern?`, it refers to it as `allowed_patterns.any?`, so `return false if allowed_patterns .empty?` is a completely unnecessary early return.

https://github.com/rubocop/rubocop/blob/e7ccb0200d013d1c4b75b3face0dea0538620769/lib/rubocop/cop/mixin/allowed_pattern.rb#L29-L31

```
def matches_allowed_pattern?(line)
  allowed_patterns.any? { |pattern| Regexp.new(pattern).match?(line) }
end
```
@ydah ydah marked this pull request as draft October 21, 2024 08:31
@ydah
Copy link
Member Author

ydah commented Oct 21, 2024

Close this PR because it was found to operate differently from the current specification.

@ydah ydah closed this Oct 21, 2024
@ydah ydah deleted the refactor-cwc branch October 21, 2024 09:59
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.

4 participants