class RuboCop::Cop::RSpec::SortMetadata
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'
Constants
- MSG
Public Instance Methods
on_metadata(symbols, hash)
click to toggle source
# File lib/rubocop/cop/rspec/sort_metadata.rb, line 26 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 Instance Methods
crime_scene(symbols, pairs)
click to toggle source
# File lib/rubocop/cop/rspec/sort_metadata.rb, line 38 def crime_scene(symbols, pairs) metadata = symbols + pairs range_between( metadata.first.source_range.begin_pos, metadata.last.source_range.end_pos ) end
replacement(symbols, pairs)
click to toggle source
# File lib/rubocop/cop/rspec/sort_metadata.rb, line 47 def replacement(symbols, pairs) (sort_symbols(symbols) + sort_pairs(pairs)).map(&:source).join(', ') end
sort_pairs(pairs)
click to toggle source
# File lib/rubocop/cop/rspec/sort_metadata.rb, line 55 def sort_pairs(pairs) pairs.sort_by { |pair| pair.key.source.downcase } end
sort_symbols(symbols)
click to toggle source
# File lib/rubocop/cop/rspec/sort_metadata.rb, line 59 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
sorted?(symbols, pairs)
click to toggle source
# File lib/rubocop/cop/rspec/sort_metadata.rb, line 51 def sorted?(symbols, pairs) symbols == sort_symbols(symbols) && pairs == sort_pairs(pairs) end