class RubbyCop::Cop::Bundler::OrderedGems

Gems should be alphabetically sorted within groups.

@example

# bad
gem 'rubbycop'
gem 'rspec'

# good
gem 'rspec'
gem 'rubbycop'

# good
gem 'rubbycop'

gem 'rspec'

# good only if TreatCommentsAsGroupSeparators is true
# For code quality
gem 'rubbycop'
# For tests
gem 'rspec'

Constants

MSG

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubbycop/cop/bundler/ordered_gems.rb, line 67
def autocorrect(node)
  previous = previous_declaration(node)

  current_range = declaration_with_comment(node)
  previous_range = declaration_with_comment(previous)

  lambda do |corrector|
    swap_range(corrector, current_range, previous_range)
  end
end
case_insensitive_out_of_order?(string_a, string_b) click to toggle source
# File lib/rubbycop/cop/bundler/ordered_gems.rb, line 46
def case_insensitive_out_of_order?(string_a, string_b)
  string_a.downcase < string_b.downcase
end
consecutive_lines(previous, current) click to toggle source
# File lib/rubbycop/cop/bundler/ordered_gems.rb, line 50
def consecutive_lines(previous, current)
  first_line = get_source_range(current).first_line
  previous.source_range.last_line == first_line - 1
end
declaration_with_comment(node) click to toggle source
# File lib/rubbycop/cop/bundler/ordered_gems.rb, line 78
def declaration_with_comment(node)
  buffer = processed_source.buffer
  begin_pos = get_source_range(node).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
get_source_range(node) click to toggle source
# File lib/rubbycop/cop/bundler/ordered_gems.rb, line 99
def get_source_range(node)
  unless cop_config['TreatCommentsAsGroupSeparators']
    first_comment = processed_source.ast_with_comments[node].first
    return first_comment.loc.expression unless first_comment.nil?
  end
  node.source_range
end
investigate(processed_source) click to toggle source
# File lib/rubbycop/cop/bundler/ordered_gems.rb, line 33
def investigate(processed_source)
  return if processed_source.ast.nil?
  gem_declarations(processed_source.ast)
    .each_cons(2) do |previous, current|
    next unless consecutive_lines(previous, current)
    next unless case_insensitive_out_of_order?(
      current.children[2].children.first.to_s,
      previous.children[2].children.first.to_s
    )
    register_offense(previous, current)
  end
end
previous_declaration(node) click to toggle source
# File lib/rubbycop/cop/bundler/ordered_gems.rb, line 93
def previous_declaration(node)
  declarations = gem_declarations(processed_source.ast)
  node_index = declarations.find_index(node)
  declarations.to_a[node_index - 1]
end
register_offense(previous, current) click to toggle source
# File lib/rubbycop/cop/bundler/ordered_gems.rb, line 55
def register_offense(previous, current)
  add_offense(
    current,
    current.source_range,
    format(
      MSG,
      current.children[2].children.first,
      previous.children[2].children.first
    )
  )
end
swap_range(corrector, range1, range2) click to toggle source
# File lib/rubbycop/cop/bundler/ordered_gems.rb, line 86
def swap_range(corrector, range1, range2)
  src1 = range1.source
  src2 = range2.source
  corrector.replace(range1, src2)
  corrector.replace(range2, src1)
end