Sha256: b429abe62c74b9feca037f7cc48c7af8a1e27cda64f181ea0ad8f3dbe6cebfad

Contents?: true

Size: 1.98 KB

Versions: 24

Compression:

Stored size: 1.98 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 false unless next_sibling
          return false unless example?(next_sibling)

          next_sibling.single_line?
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.3/lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-3.3.0 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-3.2.0 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/empty_line_after_example.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/empty_line_after_example.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.31.0 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.30.0 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.29.2 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.29.1 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.29.0 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.28.0 lib/rubocop/cop/rspec/empty_line_after_example.rb
rubocop-rspec-2.27.1 lib/rubocop/cop/rspec/empty_line_after_example.rb