Skip to content

Commit

Permalink
Add block-pass support to MultipleMemoizedHelpers
Browse files Browse the repository at this point in the history
  • Loading branch information
pirj committed Aug 7, 2020
1 parent e894b2e commit cacb38d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
22 changes: 15 additions & 7 deletions lib/rubocop/cop/rspec/multiple_memoized_helpers.rb
Expand Up @@ -114,14 +114,22 @@ def all_helpers(node)
end

def helpers(node)
example_group = RuboCop::RSpec::ExampleGroup.new(node)
variables =
if allow_subject?
example_group.lets
else
example_group.lets + example_group.subjects
variable_nodes(node).map do |variable_node|
if variable_node.block_type?
variable_definition?(variable_node.send_node)
else # block-pass (`let(:foo, &bar)`)
variable_definition?(variable_node)
end
variables.map { |variable| variable_definition?(variable.send_node) }
end
end

def variable_nodes(node)
example_group = RuboCop::RSpec::ExampleGroup.new(node)
if allow_subject?
example_group.lets
else
example_group.lets + example_group.subjects
end
end

def max
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/rspec/variable.rb
Expand Up @@ -8,7 +8,7 @@ module Variable
extend RuboCop::NodePattern::Macros

def_node_matcher :variable_definition?, <<~PATTERN
(send #rspec? #{(Helpers::ALL + Subject::ALL).node_pattern_union}
(send nil? #{(Helpers::ALL + Subject::ALL).node_pattern_union}
$({sym str dsym dstr} ...) ...)
PATTERN
end
Expand Down
3 changes: 3 additions & 0 deletions spec/smoke_tests/weird_rspec_spec.rb
Expand Up @@ -11,6 +11,9 @@
let (:foo) { 1 }
let! (:bar){}

bar = -> {}
let(:foo, &bar)

let :a do end

let(:bar) { <<-HEREDOC }
Expand Down

0 comments on commit cacb38d

Please sign in to comment.