Sha256: ae4c8ff4773e8cbe24591a511cfc2541471e959f3b9381a70762e36e1193f9a1

Contents?: true

Size: 1.63 KB

Versions: 23

Compression:

Stored size: 1.63 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 < 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

23 entries across 23 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/bundler/ordered_gems.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.30.1 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.30.0 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.29.1 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.29.0 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.28.2 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.28.1 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.28.0 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.27.0 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.26.1 lib/rubocop/cop/bundler/ordered_gems.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.26.0 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.25.1 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.25.0 lib/rubocop/cop/bundler/ordered_gems.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.24.1 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.24.0 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.23.0 lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-1.22.3 lib/rubocop/cop/bundler/ordered_gems.rb