Sha256: 6df2c21d488e1b602135c43a2c9082caf0179eea0c7cd1f2ea9216c95a213741

Contents?: true

Size: 1.29 KB

Versions: 17

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # This auto-corrects gem dependency order
    class OrderedGemCorrector
      extend OrderedGemNode

      class << self
        attr_reader :processed_source, :comments_as_separators

        def correct(processed_source, node,
                    previous_declaration, comments_as_separators)
          @processed_source = processed_source
          @comments_as_separators = comments_as_separators

          current_range = declaration_with_comment(node)
          previous_range = declaration_with_comment(previous_declaration)

          ->(corrector) do swap_range(corrector, current_range, previous_range) end
        end

        private

        def declaration_with_comment(node)
          buffer = processed_source.buffer
          begin_pos = get_source_range(node, comments_as_separators).begin_pos
          end_line = buffer.line_for_position(node.loc.expression.end_pos)
          end_pos = buffer.line_range(end_line).end_pos
          Parser::Source::Range.new(buffer, begin_pos, end_pos)
        end

        def swap_range(corrector, range1, range2)
          src1 = range1.source
          src2 = range2.source
          corrector.replace(range1, src2)
          corrector.replace(range2, src1)
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 3 rubygems

Version Path
rubocop-1.21.0 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.20.0 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.19.1 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.19.0 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.18.4 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.18.3 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.18.2 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.18.1 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.18.0 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.17.0 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.16.1 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.16.0 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.15.0 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
cocRb-0.1.0 .bundle/ruby/3.0.0/gems/rubocop-1.14.0/lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.14.0 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.13.0 lib/rubocop/cop/correctors/ordered_gem_corrector.rb