class RuboCop::Cop::Bundler::DuplicatedGem

A Gem’s requirements should be listed only once in a Gemfile. @example

# bad
gem 'rubocop'
gem 'rubocop'

# bad
group :development do
  gem 'rubocop'
end

group :test do
  gem 'rubocop'
end

# good
group :development, :test do
  gem 'rubocop'
end

# good
gem 'rubocop', groups: [:development, :test]

# good - conditional declaration
if Dir.exist?(local)
  gem 'rubocop', path: local
elsif ENV['RUBOCOP_VERSION'] == 'master'
  gem 'rubocop', git: 'https://github.com/rubocop/rubocop.git'
else
  gem 'rubocop', '~> 0.90.0'
end

Constants

MSG

Public Instance Methods

on_new_investigation() click to toggle source
# File lib/rubocop/cop/bundler/duplicated_gem.rb, line 44
def on_new_investigation
  return if processed_source.blank?

  duplicated_gem_nodes.each do |nodes|
    nodes[1..].each do |node|
      register_offense(node, node.first_argument.to_a.first, nodes.first.first_line)
    end
  end
end

Private Instance Methods

conditional_declaration?(nodes) click to toggle source
# File lib/rubocop/cop/bundler/duplicated_gem.rb, line 66
def conditional_declaration?(nodes)
  parent = nodes[0].each_ancestor.find { |ancestor| !ancestor.begin_type? }
  return false unless parent&.if_type? || parent&.when_type?

  root_conditional_node = parent.if_type? ? parent : parent.parent
  nodes.all? { |node| within_conditional?(node, root_conditional_node) }
end
duplicated_gem_nodes() click to toggle source
# File lib/rubocop/cop/bundler/duplicated_gem.rb, line 59
def duplicated_gem_nodes
  gem_declarations(processed_source.ast)
    .group_by(&:first_argument)
    .values
    .select { |nodes| nodes.size > 1 && !conditional_declaration?(nodes) }
end
register_offense(node, gem_name, line_of_first_occurrence) click to toggle source
# File lib/rubocop/cop/bundler/duplicated_gem.rb, line 80
def register_offense(node, gem_name, line_of_first_occurrence)
  line_range = node.loc.column...node.loc.last_column
  offense_location = source_range(processed_source.buffer, node.first_line, line_range)
  message = format(
    MSG,
    gem_name: gem_name,
    line_of_first_occurrence: line_of_first_occurrence
  )
  add_offense(offense_location, message: message)
end
within_conditional?(node, conditional_node) click to toggle source
# File lib/rubocop/cop/bundler/duplicated_gem.rb, line 74
def within_conditional?(node, conditional_node)
  conditional_node.branches.any? do |branch|
    branch == node || branch.child_nodes.include?(node)
  end
end