Conversation
To check context with single example. Closes rubocop#2001
b5295a5 to
4d58956
Compare
|
Seems like we have a bunch of occurrences in this repo where we use single example inside context. |
|
It’s doubtful whether this should be regarded as "redundant" or as an appropriate use of vocabulary, as it depend on individual judgment. The justification provided in #2001 doesn’t seem entirely sufficient, at the least, it seems this cop should be disabled by default. |
|
IMO, I am reluctant to add this cop. Our project goal is to enforce the guidelines and best practices outlined in the community RSpec style guide. Currently, I am skeptical about whether this approach qualifies as a best practice. As a first step, it might be better to start by discussing in the RSpec Style Guide. |
|
I like this cop. I have often manually changed specs like context "when foo" do
it "does bar" do
end
endinto it "does bar when foo" do
endbut I agree that it should probably be disabled by default. |
| # @!method redundant_context?(node) | ||
| def_node_matcher :redundant_context?, <<~PATTERN | ||
| (block | ||
| (send #rspec? :context _) |
There was a problem hiding this comment.
Just specifically context and it?
I’dd love to have at least balancing examples for describe/example aliases if it’s a deliberate design choice.
| # @!method redundant_context?(node) | ||
| def_node_matcher :redundant_context?, <<~PATTERN | ||
| (block | ||
| (send #rspec? :context _) |
There was a problem hiding this comment.
The _ suggests that it only has one anything argument, with an assumption that it’s a docstring. But what if there’s a secondary string argument, or symbolic/hash metadata, too?
| (block | ||
| (send #rspec? :context _) | ||
| _ | ||
| (block (send _ :it ...) ...)) |
There was a problem hiding this comment.
Cosmetic: I think the receiver should be nil here, not _
|
|
||
| add_offense(node) | ||
| end | ||
| alias on_numblock on_block |
There was a problem hiding this comment.
Will a numblock match with the current node pattern?
| RUBY | ||
| end | ||
|
|
||
| it 'does not register offense when multiple examples inside context' do |
There was a problem hiding this comment.
It is more fair to say “when the example is the only statement inside the example group. Eg another it_behaves_like or a method def would cause the cop to ignore the group.
Are there any cases with more than one statement (but one example) that we still want to flag/correct?
If not - just ignore this note, just make the description more generic, and maybe add another example with an example and some other statement in an example group.
|
|
||
| RSpec/RedundantContext: | ||
| Description: Detect redundant `context` hook. | ||
| Enabled: pending |
There was a problem hiding this comment.
I’m usually strongly against disabling cops, but this one, even though certainly serving some projects that strive to enforce this style, isn’t a good fit for the majority.
I support what @koic and @bquorning said.
|
If we are going to disable on default this Cop, I don’t think it should be added in the first place. It might be useful as a custom Cop for users, but I’m skeptical about whether it should be one that we maintain. For example, in my case, I group such Cops into my own custom Cop collection: |
|
Fair enough, @ydah @bquorning wdyt? |
To check context with single example.
Closes #2001
Before submitting the PR make sure the following are checked:
master(if not - rebase it).CHANGELOG.mdif the new code introduces user-observable changes.bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).If you have created a new cop:
config/default.yml.Enabled: pendinginconfig/default.yml.Enabled: truein.rubocop.yml.VersionAdded: "<<next>>"indefault/config.yml.