Skip to content

Commit

Permalink
Fix MethodObjectAsBlock using block arguments
Browse files Browse the repository at this point in the history
I think the plain old block arguments are much easier to read than
numbered parameters - except maybe in a few cases. E.g. is
`{ |n| let?(n) }` really better to read than `{ let?(_1) }`?
  • Loading branch information
bquorning committed May 5, 2024
1 parent e62faef commit a417b30
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/expect_actual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def simple_literal?(node)

def complex_literal?(node)
COMPLEX_LITERALS.include?(node.type) &&
node.each_child_node.all? { literal?(_1) }
node.each_child_node.all? { |child_node| literal?(child_node) }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/file_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def ensure_correct_file_path(send_node, example_group, arguments)
end

def routing_spec?(args)
args.any? { routing_metadata?(_1) } || routing_spec_path?
args.any? { |arg| routing_metadata?(arg) } || routing_spec_path?
end

def pattern_for(example_group, arguments)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/leaky_constant_declaration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def on_module(node)
private

def inside_describe_block?(node)
node.each_ancestor(:block).any? { spec_group?(_1) }
node.each_ancestor(:block).any? { |ancestor| spec_group?(ancestor) }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/multiple_describes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MultipleDescribes < Base

def on_top_level_group(node)
top_level_example_groups =
top_level_groups.select { example_group?(_1) }
top_level_groups.select { |group| example_group?(group) }

return if top_level_example_groups.one?
return unless top_level_example_groups.first.equal?(node)
Expand Down
6 changes: 2 additions & 4 deletions lib/rubocop/cop/rspec/multiple_memoized_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,8 @@ def on_new_investigation
attr_reader :example_group_memoized_helpers

def all_helpers(node)
[
*helpers(node),
*node.each_ancestor(:block).flat_map { helpers(_1) }
]
helpers(node) +
node.each_ancestor(:block).flat_map { |ancestor| helpers(ancestor) }
end

def helpers(node)
Expand Down
7 changes: 5 additions & 2 deletions lib/rubocop/cop/rspec/named_subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,11 @@ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
private

def ignored_shared_example?(node)
cop_config['IgnoreSharedExamples'] &&
node.each_ancestor(:block).any? { shared_example?(_1) }
return false unless cop_config['IgnoreSharedExamples']

node.each_ancestor(:block).any? do |ancestor|
shared_example?(ancestor)
end
end

def check_explicit_subject(node)
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/rspec/subject_stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ class SubjectStub < Base
PATTERN

def on_top_level_group(node)
@explicit_subjects = find_all_explicit(node) { subject?(_1) }
@subject_overrides = find_all_explicit(node) { let?(_1) }
@explicit_subjects = find_all_explicit(node) { |n| subject?(n) }
@subject_overrides = find_all_explicit(node) { |n| let?(n) }

find_subject_expectations(node) do |stub|
add_offense(stub)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def deep_dup(object)
when Array
object.map { |item| deep_dup(item) }
when Hash
object.transform_values { deep_dup(_1) }
object.transform_values { |value| deep_dup(value) }
else
object # only collections undergo modifications and need duping
end
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module SpecHelper
Dir
.glob(File.expand_path(spec_helper_glob, __dir__))
.sort
.each { require(_1) }
.each { |path| require path }

RSpec.configure do |config|
# Set metadata so smoke tests are run on all cop specs
Expand Down

0 comments on commit a417b30

Please sign in to comment.