123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::OrderedGemNode

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Extended In:
Included In:
Defined in: lib/rubocop/cop/mixin/ordered_gem_node.rb

Overview

Common functionality for Bundler/OrderedGems and Gemspec/OrderedDependencies.

Instance Method Summary

Instance Method Details

#case_insensitive_out_of_order?(string_a, string_b) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/ordered_gem_node.rb', line 23

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

#consecutive_lines(previous, current) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/ordered_gem_node.rb', line 27

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

#find_gem_name(gem_node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/ordered_gem_node.rb', line 55

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

  find_gem_name(gem_node.receiver)
end

#gem_canonical_name(name) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/ordered_gem_node.rb', line 18

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

#gem_name(declaration_node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/ordered_gem_node.rb', line 49

def gem_name(declaration_node)
  gem_node = declaration_node.first_argument

  find_gem_name(gem_node)
end

#get_source_range(node, comments_as_separators) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/ordered_gem_node.rb', line 10

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

#register_offense(previous, current) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/ordered_gem_node.rb', line 32

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

#treat_comments_as_separators (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/ordered_gem_node.rb', line 61

def treat_comments_as_separators
  cop_config['TreatCommentsAsGroupSeparators']
end