Sha256: f2b2ecbd615848fdc55d2ca4b2c4a207d99d3052e2a45f5f6e3b435df96986ca

Contents?: true

Size: 1.79 KB

Versions: 57

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # Common functionality for Bundler/OrderedGems and
    # Gemspec/OrderedDependencies.
    module OrderedGemNode
      private

      def get_source_range(node, comments_as_separators)
        unless comments_as_separators
          first_comment = processed_source.ast_with_comments[node].first
          return first_comment.loc.expression unless first_comment.nil?
        end
        node.source_range
      end

      def gem_canonical_name(name)
        name = name.tr('-_', '') unless cop_config['ConsiderPunctuation']
        name.downcase
      end

      def case_insensitive_out_of_order?(string_a, string_b)
        gem_canonical_name(string_a) < gem_canonical_name(string_b)
      end

      def consecutive_lines(previous, current)
        first_line = get_source_range(current, treat_comments_as_separators).first_line
        previous.source_range.last_line == first_line - 1
      end

      def register_offense(previous, current)
        message = format(
          self.class::MSG,
          previous: gem_name(current),
          current: gem_name(previous)
        )

        add_offense(current, message: message) do |corrector|
          OrderedGemCorrector.correct(
            processed_source,
            current,
            previous_declaration(current),
            treat_comments_as_separators
          ).call(corrector)
        end
      end

      def gem_name(declaration_node)
        gem_node = declaration_node.first_argument

        find_gem_name(gem_node)
      end

      def find_gem_name(gem_node)
        return gem_node.str_content if gem_node.str_type?

        find_gem_name(gem_node.receiver)
      end

      def treat_comments_as_separators
        cop_config['TreatCommentsAsGroupSeparators']
      end
    end
  end
end

Version data entries

57 entries across 51 versions & 6 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/ordered_gem_node.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/ordered_gem_node.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/ordered_gem_node.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/mixin/ordered_gem_node.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/mixin/ordered_gem_node.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/ordered_gem_node.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/mixin/ordered_gem_node.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/ordered_gem_node.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/mixin/ordered_gem_node.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/mixin/ordered_gem_node.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/mixin/ordered_gem_node.rb
zilla-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.46.0/lib/rubocop/cop/mixin/ordered_gem_node.rb
rubocop-1.46.0 lib/rubocop/cop/mixin/ordered_gem_node.rb
rubocop-1.45.1 lib/rubocop/cop/mixin/ordered_gem_node.rb
rubocop-1.45.0 lib/rubocop/cop/mixin/ordered_gem_node.rb
rubocop-1.44.1 lib/rubocop/cop/mixin/ordered_gem_node.rb
rubocop-1.44.0 lib/rubocop/cop/mixin/ordered_gem_node.rb
rubocop-1.43.0 lib/rubocop/cop/mixin/ordered_gem_node.rb
rubocop-1.42.0 lib/rubocop/cop/mixin/ordered_gem_node.rb
rubocop-1.41.1 lib/rubocop/cop/mixin/ordered_gem_node.rb