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

Fix RBS type definitions #1098

Merged
merged 16 commits into from
Aug 24, 2022
4 changes: 2 additions & 2 deletions lib/rbs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ class <<self
attr_reader :logger_output

def logger
@logger ||= Logger.new(logger_output || STDERR, level: logger_level || "warn", progname: "rbs")
@logger ||= Logger.new(logger_output || STDERR, level: logger_level || Logger::WARN, progname: "rbs")
end

def logger_output=(val)
@logger_output = val
@logger = nil
@logger_output = val
end

def logger_level=(level)
Expand Down
2 changes: 1 addition & 1 deletion lib/rbs/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ def run_test(args, options)
env_hash = {
'RUBYOPT' => "#{ENV['RUBYOPT']} -rrbs/test/setup",
'RBS_TEST_OPT' => test_opt(options),
'RBS_TEST_LOGLEVEL' => RBS.logger_level,
'RBS_TEST_LOGLEVEL' => %w(DEBUG INFO WARN ERROR FATAL)[RBS.logger_level || 5] || "UNKNOWN",
'RBS_TEST_SAMPLE_SIZE' => sample_size,
'RBS_TEST_DOUBLE_SUITE' => double_suite,
'RBS_TEST_UNCHECKED_CLASSES' => (unchecked_classes.join(',') unless unchecked_classes.empty?),
Expand Down
6 changes: 5 additions & 1 deletion lib/rbs/collection/config/lockfile_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def generate
end

private def assign_gem(name:, version:)
# @type var locked: gem_entry?
locked = lock&.gem(name)
specified = config.gem(name)

Expand All @@ -51,15 +52,18 @@ def generate

installed_version = version
best_version = find_best_version(version: installed_version, versions: source.versions({ 'name' => name }))

locked = {
'name' => name,
'version' => best_version.to_s,
'source' => source.to_lockfile,
}
end

locked or raise

upsert_gem specified, locked
source = Sources.from_config_entry(locked['source'])
source = Sources.from_config_entry(locked['source'] || raise)
source.dependencies_of(locked)&.each do |dep|
@gem_queue.push({ name: dep['name'], version: nil} )
end
Expand Down
6 changes: 3 additions & 3 deletions lib/rbs/constant_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ def resolve_constant_reference(name, context:)
name_to_constant(TypeName.new(name: head, namespace: Namespace.root))
else
resolve_constant_reference_context(head, context: context) ||
context.first.yield_self do |first_contet|
raise unless first_contet
resolve_constant_reference_inherit(head, scopes: constant_scopes(first_contet.to_type_name))
context.first.yield_self do |first_context|
raise unless first_context
resolve_constant_reference_inherit(head, scopes: constant_scopes(first_context.to_type_name))
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/rbs/definition_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -812,8 +812,10 @@ def merge_method(type_name, methods, name, method, sub, implemented_in: :keep, k
end
end

def try_cache(type_name, cache:, key: type_name)
def try_cache(type_name, cache:, key: nil)
# @type var cc: Hash[untyped, Definition | nil]
# @type var key: untyped
key ||= type_name
cc = _ = cache

cc[key] ||= yield
Expand Down
2 changes: 1 addition & 1 deletion lib/rbs/location_aux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def range
end

def source
@source ||= buffer.content[range] or raise
@source ||= (buffer.content[range] || raise)
end

def to_s
Expand Down
2 changes: 1 addition & 1 deletion lib/rbs/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Namespace

def initialize(path:, absolute:)
@path = path
@absolute = absolute
@absolute = absolute ? true : false
end

def self.empty
Expand Down
13 changes: 7 additions & 6 deletions lib/rbs/prototype/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def block_from_body(node)
yields.each do |yield_node|
array_content = yield_node.children[0]&.children&.compact || []

# @type var keywords: node?
positionals, keywords = if keyword_hash?(array_content.last)
[array_content.take(array_content.size - 1), array_content.last]
else
Expand Down Expand Up @@ -88,12 +89,12 @@ def any_node?(node, nodes: [], &block)
end

def keyword_hash?(node)
if node
if node.type == :HASH
node.children[0].children.compact.each_slice(2).all? {|key, _|
key.type == :LIT && key.children[0].is_a?(Symbol)
}
end
if node && node.type == :HASH
node.children[0].children.compact.each_slice(2).all? {|key, _|
key.type == :LIT && key.children[0].is_a?(Symbol)
}
else
false
end
end

Expand Down
13 changes: 7 additions & 6 deletions sig/annotation.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ module RBS
module AST
# RBS allows writing annotations of declarations and members.
#
# %a{annotation_here}
# class Hello
# %a{rbs:test:skip}
# def foo: () -> void
# end
#
# ```rbs
# %a{annotation_here}
# class Hello
# %a{rbs:test:skip}
# def foo: () -> void
# end
# ```
class Annotation
attr_reader string: String
attr_reader location: Location[untyped, untyped]?
Expand Down
2 changes: 2 additions & 0 deletions sig/builtin_names.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module RBS
class Name
attr_reader name: TypeName

@singleton_type: Types::t?

def initialize: (name: TypeName) -> void

def to_s: () -> String
Expand Down
16 changes: 12 additions & 4 deletions sig/collection/config.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ module RBS
attr_reader lock_path: Pathname
attr_reader gemfile_lock: Bundler::LockfileParser

type gem_queue_entry = { name: String, version: String? }

@gem_queue: Array[gem_queue_entry]

def self.generate: (config_path: Pathname, gemfile_lock_path: Pathname, ?with_lockfile: boolish) -> Config

def initialize: (config_path: Pathname, gemfile_lock_path: Pathname, with_lockfile: boolish) -> void
Expand All @@ -28,7 +32,7 @@ module RBS

def remove_ignored_gems!: () -> void

def find_source: (name: String) -> untyped
def find_source: (name: String) -> Sources::_Source?

def find_best_version: (version: String?, versions: Array[String]) -> Gem::Version
end
Expand All @@ -44,6 +48,10 @@ module RBS

@config_path: Pathname

@data: untyped

@sources: Array[Sources::_Source]

def self.generate_lockfile: (config_path: Pathname, gemfile_lock_path: Pathname, ?with_lockfile: boolish) -> Config

def self.from_path: (Pathname path) -> Config
Expand All @@ -55,17 +63,17 @@ module RBS
# config_path is necessary to resolve relative repo_path
def initialize: (untyped data, config_path: Pathname) -> void

def add_gem: (untyped gem) -> untyped
def add_gem: (gem_entry gem) -> void

def gem: (String gem_name) -> untyped
def gem: (String gem_name) -> gem_entry?

def repo_path: () -> Pathname

def sources: () -> Array[Sources::_Source]

def dump_to: (Pathname) -> void

def gems: () -> Array[untyped]
def gems: () -> Array[gem_entry]

def check_rbs_availability!: () -> void
end
Expand Down
2 changes: 2 additions & 0 deletions sig/collection/installer.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module RBS

private

@source_for: Hash[Sources::source_entry, Sources::_Source]

def source_for: (Config::gem_entry) -> Sources::_Source
end
end
Expand Down
9 changes: 7 additions & 2 deletions sig/collection/sources.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ module RBS
end

type source_entry = Git::source_entry
| Stdlib::source_entry
| Rubygems::source_entry
| Stdlib::source_entry
| Rubygems::source_entry

type manifest_entry = {
"dependencies" => Array[{"name" => String}]?,
}
Expand Down Expand Up @@ -57,6 +58,10 @@ module RBS

private

@git_dir: Pathname?

@resolved_revision: String?

def _install: (dest: Pathname , config_entry: Config::gem_entry) -> void

def cp_r: (Pathname, Pathname) -> void
Expand Down
4 changes: 4 additions & 0 deletions sig/environment.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ module RBS
attr_reader name: TypeName
attr_reader decls: Array[D[M]]

@primary: D[M]?

def initialize: (name: TypeName) -> void

def insert: (decl: M, outer: Array[module_decl]) -> void
Expand Down Expand Up @@ -115,6 +117,8 @@ module RBS

def inspect: () -> String

@buffers: Array[Buffer]

def buffers: () -> Array[Buffer]

def buffers_decls: () -> Hash[Buffer, Array[AST::Declarations::t]]
Expand Down
12 changes: 8 additions & 4 deletions sig/location.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ module RBS
#
# A location can have _child_ locations.
#
# ```
#
# ```
#
class Location[in RequiredChildKeys, in OptionalChildKeys]
# The buffer this location points on.
attr_reader buffer (): Buffer
Expand Down Expand Up @@ -42,13 +38,21 @@ module RBS

attr_reader start_loc (): Buffer::loc

@start_loc: Buffer::loc?

attr_reader end_loc (): Buffer::loc

@end_loc: Buffer::loc?

attr_reader range (): Range[Integer]

@range: Range[Integer]?

# A substring of buffer associated to the location.
def source: () -> String

@source: String?

def to_s: () -> String

# Returns a string representation suitable for terminal output.
Expand Down
2 changes: 1 addition & 1 deletion sig/members.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ module RBS

include _HashEqual

def update: (?name: Symbol, ?type: Types::t, ?ivar_name: Symbol | false | nil, ?kind: kind, ?annotations: Array[Annotation], ?location: loc?, ?comment: Comment?, ?visibility: visibility) -> instance
def update: (?name: Symbol, ?type: Types::t, ?ivar_name: Symbol | false | nil, ?kind: kind, ?annotations: Array[Annotation], ?location: loc?, ?comment: Comment?, ?visibility: visibility?) -> instance
end

class AttrReader < Base
Expand Down
Loading