Sha256: d3ab069c1ccc43536d64c9b9fe6fda9bb7c6bb691c4eb9c16e41ad477ad4583e

Contents?: true

Size: 1.71 KB

Versions: 8

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Bundler
      # 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]
      class DuplicatedGem < Cop
        MSG = 'Gem `%s` requirements already given on line %d ' \
              'of the Gemfile.'.freeze

        def investigate(processed_source)
          return unless processed_source.ast

          duplicated_gem_nodes.each do |nodes|
            nodes[1..-1].each do |node|
              offense(
                node,
                node.method_args.first.to_a.first,
                nodes.first.loc.line
              )
            end
          end
        end

        private

        def_node_search :gem_declarations, '(send nil :gem str ...)'

        def duplicated_gem_nodes
          gem_declarations(processed_source.ast)
            .group_by { |e| e.method_args.first }
            .keep_if { |_, nodes| nodes.length > 1 }
            .values
        end

        def offense(node, gem_name, line_of_first_occurence)
          line_range = node.loc.column...node.loc.last_column

          add_offense(
            node,
            source_range(processed_source.buffer, node.loc.line, line_range),
            format(MSG, gem_name, line_of_first_occurence)
          )
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/bundler/duplicated_gem.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/bundler/duplicated_gem.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/bundler/duplicated_gem.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/bundler/duplicated_gem.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/bundler/duplicated_gem.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/bundler/duplicated_gem.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/bundler/duplicated_gem.rb
rubocop-0.46.0 lib/rubocop/cop/bundler/duplicated_gem.rb