Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment loader api #370

Merged
merged 2 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 55 additions & 35 deletions lib/rbs/environment_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,62 +75,82 @@ def gem?(name, version)
self.class.gem_sig_path(name, version)
end

def each_signature(path = nil, immediate: true, &block)
def each_signature(path, immediate: true, &block)
if block_given?
if path
case
when path.file?
if path.extname == ".rbs" || immediate
yield path
end
when path.directory?
path.children.each do |child|
each_signature child, immediate: false, &block
end
case
when path.file?
if path.extname == ".rbs" || immediate
yield path
end
else
paths.each do |path|
case path
when Pathname
each_signature path, immediate: immediate, &block
when LibraryPath
each_signature path.path, immediate: immediate, &block
when GemPath
each_signature path.path, immediate: immediate, &block
end
when path.directory?
path.children.each do |child|
each_signature child, immediate: false, &block
end
end
else
enum_for :each_signature, path, immediate: immediate
end
end

def no_builtin!
@no_builtin = true
def each_library_path
paths.each do |path|
case path
when Pathname
yield path, path
when LibraryPath
yield path, path.path
when GemPath
yield path, path.path
end
end
end

def no_builtin!(skip = true)
@no_builtin = skip
self
end

def no_builtin?
@no_builtin
end

def load(env:)
signature_files = []
def each_decl
if block_given?
signature_files = []

unless no_builtin?
signature_files.push(*each_signature(stdlib_root + "builtin"))
end
unless no_builtin?
each_signature(stdlib_root + "builtin") do |path|
signature_files << [:stdlib, path]
end
end

each_signature do |path|
signature_files.push path
each_library_path do |library_path, pathname|
each_signature(pathname) do |path|
signature_files << [library_path, path]
end
end

signature_files.each do |lib_path, file_path|
buffer = Buffer.new(name: file_path.to_s, content: file_path.read)
Parser.parse_signature(buffer).each do |decl|
yield decl, buffer, file_path, lib_path
end
end
else
enum_for :each_decl
end
end

def load(env:)
loadeds = []

signature_files.each do |file|
buffer = Buffer.new(name: file.to_s, content: file.read)
each_decl do |decl, buffer, file_path, lib_path|
env.buffers.push(buffer)
Parser.parse_signature(buffer).each do |decl|
env << decl
end
env << decl
loadeds << [decl, file_path, lib_path]
end

loadeds
end
end
end
46 changes: 46 additions & 0 deletions test/rbs/environment_loader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,50 @@ def test_gem_path_vendored
assert_equal gem_root + "racc", racc_path.path
end
end

def test_load_twice
env = Environment.new

with_signatures do |path|
EnvironmentLoader.new().tap do |loader|
loadeds = loader.load(env: env)

loadeds.each do |(decl, file_path, lib_path)|
assert_operator decl, :is_a?, Declarations::Base
assert_instance_of Pathname, file_path
assert_equal :stdlib, lib_path
end
end

assert env.declarations.any? {|decl| decl.is_a?(Declarations::Class) && decl.name.name == :BasicObject }

EnvironmentLoader.new().no_builtin!.tap do |loader|
loader.add(library: "pathname")
loadeds = loader.load(env: env)

assert env.declarations.any? {|decl| decl.is_a?(Declarations::Class) && decl.name.name == :Pathname }

loadeds.each do |(decl, file_path, lib_path)|
assert_operator decl, :is_a?, Declarations::Base
assert_instance_of Pathname, file_path
assert_instance_of EnvironmentLoader::LibraryPath, lib_path
assert_equal 'pathname', lib_path.name
end
end

EnvironmentLoader.new.no_builtin!.tap do |loader|
loader.add(path: path)
loadeds = loader.load(env: env)

assert env.declarations.any? {|decl| decl.is_a?(Declarations::Class) && decl.name.name == :PeopleController }

loadeds.each do |(decl, file_path, lib_path)|
assert_operator decl, :is_a?, Declarations::Base
assert_instance_of Pathname, file_path
assert_instance_of Pathname, lib_path
assert_equal path, lib_path
end
end
end
end
end