Skip to content

Commit 35e03da

Browse files
authored
Merge pull request #2919 from ruby/inline-instance-var-annotation
Inline instance variable annotation in module declaration
2 parents 348e456 + e91773e commit 35e03da

File tree

3 files changed

+47
-25
lines changed

3 files changed

+47
-25
lines changed

lib/rbs/inline_parser.rb

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,27 @@ 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
122+
visit_class_or_module_body(class_decl, node)
123+
end
124+
125+
def visit_module_node(node)
126+
return if skip_node?(node)
127+
128+
unless module_name = constant_as_type_name(node.constant_path)
129+
diagnostics << Diagnostic::NonConstantModuleName.new(
130+
rbs_location(node.constant_path.location),
131+
"Module name must be a constant"
132+
)
133+
return
134+
end
135+
136+
module_decl = AST::Ruby::Declarations::ModuleDecl.new(buffer, module_name, node)
137+
insert_declaration(module_decl)
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
123143
visit_child_nodes(node)
124144

125145
node.child_nodes.each do |child_node|
@@ -137,7 +157,7 @@ def visit_class_node(node)
137157
block.each_paragraph([]) do |paragraph|
138158
case paragraph
139159
when AST::Ruby::Annotations::InstanceVariableAnnotation
140-
class_decl.members << AST::Ruby::Members::InstanceVariableMember.new(buffer, paragraph)
160+
decl.members << AST::Ruby::Members::InstanceVariableMember.new(buffer, paragraph)
141161
when Location
142162
# Skip
143163
when AST::Ruby::CommentBlock::AnnotationSyntaxError
@@ -150,29 +170,7 @@ def visit_class_node(node)
150170
report_unused_annotation(*unused_annotations)
151171
end
152172

153-
class_decl.members.sort_by! { _1.location.start_line }
154-
end
155-
156-
def visit_module_node(node)
157-
return if skip_node?(node)
158-
159-
unless module_name = constant_as_type_name(node.constant_path)
160-
diagnostics << Diagnostic::NonConstantModuleName.new(
161-
rbs_location(node.constant_path.location),
162-
"Module name must be a constant"
163-
)
164-
return
165-
end
166-
167-
module_decl = AST::Ruby::Declarations::ModuleDecl.new(buffer, module_name, node)
168-
insert_declaration(module_decl)
169-
push_module_nesting(module_decl) do
170-
visit_child_nodes(node)
171-
end
172-
173-
comments.each_enclosed_block(node) do |block|
174-
report_unused_block(block)
175-
end
173+
decl.members.sort_by! { _1.location.start_line }
176174
end
177175

178176
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

test/rbs/inline_parser_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,28 @@ def initialize(name, age)
15021502
end
15031503
end
15041504

1505+
def test_parse__instance_variable_in_module
1506+
result = parse(<<~RUBY)
1507+
module Foo
1508+
# @rbs @bar: String
1509+
end
1510+
RUBY
1511+
1512+
assert_empty result.diagnostics
1513+
1514+
result.declarations[0].tap do |decl|
1515+
assert_instance_of RBS::AST::Ruby::Declarations::ModuleDecl, decl
1516+
1517+
assert_equal 1, decl.members.size
1518+
1519+
decl.members[0].tap do |member|
1520+
assert_instance_of RBS::AST::Ruby::Members::InstanceVariableMember, member
1521+
assert_equal :@bar, member.name
1522+
assert_equal "String", member.type.to_s
1523+
end
1524+
end
1525+
end
1526+
15051527
def test_error__instance_variable_ignored
15061528
result = parse(<<~RUBY)
15071529
# @rbs @global_decl: String

0 commit comments

Comments
 (0)