Skip to content

Commit

Permalink
Add Node#any_block_type? to determine if a node is either a block o…
Browse files Browse the repository at this point in the history
…r numblock
  • Loading branch information
Earlopain authored and marcandre committed Jan 27, 2025
1 parent 6469d88 commit 5763d46
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions changelog/new_group_block.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#356](https://github.com/rubocop/rubocop-ast/pull/356): Added `:any_block` as an alias for `:block` and `:numblock`, use it with `Node#any_block_type?`. Also available in node patterns: `{block numblock}` can become `any_block`. ([@earlopain][])
21 changes: 14 additions & 7 deletions lib/rubocop/ast/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ class Node < Parser::AST::Node # rubocop:disable Metrics/ClassLength
erange: :range,

send: :call,
csend: :call
csend: :call,

block: :any_block,
numblock: :any_block
}.freeze
private_constant :GROUP_FOR_TYPE

Expand Down Expand Up @@ -340,7 +343,7 @@ def source_length

# @!method receiver(node = self)
def_node_matcher :receiver, <<~PATTERN
{(send $_ ...) ({block numblock} (call $_ ...) ...)}
{(send $_ ...) (any_block (call $_ ...) ...)}
PATTERN

# @!method str_content(node = self)
Expand Down Expand Up @@ -525,6 +528,10 @@ def range_type?
GROUP_FOR_TYPE[type] == :range
end

def any_block_type?
GROUP_FOR_TYPE[type] == :any_block
end

def guard_clause?
node = operator_keyword? ? rhs : self

Expand Down Expand Up @@ -555,7 +562,7 @@ def loc_is?(which_loc, str)
PATTERN

# @!method lambda?(node = self)
def_node_matcher :lambda?, '({block numblock} (send nil? :lambda) ...)'
def_node_matcher :lambda?, '(any_block (send nil? :lambda) ...)'

# @!method lambda_or_proc?(node = self)
def_node_matcher :lambda_or_proc?, '{lambda? proc?}'
Expand All @@ -568,7 +575,7 @@ def loc_is?(which_loc, str)
{
(send #global_const?({:Class :Module :Struct}) :new ...)
(send #global_const?(:Data) :define ...)
({block numblock} {
(any_block {
(send #global_const?({:Class :Module :Struct}) :new ...)
(send #global_const?(:Data) :define ...)
} ...)
Expand All @@ -578,20 +585,20 @@ def loc_is?(which_loc, str)
# @deprecated Use `:class_constructor?`
# @!method struct_constructor?(node = self)
def_node_matcher :struct_constructor?, <<~PATTERN
({block numblock} (send #global_const?(:Struct) :new ...) _ $_)
(any_block (send #global_const?(:Struct) :new ...) _ $_)
PATTERN

# @!method class_definition?(node = self)
def_node_matcher :class_definition?, <<~PATTERN
{(class _ _ $_)
(sclass _ $_)
({block numblock} (send #global_const?({:Struct :Class}) :new ...) _ $_)}
(any_block (send #global_const?({:Struct :Class}) :new ...) _ $_)}
PATTERN

# @!method module_definition?(node = self)
def_node_matcher :module_definition?, <<~PATTERN
{(module _ $_)
({block numblock} (send #global_const?(:Module) :new ...) _ $_)}
(any_block (send #global_const?(:Module) :new ...) _ $_)}
PATTERN

# Some expressions are evaluated for their value, some for their side
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/ast/node/mixin/method_dispatch_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def implicit_call?
#
# @return [Boolean] whether the dispatched method has a block
def block_literal?
(parent&.block_type? || parent&.numblock_type?) && eql?(parent.send_node)
parent&.any_block_type? && eql?(parent.send_node)
end

# Checks whether this node is an arithmetic operation
Expand Down Expand Up @@ -260,7 +260,7 @@ def binary_operation?
^{ # or the parent is...
sclass class module class_constructor? # a class-like node
[ { # or some "wrapper"
kwbegin begin block numblock
kwbegin begin any_block
(if _condition <%0 _>) # note: we're excluding the condition of `if` nodes
}
#in_macro_scope? # that is itself in a macro scope
Expand Down

0 comments on commit 5763d46

Please sign in to comment.