Skip to content

Commit

Permalink
Add support for multiline offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
bquorning committed Oct 21, 2022
1 parent 7dd4917 commit db3f827
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
6 changes: 2 additions & 4 deletions lib/rubocop/cop/rspec/sort_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def on_block(node)

def investigate(symbols, pairs)
return if sorted?(symbols, pairs)
return unless (crime_scene = crime_scene(symbols, pairs))

crime_scene = crime_scene(symbols, pairs)
add_offense(crime_scene) do |corrector|
corrector.replace(crime_scene, replacement(symbols, pairs))
end
Expand All @@ -67,12 +67,10 @@ def investigate(symbols, pairs)
def crime_scene(symbols, pairs)
metadata = symbols + pairs

range = range_between(
range_between(
metadata.first.loc.expression.begin_pos,
metadata.last.loc.expression.end_pos
)

range if range.last_line == range.first_line
end

def replacement(symbols, pairs)
Expand Down
11 changes: 8 additions & 3 deletions spec/rubocop/cop/rspec/sort_metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,18 @@
RUBY
end

it 'does not register an offense ' \
'when the metadata is not on one single line' do
expect_no_offenses(<<~RUBY)
it 'registers an offense also when the metadata is not on one single line' do
expect_offense(<<~RUBY)
RSpec.describe 'Something', :foo, :bar,
^^^^^^^^^^^ Sort metadata alphabetically.
baz: 'goo' do
end
RUBY

expect_correction(<<~RUBY)
RSpec.describe 'Something', :bar, :foo, baz: 'goo' do
end
RUBY
end

it 'registers an offense when using only symbol metadata ' \
Expand Down

0 comments on commit db3f827

Please sign in to comment.