Skip to content

Commit

Permalink
Allow split description strings in ExampleDescription
Browse files Browse the repository at this point in the history
Splitting the description string after `it` with a `+` is
not normal, but it can occur during auto-correction as an
intermediate state of the source code. Make sure the cop
doesn't crash.
  • Loading branch information
jonas054 authored and bbatsov committed Apr 6, 2021
1 parent 1cb60f0 commit c9b96e1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/rubocop/cop/internal_affairs/example_description.rb
Expand Up @@ -80,10 +80,14 @@ def on_send(node)
private

def check_description(description, regexps, message)
return unless regexps.any? { |regexp| regexp.match?(description.value) }
return unless regexps.any? { |regexp| regexp.match?(string_contents(description)) }

add_offense(description, message: message)
end

def string_contents(node)
node.str_type? ? node.value : node.source
end
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/internal_affairs/example_description_spec.rb
Expand Up @@ -46,6 +46,14 @@
RUBY
end

it 'does not crash when given a proper description that is split with +' do
expect_no_offenses(<<~RUBY)
it "does " + 'not register an offense' do
expect_no_offense('code')
end
RUBY
end

it 'does not register an offense when given an unexpected description' do
expect_no_offenses(<<~RUBY)
it 'foo bar baz' do
Expand Down

0 comments on commit c9b96e1

Please sign in to comment.