We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d67b82e commit fa0f0eaCopy full SHA for fa0f0ea
7 files changed
lib/rbi/index.rb
@@ -217,7 +217,17 @@ class TEnumBlock
217
# @override
218
#: -> Array[String]
219
def index_ids
220
- [to_s]
+ [fully_qualified_name]
221
+ end
222
223
+
224
+ class TEnumValue
225
+ include Indexable
226
227
+ # @override
228
+ #: -> Array[String]
229
+ def index_ids
230
231
end
232
233
lib/rbi/model.rb
@@ -1037,6 +1037,29 @@ def to_s
1037
1038
1039
1040
+ class TEnumValue < NodeWithComments
1041
+ #: String
1042
+ attr_reader :name
1043
1044
+ #: (String name, ?loc: Loc?, ?comments: Array[Comment]) ?{ (TEnumValue node) -> void } -> void
1045
+ def initialize(name, loc: nil, comments: [], &block)
1046
+ super(loc: loc, comments: comments)
1047
+ @name = name
1048
+ block&.call(self)
1049
1050
1051
+ #: -> String
1052
+ def fully_qualified_name
1053
+ "#{parent_scope&.fully_qualified_name}::#{name}"
1054
1055
1056
1057
1058
+ def to_s
1059
+ fully_qualified_name
1060
1061
1062
1063
# Sorbet's misc.
1064
1065
class Helper < NodeWithComments
lib/rbi/parser.rb
@@ -229,6 +229,17 @@ def visit_constant_assign(node)
loc: node_loc(node),
comments: node_comments(node),
)
+ elsif t_enum_value?(node)
+ TEnumValue.new(
234
+ case node
235
+ when Prism::ConstantWriteNode
236
+ node.name.to_s
237
+ when Prism::ConstantPathWriteNode
238
+ node_string!(node.target)
239
+ end,
240
+ loc: node_loc(node),
241
+ comments: node_comments(node),
242
+ )
243
else
244
Const.new(
245
case node
@@ -819,6 +830,11 @@ def set_root_tree_loc
819
830
def type_variable_definition?(node)
820
831
node.is_a?(Prism::CallNode) && (node.message == "type_member" || node.message == "type_template")
821
832
833
834
+ #: (Prism::Node? node) -> bool
835
+ def t_enum_value?(node)
836
+ current_scope.is_a?(TEnumBlock) && node.is_a?(Prism::ConstantWriteNode) && node.value.slice == "new"
837
822
838
823
839
824
840
class SigBuilder < Visitor
lib/rbi/printer.rb
@@ -542,6 +542,16 @@ def visit_tenum_block(node)
542
printl("end")
543
544
545
546
+ #: (TEnumValue node) -> void
547
+ def visit_tenum_value(node)
548
+ print_blank_line_before(node)
549
+ print_loc(node)
550
+ visit_all(node.comments)
551
552
+ printl("#{node.name} = new")
553
554
555
556
#: (TypeMember node) -> void
557
def visit_type_member(node)
lib/rbi/rbs_printer.rb
@@ -645,19 +645,22 @@ def visit_tenum(node)
645
646
#: (TEnumBlock node) -> void
647
def visit_tenum_block(node)
648
- node.nodes.each do |child|
649
- child = if child.is_a?(Const) && child.value == "new"
650
- parent = node.parent_scope
651
- Const.new(
652
- child.name,
653
- "T.let(nil, #{parent.is_a?(TEnum) ? parent.name : "T.untyped"})",
654
- comments: child.comments,
655
- )
656
- else
657
- child
658
- end
659
- visit(child)
660
- @previous_node = child
+ visit_all(node.nodes)
+ t_enum = node.parent_scope&.parent_scope
+ if t_enum.is_a?(TEnum)
661
+ printl("#{node.name}: #{t_enum.name}")
662
+ else
663
+ printl("#{node.name}: untyped")
664
665
666
lib/rbi/visitor.rb
@@ -88,6 +88,8 @@ def visit(node)
88
visit_sig(node)
89
when SigParam
90
visit_sig_param(node)
91
+ when TEnumValue
92
+ visit_tenum_value(node)
93
when TStructConst
94
visit_tstruct_const(node)
95
when TStructProp
@@ -222,6 +224,9 @@ def visit_tenum(node); end
def visit_tenum_block(node); end
+ def visit_tenum_value(node); end
#: (Helper node) -> void
def visit_helper(node); end
test/rbi/parser_test.rb
@@ -309,7 +309,14 @@ def baz; end
309
310
# Make sure the enums are not parsed as normal classes
311
enum = tree.nodes.first
312
- assert_equal(TEnum, enum.class)
+ assert_instance_of(TEnum, enum)
313
314
+ block = T.cast(enum, TEnum).nodes.first
315
+ assert_instance_of(TEnumBlock, block)
316
317
+ values = T.cast(block, TEnumBlock).nodes
318
+ assert_equal(3, values.size)
319
+ assert_equal(3, values.grep(TEnumValue).size)
320
321
assert_equal(rbi, tree.string)
322
@@ -329,7 +336,13 @@ def baz; end
329
336
330
337
331
338
332
339
340
341
342
343
344
+ value = T.cast(block, TEnumBlock).nodes.first
345
+ assert_instance_of(TEnumValue, value)
333
346
334
347
335
348
0 commit comments