Sha256: 8e77a650fb79ece3f81b76bb76cbe9f17b75fad401325e5ce310306b4951ebc2

Contents?: true

Size: 1.18 KB

Versions: 7

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks if there is an empty line after subject block.
      #
      # @example
      #   # bad
      #     subject(:obj) { described_class }
      #     let(:foo) { bar }
      #
      #   # good
      #     subject(:obj) { described_class }
      #
      #     let(:foo) { bar }
      class EmptyLineAfterSubject < Cop
        MSG = 'Add empty line after `subject`.'.freeze

        def_node_matcher :subject?, '(block $(send nil :subject ...) args ...)'

        def on_block(node)
          return unless subject?(node) && !in_spec_block?(node)
          return if node.equal?(node.parent.children.last)

          send_line = node.loc.end.line
          next_line = processed_source[send_line]
          return if next_line.blank?

          add_offense(node, :expression, MSG)
        end

        def autocorrect(node)
          ->(corrector) { corrector.insert_after(node.loc.end, "\n") }
        end

        private

        def in_spec_block?(node)
          node.each_ancestor(:block).any? do |ancestor|
            Examples::ALL.include?(ancestor.method_name)
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rubocop-rspec-1.18.0 lib/rubocop/cop/rspec/empty_line_after_subject.rb
rubocop-rspec-1.17.1 lib/rubocop/cop/rspec/empty_line_after_subject.rb
rubocop-rspec-1.17.0 lib/rubocop/cop/rspec/empty_line_after_subject.rb
rubocop-rspec-1.16.0 lib/rubocop/cop/rspec/empty_line_after_subject.rb
rubocop-rspec-1.15.1 lib/rubocop/cop/rspec/empty_line_after_subject.rb
rubocop-rspec-1.15.0 lib/rubocop/cop/rspec/empty_line_after_subject.rb
rubocop-rspec-1.14.0 lib/rubocop/cop/rspec/empty_line_after_subject.rb