diff --git a/lib/rubocop/cop/rspec/multiple_memoized_helpers.rb b/lib/rubocop/cop/rspec/multiple_memoized_helpers.rb index 38dbbe896..a7295f955 100644 --- a/lib/rubocop/cop/rspec/multiple_memoized_helpers.rb +++ b/lib/rubocop/cop/rspec/multiple_memoized_helpers.rb @@ -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 diff --git a/lib/rubocop/rspec/variable.rb b/lib/rubocop/rspec/variable.rb index 2dfe92c0c..887f20ca7 100644 --- a/lib/rubocop/rspec/variable.rb +++ b/lib/rubocop/rspec/variable.rb @@ -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 diff --git a/spec/smoke_tests/weird_rspec_spec.rb b/spec/smoke_tests/weird_rspec_spec.rb index df876e6c6..4d3d29dc6 100644 --- a/spec/smoke_tests/weird_rspec_spec.rb +++ b/spec/smoke_tests/weird_rspec_spec.rb @@ -11,6 +11,9 @@ let (:foo) { 1 } let! (:bar){} + bar = -> {} + let(:foo, &bar) + let :a do end let(:bar) { <<-HEREDOC }