Module: Decode::RBS::Type
- Defined in:
- lib/decode/rbs/type.rb
Overview
Utilities for working with RBS types.
Class Method Summary collapse
-
.nullable?(rbs_type) ⇒ Boolean
Check if an RBS type represents a nullable/optional type This method recursively traverses the type tree to find nil anywhere.
-
.parse(type_string) ⇒ Object
Parse a type string and convert it to RBS type.
Class Method Details
.nullable?(rbs_type) ⇒ Boolean
Check if an RBS type represents a nullable/optional type This method recursively traverses the type tree to find nil anywhere
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/decode/rbs/type.rb', line 17 def self.nullable?(rbs_type) case rbs_type when ::RBS::Types::Optional # Type? form - directly optional true when ::RBS::Types::Union # Type | nil form - recursively check all union members rbs_type.types.any? {|type| nullable?(type)} when ::RBS::Types::Tuple # [Type] form - recursively check all tuple elements rbs_type.types.any? {|type| nullable?(type)} when ::RBS::Types::Bases::Nil # Direct nil type true else false end end |
.parse(type_string) ⇒ Object
Parse a type string and convert it to RBS type
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/decode/rbs/type.rb', line 39 def self.parse(type_string) # This is for backwards compatibility with the old syntax, eventually we will emit warnings for these: type_string = type_string.tr("()", "[]") type_string.gsub!(/\s*\| Nil/, "?") type_string.gsub!("Boolean", "bool") return ::RBS::Parser.parse_type(type_string) rescue => error warn("Failed to parse type string: #{type_string}") if $DEBUG return ::RBS::Parser.parse_type("untyped") end |