-
-
Notifications
You must be signed in to change notification settings - Fork 278
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
Add new RedundantContext
cop
#2020
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Detect redundant `context` hook. | ||
# | ||
# @example | ||
# # bad | ||
# context 'when condition' do | ||
# it 'tests something' do | ||
# end | ||
# end | ||
# | ||
# # good | ||
# it 'tests something when condition' do | ||
# end | ||
# | ||
class RedundantContext < Base | ||
MSG = 'Redundant context with single example.' | ||
|
||
# @!method redundant_context?(node) | ||
def_node_matcher :redundant_context?, <<~PATTERN | ||
(block | ||
(send #rspec? :context _) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just specifically There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 _ :it ...) ...)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cosmetic: I think the receiver should be nil here, not _ |
||
PATTERN | ||
|
||
def on_block(node) | ||
return unless redundant_context?(node) | ||
|
||
add_offense(node) | ||
end | ||
alias on_numblock on_block | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will a numblock match with the current node pattern? |
||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::RedundantContext do | ||
it 'registers an offense when single example inside context' do | ||
expect_offense(<<~RUBY) | ||
context 'when condition' do | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Redundant context with single example. | ||
it 'does something' do | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register offense when multiple examples inside context' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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? |
||
expect_no_offenses(<<~RUBY) | ||
context 'when condition' do | ||
it 'does something' do | ||
end | ||
it 'does something else' do | ||
end | ||
end | ||
RUBY | ||
end | ||
end |
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.
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.