Sha256: e543d9e55060f425c668a6869fc2344d8e549495277180c1dff8ad222cb7b5ed

Contents?: true

Size: 1.48 KB

Versions: 8

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true
module RuboCop
  module Cop
    module Bundler
      # Gems in consecutive lines should be alphabetically sorted
      # @example
      #   # bad
      #   gem 'rubocop'
      #   gem 'rspec'
      #
      #   # good
      #   gem 'rspec'
      #   gem 'rubocop'
      #
      #   # good
      #   gem 'rubocop'
      #
      #   gem 'rspec'
      class OrderedGems < Cop
        MSG = 'Gem `%s` should appear before `%s` in their gem group.'.freeze
        def investigate(processed_source)
          return if processed_source.ast.nil?
          gem_declarations(processed_source.ast)
            .each_cons(2) do |previous, current|
            next unless consecutive_lines(previous, current)
            next unless current.children[2].children.first.to_s <
                        previous.children[2].children.first.to_s
            register_offense(previous, current)
          end
        end

        def consecutive_lines(previous, current)
          previous.source_range.last_line == current.source_range.first_line - 1
        end

        def register_offense(previous, current)
          add_offense(
            current,
            current.source_range,
            format(
              MSG,
              current.children[2].children.first,
              previous.children[2].children.first
            )
          )
        end

        def_node_search :gem_declarations, <<-PATTERN
          (:send, nil, :gem, ...)
        PATTERN
      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/ordered_gems.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/bundler/ordered_gems.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/bundler/ordered_gems.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/bundler/ordered_gems.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/bundler/ordered_gems.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/bundler/ordered_gems.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/bundler/ordered_gems.rb
rubocop-0.46.0 lib/rubocop/cop/bundler/ordered_gems.rb