Skip to content

Commit 974136e

Browse files
committed
Add annotations for empty collections
1 parent 4196a55 commit 974136e

File tree

13 files changed

+48
-36
lines changed

13 files changed

+48
-36
lines changed

lib/rbs/annotate/rdoc_source.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def load
3131
end
3232

3333
def find_class(typename)
34-
classes = []
34+
classes = [] #: Array[::RDoc::ClassModule]
3535

3636
@stores.each do |store|
3737
if klass = store.find_class_or_module(typename.relative!.to_s)

lib/rbs/cli.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -554,11 +554,11 @@ def run_prototype(args, options)
554554
when "rbi", "rb"
555555
run_prototype_file(format, args)
556556
when "runtime"
557-
require_libs = []
558-
relative_libs = []
557+
require_libs = [] #: Array[String]
558+
relative_libs = [] #: Array[String]
559559
merge = false
560560
todo = false
561-
owners_included = []
561+
owners_included = [] #: Array[Symbol]
562562
outline = false
563563
autoload = false
564564

@@ -591,7 +591,7 @@ def run_prototype(args, options)
591591
todo = true
592592
end
593593
opts.on("--method-owner CLASS", "Generate method prototypes if the owner of the method is [CLASS]") do |klass|
594-
owners_included << klass
594+
owners_included << klass.to_sym
595595
end
596596
opts.on("--outline", "Generates only module/class/constant declaration (no method definition)") do
597597
outline = true
@@ -615,9 +615,9 @@ def autoload(name, path)
615615
::Module.prepend(hook)
616616
::Kernel.prepend(hook)
617617

618-
arguments = []
618+
arguments = [] #: Array[[Module, interned]]
619619
TracePoint.new(:call) do |tp|
620-
base = tp.self.kind_of?(Module) ? tp.self : Kernel
620+
base = tp.self.kind_of?(Module) ? tp.self : Kernel #: Module
621621
name = (tp.binding or raise).local_variable_get(:name)
622622
arguments << [base, name]
623623
end.enable(target: hook.instance_method(:autoload), &block)
@@ -979,7 +979,7 @@ def run_annotate(args, options)
979979
end
980980

981981
def test_opt options
982-
opts = []
982+
opts = [] #: Array[String]
983983

984984
opts.push(*options.repos.map {|dir| "--repo #{Shellwords.escape(dir)}"})
985985
opts.push(*options.dirs.map {|dir| "-I #{Shellwords.escape(dir)}"})
@@ -1056,7 +1056,7 @@ def run_collection(args, options)
10561056
require 'bundler'
10571057

10581058
opts = collection_options(args)
1059-
params = {}
1059+
params = {} #: Hash[Symbol, untyped]
10601060
opts.order args.drop(1), into: params
10611061
config_path = options.config_path or raise
10621062
lock_path = Collection::Config.to_lockfile_path(config_path)

lib/rbs/cli/diff.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ def initialize(argv:, library_options:, stdout: $stdout, stderr: $stderr)
1111
# @type var type_name: String?
1212
type_name = nil
1313
library_options = library_options
14-
before_path = []
15-
after_path = []
14+
before_path = [] #: Array[String]
15+
after_path = [] #: Array[String]
1616
detail = false
1717

1818
opt = OptionParser.new do |o|

lib/rbs/collection/config.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ def sources
7272
end
7373

7474
def gems
75-
@data['gems'] ||= []
75+
@data['gems'] ||= (
76+
[] #: Array[gem_entry]
77+
)
7678
end
7779
end
7880
end

lib/rbs/definition_builder/ancestor_builder.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def one_instance_ancestors(type_name)
209209
super_args = super_class.args
210210
else
211211
super_name = BuiltinNames::Object.name
212-
super_args = []
212+
super_args = [] #: Array[Types::t]
213213
end
214214

215215
super_name = env.normalize_module_name(super_name)
@@ -525,7 +525,7 @@ def singleton_ancestors(type_name, building_ancestors: [])
525525

526526
one_ancestors = one_singleton_ancestors(type_name)
527527

528-
ancestors = []
528+
ancestors = [] #: Array[Definition::Ancestor::t]
529529

530530
case super_class = one_ancestors.super_class
531531
when Definition::Ancestor::Instance
@@ -581,7 +581,7 @@ def interface_ancestors(type_name, building_ancestors: [])
581581
building_ancestors.push self_ancestor
582582

583583
one_ancestors = one_interface_ancestors(type_name)
584-
ancestors = []
584+
ancestors = [] #: Array[Definition::Ancestor::t]
585585

586586
included_interfaces = one_ancestors.included_interfaces or raise
587587
included_interfaces.each do |a|

lib/rbs/environment_loader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Library < Struct.new(:name, :version, keyword_init: true)
2626
DEFAULT_CORE_ROOT = Pathname(_ = __dir__) + "../../core"
2727

2828
def self.gem_sig_path(name, version)
29-
requirements = []
29+
requirements = [] #: Array[String]
3030
requirements << version if version
3131
spec = Gem::Specification.find_by_name(name, *requirements)
3232
path = Pathname(spec.gem_dir) + "sig"

lib/rbs/parser_aux.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ def self.buffer(source)
7070
untyped
7171
true
7272
false
73-
).each_with_object({}) do |keyword, hash|
74-
hash[keyword] = nil
73+
).each_with_object({}) do |keyword, hash| #$ Hash[String, bot]
74+
hash[keyword] = _ = nil
7575
end
7676
end
7777
end

lib/rbs/prototype/rb.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def decls
7575
def parse(string)
7676
# @type var comments: Hash[Integer, AST::Comment]
7777
comments = Ripper.lex(string).yield_self do |tokens|
78-
code_lines = {}
79-
tokens.each.with_object({}) do |token, hash|
78+
code_lines = {} #: Hash[Integer, bool]
79+
tokens.each.with_object({}) do |token, hash| #$ Hash[Integer, AST::Comment]
8080
case token[1]
8181
when :on_sp, :on_ignored_nl
8282
# skip
@@ -643,11 +643,11 @@ def literal_to_type(node)
643643
children = list.children
644644
children.pop
645645
else
646-
children = []
646+
children = [] #: Array[untyped]
647647
end
648648

649-
key_types = []
650-
value_types = []
649+
key_types = [] #: Array[Types::t]
650+
value_types = [] #: Array[Types::t]
651651
children.each_slice(2) do |k, v|
652652
if k
653653
key_types << literal_to_type(k)
@@ -659,7 +659,10 @@ def literal_to_type(node)
659659
end
660660

661661
if !key_types.empty? && key_types.all? { |t| t.is_a?(Types::Literal) }
662-
fields = key_types.map { |t| t.literal }.zip(value_types).to_h
662+
fields = key_types.map {|t|
663+
t.is_a?(Types::Literal) or raise
664+
t.literal
665+
}.zip(value_types).to_h #: Hash[Types::Literal::literal, Types::t]
663666
Types::Record.new(fields: fields, location: nil)
664667
else
665668
key_type = types_to_union_type(key_types)

lib/rbs/prototype/rbi.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ def initialize
1717

1818
def parse(string)
1919
comments = Ripper.lex(string).yield_self do |tokens|
20-
tokens.each.with_object({}) do |token, hash|
21-
# @type var hash: Hash[Integer, AST::Comment]
22-
20+
tokens.each.with_object({}) do |token, hash| #$ Hash[Integer, AST::Comment]
2321
if token[1] == :on_comment
2422
line = token[0][0]
2523
body = token[2][2..-1] or raise
@@ -325,11 +323,17 @@ def method_type(args_node, type_node, variables:, overloads:)
325323
end
326324
end
327325
when :type_parameters
328-
type_params = []
326+
type_params = [] #: Array[AST::TypeParam]
329327

330328
each_arg args do |node|
331329
if name = symbol_literal_node?(node)
332-
type_params << name
330+
type_params << AST::TypeParam.new(
331+
name: name,
332+
variance: :invariant,
333+
upper_bound: nil,
334+
location: nil,
335+
default_type: nil
336+
)
333337
end
334338
end
335339

lib/rbs/prototype/runtime.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ def skip_constant?(module_name:, name:)
4848

4949
def mixin_decls(type_name)
5050
type_name_absolute = type_name.absolute!
51-
(@mixin_decls_cache ||= {}).fetch(type_name_absolute) do
51+
@mixin_decls_cache ||= {} #: Hash[TypeName, Array[AST::Members::Mixin]]
52+
@mixin_decls_cache.fetch(type_name_absolute) do
5253
@mixin_decls_cache[type_name_absolute] = @builder.env.class_decls[type_name_absolute].decls.flat_map do |d|
5354
d.decl.members.select { |m| m.kind_of?(AST::Members::Mixin) }
5455
end

0 commit comments

Comments
 (0)