Skip to content

Commit

Permalink
Merge pull request #1547 from ydah/fix-false-positive-rspec-context-m…
Browse files Browse the repository at this point in the history
…ethod

Fix a false positive for `RSpec/ContextMethod` when multi-line context with `#` at the beginning
  • Loading branch information
bquorning authored Jan 13, 2023
2 parents 9280a68 + 64d9120 commit 5c4b6a4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Fix a false negative for `RSpec/Pending` when using skipped in metadata is multiline string. ([@ydah])
- Fix a false positive for `RSpec/NoExpectationExample` when using skipped in metadata is multiline string. ([@ydah])
- Fix a false positive for `RSpec/ContextMethod` when multi-line context with `#` at the beginning. ([@ydah])

## 2.17.0 (2023-01-13)

Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/cop/rspec/context_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ class ContextMethod < Base

# @!method context_method(node)
def_node_matcher :context_method, <<-PATTERN
(block (send #rspec? :context $(str #method_name?) ...) ...)
(block
(send #rspec? :context
${(str #method_name?) (dstr (str #method_name?) ...)}
...)
...)
PATTERN

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/rspec/context_method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,19 @@
end
RUBY
end

it 'flags multi-line context with `#` at the beginning' do
expect_offense(<<-'RUBY')
context '#foo_bar' \
^^^^^^^^^^^^ Use `describe` for testing methods.
'.baz'do
end
RUBY

expect_correction(<<-'RUBY')
describe '#foo_bar' \
'.baz'do
end
RUBY
end
end

0 comments on commit 5c4b6a4

Please sign in to comment.