Sha256: 7449b5d71c552b2154cde3911157b786bd77325bedb8815644555684af98887e

Contents?: true

Size: 1.95 KB

Versions: 9

Compression:

Stored size: 1.95 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

        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

9 entries across 9 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.13.1/lib/rubocop/cop/rspec/empty_line_after_example.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.13.1/lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.15.0 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.14.2 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.14.1 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.14.0 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.13.2 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.13.1 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.13.0 lib/rubocop/cop/rspec/empty_line_after_example.rb