Sha256: 03232f3f8e32eefeded9e8389204c929230a0a5c922788afb8099ae346292f8d

Contents?: true

Size: 1.34 KB

Versions: 70

Compression:

Stored size: 1.34 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::CommentsHelp
        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 = start_line_position(other)

          corrector.insert_before(position, "#{source(original)}\n")
          corrector.remove(node_range_with_surrounding_space(original))
        end

        def move_after(other)
          position = end_line_position(other)

          corrector.insert_after(position, "\n#{source(original)}")
          corrector.remove(node_range_with_surrounding_space(original))
        end

        private

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

        def node_range(node)
          source_range_with_comment(node)
        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

70 entries across 64 versions & 7 rubygems

Version Path
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.22.0 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.21.0 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.20.0 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.19.0 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.18.1 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.18.0 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.17.1 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.17.0 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.16.0 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.15.0 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.14.2 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.14.1 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.14.0 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.13.2 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.13.1 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.13.0 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.12.1 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.12.0 lib/rubocop/rspec/corrector/move_node.rb
rubocop-rspec-2.11.1 lib/rubocop/rspec/corrector/move_node.rb