Sha256: 2ec789f24e102f81494084710a16e42339af042004be7a4536d7127d4f60d537

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module RuboCop
  module RSpec
    module Corrector
      # Helper methods to move a node
      class MoveNode
        include RuboCop::Cop::RangeHelp
        include RuboCop::Cop::RSpec::FinalEndLocation

        attr_reader :original, :corrector, :processed_source

        def initialize(node, corrector, processed_source)
          @original = node
          @corrector = corrector
          @processed_source = processed_source # used by RangeHelp
        end

        def move_before(other)
          position = other.loc.expression
          indent = ' ' * other.loc.column
          newline_indent = "\n#{indent}"

          corrector.insert_before(position, source(original) + newline_indent)
          corrector.remove(node_range_with_surrounding_space(original))
        end

        def move_after(other)
          position = final_end_location(other)
          indent = ' ' * other.loc.column
          newline_indent = "\n#{indent}"

          corrector.insert_after(position, newline_indent + source(original))
          corrector.remove(node_range_with_surrounding_space(original))
        end

        private

        def source(node)
          node_range(node).source
        end

        def node_range(node)
          node.loc.expression.with(end_pos: final_end_location(node).end_pos)
        end

        def node_range_with_surrounding_space(node)
          range = node_range(node)
          range_by_whole_lines(range, include_final_newline: true)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-rspec-2.1.0 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.0.1 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.0.0 lib/rubocop/rspec/corrector/move_node.rb