Skip to content

Commit

Permalink
Merge pull request #1731 from franzliedke/1714-metadata-style-autocor…
Browse files Browse the repository at this point in the history
…rect-strings

RSpec/MetadataStyle: Fix false positive for multiple strings preceding metadata
  • Loading branch information
bquorning committed Oct 27, 2023
2 parents 1c42da1 + 950a5b4 commit 860719e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Add support single quoted string and percent string and heredoc for `RSpec/Rails/HttpStatus`. ([@ydah])
- Change to be inline disable for `RSpec/SpecFilePathFormat` like `RSpec/FilePath`. ([@ydah])
- Fix a false positive for `RSpec/MetadataStyle` with example groups having multiple string arguments. ([@franzliedke])

## 2.24.1 (2023-09-23)

Expand Down Expand Up @@ -842,6 +843,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@faucct]: https://github.com/faucct
[@foton]: https://github.com/foton
[@francois-ferrandis]: https://github.com/francois-ferrandis
[@franzliedke]: https://github.com/franzliedke
[@g-rath]: https://github.com/G-Rath
[@geniou]: https://github.com/geniou
[@gsamokovarov]: https://github.com/gsamokovarov
Expand Down
5 changes: 5 additions & 0 deletions lib/rubocop/cop/rspec/metadata_style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ 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)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/cop/rspec/mixin/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ module Metadata
def on_block(node)
rspec_configure(node) do |block_var|
metadata_in_block(node, block_var) do |metadata_arguments|
on_matadata_arguments(metadata_arguments)
on_metadata_arguments(metadata_arguments)
end
end

rspec_metadata(node) do |metadata_arguments|
on_matadata_arguments(metadata_arguments)
on_metadata_arguments(metadata_arguments)
end
end
alias on_numblock on_block
Expand All @@ -46,7 +46,7 @@ def on_metadata(_symbols, _hash)

private

def on_matadata_arguments(metadata_arguments)
def on_metadata_arguments(metadata_arguments)
*symbols, last = metadata_arguments
hash = nil
case last&.type
Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/cop/rspec/metadata_style_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@
end
end

context 'with boolean hash metadata after 2 string arguments' do
it 'registers no offense' do
expect_no_offenses(<<~RUBY)
describe 'Something', 'Something else', { a: true } do
end
RUBY
end
end

context 'with 1 symbol metadata' do
it 'registers offense' do
expect_offense(<<~RUBY)
Expand Down Expand Up @@ -234,6 +243,21 @@
end
end

context 'with symbol metadata after 2 string arguments' do
it 'registers offense' do
expect_offense(<<~RUBY)
describe 'Something', 'Something else', :a do
^^ Use hash style for metadata.
end
RUBY

expect_correction(<<~RUBY)
describe 'Something', 'Something else', a: 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 860719e

Please sign in to comment.