Skip to content

Commit e02576f

Browse files
authored
Merge pull request #1947 from cbliard/fix-metadata-style-cop-with-non-literal-args
Fix false-negative and error for `RSpec/MetadataStyle`
2 parents ff213ae + dbd87bb commit e02576f

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Master (Unreleased)
44

5+
- Fix false-negative and error for `RSpec/MetadataStyle` when non-literal args are used in metadata in `EnforceStyle: hash`. ([@cbliard])
6+
57
## 3.0.4 (2024-08-05)
68

79
- Fix false-negative for `UnspecifiedException` when matcher is chained. ([@r7kamura])
@@ -911,6 +913,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
911913
[@bquorning]: https://github.com/bquorning
912914
[@brentwheeldon]: https://github.com/BrentWheeldon
913915
[@brianhawley]: https://github.com/BrianHawley
916+
[@cbliard]: https://github.com/cbliard
914917
[@cfabianski]: https://github.com/cfabianski
915918
[@clupprich]: https://github.com/clupprich
916919
[@composerinteralia]: https://github.com/composerinteralia

lib/rubocop/cop/rspec/metadata_style.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,8 @@ class MetadataStyle < Base # rubocop:disable Metrics/ClassLength
4545
PATTERN
4646

4747
def on_metadata(symbols, hash)
48-
# RSpec example groups accept two string arguments. In such a case,
49-
# the rspec_metadata matcher will interpret the second string
50-
# argument as a metadata symbol.
51-
symbols.shift if symbols.first&.str_type?
52-
5348
symbols.each do |symbol|
54-
on_metadata_symbol(symbol)
49+
on_metadata_symbol(symbol) if symbol.sym_type?
5550
end
5651

5752
return unless hash

spec/rubocop/cop/rspec/metadata_style_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@
9494
end
9595
end
9696

97+
context 'with non-literal metadata and symbol metadata' do
98+
it 'registers no offense' do
99+
expect_no_offenses(<<~RUBY)
100+
describe 'Something', a, :b do
101+
end
102+
RUBY
103+
end
104+
end
105+
97106
context 'with boolean keyword arguments metadata and symbol metadata' do
98107
it 'registers offense' do
99108
expect_offense(<<~RUBY)
@@ -243,6 +252,15 @@
243252
end
244253
end
245254

255+
context 'with 2 non-literal metadata' do
256+
it 'registers no offense' do
257+
expect_no_offenses(<<~RUBY)
258+
describe 'Something', a, b do
259+
end
260+
RUBY
261+
end
262+
end
263+
246264
context 'with symbol metadata after 2 string arguments' do
247265
it 'registers offense' do
248266
expect_offense(<<~RUBY)
@@ -258,6 +276,21 @@
258276
end
259277
end
260278

279+
context 'with symbol metadata after non-literal metadata' do
280+
it 'registers offense' do
281+
expect_offense(<<~RUBY)
282+
describe 'Something', a, :b do
283+
^^ Use hash style for metadata.
284+
end
285+
RUBY
286+
287+
expect_correction(<<~RUBY)
288+
describe 'Something', a, b: true do
289+
end
290+
RUBY
291+
end
292+
end
293+
261294
context 'with symbol metadata with parentheses' do
262295
it 'registers offense' do
263296
expect_offense(<<~RUBY)

0 commit comments

Comments
 (0)