Skip to content

Commit e91773e

Browse files
committed
Refactor inline_parser
1 parent 6a5ee75 commit e91773e

2 files changed

Lines changed: 10 additions & 35 deletions

File tree

lib/rbs/inline_parser.rb

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -119,38 +119,7 @@ def visit_class_node(node)
119119

120120
class_decl = AST::Ruby::Declarations::ClassDecl.new(buffer, class_name, node, super_class)
121121
insert_declaration(class_decl)
122-
push_module_nesting(class_decl) do
123-
visit_child_nodes(node)
124-
125-
node.child_nodes.each do |child_node|
126-
if child_node
127-
comments.each_enclosed_block(child_node) do |block|
128-
report_unused_block(block)
129-
end
130-
end
131-
end
132-
end
133-
134-
comments.each_enclosed_block(node) do |block|
135-
unused_annotations = [] #: Array[AST::Ruby::CommentBlock::AnnotationSyntaxError | AST::Ruby::Annotations::leading_annotation]
136-
137-
block.each_paragraph([]) do |paragraph|
138-
case paragraph
139-
when AST::Ruby::Annotations::InstanceVariableAnnotation
140-
class_decl.members << AST::Ruby::Members::InstanceVariableMember.new(buffer, paragraph)
141-
when Location
142-
# Skip
143-
when AST::Ruby::CommentBlock::AnnotationSyntaxError
144-
unused_annotations << paragraph
145-
else
146-
unused_annotations << paragraph
147-
end
148-
end
149-
150-
report_unused_annotation(*unused_annotations)
151-
end
152-
153-
class_decl.members.sort_by! { _1.location.start_line }
122+
visit_class_or_module_body(class_decl, node)
154123
end
155124

156125
def visit_module_node(node)
@@ -166,7 +135,11 @@ def visit_module_node(node)
166135

167136
module_decl = AST::Ruby::Declarations::ModuleDecl.new(buffer, module_name, node)
168137
insert_declaration(module_decl)
169-
push_module_nesting(module_decl) do
138+
visit_class_or_module_body(module_decl, node)
139+
end
140+
141+
def visit_class_or_module_body(decl, node)
142+
push_module_nesting(decl) do
170143
visit_child_nodes(node)
171144

172145
node.child_nodes.each do |child_node|
@@ -184,7 +157,7 @@ def visit_module_node(node)
184157
block.each_paragraph([]) do |paragraph|
185158
case paragraph
186159
when AST::Ruby::Annotations::InstanceVariableAnnotation
187-
module_decl.members << AST::Ruby::Members::InstanceVariableMember.new(buffer, paragraph)
160+
decl.members << AST::Ruby::Members::InstanceVariableMember.new(buffer, paragraph)
188161
when Location
189162
# Skip
190163
when AST::Ruby::CommentBlock::AnnotationSyntaxError
@@ -197,7 +170,7 @@ def visit_module_node(node)
197170
report_unused_annotation(*unused_annotations)
198171
end
199172

200-
module_decl.members.sort_by! { _1.location.start_line }
173+
decl.members.sort_by! { _1.location.start_line }
201174
end
202175

203176
def visit_def_node(node)

sig/inline_parser.rbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ module RBS
110110

111111
def report_unused_block: (AST::Ruby::CommentBlock) -> void
112112

113+
def visit_class_or_module_body: (module_context, Prism::ClassNode | Prism::ModuleNode) -> void
114+
113115
private
114116

115117
def parse_mixin_call: (Prism::CallNode) -> void

0 commit comments

Comments
 (0)