Class: RBS::Inline::AST::Members::RubyDef
- Defined in:
- lib/rbs/inline/ast/members.rb
Instance Attribute Summary collapse
-
#assertion ⇒ Object
readonly
Assertion given at the end of the method name.
-
#comments ⇒ Object
readonly
: AnnotationParser::ParsingResult?.
-
#node ⇒ Object
readonly
: Prism::DefNode.
-
#singleton_instance ⇒ Object
The function is defined as singleton and instance method (as known as module_function).
-
#visibility ⇒ Object
readonly
The visibility directly attached to the ‘def` node.
Attributes inherited from Base
Instance Method Summary collapse
-
#annotated_method_types ⇒ Object
Returns ‘nil` if no `@rbs METHOD-TYPE` or `#:` annotation is given.
-
#block_type_annotation ⇒ Object
: AST::Annotations::BlockType?.
-
#double_splat_param_type_annotation ⇒ Object
: Annotations::DoubleSplatParamType?.
-
#initialize(node, comments, visibility, singleton_instance, assertion) ⇒ RubyDef
constructor
A new instance of RubyDef.
-
#method_annotations ⇒ Object
: Array.
-
#method_name ⇒ Object
Returns the name of the method.
-
#method_overloads ⇒ Object
: Array.
-
#overloading? ⇒ Boolean
: bool.
-
#override_annotation ⇒ Object
Returns the ‘@rbs override` annotation.
-
#return_type ⇒ Object
: Types::t?.
-
#splat_param_type_annotation ⇒ Object
: Annotations::SplatParamType?.
-
#var_type_hash ⇒ Object
: Hash[Symbol, Types::t?].
Methods inherited from Base
Constructor Details
#initialize(node, comments, visibility, singleton_instance, assertion) ⇒ RubyDef
Returns a new instance of RubyDef.
57 58 59 60 61 62 63 64 65 |
# File 'lib/rbs/inline/ast/members.rb', line 57 def initialize(node, comments, visibility, singleton_instance, assertion) #: void @node = node @comments = comments @visibility = visibility @singleton_instance = singleton_instance @assertion = assertion super(node.location) end |
Instance Attribute Details
#assertion ⇒ Object (readonly)
Assertion given at the end of the method name
50 51 52 |
# File 'lib/rbs/inline/ast/members.rb', line 50 def assertion @assertion end |
#comments ⇒ Object (readonly)
: AnnotationParser::ParsingResult?
32 33 34 |
# File 'lib/rbs/inline/ast/members.rb', line 32 def comments @comments end |
#node ⇒ Object (readonly)
: Prism::DefNode
31 32 33 |
# File 'lib/rbs/inline/ast/members.rb', line 31 def node @node end |
#singleton_instance ⇒ Object
The function is defined as singleton and instance method (as known as module_function)
46 47 48 |
# File 'lib/rbs/inline/ast/members.rb', line 46 def singleton_instance @singleton_instance end |
#visibility ⇒ Object (readonly)
The visibility directly attached to the ‘def` node
‘nil` when the `def` node is not passed to `private`/`public` calls.
“‘rb def foo() end # <= nil private def foo() end # <= :private “`
42 43 44 |
# File 'lib/rbs/inline/ast/members.rb', line 42 def visibility @visibility end |
Instance Method Details
#annotated_method_types ⇒ Object
Returns ‘nil` if no `@rbs METHOD-TYPE` or `#:` annotation is given
Returns an empty array if only ‘…` method type is given.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rbs/inline/ast/members.rb', line 76 def annotated_method_types #: Array[MethodType]? if comments method_type_annotations = comments.each_annotation.select do |annotation| annotation.is_a?(Annotations::MethodTypeAssertion) || annotation.is_a?(Annotations::Method) || annotation.is_a?(Annotations::Dot3Assertion) end return nil if method_type_annotations.empty? method_type_annotations.each_with_object([]) do |annotation, method_types| #$ Array[MethodType] case annotation when Annotations::MethodTypeAssertion method_types << annotation.method_type when Annotations::Method annotation.each_method_type do method_types << _1 end end end end end |
#block_type_annotation ⇒ Object
: AST::Annotations::BlockType?
305 306 307 308 309 310 311 |
# File 'lib/rbs/inline/ast/members.rb', line 305 def block_type_annotation #: AST::Annotations::BlockType? if comments comments.each_annotation.find do |annotation| annotation.is_a?(AST::Annotations::BlockType) end #: AST::Annotations::BlockType? end end |
#double_splat_param_type_annotation ⇒ Object
: Annotations::DoubleSplatParamType?
137 138 139 140 141 142 143 |
# File 'lib/rbs/inline/ast/members.rb', line 137 def double_splat_param_type_annotation #: Annotations::DoubleSplatParamType? if comments comments.each_annotation.find do |annotation| annotation.is_a?(Annotations::DoubleSplatParamType) end #: Annotations::DoubleSplatParamType? end end |
#method_annotations ⇒ Object
: Array
282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/rbs/inline/ast/members.rb', line 282 def method_annotations #: Array[RBS::AST::Annotation] if comments comments.each_annotation.flat_map do |annotation| if annotation.is_a?(AST::Annotations::RBSAnnotation) annotation.annotations else [] end end else [] end end |
#method_name ⇒ Object
Returns the name of the method
68 69 70 |
# File 'lib/rbs/inline/ast/members.rb', line 68 def method_name #: Symbol node.name end |
#method_overloads ⇒ Object
: Array
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/rbs/inline/ast/members.rb', line 161 def method_overloads #: Array[RBS::AST::Members::MethodDefinition::Overload] case when method_types = annotated_method_types method_types.map do |method_type| RBS::AST::Members::MethodDefinition::Overload.new( method_type: method_type, annotations: [] ) end else required_positionals = [] #: Array[Types::Function::Param] optional_positionals = [] #: Array[Types::Function::Param] rest_positionals = nil #: Types::Function::Param? required_keywords = {} #: Hash[Symbol, Types::Function::Param] optional_keywords = {} #: Hash[Symbol, Types::Function::Param] rest_keywords = nil #: Types::Function::Param? if node.parameters node.parameters.requireds.each do |param| case param when Prism::RequiredParameterNode required_positionals << Types::Function::Param.new( name: param.name, type: var_type_hash[param.name] || Types::Bases::Any.new(location: nil), location: nil ) end end node.parameters.optionals.each do |param| case param when Prism::OptionalParameterNode optional_positionals << Types::Function::Param.new( name: param.name, type: var_type_hash[param.name] || Types::Bases::Any.new(location: nil), location: nil ) end end if (rest = node.parameters.rest).is_a?(Prism::RestParameterNode) splat_param_type = splat_param_type_annotation if splat_param_type && splat_param_type.type splat_type = splat_param_type.type end rest_positionals = Types::Function::Param.new( name: rest.name, type: splat_type || Types::Bases::Any.new(location: nil), location: nil ) end node.parameters.keywords.each do |node| if node.is_a?(Prism::RequiredKeywordParameterNode) required_keywords[node.name] = Types::Function::Param.new( name: nil, type: var_type_hash[node.name] || Types::Bases::Any.new(location: nil), location: nil ) end if node.is_a?(Prism::OptionalKeywordParameterNode) optional_keywords[node.name] = Types::Function::Param.new( name: nil, type: var_type_hash[node.name] || Types::Bases::Any.new(location: nil), location: nil ) end end if (kw_rest = node.parameters.keyword_rest).is_a?(Prism::KeywordRestParameterNode) double_splat_param_type = double_splat_param_type_annotation if double_splat_param_type && double_splat_param_type.type double_splat_type = double_splat_param_type.type end rest_keywords = Types::Function::Param.new( name: kw_rest.name, type: double_splat_type || Types::Bases::Any.new(location: nil), location: nil) end if node.parameters.block block = Types::Block.new( type: Types::UntypedFunction.new(return_type: Types::Bases::Any.new(location: nil)), required: false, self_type: nil ) end end if type = block_type_annotation&.type block = type end [ RBS::AST::Members::MethodDefinition::Overload.new( method_type: RBS::MethodType.new( type_params: [], type: Types::Function.new( required_positionals: required_positionals, optional_positionals: optional_positionals, rest_positionals: rest_positionals, trailing_positionals: [], required_keywords: required_keywords, optional_keywords: optional_keywords, rest_keywords: rest_keywords, return_type: return_type || Types::Bases::Any.new(location: nil) ), block: block, location: nil ), annotations: [] ) ] end end |
#overloading? ⇒ Boolean
: bool
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/rbs/inline/ast/members.rb', line 145 def overloading? #: bool if comments comments.each_annotation do |annotation| if annotation.is_a?(Annotations::Method) return true if annotation.overloading end if annotation.is_a?(Annotations::Dot3Assertion) return true end end false else false end end |
#override_annotation ⇒ Object
Returns the ‘@rbs override` annotation
297 298 299 300 301 302 303 |
# File 'lib/rbs/inline/ast/members.rb', line 297 def override_annotation #: AST::Annotations::Override? if comments comments.each_annotation.find do |annotation| annotation.is_a?(AST::Annotations::Override) end #: AST::Annotations::Override? end end |
#return_type ⇒ Object
: Types::t?
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rbs/inline/ast/members.rb', line 97 def return_type #: Types::t? if assertion return assertion.type end if comments annot = comments.each_annotation.find {|annot| annot.is_a?(Annotations::ReturnType ) } #: Annotations::ReturnType? if annot annot.type end end end |
#splat_param_type_annotation ⇒ Object
: Annotations::SplatParamType?
129 130 131 132 133 134 135 |
# File 'lib/rbs/inline/ast/members.rb', line 129 def splat_param_type_annotation #: Annotations::SplatParamType? if comments comments.each_annotation.find do |annotation| annotation.is_a?(Annotations::SplatParamType) end #: Annotations::SplatParamType? end end |
#var_type_hash ⇒ Object
: Hash[Symbol, Types::t?]
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/rbs/inline/ast/members.rb', line 110 def var_type_hash #: Hash[Symbol, Types::t?] types = {} #: Hash[Symbol, Types::t?] if comments comments.each_annotation.each do |annotation| if annotation.is_a?(Annotations::VarType) name = annotation.name type = annotation.type if name types[name] = type end end end end types end |