Sha256: d57a2c378050f7d509f8162482006e34f43ddb14345060535426636947502156

Contents?: true

Size: 1.87 KB

Versions: 14

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Bundler
      # Gems should be alphabetically sorted within groups.
      #
      # @example
      #   # bad
      #   gem 'rubocop'
      #   gem 'rspec'
      #
      #   # good
      #   gem 'rspec'
      #   gem 'rubocop'
      #
      #   # good
      #   gem 'rubocop'
      #
      #   gem 'rspec'
      #
      #   # good only if TreatCommentsAsGroupSeparators is true
      #   # For code quality
      #   gem 'rubocop'
      #   # For tests
      #   gem 'rspec'
      class OrderedGems < Cop
        include ConfigurableEnforcedStyle
        include OrderedGemNode

        MSG = 'Gems should be sorted in an alphabetical order within their '\
              'section of the Gemfile. '\
              'Gem `%<previous>s` should appear before `%<current>s`.'

        def investigate(processed_source)
          return if processed_source.blank?

          gem_declarations(processed_source.ast)
            .each_cons(2) do |previous, current|
            next unless consecutive_lines(previous, current)
            next unless case_insensitive_out_of_order?(
              gem_name(current),
              gem_name(previous)
            )

            register_offense(previous, current)
          end
        end

        def autocorrect(node)
          OrderedGemCorrector.correct(
            processed_source,
            node,
            previous_declaration(node),
            treat_comments_as_separators
          )
        end

        private

        def previous_declaration(node)
          declarations = gem_declarations(processed_source.ast)
          node_index = declarations.map(&:location).find_index(node.location)
          declarations.to_a[node_index - 1]
        end

        def_node_search :gem_declarations, <<-PATTERN
          (:send nil? :gem str ...)
        PATTERN
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/bundler/ordered_gems.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/bundler/ordered_gems.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/bundler/ordered_gems.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/bundler/ordered_gems.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/bundler/ordered_gems.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/bundler/ordered_gems.rb
zuora_connect_ui-0.8.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/bundler/ordered_gems.rb
zuora_connect_ui-0.8.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/bundler/ordered_gems.rb
zuora_connect_ui-0.7.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/bundler/ordered_gems.rb
zuora_connect_ui-0.7.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-0.72.0 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-0.71.0 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-0.70.0 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-0.69.0 lib/rubocop/cop/bundler/ordered_gems.rb