Skip to content

Commit 2b3fb84

Browse files
authored
Add types for rubocop ast (#925)
* rubocop-ast: Add types for rubocop-ast RuboCop::AST::NextNode * rubocop-ast: Add types for rubocop-ast RuboCop::AST::Procarg0Node * rubocop-ast: Add types for rubocop-ast RuboCop::AST::RescueNode RuboCop::AST::ResbodyNode * rubocop-ast: Add types for rubocop-ast RuboCop::AST::SelfClassNode
1 parent 4873ad0 commit 2b3fb84

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

gems/rubocop-ast/1.30/_test/test.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,11 @@ def test
659659
module_node.body
660660
end
661661

662+
next_node = RuboCop::AST::ProcessedSource.new('next 1', RUBY_VERSION.to_f).ast
663+
if next_node.is_a?(RuboCop::AST::NextNode)
664+
next_node.arguments
665+
end
666+
662667
or_asgn_node = RuboCop::AST::ProcessedSource.new('a ||= 1', RUBY_VERSION.to_f).ast
663668
if or_asgn_node.is_a?(RuboCop::AST::OrAsgnNode)
664669
or_asgn_node.assignment_node
@@ -806,11 +811,44 @@ def test
806811
regexp_node.loc.end
807812
end
808813

814+
rescue_node = RuboCop::AST::ProcessedSource.new(<<EOM, RUBY_VERSION.to_f).ast&.child_nodes&.first
815+
begin
816+
do_something
817+
rescue
818+
retry
819+
end
820+
EOM
821+
if rescue_node.is_a?(RuboCop::AST::RescueNode)
822+
rescue_node.body
823+
rescue_node.resbody_branches
824+
rescue_node.branches
825+
rescue_node.else_branch
826+
rescue_node.else?
827+
resbody_node = rescue_node.resbody_branches.first
828+
if resbody_node.is_a?(RuboCop::AST::ResbodyNode)
829+
resbody_node.body
830+
resbody_node.exceptions
831+
resbody_node.exception_variable
832+
resbody_node.branch_index
833+
end
834+
end
835+
809836
return_node = RuboCop::AST::ProcessedSource.new('return 1', RUBY_VERSION.to_f).ast
810837
if return_node.is_a?(RuboCop::AST::ReturnNode)
811838
return_node.arguments
812839
end
813840

841+
self_class_node = RuboCop::AST::ProcessedSource.new(<<EOM, RUBY_VERSION.to_f).ast
842+
class << self
843+
def foo
844+
end
845+
end
846+
EOM
847+
if self_class_node.is_a?(RuboCop::AST::SelfClassNode)
848+
self_class_node.identifier
849+
self_class_node.body
850+
end
851+
814852
until_node = RuboCop::AST::ProcessedSource.new('1 until true', RUBY_VERSION.to_f).ast
815853
if until_node.is_a?(RuboCop::AST::UntilNode)
816854
until_node.single_line_condition?

gems/rubocop-ast/1.30/rubocop-ast.rbs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,10 @@ module RuboCop
590590
def body: () -> Node?
591591
end
592592

593+
class NextNode < Node
594+
include ParameterizedNode::WrappedArguments
595+
end
596+
593597
class OpAsgnNode < Node
594598
def assignment_node: () -> AsgnNode
595599
def name: () -> Symbol
@@ -620,6 +624,10 @@ module RuboCop
620624
alias loc location
621625
end
622626

627+
class Procarg0Node < ArgNode
628+
def name: () -> Symbol?
629+
end
630+
623631
class RangeNode < Node
624632
def begin: () -> Node?
625633
def end: () -> Node?
@@ -645,10 +653,30 @@ module RuboCop
645653
alias loc location
646654
end
647655

656+
class ResbodyNode < Node
657+
def body: () -> Node?
658+
def exceptions: () -> Array[Node]
659+
def exception_variable: () -> Node?
660+
def branch_index: () -> Integer
661+
end
662+
663+
class RescueNode < Node
664+
def body: () -> Node?
665+
def resbody_branches: () -> Array[ResbodyNode]
666+
def branches: () -> Array[Node | nil]
667+
def else_branch: () -> Node?
668+
def else?: () -> bool
669+
end
670+
648671
class ReturnNode < Node
649672
include ParameterizedNode::WrappedArguments
650673
end
651674

675+
class SelfClassNode < Node
676+
def identifier: () -> Node
677+
def body: () -> Node?
678+
end
679+
652680
class SendNode < Node
653681
include ParameterizedNode::RestArguments
654682
include MethodDispatchNode

0 commit comments

Comments
 (0)