We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 5ddb08a commit 52e51ceCopy full SHA for 52e51ce
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
@@ -228,6 +228,17 @@ def visit_constant_assign(node)
current_scope << if struct
struct
+ elsif t_enum_value?(node)
+ TEnumValue.new(
+ case node
234
+ when Prism::ConstantWriteNode
235
+ node.name.to_s
236
+ when Prism::ConstantPathWriteNode
237
+ node_string!(node.target)
238
+ end,
239
+ loc: node_loc(node),
240
+ comments: node_comments(node),
241
+ )
242
else
243
adjusted_node_location = adjust_prism_location_for_heredoc(node)
244
@@ -840,6 +851,11 @@ def set_root_tree_loc
840
851
def type_variable_definition?(node)
841
852
node.is_a?(Prism::CallNode) && (node.message == "type_member" || node.message == "type_template")
842
853
854
855
+ #: (Prism::Node? node) -> bool
856
+ def t_enum_value?(node)
857
+ current_scope.is_a?(TEnumBlock) && node.is_a?(Prism::ConstantWriteNode) && node.value.slice == "new"
858
843
859
844
860
845
861
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
@@ -368,7 +368,14 @@ def baz; end
368
369
# Make sure the enums are not parsed as normal classes
370
enum = tree.nodes.first
371
- assert_equal(TEnum, enum.class)
+ assert_instance_of(TEnum, enum)
372
373
+ block = T.cast(enum, TEnum).nodes.first
374
+ assert_instance_of(TEnumBlock, block)
375
376
+ values = T.cast(block, TEnumBlock).nodes
377
+ assert_equal(3, values.size)
378
+ assert_equal(3, values.grep(TEnumValue).size)
379
380
assert_equal(rbi, tree.string)
381
@@ -388,7 +395,13 @@ def baz; end
388
395
389
396
390
397
391
398
399
400
401
402
403
+ value = T.cast(block, TEnumBlock).nodes.first
404
+ assert_instance_of(TEnumValue, value)
392
405
393
406
394
407
0 commit comments