Skip to content

Commit

Permalink
Address code review
Browse files Browse the repository at this point in the history
  • Loading branch information
andrykonchin committed Jun 29, 2020
1 parent 12a45e7 commit 8a2a365
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions lib/rubocop/cop/rspec/nested_groups.rb
Expand Up @@ -97,32 +97,32 @@ class NestedGroups < Cop
"Configuration key `#{DEPRECATED_MAX_KEY}` for #{cop_name} is " \
'deprecated in favor of `Max`. Please use that instead.'

def_node_search :find_contexts, ExampleGroups::ALL.block_pattern
def_node_matcher :context?, ExampleGroups::ALL.block_pattern
def_node_search :find_example_groups, ExampleGroups::ALL.block_pattern
def_node_matcher :example_group?, ExampleGroups::ALL.block_pattern

def on_top_level_describe(node, _args)
find_nested_contexts(node.parent) do |context, nesting|
find_nested_example_groups(node.parent) do |example_group, nesting|
self.max = nesting
add_offense(
context.send_node,
example_group.send_node,
message: message(nesting)
)
end
end

private

def find_nested_contexts(node)
find_contexts(node) do |nested_context|
nesting = context_nesting_count(nested_context)
def find_nested_example_groups(node)
find_example_groups(node) do |example_group|
nesting = nesting_count(example_group)

yield(nested_context, nesting) if nesting > max_nesting
yield(example_group, nesting) if nesting > max_nesting
end
end

def context_nesting_count(node)
parents = node.ancestors.select { |a| context?(a) }
parents.count + 1
def nesting_count(node)
count = node.each_ancestor(:block).count { |n| example_group?(n) }
count + 1
end

def message(nesting)
Expand Down

0 comments on commit 8a2a365

Please sign in to comment.