Sha256: 539be07777af32d200ed3e121ec74cba88238fa7b5d59b32e303493e4078d6a1

Contents?: true

Size: 1.96 KB

Versions: 20

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks if there is an empty line after example blocks.
      #
      # @example
      #   # bad
      #   RSpec.describe Foo do
      #     it 'does this' do
      #     end
      #     it 'does that' do
      #     end
      #   end
      #
      #   # good
      #   RSpec.describe Foo do
      #     it 'does this' do
      #     end
      #
      #     it 'does that' do
      #     end
      #   end
      #
      #   # fair - it's ok to have non-separated one-liners
      #   RSpec.describe Foo do
      #     it { one }
      #     it { two }
      #   end
      #
      # @example with AllowConsecutiveOneLiners configuration
      #   # rubocop.yml
      #   # RSpec/EmptyLineAfterExample:
      #   #   AllowConsecutiveOneLiners: false
      #
      #   # bad
      #   RSpec.describe Foo do
      #     it { one }
      #     it { two }
      #   end
      #
      class EmptyLineAfterExample < Base
        extend AutoCorrector
        include EmptyLineSeparation

        MSG = 'Add an empty line after `%<example>s`.'

        def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
          return unless example?(node)
          return if allowed_one_liner?(node)

          missing_separating_line_offense(node) do |method|
            format(MSG, example: method)
          end
        end

        private

        def allowed_one_liner?(node)
          consecutive_one_liner?(node) && allow_consecutive_one_liners?
        end

        def allow_consecutive_one_liners?
          cop_config['AllowConsecutiveOneLiners']
        end

        def consecutive_one_liner?(node)
          node.single_line? && next_one_line_example?(node)
        end

        def next_one_line_example?(node)
          next_sibling = node.right_sibling
          return unless next_sibling
          return unless example?(next_sibling)

          next_sibling.single_line?
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.25.0 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.24.1 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.24.0 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.23.2 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.23.1 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.23.0 lib/rubocop/cop/rspec/empty_line_after_example.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/empty_line_after_example.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/empty_line_after_example.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/empty_line_after_example.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.22.0 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.21.0 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.20.0 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.19.0 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.18.1 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.18.0 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.17.1 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.17.0 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.16.0 lib/rubocop/cop/rspec/empty_line_after_example.rb