123456789_123456789_123456789_123456789_123456789_

Class: TypeProf::RBSReader

Relationships & Source Files
Namespace Children
Exceptions:
Inherits: Object
Defined in: lib/typeprof/import.rb

Class Method Summary

Instance Method Summary

Constructor Details

.newRBSReader

[ GitHub ]

  
# File 'lib/typeprof/import.rb', line 5

def initialize
  @repo = RBS::Repository.new
  collection_path = Config.current.collection_path
  if collection_path&.exist?
    collection_lock = RBS::Collection::Config.lockfile_of(collection_path)
    @repo.add(collection_lock.repo_path)
  end
  @env, @loaded_gems, @builtin_env_json = RBSReader.get_builtin_env
end

Class Method Details

.get_builtin_env

[ GitHub ]

  
# File 'lib/typeprof/import.rb', line 16

def self.get_builtin_env
  @loaded_gems = []
  unless @builtin_env
    @builtin_env = RBS::Environment.new

    loader = RBS::EnvironmentLoader.new

    # TODO: invalidate this cache when rbs_collection.yml was changed
    collection_path = Config.current.collection_path
    if collection_path&.exist?
      collection_lock = RBS::Collection::Config.lockfile_of(collection_path)
      collection_lock.gems.each {|gem| @loaded_gems << gem["name"] }
      loader.add_collection(collection_lock)
    end

    new_decls = loader.load(env: @builtin_env).map {|decl,| decl }
    @builtin_env_json = load_rbs(@builtin_env, new_decls)
  end

  return @builtin_env.dup, @loaded_gems.dup, @builtin_env_json
end

.load_rbs(env, new_decls)

[ GitHub ]

  
# File 'lib/typeprof/import.rb', line 87

def self.load_rbs(env, new_decls)
  all_env = env.resolve_type_names
  resolver = RBS::TypeNameResolver.from_env(all_env)
  cur_env = RBS::Environment.new
  new_decls.each do |decl|
    cur_env << env.resolve_declaration(resolver, decl, outer: [], prefix: RBS::Namespace.root)
  end

  RBS2JSON.new(all_env, cur_env).dump_json
end

Instance Method Details

#load_builtin

[ GitHub ]

  
# File 'lib/typeprof/import.rb', line 38

def load_builtin
  @builtin_env_json
end

#load_library(lib)

[ GitHub ]

  
# File 'lib/typeprof/import.rb', line 44

def load_library(lib)
  loader = RBS::EnvironmentLoader.new(core_root: nil, repository: @repo)
  if @loaded_gems.include?(lib)
    raise RBSCollectionDefined
  end
  @loaded_gems << lib
  loader.add(library: lib)

  case lib
  when 'bigdecimal-math'
    loader.add(library: 'bigdecimal')
  when "yaml"
    loader.add(library: "pstore")
    loader.add(library: "dbm")
  when "logger"
    loader.add(library: "monitor")
  when "csv"
    loader.add(library: "forwardable")
  when "prime"
    loader.add(library: "singleton")
  end

  new_decls = loader.load(env: @env).map {|decl,| decl }
  RBSReader.load_rbs(@env, new_decls)
end

#load_paths(paths)

[ GitHub ]

  
# File 'lib/typeprof/import.rb', line 70

def load_paths(paths)
  loader = RBS::EnvironmentLoader.new(core_root: nil, repository: @repo)
  paths.each {|path| loader.add(path: path) }
  new_decls = loader.load(env: @env).map {|decl,| decl }
  RBSReader.load_rbs(@env, new_decls)
end

#load_rbs_string(name, content)

[ GitHub ]

  
# File 'lib/typeprof/import.rb', line 77

def load_rbs_string(name, content)
  buffer = RBS::Buffer.new(name: name, content: content)
  new_decls = []
  RBS::Parser.parse_signature(buffer).each do |decl|
    @env << decl
    new_decls << decl
  end
  RBSReader.load_rbs(@env, new_decls)
end