Class: RBS::Inline::AST::Declarations::ConstantDecl
- Includes:
- ConstantUtil
- Defined in:
- lib/rbs/inline/ast/declarations.rb,
sig/generated/rbs/inline/ast/declarations.rbs
Instance Attribute Summary collapse
-
#assertion ⇒ Annotations::TypeAssertion?
readonly
: Annotations::TypeAssertion?.
-
#comments ⇒ AnnotationParser::ParsingResult?
readonly
: AnnotationParser::ParsingResult?.
-
#node ⇒ Prism::ConstantWriteNode
readonly
: Prism::ConstantWriteNode.
Instance Method Summary collapse
- #constant_name ⇒ TypeName?
- #infer_array_element_type(node) ⇒ Types::t
- #infer_hash_element_type(node) ⇒ Types::t
- #infer_type_from_node(node) ⇒ Types::t?
-
#initialize(node, comments, assertion) ⇒ ConstantDecl
constructor
A new instance of ConstantDecl.
- #literal_type ⇒ Types::t?
-
#start_line ⇒ Integer
: Integer.
- #type(default_type) ⇒ Types::t
Methods included from ConstantUtil
Constructor Details
#initialize(node, comments, assertion) ⇒ ConstantDecl
Returns a new instance of ConstantDecl.
184 185 186 187 188 |
# File 'lib/rbs/inline/ast/declarations.rb', line 184 def initialize(node, comments, assertion) #: void @node = node @comments = comments @assertion = assertion end |
Instance Attribute Details
#assertion ⇒ Annotations::TypeAssertion? (readonly)
: Annotations::TypeAssertion?
179 180 181 |
# File 'lib/rbs/inline/ast/declarations.rb', line 179 def assertion @assertion end |
#comments ⇒ AnnotationParser::ParsingResult? (readonly)
: AnnotationParser::ParsingResult?
178 179 180 |
# File 'lib/rbs/inline/ast/declarations.rb', line 178 def comments @comments end |
#node ⇒ Prism::ConstantWriteNode (readonly)
: Prism::ConstantWriteNode
177 178 179 |
# File 'lib/rbs/inline/ast/declarations.rb', line 177 def node @node end |
Instance Method Details
#constant_name ⇒ TypeName?
217 218 219 |
# File 'lib/rbs/inline/ast/declarations.rb', line 217 def constant_name TypeName.new(name: node.name, namespace: Namespace.empty) end |
#infer_array_element_type(node) ⇒ Types::t
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/rbs/inline/ast/declarations.rb', line 252 def infer_array_element_type(node) return BuiltinNames::Array.instance_type if node.elements.empty? element_types = [] #: Array[Types::t] node.elements.each do |elem| type = infer_type_from_node(elem) return BuiltinNames::Array.instance_type unless type element_types << type end element_types.uniq!(&:to_s) # Union types are not currently supported. case element_types.size when 1 BuiltinNames::Array.instance_type(element_types.first || raise) else BuiltinNames::Array.instance_type( Types::Bases::Any.new(location: nil) ) end end |
#infer_hash_element_type(node) ⇒ Types::t
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/rbs/inline/ast/declarations.rb', line 277 def infer_hash_element_type(node) return BuiltinNames::Hash.instance_type if node.elements.empty? key_types = [] #: Array[Types::t] value_types = [] #: Array[Types::t] node.elements.each do |elem| case elem when Prism::AssocNode key_type = infer_type_from_node(elem.key) value_type = infer_type_from_node(elem.value) return BuiltinNames::Hash.instance_type unless key_type && value_type key_types << key_type value_types << value_type else return BuiltinNames::Hash.instance_type end end key_types.uniq!(&:to_s) value_types.uniq!(&:to_s) # Union types are not currently supported. key_type = case key_types.size when 1 key_types.first || raise else Types::Bases::Any.new(location: nil) end value_type = case value_types.size when 1 value_types.first || raise else Types::Bases::Any.new(location: nil) end BuiltinNames::Hash.instance_type(key_type, value_type) end |
#infer_type_from_node(node) ⇒ Types::t?
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/rbs/inline/ast/declarations.rb', line 229 def infer_type_from_node(node) case node when Prism::StringNode, Prism::InterpolatedStringNode BuiltinNames::String.instance_type when Prism::SymbolNode, Prism::InterpolatedSymbolNode BuiltinNames::Symbol.instance_type when Prism::RegularExpressionNode, Prism::InterpolatedRegularExpressionNode BuiltinNames::Regexp.instance_type when Prism::IntegerNode BuiltinNames::Integer.instance_type when Prism::FloatNode BuiltinNames::Float.instance_type when Prism::ArrayNode infer_array_element_type(node) when Prism::HashNode infer_hash_element_type(node) when Prism::TrueNode, Prism::FalseNode Types::Bases::Bool.new(location: nil) end end |
#literal_type ⇒ Types::t?
211 212 213 |
# File 'lib/rbs/inline/ast/declarations.rb', line 211 def literal_type infer_type_from_node(node.value) end |
#start_line ⇒ Integer
: Integer
221 222 223 |
# File 'lib/rbs/inline/ast/declarations.rb', line 221 def start_line #: Integer node.location.start_line end |
#type(default_type) ⇒ Types::t
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/rbs/inline/ast/declarations.rb', line 192 def type(default_type) if assertion case assertion.type when MethodType, nil # skip else return assertion.type end end if literal = literal_type return literal end default_type end |