Skip to content

Commit

Permalink
Add annotations for pattern matcher definitions.
Browse files Browse the repository at this point in the history
See rubocop/rubocop#9506 for more details.
  • Loading branch information
dvandersluis committed Mar 2, 2021
1 parent 942e39f commit 43156ac
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ AllCops:
- 'spec/rubocop/ast/fixtures/*'
TargetRubyVersion: 2.4

InternalAffairs/NodeMatcherDirective:
Exclude:
- 'spec/rubocop/ast/node_pattern_spec.rb'

Naming/PredicateName:
# Method define macros for dynamically generated method.
MethodDefinitionMacros:
Expand Down
15 changes: 15 additions & 0 deletions lib/rubocop/ast/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,12 @@ def source_length

## Destructuring

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

# @!method str_content(node = self)
def_node_matcher :str_content, '(str $_)'

def const_name
Expand All @@ -289,6 +291,7 @@ def const_name
end
end

# @!method defined_module0(node = self)
def_node_matcher :defined_module0, <<~PATTERN
{(class (const $_ $_) ...)
(module (const $_ $_) ...)
Expand Down Expand Up @@ -334,6 +337,7 @@ def empty_source?
end

# Some cops treat the shovel operator as a kind of assignment.
# @!method assignment_or_similar?(node = self)
def_node_matcher :assignment_or_similar?, <<~PATTERN
{assignment? (send _recv :<< ...)}
PATTERN
Expand Down Expand Up @@ -469,37 +473,47 @@ def guard_clause?
node.match_guard_clause?
end

# @!method match_guard_clause?(node = self)
def_node_matcher :match_guard_clause?, <<~PATTERN
[${(send nil? {:raise :fail} ...) return break next} single_line?]
PATTERN

# @!method proc?(node = self)
def_node_matcher :proc?, <<~PATTERN
{(block (send nil? :proc) ...)
(block (send #global_const?(:Proc) :new) ...)
(send #global_const?(:Proc) :new)}
PATTERN

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

# @!method lambda_or_proc?(node = self)
def_node_matcher :lambda_or_proc?, '{lambda? proc?}'

# @!method global_const?(node = self, name)
def_node_matcher :global_const?, '(const {nil? cbase} %1)'

# @!method class_constructor?(node = self)
def_node_matcher :class_constructor?, <<~PATTERN
{ (send #global_const?({:Class :Module :Struct}) :new ...)
(block (send #global_const?({:Class :Module :Struct}) :new ...) ...)}
PATTERN

# @deprecated Use `:class_constructor?`
# @!method struct_constructor?(node = self)
def_node_matcher :struct_constructor?, <<~PATTERN
(block (send #global_const?(:Struct) :new ...) _ $_)
PATTERN

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

# @!method module_definition?(node = self)
def_node_matcher :module_definition?, <<~PATTERN
{(module _ $_)
(block (send #global_const?(:Module) :new ...) _ $_)}
Expand Down Expand Up @@ -633,6 +647,7 @@ def parent_module_name_for_block(ancestor)
end
end

# @!method new_class_or_module_block?(node = self)
def_node_matcher :new_class_or_module_block?, <<~PATTERN
^(casgn _ _ (block (send (const _ {:Class :Module}) :new) ...))
PATTERN
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/ast/node/mixin/method_dispatch_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ def binary_operation?

private

# @!method in_macro_scope?(node = self)
def_node_matcher :in_macro_scope?, <<~PATTERN
{
root? # Either a root node,
Expand All @@ -239,14 +240,17 @@ def binary_operation?
}
PATTERN

# @!method adjacent_def_modifier?(node = self)
def_node_matcher :adjacent_def_modifier?, <<~PATTERN
(send nil? _ ({def defs} ...))
PATTERN

# @!method bare_access_modifier_declaration?(node = self)
def_node_matcher :bare_access_modifier_declaration?, <<~PATTERN
(send nil? {:public :protected :private :module_function})
PATTERN

# @!method non_bare_access_modifier_declaration?(node = self)
def_node_matcher :non_bare_access_modifier_declaration?, <<~PATTERN
(send nil? {:public :protected :private :module_function} _)
PATTERN
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/ast/node/send_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class SendNode < Node
include ParameterizedNode::RestArguments
include MethodDispatchNode

# @!method attribute_accessor?(node = self)
def_node_matcher :attribute_accessor?, <<~PATTERN
[(send nil? ${:attr_reader :attr_writer :attr_accessor :attr} $...)
(_ _ _ _ ...)]
Expand Down

0 comments on commit 43156ac

Please sign in to comment.