Skip to content

Commit

Permalink
Fix false-negative and error for RSpec/MetadataStyle
Browse files Browse the repository at this point in the history
When RSpec/MetadataStyle cop is used with `EnforcedStyle: hash`,
offenses must be registered only for metadata arguments being symbols.

It was previously producing an exception if a variable was used in the
metadata arguments (except if it was the last one).
  • Loading branch information
cbliard committed Aug 8, 2024
1 parent ff213ae commit dbd87bb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

- Fix false-negative and error for `RSpec/MetadataStyle` when non-literal args are used in metadata in `EnforceStyle: hash`. ([@cbliard])

## 3.0.4 (2024-08-05)

- Fix false-negative for `UnspecifiedException` when matcher is chained. ([@r7kamura])
Expand Down Expand Up @@ -911,6 +913,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@bquorning]: https://github.com/bquorning
[@brentwheeldon]: https://github.com/BrentWheeldon
[@brianhawley]: https://github.com/BrianHawley
[@cbliard]: https://github.com/cbliard
[@cfabianski]: https://github.com/cfabianski
[@clupprich]: https://github.com/clupprich
[@composerinteralia]: https://github.com/composerinteralia
Expand Down
7 changes: 1 addition & 6 deletions lib/rubocop/cop/rspec/metadata_style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,8 @@ class MetadataStyle < Base # rubocop:disable Metrics/ClassLength
PATTERN

def on_metadata(symbols, hash)
# RSpec example groups accept two string arguments. In such a case,
# the rspec_metadata matcher will interpret the second string
# argument as a metadata symbol.
symbols.shift if symbols.first&.str_type?

symbols.each do |symbol|
on_metadata_symbol(symbol)
on_metadata_symbol(symbol) if symbol.sym_type?
end

return unless hash
Expand Down
33 changes: 33 additions & 0 deletions spec/rubocop/cop/rspec/metadata_style_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@
end
end

context 'with non-literal metadata and symbol metadata' do
it 'registers no offense' do
expect_no_offenses(<<~RUBY)
describe 'Something', a, :b do
end
RUBY
end
end

context 'with boolean keyword arguments metadata and symbol metadata' do
it 'registers offense' do
expect_offense(<<~RUBY)
Expand Down Expand Up @@ -243,6 +252,15 @@
end
end

context 'with 2 non-literal metadata' do
it 'registers no offense' do
expect_no_offenses(<<~RUBY)
describe 'Something', a, b do
end
RUBY
end
end

context 'with symbol metadata after 2 string arguments' do
it 'registers offense' do
expect_offense(<<~RUBY)
Expand All @@ -258,6 +276,21 @@
end
end

context 'with symbol metadata after non-literal metadata' do
it 'registers offense' do
expect_offense(<<~RUBY)
describe 'Something', a, :b do
^^ Use hash style for metadata.
end
RUBY

expect_correction(<<~RUBY)
describe 'Something', a, b: true do
end
RUBY
end
end

context 'with symbol metadata with parentheses' do
it 'registers offense' do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit dbd87bb

Please sign in to comment.