Sha256: 6d2207956f7f24c8f8effea1ccc76d71bf5b08cd1f977dd968ca34a8089b74bd

Contents?: true

Size: 1.57 KB

Versions: 17

Compression:

Stored size: 1.57 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)
      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

17 entries across 17 versions & 3 rubygems

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