Skip to content

Commit ceb76ef

Browse files
authored
Migrate T.cast to RBS comment syntax (#3440)
1 parent 9cb36e7 commit ceb76ef

33 files changed

Lines changed: 163 additions & 175 deletions

lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def on_singleton_class_node_enter(node)
132132
name = (expression.is_a?(Prism::SelfNode) ? "<Class:#{last_name_in_stack}>" : "<Class:#{expression.slice}>")
133133
real_nesting = Index.actual_nesting(@stack, name)
134134

135-
existing_entries = T.cast(@index[real_nesting.join("::")], T.nilable(T::Array[Entry::SingletonClass]))
135+
existing_entries = @index[real_nesting.join("::")] #: as Array[Entry::SingletonClass]?
136136

137137
if existing_entries
138138
entry = existing_entries.first #: as !nil
@@ -903,10 +903,9 @@ def handle_private_class_method(node)
903903

904904
# private_class_method accepts strings, symbols or arrays of strings and symbols as arguments. Here we build a
905905
# single list of all of the method names that have to be made private
906-
arrays, others = T.cast(
907-
arguments.partition { |argument| argument.is_a?(Prism::ArrayNode) },
908-
[T::Array[Prism::ArrayNode], T::Array[Prism::Node]],
909-
)
906+
arrays, others = arguments.partition do |argument|
907+
argument.is_a?(Prism::ArrayNode)
908+
end #: as [Array[Prism::ArrayNode], Array[Prism::Node]]
910909
arrays.each { |array| others.concat(array.elements) }
911910

912911
names = others.filter_map do |argument|

lib/ruby_indexer/lib/ruby_indexer/enhancement.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33

44
module RubyIndexer
55
class Enhancement
6-
extend T::Sig
76
extend T::Helpers
87

98
abstract!
109

1110
@enhancements = [] #: Array[Class[Enhancement]]
1211

1312
class << self
14-
extend T::Sig
15-
1613
#: (Class[Enhancement] child) -> void
1714
def inherited(child)
1815
@enhancements << child

lib/ruby_indexer/lib/ruby_indexer/entry.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ def comments
107107
end
108108

109109
class ModuleOperation
110-
extend T::Sig
111110
extend T::Helpers
112111

113112
abstract!
@@ -125,7 +124,6 @@ class Include < ModuleOperation; end
125124
class Prepend < ModuleOperation; end
126125

127126
class Namespace < Entry
128-
extend T::Sig
129127
extend T::Helpers
130128

131129
abstract!
@@ -203,7 +201,6 @@ class Constant < Entry
203201

204202
class Parameter
205203
extend T::Helpers
206-
extend T::Sig
207204

208205
abstract!
209206

@@ -563,7 +560,8 @@ def matches?(arguments)
563560
end
564561

565562
keyword_hash_nodes, positional_args = arguments.partition { |arg| arg.is_a?(Prism::KeywordHashNode) }
566-
keyword_args = T.cast(keyword_hash_nodes.first, T.nilable(Prism::KeywordHashNode))&.elements
563+
keyword_args = keyword_hash_nodes.first #: as Prism::KeywordHashNode?
564+
&.elements
567565
forwarding_arguments, positionals = positional_args.partition do |arg|
568566
arg.is_a?(Prism::ForwardingArgumentsNode)
569567
end

lib/ruby_indexer/lib/ruby_indexer/index.rb

Lines changed: 30 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def initialize
6060
@entries = {} #: Hash[String, Array[Entry]]
6161

6262
# Holds all entries in the index using a prefix tree for searching based on prefixes to provide autocompletion
63-
@entries_tree = PrefixTree[T::Array[Entry]].new #: PrefixTree[Array[Entry]]
63+
@entries_tree = PrefixTree.new #: PrefixTree[Array[Entry]]
6464

6565
# Holds references to where entries where discovered so that we can easily delete them
6666
# {
@@ -71,7 +71,7 @@ def initialize
7171
@uris_to_entries = {} #: Hash[String, Array[Entry]]
7272

7373
# Holds all require paths for every indexed item so that we can provide autocomplete for requires
74-
@require_paths_tree = PrefixTree[URI::Generic].new #: PrefixTree[URI::Generic]
74+
@require_paths_tree = PrefixTree.new #: PrefixTree[URI::Generic]
7575

7676
# Holds the linearized ancestors list for every namespace
7777
@ancestors = {} #: Hash[String, Array[String]]
@@ -147,7 +147,7 @@ def search_require_paths(query)
147147

148148
# Searches for a constant based on an unqualified name and returns the first possible match regardless of whether
149149
# there are more possible matching entries
150-
#: (String name) -> Array[(Entry::Namespace | Entry::ConstantAlias | Entry::UnresolvedConstantAlias | Entry::Constant)]?
150+
#: (String name) -> Array[Entry::Constant | Entry::ConstantAlias | Entry::Namespace | Entry::UnresolvedConstantAlias]?
151151
def first_unqualified_const(name)
152152
# Look for an exact match first
153153
_name, entries = @entries.find do |const_name, _entries|
@@ -161,15 +161,7 @@ def first_unqualified_const(name)
161161
end
162162
end
163163

164-
T.cast(
165-
entries,
166-
T.nilable(T::Array[T.any(
167-
Entry::Namespace,
168-
Entry::ConstantAlias,
169-
Entry::UnresolvedConstantAlias,
170-
Entry::Constant,
171-
)]),
172-
)
164+
entries #: as Array[Entry::Constant | Entry::ConstantAlias | Entry::Namespace | Entry::UnresolvedConstantAlias]?
173165
end
174166

175167
# Searches entries in the index based on an exact prefix, intended for providing autocomplete. All possible matches
@@ -272,19 +264,11 @@ def method_completion_candidates(name, receiver_name)
272264
completion_items.values.map!(&:first)
273265
end
274266

275-
#: (String name, Array[String] nesting) -> Array[Array[(Entry::Constant | Entry::ConstantAlias | Entry::Namespace | Entry::UnresolvedConstantAlias)]]
267+
#: (String name, Array[String] nesting) -> Array[Array[Entry::Constant | Entry::ConstantAlias | Entry::Namespace | Entry::UnresolvedConstantAlias]]
276268
def constant_completion_candidates(name, nesting)
277269
# If we have a top level reference, then we don't need to include completions inside the current nesting
278270
if name.start_with?("::")
279-
return T.cast(
280-
@entries_tree.search(name.delete_prefix("::")),
281-
T::Array[T::Array[T.any(
282-
Entry::Constant,
283-
Entry::ConstantAlias,
284-
Entry::Namespace,
285-
Entry::UnresolvedConstantAlias,
286-
)]],
287-
)
271+
return @entries_tree.search(name.delete_prefix("::")) #: as Array[Array[Entry::Constant | Entry::ConstantAlias | Entry::Namespace | Entry::UnresolvedConstantAlias]] # rubocop:disable Layout/LineLength
288272
end
289273

290274
# Otherwise, we have to include every possible constant the user might be referring to. This is essentially the
@@ -310,15 +294,7 @@ def constant_completion_candidates(name, nesting)
310294
# Top level constants
311295
entries.concat(@entries_tree.search(name))
312296
entries.uniq!
313-
T.cast(
314-
entries,
315-
T::Array[T::Array[T.any(
316-
Entry::Constant,
317-
Entry::ConstantAlias,
318-
Entry::Namespace,
319-
Entry::UnresolvedConstantAlias,
320-
)]],
321-
)
297+
entries #: as Array[Array[Entry::Constant | Entry::ConstantAlias | Entry::Namespace | Entry::UnresolvedConstantAlias]] # rubocop:disable Layout/LineLength
322298
end
323299

324300
# Resolve a constant to its declaration based on its name and the nesting where the reference was found. Parameter
@@ -328,7 +304,7 @@ def constant_completion_candidates(name, nesting)
328304
# nesting: the nesting structure where the reference was found (e.g.: ["Foo", "Bar"])
329305
# seen_names: this parameter should not be used by consumers of the api. It is used to avoid infinite recursion when
330306
# resolving circular references
331-
#: (String name, Array[String] nesting, ?Array[String] seen_names) -> Array[(Entry::Namespace | Entry::ConstantAlias | Entry::UnresolvedConstantAlias)]?
307+
#: (String name, Array[String] nesting, ?Array[String] seen_names) -> Array[Entry::Constant | Entry::ConstantAlias | Entry::Namespace | Entry::UnresolvedConstantAlias]?
332308
def resolve(name, nesting, seen_names = [])
333309
# If we have a top level reference, then we just search for it straight away ignoring the nesting
334310
if name.start_with?("::")
@@ -586,7 +562,7 @@ def linearized_ancestors_of(fully_qualified_name)
586562
# and find inherited instance variables as well
587563
#: (String variable_name, String owner_name) -> Array[Entry::InstanceVariable]?
588564
def resolve_instance_variable(variable_name, owner_name)
589-
entries = T.cast(self[variable_name], T.nilable(T::Array[Entry::InstanceVariable]))
565+
entries = self[variable_name] #: as Array[Entry::InstanceVariable]?
590566
return unless entries
591567

592568
ancestors = linearized_ancestors_of(owner_name)
@@ -610,7 +586,7 @@ def resolve_class_variable(variable_name, owner_name)
610586
# include the `@` prefix
611587
#: (String name, String owner_name) -> Array[(Entry::InstanceVariable | Entry::ClassVariable)]
612588
def instance_variable_completion_candidates(name, owner_name)
613-
entries = T.cast(prefix_search(name).flatten, T::Array[T.any(Entry::InstanceVariable, Entry::ClassVariable)])
589+
entries = prefix_search(name).flatten #: as Array[Entry::InstanceVariable | Entry::ClassVariable]
614590
# Avoid wasting time linearizing ancestors if we didn't find anything
615591
return entries if entries.empty?
616592

@@ -640,7 +616,7 @@ def instance_variable_completion_candidates(name, owner_name)
640616

641617
#: (String name, String owner_name) -> Array[Entry::ClassVariable]
642618
def class_variable_completion_candidates(name, owner_name)
643-
entries = T.cast(prefix_search(name).flatten, T::Array[Entry::ClassVariable])
619+
entries = prefix_search(name).flatten #: as Array[Entry::ClassVariable]
644620
# Avoid wasting time linearizing ancestors if we didn't find anything
645621
return entries if entries.empty?
646622

@@ -676,15 +652,13 @@ def handle_change(uri, source = nil, &block)
676652
# indirect means like including a module that than includes the ancestor. Trying to figure out exactly which
677653
# ancestors need to be deleted is too expensive. Therefore, if any of the namespace entries has a change to their
678654
# ancestor hash, we clear all ancestors and start linearizing lazily again from scratch
679-
original_map = T.cast(
680-
original_entries.select { |e| e.is_a?(Entry::Namespace) },
681-
T::Array[Entry::Namespace],
682-
).to_h { |e| [e.name, e.ancestor_hash] }
655+
original_map = original_entries
656+
.select { |e| e.is_a?(Entry::Namespace) } #: as Array[Entry::Namespace]
657+
.to_h { |e| [e.name, e.ancestor_hash] }
683658

684-
updated_map = T.cast(
685-
updated_entries.select { |e| e.is_a?(Entry::Namespace) },
686-
T::Array[Entry::Namespace],
687-
).to_h { |e| [e.name, e.ancestor_hash] }
659+
updated_map = updated_entries
660+
.select { |e| e.is_a?(Entry::Namespace) } #: as Array[Entry::Namespace]
661+
.to_h { |e| [e.name, e.ancestor_hash] }
688662

689663
@ancestors.clear if original_map.any? { |name, hash| updated_map[name] != hash }
690664
end
@@ -713,7 +687,7 @@ def length
713687
def existing_or_new_singleton_class(name)
714688
*_namespace, unqualified_name = name.split("::")
715689
full_singleton_name = "#{name}::<Class:#{unqualified_name}>"
716-
singleton = T.cast(self[full_singleton_name]&.first, T.nilable(Entry::SingletonClass))
690+
singleton = self[full_singleton_name]&.first #: as Entry::SingletonClass?
717691

718692
unless singleton
719693
attached_ancestor = self[name]&.first #: as !nil
@@ -841,14 +815,11 @@ def linearize_superclass( # rubocop:disable Metrics/ParameterLists
841815
)
842816
# Find the first class entry that has a parent class. Notice that if the developer makes a mistake and inherits
843817
# from two different classes in different files, we simply ignore it
844-
superclass = T.cast(
845-
if singleton_levels > 0
846-
self[attached_class_name]&.find { |n| n.is_a?(Entry::Class) && n.parent_class }
847-
else
848-
namespace_entries.find { |n| n.is_a?(Entry::Class) && n.parent_class }
849-
end,
850-
T.nilable(Entry::Class),
851-
)
818+
superclass = if singleton_levels > 0
819+
self[attached_class_name]&.find { |n| n.is_a?(Entry::Class) && n.parent_class }
820+
else
821+
namespace_entries.find { |n| n.is_a?(Entry::Class) && n.parent_class }
822+
end #: as Entry::Class?
852823

853824
if superclass
854825
# If the user makes a mistake and creates a class that inherits from itself, this method would throw a stack
@@ -882,7 +853,7 @@ def linearize_superclass( # rubocop:disable Metrics/ParameterLists
882853
elsif singleton_levels > 0
883854
# When computing the linearization for a module's singleton class, it inherits from the linearized ancestors of
884855
# the `Module` class
885-
mod = T.cast(self[attached_class_name]&.find { |n| n.is_a?(Entry::Module) }, T.nilable(Entry::Module))
856+
mod = self[attached_class_name]&.find { |n| n.is_a?(Entry::Module) } #: as Entry::Module?
886857

887858
if mod
888859
module_class_name_parts = ["Module"]
@@ -922,7 +893,7 @@ def resolve_alias(entry, seen_names)
922893
resolved_alias
923894
end
924895

925-
#: (String name, Array[String] nesting, Array[String] seen_names) -> Array[(Entry::Namespace | Entry::ConstantAlias | Entry::UnresolvedConstantAlias)]?
896+
#: (String name, Array[String] nesting, Array[String] seen_names) -> Array[Entry::Constant | Entry::ConstantAlias | Entry::Namespace | Entry::UnresolvedConstantAlias]?
926897
def lookup_enclosing_scopes(name, nesting, seen_names)
927898
nesting.length.downto(1) do |i|
928899
namespace = nesting[0...i] #: as !nil
@@ -942,7 +913,7 @@ def lookup_enclosing_scopes(name, nesting, seen_names)
942913
nil
943914
end
944915

945-
#: (String name, Array[String] nesting, Array[String] seen_names) -> Array[(Entry::Namespace | Entry::ConstantAlias | Entry::UnresolvedConstantAlias)]?
916+
#: (String name, Array[String] nesting, Array[String] seen_names) -> Array[Entry::Constant | Entry::ConstantAlias | Entry::Namespace | Entry::UnresolvedConstantAlias]?
946917
def lookup_ancestor_chain(name, nesting, seen_names)
947918
*nesting_parts, constant_name = build_non_redundant_full_name(name, nesting).split("::")
948919
return if nesting_parts.empty?
@@ -1027,18 +998,13 @@ def build_non_redundant_full_name(name, nesting)
1027998
name_parts.join("::")
1028999
end
10291000

1030-
#: (String full_name, Array[String] seen_names) -> Array[(Entry::Namespace | Entry::ConstantAlias | Entry::UnresolvedConstantAlias)]?
1001+
#: (String full_name, Array[String] seen_names) -> Array[Entry::Constant | Entry::ConstantAlias | Entry::Namespace | Entry::UnresolvedConstantAlias]?
10311002
def direct_or_aliased_constant(full_name, seen_names)
10321003
entries = @entries[full_name] || @entries[follow_aliased_namespace(full_name)]
10331004

1034-
T.cast(
1035-
entries&.map { |e| e.is_a?(Entry::UnresolvedConstantAlias) ? resolve_alias(e, seen_names) : e },
1036-
T.nilable(T::Array[T.any(
1037-
Entry::Namespace,
1038-
Entry::ConstantAlias,
1039-
Entry::UnresolvedConstantAlias,
1040-
)]),
1041-
)
1005+
entries&.map do |e|
1006+
e.is_a?(Entry::UnresolvedConstantAlias) ? resolve_alias(e, seen_names) : e
1007+
end #: as Array[Entry::Constant | Entry::ConstantAlias | Entry::Namespace | Entry::UnresolvedConstantAlias])?
10421008
end
10431009

10441010
# Attempt to resolve a given unresolved method alias. This method returns the resolved alias if we managed to

lib/ruby_indexer/test/rbs_indexer_test.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,9 @@ def parse_rbs_methods(rbs, method_name)
373373
pathname = Pathname.new("/file.rbs")
374374
indexer.process_signature(pathname, declarations)
375375
entry = index[method_name] #: as !nil
376-
.first
377-
T.cast(entry, Entry::Method).signatures
376+
.first #: as Entry::Method
377+
378+
entry.signatures
378379
end
379380
end
380381
end

lib/ruby_lsp/listeners/document_link.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,13 @@ def extract_document_link(node)
105105
match = comment.location.slice.match(%r{source://.*#\d+$})
106106
return unless match
107107

108-
uri = T.cast(
109-
begin
110-
URI(
111-
match[0], #: as !nil
112-
)
113-
rescue URI::Error
114-
nil
115-
end,
116-
T.nilable(URI::Source),
117-
)
108+
uri = begin
109+
URI(
110+
match[0], #: as !nil
111+
)
112+
rescue URI::Error
113+
nil
114+
end #: as URI::Source?
118115
return unless uri
119116

120117
gem_version = resolve_version(uri)

lib/ruby_lsp/listeners/spec_style.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
module RubyLsp
55
module Listeners
66
class SpecStyle < TestDiscovery
7-
extend T::Sig
8-
97
#: (response_builder: ResponseBuilders::TestCollection, global_state: GlobalState, dispatcher: Prism::Dispatcher, uri: URI::Generic) -> void
108
def initialize(response_builder, global_state, dispatcher, uri)
119
super

lib/ruby_lsp/requests/code_action_resolve.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -365,17 +365,7 @@ def create_attribute_accessor
365365
end
366366
end
367367

368-
node = T.cast(
369-
node,
370-
T.any(
371-
Prism::InstanceVariableAndWriteNode,
372-
Prism::InstanceVariableOperatorWriteNode,
373-
Prism::InstanceVariableOrWriteNode,
374-
Prism::InstanceVariableReadNode,
375-
Prism::InstanceVariableTargetNode,
376-
Prism::InstanceVariableWriteNode,
377-
),
378-
)
368+
node = node #: as Prism::InstanceVariableAndWriteNode | Prism::InstanceVariableOperatorWriteNode | Prism::InstanceVariableOrWriteNode | Prism::InstanceVariableReadNode | Prism::InstanceVariableTargetNode | Prism::InstanceVariableWriteNode # rubocop:disable Layout/LineLength
379369

380370
node_context = @document.locate_node(
381371
{

lib/ruby_lsp/requests/code_lens.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def provider
2020

2121
#: (GlobalState global_state, URI::Generic uri, Prism::Dispatcher dispatcher) -> void
2222
def initialize(global_state, uri, dispatcher)
23-
@response_builder = ResponseBuilders::CollectionResponseBuilder[Interface::CodeLens]
23+
@response_builder = ResponseBuilders::CollectionResponseBuilder
2424
.new #: ResponseBuilders::CollectionResponseBuilder[Interface::CodeLens]
2525
super()
2626
Listeners::CodeLens.new(@response_builder, global_state, uri, dispatcher)

lib/ruby_lsp/requests/completion.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def initialize(document, global_state, params, sorbet_level, dispatcher)
6060
],
6161
code_units_cache: document.code_units_cache,
6262
)
63-
@response_builder = ResponseBuilders::CollectionResponseBuilder[Interface::CompletionItem]
63+
@response_builder = ResponseBuilders::CollectionResponseBuilder
6464
.new #: ResponseBuilders::CollectionResponseBuilder[Interface::CompletionItem]
6565

6666
Listeners::Completion.new(

0 commit comments

Comments
 (0)