lib/rubocop/cop/rspec/nested_groups.rb in rubocop-rspec-1.12.0 vs lib/rubocop/cop/rspec/nested_groups.rb in rubocop-rspec-1.13.0
- old
+ new
@@ -3,11 +3,11 @@
module RuboCop
module Cop
module RSpec
# Checks for nested example groups.
#
- # This cop is configurable using the `MaxNesting` option
+ # This cop is configurable using the `Max` option
#
# @example
# # bad
# context 'when using some feature' do
# let(:some) { :various }
@@ -54,11 +54,11 @@
#
# @example configuration
#
# # .rubocop.yml
# RSpec/NestedGroups:
- # MaxNesting: 2
+ # Max: 2
#
# context 'when using some feature' do
# let(:some) { :various }
# let(:feature) { :setup }
#
@@ -85,35 +85,39 @@
# end
#
class NestedGroups < Cop
include RuboCop::RSpec::TopLevelDescribe
- MSG = 'Maximum example group nesting exceeded'.freeze
+ MSG = 'Maximum example group nesting exceeded [%d/%d].'.freeze
DEPRECATED_MAX_KEY = 'MaxNesting'.freeze
DEPRECATION_WARNING =
"Configuration key `#{DEPRECATED_MAX_KEY}` for #{cop_name} is " \
'deprecated in favor of `Max`. Please use that instead.'.freeze
def_node_search :find_contexts, ExampleGroups::ALL.block_pattern
def on_top_level_describe(node, _)
- find_nested_contexts(node.parent) do |context|
- add_offense(context.children.first, :expression)
+ find_nested_contexts(node.parent) do |context, nesting|
+ add_offense(context.children.first, :expression, message(nesting))
end
end
private
def find_nested_contexts(node, nesting: 1, &block)
find_contexts(node) do |nested_context|
- yield(nested_context) if nesting > max_nesting
+ yield(nested_context, nesting) if nesting > max_nesting
nested_context.each_child_node do |child|
find_nested_contexts(child, nesting: nesting + 1, &block)
end
end
+ end
+
+ def message(nesting)
+ format(MSG, nesting, max_nesting)
end
def max_nesting
@max_nesting ||= Integer(max_nesting_config)
end