Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false-negative and error for RSpec/MetadataStyle #1947

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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