Sha256: 7f9fedd34c17c1c53a30a74980816dcf82cb2def062dfb610410627906b64c71

Contents?: true

Size: 1.82 KB

Versions: 143

Compression:

Stored size: 1.82 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'
      #
      # @example TreatCommentsAsGroupSeparators: true (default)
      #   # good
      #   # For code quality
      #   gem 'rubocop'
      #   # For tests
      #   gem 'rspec'
      #
      # @example TreatCommentsAsGroupSeparators: false
      #   # bad
      #   # For code quality
      #   gem 'rubocop'
      #   # For tests
      #   gem 'rspec'
      class OrderedGems < Base
        extend AutoCorrector
        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 on_new_investigation
          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

        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

        # @!method gem_declarations(node)
        def_node_search :gem_declarations, <<~PATTERN
          (:send nil? :gem str ...)
        PATTERN
      end
    end
  end
end

Version data entries

143 entries across 143 versions & 14 rubygems

Version Path
rubocop-1.74.0 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.73.2 lib/rubocop/cop/bundler/ordered_gems.rb
siteimprove_api_client-1.0.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.73.1/lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.73.1 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.73.0 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.72.2 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.72.1 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.72.0 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.71.2 lib/rubocop/cop/bundler/ordered_gems.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-1.71.1/lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.71.1 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.71.0 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.70.0 lib/rubocop/cop/bundler/ordered_gems.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.69.2 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.69.1 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.69.0 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.68.0 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.67.0 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.66.1 lib/rubocop/cop/bundler/ordered_gems.rb