Class: RuboCop::Cop::RSpec::SortMetadata

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp, Metadata
Defined in:
lib/rubocop/cop/rspec/sort_metadata.rb

Overview

Sort RSpec metadata alphabetically.

Examples:

# 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'

Constant Summary collapse

MSG =
'Sort metadata alphabetically.'

Instance Method Summary collapse

Methods included from Metadata

#metadata_in_block, #on_block, #rspec_configure, #rspec_metadata

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Methods included from RSpec::Language::NodePattern

#block_or_numblock_pattern, #block_pattern, #numblock_pattern, #send_pattern

Methods inherited from Base

inherited, #on_new_investigation

Instance Method Details

#on_metadata(symbols, hash) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/rubocop/cop/rspec/sort_metadata.rb', line 26

def (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