Skip to content

Commit cd91408

Browse files
authored
Merge pull request #2046 from rubocop/use-new-ast-features
Use new ast features
2 parents 14cbb9b + dd865ae commit cd91408

14 files changed

+19
-45
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,6 @@
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# This cop supports safe autocorrection (--autocorrect).
10-
InternalAffairs/NodePatternGroups:
11-
Exclude:
12-
- 'lib/rubocop/cop/rspec/described_class.rb'
13-
- 'lib/rubocop/cop/rspec/described_class_module_wrapping.rb'
14-
- 'lib/rubocop/cop/rspec/hook_argument.rb'
15-
- 'lib/rubocop/cop/rspec/hooks_before_examples.rb'
16-
- 'lib/rubocop/cop/rspec/no_expectation_example.rb'
17-
- 'lib/rubocop/cop/rspec/pending_without_reason.rb'
18-
- 'lib/rubocop/cop/rspec/redundant_around.rb'
19-
- 'lib/rubocop/rspec/language.rb'
20-
21-
# This cop supports safe autocorrection (--autocorrect).
22-
InternalAffairs/NodeTypeMultiplePredicates:
23-
Exclude:
24-
- 'lib/rubocop/cop/rspec/empty_example_group.rb'
25-
- 'lib/rubocop/cop/rspec/predicate_matcher.rb'
26-
- 'lib/rubocop/cop/rspec/receive_messages.rb'
27-
- 'lib/rubocop/cop/rspec/variable_definition.rb'
28-
- 'lib/rubocop/cop/rspec/variable_name.rb'
29-
309
Rake/MethodDefinitionInTask:
3110
Exclude:
3211
- 'tasks/cut_release.rake'

lib/rubocop/cop/rspec/described_class.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class DescribedClass < Base # rubocop:disable Metrics/ClassLength
8383

8484
# @!method rspec_block?(node)
8585
def_node_matcher :rspec_block?,
86-
'({block numblock} (send #rspec? #ALL.all ...) ...)'
86+
'(any_block (send #rspec? #ALL.all ...) ...)'
8787

8888
# @!method scope_changing_syntax?(node)
8989
def_node_matcher :scope_changing_syntax?, '{def class module}'

lib/rubocop/cop/rspec/described_class_module_wrapping.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class DescribedClassModuleWrapping < Base
2424

2525
# @!method include_rspec_blocks?(node)
2626
def_node_search :include_rspec_blocks?, <<~PATTERN
27-
({block numblock} (send #explicit_rspec? #ExampleGroups.all ...) ...)
27+
(any_block (send #explicit_rspec? #ExampleGroups.all ...) ...)
2828
PATTERN
2929

3030
def on_module(node)

lib/rubocop/cop/rspec/empty_example_group.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,15 @@ def offensive?(body)
155155
return true unless body
156156
return false if conditionals_with_examples?(body)
157157

158-
if body.if_type? || body.case_type?
158+
if body.type?(:if, :case)
159159
!examples_in_branches?(body)
160160
else
161161
!examples?(body)
162162
end
163163
end
164164

165165
def conditionals_with_examples?(body)
166-
return false unless body.begin_type? || body.case_type?
166+
return false unless body.type?(:begin, :case)
167167

168168
body.each_descendant(:if, :case).any? do |condition_node|
169169
examples_in_branches?(condition_node)
@@ -172,7 +172,7 @@ def conditionals_with_examples?(body)
172172

173173
def examples_in_branches?(condition_node)
174174
return false unless condition_node
175-
return false if !condition_node.if_type? && !condition_node.case_type?
175+
return false unless condition_node.type?(:if, :case)
176176

177177
condition_node.branches.any? { |branch| examples?(branch) }
178178
end

lib/rubocop/cop/rspec/hook_argument.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ class HookArgument < Base
6767

6868
# @!method scoped_hook(node)
6969
def_node_matcher :scoped_hook, <<~PATTERN
70-
({block numblock} $(send _ #Hooks.all (sym ${:each :example})) ...)
70+
(any_block $(send _ #Hooks.all (sym ${:each :example})) ...)
7171
PATTERN
7272

7373
# @!method unscoped_hook(node)
7474
def_node_matcher :unscoped_hook, <<~PATTERN
75-
({block numblock} $(send _ #Hooks.all) ...)
75+
(any_block $(send _ #Hooks.all) ...)
7676
PATTERN
7777

7878
def on_block(node)

lib/rubocop/cop/rspec/hooks_before_examples.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class HooksBeforeExamples < Base
3030
# @!method example_or_group?(node)
3131
def_node_matcher :example_or_group?, <<~PATTERN
3232
{
33-
({block numblock} {
33+
(any_block {
3434
(send #rspec? #ExampleGroups.all ...)
3535
(send nil? #Examples.all ...)
3636
} ...)

lib/rubocop/cop/rspec/no_expectation_example.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class NoExpectationExample < Base
6565
# @param [RuboCop::AST::Node] node
6666
# @return [Boolean]
6767
def_node_matcher :regular_or_focused_example?, <<~PATTERN
68-
({block numblock} (send nil? {#Examples.regular #Examples.focused} ...) ...)
68+
(any_block (send nil? {#Examples.regular #Examples.focused} ...) ...)
6969
PATTERN
7070

7171
# @!method includes_expectation?(node)

lib/rubocop/cop/rspec/pending_without_reason.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ class PendingWithoutReason < Base
6363
def_node_matcher :skipped_in_example?, <<~PATTERN
6464
{
6565
(send nil? ${#Examples.skipped #Examples.pending})
66-
(block (send nil? ${#Examples.skipped}) ...)
67-
(numblock (send nil? ${#Examples.skipped}) ...)
66+
(any_block (send nil? ${#Examples.skipped}) ...)
6867
}
6968
PATTERN
7069

@@ -75,7 +74,7 @@ class PendingWithoutReason < Base
7574

7675
# @!method skipped_by_example_method_with_block?(node)
7776
def_node_matcher :skipped_by_example_method_with_block?, <<~PATTERN
78-
({block numblock} (send nil? ${#Examples.skipped #Examples.pending} ...) ...)
77+
(any_block (send nil? ${#Examples.skipped #Examples.pending} ...) ...)
7978
PATTERN
8079

8180
# @!method metadata_without_reason?(node)

lib/rubocop/cop/rspec/predicate_matcher.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def uncorrectable_matcher?(node, matcher)
182182

183183
def heredoc_argument?(matcher)
184184
matcher.arguments.select do |arg|
185-
arg.str_type? || arg.dstr_type? || arg.xstr_type?
185+
arg.type?(:str, :dstr, :xstr)
186186
end.any?(&:heredoc?)
187187
end
188188

lib/rubocop/cop/rspec/receive_messages.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ def item_range_by_whole_lines(item)
148148
end
149149

150150
def heredoc_or_splat?(node)
151-
((node.str_type? || node.dstr_type?) && node.heredoc?) ||
152-
node.splat_type?
151+
(node.type?(:str, :dstr) && node.heredoc?) || node.splat_type?
153152
end
154153

155154
def requires_quotes?(value)

0 commit comments

Comments
 (0)