Sha256: e81f7a8ef59ebaad143c38b69b588f727be8ea02b39bcd9cc70e8091f7fb40c7

Contents?: true

Size: 1.35 KB

Versions: 30

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # This autocorrects gem dependency order
    class OrderedGemCorrector
      class << self
        include OrderedGemNode
        include RangeHelp

        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) { swap_range(corrector, current_range, previous_range) }
        end

        private

        def declaration_with_comment(node)
          buffer = processed_source.buffer
          begin_pos = range_by_whole_lines(get_source_range(node, comments_as_separators)).begin_pos
          end_line = buffer.line_for_position(node.loc.expression.end_pos)
          end_pos = range_by_whole_lines(buffer.line_range(end_line),
                                         include_final_newline: true).end_pos

          range_between(begin_pos, end_pos)
        end

        def swap_range(corrector, range1, range2)
          corrector.insert_before(range2, range1.source)
          corrector.remove(range1)
        end
      end
    end
  end
end

Version data entries

30 entries across 26 versions & 3 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/ordered_gem_corrector.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/ordered_gem_corrector.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/ordered_gem_corrector.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/correctors/ordered_gem_corrector.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/ordered_gem_corrector.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/correctors/ordered_gem_corrector.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/correctors/ordered_gem_corrector.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/correctors/ordered_gem_corrector.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.43.0 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.42.0 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.41.1 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.41.0 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.40.0 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.39.0 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.38.0 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.37.1 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.37.0 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.36.0 lib/rubocop/cop/correctors/ordered_gem_corrector.rb
rubocop-1.35.1 lib/rubocop/cop/correctors/ordered_gem_corrector.rb