Skip to content

Commit 30657db

Browse files
committed
Add support for itblock nodes
1 parent 8c2b6b4 commit 30657db

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+487
-36
lines changed

CHANGELOG.md

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

33
## Master (Unreleased)
44

5+
- Add support for `itblock` nodes. ([@Darhazer])
56
- `RSpec/ScatteredLet` now preserves the order of `let`s during auto-correction. ([@Darhazer])
67
- Fix a false negative for `RSpec/EmptyLineAfterFinalLet` inside `shared_examples` / `include_examples` / `it_behaves_like` blocks. ([@Darhazer])
78
- Add autocorrect support for `RSpec/SubjectDeclaration`. ([@eugeneius])

lib/rubocop/cop/rspec/around_block.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class AroundBlock < Base
4141
(numblock (send nil? :around sym ?) ...)
4242
PATTERN
4343

44+
# @!method hook_itblock(node)
45+
def_node_matcher :hook_itblock, <<~PATTERN
46+
(itblock (send nil? :around sym ?) ...)
47+
PATTERN
48+
4449
# @!method find_arg_usage(node)
4550
def_node_search :find_arg_usage, <<~PATTERN
4651
{(send $... {:call :run}) (send _ _ $...) (yield $...) (block-pass $...)}
@@ -62,6 +67,12 @@ def on_numblock(node)
6267
end
6368
end
6469

70+
def on_itblock(node)
71+
hook_itblock(node) do
72+
check_for_itblock(node)
73+
end
74+
end
75+
6576
private
6677

6778
def add_no_arg_offense(node)
@@ -89,6 +100,17 @@ def check_for_numblock(block)
89100
message: format(MSG_UNUSED_ARG, arg: :_1)
90101
)
91102
end
103+
104+
def check_for_itblock(block)
105+
find_arg_usage(block) do |usage|
106+
return if usage.include?(s(:lvar, :it))
107+
end
108+
109+
add_offense(
110+
block.children.last,
111+
message: format(MSG_UNUSED_ARG, arg: :it)
112+
)
113+
end
92114
end
93115
end
94116
end

lib/rubocop/cop/rspec/context_method.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ContextMethod < Base
3838
...)
3939
PATTERN
4040

41-
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
41+
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
4242
context_method(node) do |context|
4343
add_offense(context) do |corrector|
4444
corrector.replace(node.send_node.loc.selector, 'describe')

lib/rubocop/cop/rspec/context_wording.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class ContextWording < Base
7070
(block (send #rspec? { :context :shared_context } $(any_str ...) ...) ...)
7171
PATTERN
7272

73-
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
73+
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
7474
context_wording(node) do |context|
7575
unless matches_allowed_pattern?(description(context))
7676
add_offense(context, message: message)

lib/rubocop/cop/rspec/described_class.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class DescribedClass < Base # rubocop:disable Metrics/ClassLength
110110
def_node_search :contains_described_class?,
111111
'(send nil? :described_class)'
112112

113-
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
113+
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
114114
# In case the explicit style is used, we need to remember what's
115115
# being described.
116116
@described_class, body = described_constant(node)

lib/rubocop/cop/rspec/empty_example_group.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class EmptyExampleGroup < Base
136136
}
137137
PATTERN
138138

139-
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
139+
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
140140
return if node.each_ancestor(:any_def).any?
141141
return if node.each_ancestor(:block).any? { |block| example?(block) }
142142

lib/rubocop/cop/rspec/empty_hook.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class EmptyHook < Base
3434
(block $(send nil? #Hooks.all ...) _ nil?)
3535
PATTERN
3636

37-
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
37+
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
3838
empty_hook?(node) do |hook|
3939
add_offense(hook) do |corrector|
4040
corrector.remove(

lib/rubocop/cop/rspec/empty_line_after_example.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class EmptyLineAfterExample < Base
4646

4747
MSG = 'Add an empty line after `%<example>s`.'
4848

49-
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
49+
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
5050
return unless example?(node)
5151
return if allowed_one_liner?(node)
5252

lib/rubocop/cop/rspec/empty_line_after_example_group.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class EmptyLineAfterExampleGroup < Base
2929

3030
MSG = 'Add an empty line after `%<example_group>s`.'
3131

32-
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
32+
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
3333
return unless spec_group?(node)
3434

3535
missing_separating_line_offense(node) do |method|

lib/rubocop/cop/rspec/empty_line_after_final_let.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class EmptyLineAfterFinalLet < Base
2828
(block (send #rspec? {#SharedGroups.all #ExampleGroups.all #Includes.all} ...) args $_)
2929
PATTERN
3030

31-
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
31+
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
3232
return unless example_group_or_include?(node)
3333

3434
final_let = node.body.child_nodes.reverse.find { |child| let?(child) }

0 commit comments

Comments
 (0)