Skip to content

Commit

Permalink
Update EnvironmentLoader API
Browse files Browse the repository at this point in the history
  • Loading branch information
soutaro committed Oct 17, 2020
1 parent 0ea7918 commit 4f44a14
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 34 deletions.
20 changes: 4 additions & 16 deletions lib/steep/project/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class TargetDSL
attr_reader :libraries
attr_reader :signatures
attr_reader :ignored_sources
attr_reader :no_builtin
attr_reader :vendor_dir
attr_reader :strictness_level
attr_reader :typing_option_hash
Expand Down Expand Up @@ -71,13 +70,10 @@ def no_builtin!(value = true)

def vendor(dir = "vendor/sigs", stdlib: nil, gems: nil)
if stdlib || gems
@vendor_dir = [
stdlib&.yield_self {|x| Pathname(x) },
gems&.yield_self {|x| Pathname(x) }
]
else
@vendor_dir = Pathname(dir)
Steep.logger.warn { "#vendor with stdlib: or gems: keyword is deprecated." }
end

@vendor_dir = Pathname(dir)
end
end

Expand Down Expand Up @@ -124,6 +120,7 @@ def target(name, template: nil, &block)
signature_patterns: target.signatures,
options: Options.new.tap do |options|
options.libraries.push(*target.libraries)
options.vendor_path = target.vendor_dir

case target.strictness_level
when :strict
Expand All @@ -133,15 +130,6 @@ def target(name, template: nil, &block)
end

options.merge!(target.typing_option_hash)

case target.vendor_dir
when Array
options.vendored_stdlib_path = target.vendor_dir[0]
options.vendored_gems_path = target.vendor_dir[1]
when Pathname
options.vendored_stdlib_path = target.vendor_dir + "stdlib"
options.vendored_gems_path = target.vendor_dir + "gems"
end
end
).tap do |target|
project.targets << target
Expand Down
6 changes: 2 additions & 4 deletions lib/steep/project/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ class Options
attr_accessor :allow_missing_definitions
attr_accessor :allow_unknown_constant_assignment
attr_accessor :allow_unknown_method_calls
attr_accessor :vendored_stdlib_path
attr_accessor :vendored_gems_path
attr_accessor :vendor_path
attr_reader :libraries

def initialize
apply_default_typing_options!
self.vendored_gems_path = nil
self.vendored_stdlib_path = nil
self.vendor_path = nil

@libraries = []
end
Expand Down
17 changes: 12 additions & 5 deletions lib/steep/project/target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,19 @@ def type_check(target_sources: source_files.values, validate_signatures: true)

def environment
@environment ||= RBS::Environment.new().yield_self do |env|
stdlib_root = options.vendored_stdlib_path || RBS::EnvironmentLoader::STDLIB_ROOT
gem_vendor_path = options.vendored_gems_path
loader = RBS::EnvironmentLoader.new(stdlib_root: stdlib_root, gem_vendor_path: gem_vendor_path)
options.libraries.each do |lib|
loader.add(library: lib)
core_root = options.vendor_path ? nil : RBS::EnvironmentLoader::DEFAULT_CORE_ROOT
repo = RBS::Repository.new(no_stdlib: !core_root)
end
loader = RBS::EnvironmentLoader.new(core_root: core_root, repository: repo)
if core_root
options.libraries.each do |lib|
loader.add(library: lib)
end
else
# All library RBSs are loaded from vendor_dir
loader.add(path: options.vendor_dir)
end

loader.load(env: env)

env.resolve_type_names
Expand Down
7 changes: 2 additions & 5 deletions test/steepfile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def test_config
end
target :Gemfile, template: :gemfile do
vendor stdlib: nil, gems: "vendor/signatures/gems"
end
EOF

Expand All @@ -39,8 +38,7 @@ def test_config
assert_equal ["app/views"], target.ignore_patterns
assert_equal ["sig", "sig-private"], target.signature_patterns
assert_equal ["set", "strong_json"], target.options.libraries
assert_equal Pathname("vendor/sigs/stdlib"), target.options.vendored_stdlib_path
assert_equal Pathname("vendor/sigs/gems"), target.options.vendored_gems_path
assert_equal Pathname("vendor/sigs"), target.options.vendor_path
assert_equal false, target.options.allow_missing_definitions
end

Expand All @@ -50,8 +48,7 @@ def test_config
assert_equal [], target.ignore_patterns
assert_equal [], target.signature_patterns
assert_equal ["gemfile"], target.options.libraries
assert_nil target.options.vendored_stdlib_path
assert_equal Pathname("vendor/signatures/gems"), target.options.vendored_gems_path
assert_nil target.options.vendor_path
assert_equal true, target.options.allow_missing_definitions
end
end
Expand Down
6 changes: 2 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,8 @@ def with_factory(paths = {}, nostdlib: false)
absolute_path.write(content)
end

env_loader = RBS::EnvironmentLoader.new()
if nostdlib
env_loader.no_builtin!
end
core_root = nostdlib ? nil : RBS::EnvironmentLoader::DEFAULT_CORE_ROOT
env_loader = RBS::EnvironmentLoader.new(core_root: core_root)
env_loader.add path: root

env = RBS::Environment.new()
Expand Down

0 comments on commit 4f44a14

Please sign in to comment.