Sha256: 08b10089c59576ca926e8d3476a19938a1d4b340cd9ad2387ac9945610c83cd9

Contents?: true

Size: 1.81 KB

Versions: 13

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Sort RSpec metadata alphabetically.
      #
      # @example
      #   # bad
      #   describe 'Something', :b, :a
      #   context 'Something', foo: 'bar', baz: true
      #   it 'works', :b, :a, foo: 'bar', baz: true
      #
      #   # good
      #   describe 'Something', :a, :b
      #   context 'Something', baz: true, foo: 'bar'
      #   it 'works', :a, :b, baz: true, foo: 'bar'
      #
      class SortMetadata < Base
        extend AutoCorrector
        include Metadata
        include RangeHelp

        MSG = 'Sort metadata alphabetically.'

        def on_metadata(symbols, hash)
          pairs = hash&.pairs || []
          return if sorted?(symbols, pairs)

          crime_scene = crime_scene(symbols, pairs)
          add_offense(crime_scene) do |corrector|
            corrector.replace(crime_scene, replacement(symbols, pairs))
          end
        end

        private

        def crime_scene(symbols, pairs)
          metadata = symbols + pairs

          range_between(
            metadata.first.source_range.begin_pos,
            metadata.last.source_range.end_pos
          )
        end

        def replacement(symbols, pairs)
          (sort_symbols(symbols) + sort_pairs(pairs)).map(&:source).join(', ')
        end

        def sorted?(symbols, pairs)
          symbols == sort_symbols(symbols) && pairs == sort_pairs(pairs)
        end

        def sort_pairs(pairs)
          pairs.sort_by { |pair| pair.key.source.downcase }
        end

        def sort_symbols(symbols)
          symbols.sort_by do |symbol|
            if symbol.str_type? || symbol.sym_type?
              symbol.value.to_s.downcase
            else
              symbol.source.downcase
            end
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 4 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.3/lib/rubocop/cop/rspec/sort_metadata.rb
rubocop-rspec-3.3.0 lib/rubocop/cop/rspec/sort_metadata.rb
rubocop-rspec-3.2.0 lib/rubocop/cop/rspec/sort_metadata.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/sort_metadata.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/sort_metadata.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/sort_metadata.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/sort_metadata.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/sort_metadata.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/sort_metadata.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/sort_metadata.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/sort_metadata.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/sort_metadata.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/sort_metadata.rb