Skip to content

Commit

Permalink
Allow ExampleGroup Concept to access config
Browse files Browse the repository at this point in the history
  • Loading branch information
sl4vr committed Jul 8, 2020
1 parent edc2c0e commit 70a2248
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/scattered_setup.rb
Expand Up @@ -42,7 +42,7 @@ def on_block(node)
end

def repeated_hooks(node)
hooks = RuboCop::RSpec::ExampleGroup.new(node)
hooks = RuboCop::RSpec::ExampleGroup.new(node, self)
.hooks
.select(&:knowable_scope?)
.group_by { |hook| [hook.name, hook.scope, hook.metadata] }
Expand Down
16 changes: 15 additions & 1 deletion lib/rubocop/rspec/example_group.rb
Expand Up @@ -4,6 +4,17 @@ module RuboCop
module RSpec
# Wrapper for RSpec example groups
class ExampleGroup < Concept
# Cop object may be passed into overriden constructor as additional param,
# so we can call predicate on cop itself and will have access to #config
# method in order to use configured language aliases for predicates.
# If no predicate_receiver passed - predicate will be called on
# ExampleGroup as if was before.
def initialize(node, predicate_receiver = self)
@predicate_receiver = predicate_receiver

super(node)
end

# @!method scope_change?(node)
#
# Detect if the node is an example group or shared example
Expand Down Expand Up @@ -32,11 +43,14 @@ def hooks

private

attr_reader :predicate_receiver

# Recursively search for predicate within the current scope
#
# Searches node and halts when a scope change is detected
#
# @param node [RuboCop::Node] node to recursively search
# @param predicate [Symbol] method to call with node as argument
#
# @return [Array<RuboCop::Node>] discovered nodes
def find_all_in_scope(node, predicate)
Expand All @@ -46,7 +60,7 @@ def find_all_in_scope(node, predicate)
end

def find_all(node, predicate)
if public_send(predicate, node)
if predicate_receiver.public_send(predicate, node)
[node]
elsif scope_change?(node) || example?(node)
[]
Expand Down

0 comments on commit 70a2248

Please sign in to comment.