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?
    lock_path = RBS::Collection::Config.to_lockfile_path(collection_path)
    if lock_path.exist?
      collection_lock = RBS::Collection::Config.from_path(lock_path)
      @repo.add(collection_lock.repo_path)
    else
      raise "Please execute 'rbs collection install'"
    end
  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 21

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?
      lock_path = RBS::Collection::Config.to_lockfile_path(collection_path)
      if lock_path.exist?
        collection_lock = RBS::Collection::Config::Lockfile.from_lockfile(lockfile_path: lock_path, data: YAML.load_file(lock_path.to_s))
        collection_lock.gems.each_value {|gem| @loaded_gems << gem[:name] }
        loader.add_collection(collection_lock)
      else
        raise "Please execute 'rbs collection install'"
      end
    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 105

def self.load_rbs(env, new_decls)
  all_env = env.resolve_type_names
  cur_env = RBS::Environment.new
  if defined?(RBS::TypeNameResolver)
    resolver = RBS::TypeNameResolver.from_env(all_env)
    new_decls.each do |decl|
      cur_env << env.resolve_declaration(resolver, decl, outer: [], prefix: RBS::Namespace.root)
    end
  else
    resolver = RBS::Resolver::TypeNameResolver.new(all_env)
    table = RBS::Environment::UseMap::Table.new()
    table.compute_children
    map = RBS::Environment::UseMap.new(table: table)
    new_decls.each do |decl|
      cur_env << s = env.resolve_declaration(resolver, map, decl, outer: [], prefix: RBS::Namespace.root)
    end
  end

  RBS2JSON.new(all_env, cur_env).dump_json
end

Instance Method Details

#load_builtin

[ GitHub ]

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

def load_builtin
  @builtin_env_json
end

#load_library(lib)

[ GitHub ]

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

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 80

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 87

def load_rbs_string(name, content)
  buffer = RBS::Buffer.new(name: name, content: content)
  new_decls = []
  ret = RBS::Parser.parse_signature(buffer)
  if ret[0].is_a?(RBS::Buffer)
    # rbs 3.0
    buffer, directives, decls = ret
    @env.add_signature(buffer: buffer, directives: directives, decls: decls)
    new_decls.concat(decls)
  else
    ret.each do |decl|
      @env << decl
      new_decls << decl
    end
  end
  RBSReader.load_rbs(@env, new_decls)
end