Class: Typeguard::TypeModel::Builder::RBSBuilder
- Inherits:
-
Object
- Object
- Typeguard::TypeModel::Builder::RBSBuilder
- Includes:
- Definitions
- Defined in:
- lib/typeguard/type_model/builder/rbs_builder.rb
Overview
Takes RBS signatures and returns a generic type model
Instance Attribute Summary collapse
-
#rbs_env ⇒ Object
readonly
Returns the value of attribute rbs_env.
Instance Method Summary collapse
-
#build ⇒ Object
ruby -e “require ‘rbs’;loader=RBS::EnvironmentLoader.new(core_root: nil);loader.add(path:Pathname(‘sig’));environment=RBS::Environment.from_loader(loader);environment.declarations.each{|cls,entries|pp cls;pp entries}”.
- #build_object(object) ⇒ Object
- #build_source(object) ⇒ Object
- #build_types(rbs_type) ⇒ Object
-
#initialize(target, _reparse) ⇒ Typeguard::Initializer::RBSInitalizer
constructor
Initializer for RBS signatures.
Constructor Details
#initialize(target, _reparse) ⇒ Typeguard::Initializer::RBSInitalizer
Returns initializer for RBS signatures.
15 16 17 18 19 20 21 22 23 |
# File 'lib/typeguard/type_model/builder/rbs_builder.rb', line 15 def initialize(target, _reparse) rbs_loader = RBS::EnvironmentLoader.new(core_root: nil) rbs_loader.add(path: Pathname(target)) @rbs_env = RBS::Environment.from_loader(rbs_loader).resolve_type_names return unless @rbs_env.declarations.empty? puts "WARNING: could not find RBS signatures for target directory '#{target}'. " \ 'Confirm that the directory exists and/or that it contains .rbs files.' end |
Instance Attribute Details
#rbs_env ⇒ Object (readonly)
Returns the value of attribute rbs_env.
12 13 14 |
# File 'lib/typeguard/type_model/builder/rbs_builder.rb', line 12 def rbs_env @rbs_env end |
Instance Method Details
#build ⇒ Object
ruby -e “require ‘rbs’;loader=RBS::EnvironmentLoader.new(core_root: nil);loader.add(path:Pathname(‘sig’));environment=RBS::Environment.from_loader(loader);environment.declarations.each{|cls,entries|pp cls;pp entries}”
26 27 28 |
# File 'lib/typeguard/type_model/builder/rbs_builder.rb', line 26 def build @rbs_env.declarations.map { |decl| build_object(decl) }.compact end |
#build_object(object) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/typeguard/type_model/builder/rbs_builder.rb', line 30 def build_object(object) case object when RBS::AST::Declarations::Class children = object.members.map { |child| build_object(child) }.compact ClassDefinition.new( name: object.name.relative!.to_s, source: build_source(object), parent: object.super_class&.to_s, type_parameters: object.type_params.map(&:name), children: children ) when RBS::AST::Declarations::Module children = object.members.map { |child| build_object(child) }.compact ModuleDefinition.new( name: object.name.relative!.to_s, source: build_source(object), type_parameters: object.type_params.map(&:name), children: children ) when RBS::AST::Members::MethodDefinition # NOTE: Currently only looking at first overload and # only at required positionals. sig = object.overloads.first.method_type.type parameters = sig.required_positionals.map do |param| ParameterDefinition.new( name: param.name, source: build_source(param), types: build_types(param.type), types_string: param.type.to_s ) end return_sig = sig.return_type returns = ReturnDefinition.new( source: build_source(return_sig), types: build_types(return_sig), types_string: return_sig.to_s ) MethodDefinition.new( name: object.name, source: build_source(object), scope: object.kind, visibility: object.visibility, parameters: parameters, returns: returns ) else raise "Unsupported RBS declaration #{object.class}" end end |
#build_source(object) ⇒ Object
92 93 94 95 |
# File 'lib/typeguard/type_model/builder/rbs_builder.rb', line 92 def build_source(object) location = object.location "#{location.buffer.name}:#{location.start_line}" end |
#build_types(rbs_type) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/typeguard/type_model/builder/rbs_builder.rb', line 79 def build_types(rbs_type) if rbs_type [Typeguard::TypeModel::Mapper::RBSMapper.parse_map(rbs_type)] else [TypeNode.new( kind: :untyped, shape: :untyped, children: [], metadata: { note: 'RBS type not provided' } )] end end |