Sha256: 39f9ca19d58ff131bab1229a781595e28ccb4e15d1b69d10edb415b81d6872bc

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # Access modifiers should be surrounded by blank lines.
      class EmptyLinesAroundAccessModifier < Cop
        include AccessModifierNode

        MSG = 'Keep a blank line before and after `%s`.'

        def on_send(node)
          return unless modifier_node?(node)

          return if empty_lines_around?(node)

          add_offense(node, :expression)
        end

        def autocorrect(node)
          @corrections << lambda do |corrector|
            send_line = node.loc.line
            previous_line = processed_source[send_line - 2]
            next_line = processed_source[send_line]

            line = Parser::Source::Range.new(
              processed_source.buffer,
              node.loc.expression.begin_pos - node.loc.column,
              node.loc.expression.end_pos
            )

            unless previous_line_empty?(previous_line)
              corrector.insert_before(line, "\n")
            end

            unless next_line_empty?(next_line)
              corrector.insert_after(line, "\n")
            end
          end
        end

        private

        def previous_line_empty?(previous_line)
          class_def?(previous_line.lstrip) || previous_line.blank?
        end

        def next_line_empty?(next_line)
          next_line.blank?
        end

        def empty_lines_around?(node)
          send_line = node.loc.line
          previous_line = processed_source[send_line - 2]
          next_line = processed_source[send_line]

          previous_line_empty?(previous_line) && next_line_empty?(next_line)
        end

        def class_def?(line)
          %w(class module).any? { |keyword| line.start_with?(keyword) }
        end

        def message(node)
          format(MSG, node.loc.selector.source)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-0.25.0 lib/rubocop/cop/style/empty_lines_around_access_modifier.rb