Skip to content

Commit 594ee7d

Browse files
committed
Add MethodTypeAnnotation class
1 parent 0f4fd2c commit 594ee7d

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

lib/rbs/ast/ruby/members.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,77 @@ def initialize(buffer)
1414
include Helpers::LocationHelper
1515
end
1616

17+
class MethodTypeAnnotation
18+
attr_reader :type_annotations
19+
20+
def initialize(type_annotations:)
21+
@type_annotations = type_annotations
22+
end
23+
24+
def map_type_name(&block)
25+
case type_annotations
26+
when Array
27+
updated_annots = type_annotations.map do |annotation|
28+
annotation.map_type_name(&block)
29+
end
30+
when Annotations::NodeTypeAssertion
31+
updated_annots = type_annotations.map_type_name(&block)
32+
end
33+
34+
MethodTypeAnnotation.new(type_annotations: updated_annots) #: self
35+
end
36+
37+
def self.build(leading_block, trailing_block, variables)
38+
unused_annotations = [] #: Array[Annotations::leading_annotation | CommentBlock::AnnotationSyntaxError]
39+
unused_trailing_annotation = nil #: Annotations::trailing_annotation | CommentBlock::AnnotationSyntaxError | nil
40+
41+
type_annotations = nil #: type_annotations
42+
43+
if trailing_block
44+
case annotation = trailing_block.trailing_annotation(variables)
45+
when Annotations::NodeTypeAssertion
46+
type_annotations = annotation
47+
else
48+
unused_trailing_annotation = annotation
49+
end
50+
end
51+
52+
if leading_block
53+
leading_block.each_paragraph(variables) do |paragraph|
54+
next if paragraph.is_a?(Location)
55+
56+
if paragraph.is_a?(CommentBlock::AnnotationSyntaxError)
57+
unused_annotations << paragraph
58+
next
59+
end
60+
61+
case paragraph
62+
when Annotations::MethodTypesAnnotation, Annotations::ColonMethodTypeAnnotation
63+
type_annotations = [] unless type_annotations
64+
if type_annotations.is_a?(Array)
65+
type_annotations << paragraph
66+
next
67+
end
68+
end
69+
70+
unused_annotations << paragraph
71+
end
72+
end
73+
74+
[
75+
MethodTypeAnnotation.new(
76+
type_annotations: type_annotations
77+
),
78+
unused_annotations,
79+
unused_trailing_annotation
80+
]
81+
end
82+
83+
def empty?
84+
type_annotations.nil?
85+
end
86+
end
87+
1788
class DefMember < Base
1889
Overload = AST::Members::MethodDefinition::Overload
1990

sig/ast/ruby/members.rbs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,29 @@ module RBS
1212

1313
type t = DefMember
1414

15+
class MethodTypeAnnotation
16+
type type_annotations = Annotations::NodeTypeAssertion | Array[Annotations::ColonMethodTypeAnnotation | Annotations::MethodTypesAnnotation] | nil
17+
18+
attr_reader type_annotations: type_annotations
19+
20+
def initialize: (type_annotations: type_annotations) -> void
21+
22+
def map_type_name: { (TypeName) -> TypeName } -> self
23+
24+
# Returns the method type annotations from the comment block
25+
#
26+
# Returns a tuple of `DefAnnotations` object, array of unused leading annotations, and unused trailing annotation.
27+
#
28+
def self.build: (CommentBlock? leading_block, CommentBlock? trailing_block, Array[Symbol]) -> [
29+
MethodTypeAnnotation,
30+
Array[Annotations::leading_annotation | CommentBlock::AnnotationSyntaxError],
31+
Annotations::trailing_annotation | CommentBlock::AnnotationSyntaxError | nil
32+
]
33+
34+
# Returns `true` if it doesn't have any annotation
35+
def empty?: () -> bool
36+
end
37+
1538
class DefMember < Base
1639
class Overload = AST::Members::MethodDefinition::Overload
1740

0 commit comments

Comments
 (0)