Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Master (Unreleased)

- Add support for `itblock` nodes. ([@Darhazer])
- `RSpec/ScatteredLet` now preserves the order of `let`s during auto-correction. ([@Darhazer])
- Fix a false negative for `RSpec/EmptyLineAfterFinalLet` inside `shared_examples` / `include_examples` / `it_behaves_like` blocks. ([@Darhazer])
- Add autocorrect support for `RSpec/SubjectDeclaration`. ([@eugeneius])
Expand Down
22 changes: 22 additions & 0 deletions lib/rubocop/cop/rspec/around_block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class AroundBlock < Base
(numblock (send nil? :around sym ?) ...)
PATTERN

# @!method hook_itblock(node)
def_node_matcher :hook_itblock, <<~PATTERN
(itblock (send nil? :around sym ?) ...)
PATTERN

# @!method find_arg_usage(node)
def_node_search :find_arg_usage, <<~PATTERN
{(send $... {:call :run}) (send _ _ $...) (yield $...) (block-pass $...)}
Expand All @@ -62,6 +67,12 @@ def on_numblock(node)
end
end

def on_itblock(node)
hook_itblock(node) do
check_for_itblock(node)
end
end

private

def add_no_arg_offense(node)
Expand Down Expand Up @@ -89,6 +100,17 @@ def check_for_numblock(block)
message: format(MSG_UNUSED_ARG, arg: :_1)
)
end

def check_for_itblock(block)
find_arg_usage(block) do |usage|
return if usage.include?(s(:lvar, :it))
end

add_offense(
block.children.last,
message: format(MSG_UNUSED_ARG, arg: :it)
)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/context_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ContextMethod < Base
...)
PATTERN

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
context_method(node) do |context|
add_offense(context) do |corrector|
corrector.replace(node.send_node.loc.selector, 'describe')
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/context_wording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ContextWording < Base
(block (send #rspec? { :context :shared_context } $(any_str ...) ...) ...)
PATTERN

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
context_wording(node) do |context|
unless matches_allowed_pattern?(description(context))
add_offense(context, message: message)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/described_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class DescribedClass < Base # rubocop:disable Metrics/ClassLength
def_node_search :contains_described_class?,
'(send nil? :described_class)'

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
# In case the explicit style is used, we need to remember what's
# being described.
@described_class, body = described_constant(node)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/empty_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class EmptyExampleGroup < Base
}
PATTERN

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return if node.each_ancestor(:any_def).any?
return if node.each_ancestor(:block).any? { |block| example?(block) }

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/empty_hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class EmptyHook < Base
(block $(send nil? #Hooks.all ...) _ nil?)
PATTERN

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
empty_hook?(node) do |hook|
add_offense(hook) do |corrector|
corrector.remove(
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/empty_line_after_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class EmptyLineAfterExample < Base

MSG = 'Add an empty line after `%<example>s`.'

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless example?(node)
return if allowed_one_liner?(node)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/empty_line_after_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class EmptyLineAfterExampleGroup < Base

MSG = 'Add an empty line after `%<example_group>s`.'

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless spec_group?(node)

missing_separating_line_offense(node) do |method|
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/empty_line_after_final_let.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class EmptyLineAfterFinalLet < Base
(block (send #rspec? {#SharedGroups.all #ExampleGroups.all #Includes.all} ...) args $_)
PATTERN

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless example_group_or_include?(node)

final_let = node.body.child_nodes.reverse.find { |child| let?(child) }
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/empty_line_after_hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def on_block(node)
end

alias on_numblock on_block
alias on_itblock on_block

private

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/empty_line_after_subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class EmptyLineAfterSubject < Base

MSG = 'Add an empty line after `%<subject>s`.'

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless subject?(node)
return unless inside_example_group?(node)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/example_length.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ExampleLength < Base

LABEL = 'Example'

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless example?(node)

check_code_length(node)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/example_without_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class ExampleWithoutDescription < Base
# @!method example_description(node)
def_node_matcher :example_description, '(send nil? _ $(str $_))'

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless example?(node)

check_example_without_description(node.send_node)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/example_wording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class ExampleWording < Base
} ...) ...)
PATTERN

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
it_description(node) do |description_node, message|
if message.match?(SHOULD_PREFIX)
add_wording_offense(description_node, MSG_SHOULD)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/expect_change.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def on_send(node)
end
end

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless style == :method_call

expect_change_with_block(node) do |receiver, message|
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/expect_in_hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def on_block(node)
end

alias on_numblock on_block
alias on_itblock on_block

private

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/expect_in_let.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ExpectInLet < Base
# @!method expectation(node)
def_node_search :expectation, '(send nil? #Expectations.all ...)'

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless let?(node)
return if node.body.nil?

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/hook_argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def on_block(node)
end

alias on_numblock on_block
alias on_itblock on_block

private

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/hooks_before_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def on_block(node)
end

alias on_numblock on_block
alias on_itblock on_block

private

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/indexed_let.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class IndexedLet < Base
}
PATTERN

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless spec_group?(node)

children = node.body&.child_nodes
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/instance_spy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class InstanceSpy < Base
...)
PATTERN

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless example?(node)

null_double(node) do |var, receiver|
Expand Down
13 changes: 13 additions & 0 deletions lib/rubocop/cop/rspec/iterated_expectation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class IteratedExpectation < Base
)
PATTERN

# @!method each_itblock?(node)
def_node_matcher :each_itblock?, <<~PATTERN
(itblock
(send ... :each) _ (...)
)
PATTERN

# @!method expectation?(node)
def_node_matcher :expectation?, <<~PATTERN
(send (send nil? :expect (lvar %)) :to ...)
Expand All @@ -55,6 +62,12 @@ def on_numblock(node)
end
end

def on_itblock(node)
each_itblock?(node) do
check_offense(node, :it)
end
end

private

def check_offense(node, argument)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/leading_subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class LeadingSubject < Base

MSG = 'Declare `subject` above any other `%<offending>s` declarations.'

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless subject?(node)
return unless inside_example_group?(node)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/let_before_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def self.autocorrect_incompatible_with
[RSpec::ScatteredLet]
end

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless example_group_with_body?(node)

check_let_declarations(node.body) if multiline_block?(node.body)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/let_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class LetSetup < Base
# @!method method_called?(node)
def_node_search :method_called?, '(send nil? %)'

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless example_or_shared_group_or_including?(node)

unused_let_bang(node) do |let|
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/missing_example_group_argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module RSpec
class MissingExampleGroupArgument < Base
MSG = 'The first argument to `%<method>s` should not be empty.'

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless example_group?(node)
return if node.send_node.arguments?

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/mixin/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def on_block(node)
end
end
alias on_numblock on_block
alias on_itblock on_block

def on_metadata(_symbols, _hash)
raise ::NotImplementedError
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/multiple_expectations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class MultipleExpectations < Base
(block (send nil? :aggregate_failures ...) ...)
PATTERN

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless example?(node)

return if example_with_aggregate_failures?(node)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/multiple_memoized_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class MultipleMemoizedHelpers < Base

exclude_limit 'Max'

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless spec_group?(node)

count = all_helpers(node).uniq.count
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/multiple_subjects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class MultipleSubjects < Base

MSG = 'Do not set more than one subject per example group'

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless example_group?(node)

subjects = RuboCop::RSpec::ExampleGroup.new(node).subjects
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/named_subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class NamedSubject < Base
# @!method subject_usage(node)
def_node_search :subject_usage, '$(send nil? :subject)'

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
if !example_or_hook_block?(node) || ignored_shared_example?(node)
return
end
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/no_expectation_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def on_block(node)
end

alias on_numblock on_block
alias on_itblock on_block
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/overwriting_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class OverwritingSetup < Base
# @!method first_argument_name(node)
def_node_matcher :first_argument_name, '(send _ _ ({str sym} $_))'

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless example_group_with_body?(node)

find_duplicates(node.body) do |duplicate, name|
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/predicate_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def on_send(node)
end
end

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
check_explicit(node) if style == :explicit
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/redundant_around.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def on_block(node)
end
end
alias on_numblock on_block
alias on_itblock on_block

def on_send(node)
return unless match_redundant_around_hook_send?(node)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/repeated_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module RSpec
class RepeatedDescription < Base
MSG = "Don't repeat descriptions within an example group."

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless example_group?(node)

repeated_descriptions(node).each do |description|
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/repeated_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RepeatedExample < Base
MSG = "Don't repeat examples within an example group. " \
'Repeated on line(s) %<lines>s.'

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless example_group?(node)

find_repeated_examples(node).each do |repeated_examples|
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/return_from_stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def on_send(node)
check_and_return_call(node)
end

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
return unless style == :and_return
return unless stub_with_block?(node)

Expand Down
Loading