Skip to content

Commit 5c4b6a4

Browse files
authored
Merge pull request #1547 from ydah/fix-false-positive-rspec-context-method
Fix a false positive for `RSpec/ContextMethod` when multi-line context with `#` at the beginning
2 parents 9280a68 + 64d9120 commit 5c4b6a4

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

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

89
## 2.17.0 (2023-01-13)
910

lib/rubocop/cop/rspec/context_method.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ class ContextMethod < Base
3131

3232
# @!method context_method(node)
3333
def_node_matcher :context_method, <<-PATTERN
34-
(block (send #rspec? :context $(str #method_name?) ...) ...)
34+
(block
35+
(send #rspec? :context
36+
${(str #method_name?) (dstr (str #method_name?) ...)}
37+
...)
38+
...)
3539
PATTERN
3640

3741
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler

spec/rubocop/cop/rspec/context_method_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,19 @@
4343
end
4444
RUBY
4545
end
46+
47+
it 'flags multi-line context with `#` at the beginning' do
48+
expect_offense(<<-'RUBY')
49+
context '#foo_bar' \
50+
^^^^^^^^^^^^ Use `describe` for testing methods.
51+
'.baz'do
52+
end
53+
RUBY
54+
55+
expect_correction(<<-'RUBY')
56+
describe '#foo_bar' \
57+
'.baz'do
58+
end
59+
RUBY
60+
end
4661
end

0 commit comments

Comments
 (0)