lib/rubocop/cop/rspec/empty_example_group.rb in rubocop-rspec-2.12.1 vs lib/rubocop/cop/rspec/empty_example_group.rb in rubocop-rspec-2.13.0

- old
+ new

@@ -4,11 +4,10 @@ module Cop module RSpec # Checks if an example group does not include any tests. # # @example usage - # # # bad # describe Bacon do # let(:bacon) { Bacon.new(chunkiness) } # let(:chunkiness) { false } # @@ -33,11 +32,16 @@ # # # good # describe Bacon do # pending 'will add tests later' # end + # class EmptyExampleGroup < Base + extend AutoCorrector + + include RangeHelp + MSG = 'Empty example group detected.' # @!method example_group_body(node) # Match example group blocks and yield their body # @@ -117,27 +121,31 @@ # @example source that matches # it { expect(myself).to be_run } # describe { it { i_run_as_well } } # # @example source that does not match - # before { it { whatever here wont run anyway } } + # before { it { whatever here won't run anyway } } # # @param node [RuboCop::AST::Node] # @return [Array<RuboCop::AST::Node>] matching nodes def_node_matcher :examples?, <<~PATTERN { #examples_directly_or_in_block? (begin <#examples_directly_or_in_block? ...>) } PATTERN - def on_block(node) + def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler return if node.each_ancestor(:def, :defs).any? return if node.each_ancestor(:block).any? { |block| example?(block) } example_group_body(node) do |body| - add_offense(node.send_node) if offensive?(body) + next unless offensive?(body) + + add_offense(node.send_node) do |corrector| + corrector.remove(removed_range(node)) + end end end private @@ -160,9 +168,16 @@ end end def examples_in_branches?(condition_node) condition_node.branches.any? { |branch| examples?(branch) } + end + + def removed_range(node) + range_by_whole_lines( + node.location.expression, + include_final_newline: true + ) end end end end end