Skip to content

Commit

Permalink
Merge pull request #1947 from cbliard/fix-metadata-style-cop-with-non…
Browse files Browse the repository at this point in the history
…-literal-args

Fix false-negative and error for `RSpec/MetadataStyle`
  • Loading branch information
pirj authored Aug 8, 2024
2 parents ff213ae + dbd87bb commit e02576f
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 e02576f

Please sign in to comment.