Skip to content

Replace T.must with comment based syntax #3400

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 8 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ GEM
simplecov_json_formatter (~> 0.1)
simplecov-html (0.13.1)
simplecov_json_formatter (0.1.4)
sorbet (0.5.11965)
sorbet-static (= 0.5.11965)
sorbet-runtime (0.5.11965)
sorbet-static (0.5.11965-universal-darwin)
sorbet-static (0.5.11965-x86_64-linux)
sorbet-static-and-runtime (0.5.11965)
sorbet (= 0.5.11965)
sorbet-runtime (= 0.5.11965)
sorbet (0.5.12017)
sorbet-static (= 0.5.12017)
sorbet-runtime (0.5.12017)
sorbet-static (0.5.12017-universal-darwin)
sorbet-static (0.5.12017-x86_64-linux)
sorbet-static-and-runtime (0.5.12017)
sorbet (= 0.5.12017)
sorbet-runtime (= 0.5.12017)
spoom (1.6.1)
erubi (>= 1.10.0)
prism (>= 0.28.0)
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_indexer/lib/ruby_indexer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def indexable_uris
# Remove user specified patterns
bundle_path = Bundler.settings["path"]&.gsub(/[\\]+/, "/")
uris.reject! do |indexable|
path = T.must(indexable.full_path)
path = indexable.full_path #: as !nil
next false if test_files_ignored_from_exclusion?(path, bundle_path)

excluded_patterns.any? { |pattern| File.fnmatch?(pattern, path, flags) }
Expand Down
9 changes: 6 additions & 3 deletions lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def on_singleton_class_node_enter(node)
existing_entries = T.cast(@index[real_nesting.join("::")], T.nilable(T::Array[Entry::SingletonClass]))

if existing_entries
entry = T.must(existing_entries.first)
entry = existing_entries.first #: as !nil
entry.update_singleton_information(
Location.from_prism_location(node.location, @code_units_cache),
Location.from_prism_location(expression.location, @code_units_cache),
Expand Down Expand Up @@ -509,7 +509,10 @@ def add_class(name_or_nesting, full_location, name_location, parent_class_name:
parent_class_name,
)

advance_namespace_stack(T.must(nesting.last), entry)
advance_namespace_stack(
nesting.last, #: as !nil
entry,
)
end

#: { (Index index, Entry::Namespace base) -> void } -> void
Expand Down Expand Up @@ -927,7 +930,7 @@ def handle_private_class_method(node)

#: -> VisibilityScope
def current_visibility_scope
T.must(@visibility_stack.last)
@visibility_stack.last #: as !nil
end

#: (Prism::ParametersNode? parameters_node) -> Array[Entry::Parameter]
Expand Down
6 changes: 4 additions & 2 deletions lib/ruby_indexer/lib/ruby_indexer/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ def private?
#: -> String
def file_name
if @uri.scheme == "untitled"
T.must(@uri.opaque)
@uri.opaque #: as !nil
else
File.basename(T.must(file_path))
File.basename(
file_path, #: as !nil
)
end
end

Expand Down
84 changes: 56 additions & 28 deletions lib/ruby_indexer/lib/ruby_indexer/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,13 @@ def add(entry, skip_prefix_tree: false)

(@entries[name] ||= []) << entry
(@uris_to_entries[entry.uri.to_s] ||= []) << entry
@entries_tree.insert(name, T.must(@entries[name])) unless skip_prefix_tree

unless skip_prefix_tree
@entries_tree.insert(
name,
@entries[name], #: as !nil
)
end
end

#: (String fully_qualified_name) -> Array[Entry]?
Expand Down Expand Up @@ -188,7 +194,8 @@ def prefix_search(query, nesting = nil)
end

results = nesting.length.downto(0).flat_map do |i|
prefix = T.must(nesting[0...i]).join("::")
prefix = nesting[0...i] #: as !nil
.join("::")
namespaced_query = prefix.empty? ? query : "#{prefix}::#{query}"
@entries_tree.search(namespaced_query)
end
Expand Down Expand Up @@ -288,7 +295,8 @@ def constant_completion_candidates(name, nesting)

# Constants defined in enclosing scopes
nesting.length.downto(1) do |i|
namespace = T.must(nesting[0...i]).join("::")
namespace = nesting[0...i] #: as !nil
.join("::")
entries.concat(@entries_tree.search("#{namespace}::#{name}"))
end

Expand Down Expand Up @@ -406,7 +414,8 @@ def index_single(uri, source, collect_comments: true)
# Indexes a File URI by reading the contents from disk
#: (URI::Generic uri, ?collect_comments: bool) -> void
def index_file(uri, collect_comments: true)
index_single(uri, File.read(T.must(uri.full_path)), collect_comments: collect_comments)
path = uri.full_path #: as !nil
index_single(uri, File.read(path), collect_comments: collect_comments)
rescue Errno::EISDIR, Errno::ENOENT
# If `path` is a directory, just ignore it and continue indexing. If the file doesn't exist, then we also ignore
# it
Expand All @@ -428,7 +437,8 @@ def follow_aliased_namespace(name, seen_names = [])
real_parts = []

(parts.length - 1).downto(0) do |i|
current_name = T.must(parts[0..i]).join("::")
current_name = parts[0..i] #: as !nil
.join("::")
entry = @entries[current_name]&.first

case entry
Expand All @@ -445,7 +455,9 @@ def follow_aliased_namespace(name, seen_names = [])
target = resolved.target
return follow_aliased_namespace("#{target}::#{real_parts.join("::")}", seen_names)
else
real_parts.unshift(T.must(parts[i]))
real_parts.unshift(
parts[i], #: as !nil
)
end
end

Expand Down Expand Up @@ -542,11 +554,12 @@ def linearized_ancestors_of(fully_qualified_name)

# The original nesting where we discovered this namespace, so that we resolve the correct names of the
# included/prepended/extended modules and parent classes
nesting = T.must(namespaces.first).nesting.flat_map { |n| n.split("::") }
nesting = namespaces.first #: as !nil
.nesting.flat_map { |n| n.split("::") }

if nesting.any?
singleton_levels.times do
nesting << "<Class:#{T.must(nesting.last)}>"
nesting << "<Class:#{nesting.last}>"
end
end

Expand Down Expand Up @@ -612,7 +625,8 @@ def instance_variable_completion_candidates(name, owner_name)
name_parts = owner_name.split("::")

if name_parts.last&.start_with?("<Class:")
attached_name = T.must(name_parts[0..-2]).join("::")
attached_name = name_parts[0..-2] #: as !nil
.join("::")
attached_ancestors = linearized_ancestors_of(attached_name)
variables.concat(class_variables.select { |e| attached_ancestors.any?(e.owner&.name) })
else
Expand Down Expand Up @@ -649,7 +663,10 @@ def handle_change(uri, source = nil, &block)
block.call(self)
else
delete(uri)
index_single(uri, T.must(source))
index_single(
uri,
source, #: as !nil
)
end

updated_entries = @uris_to_entries[key]
Expand Down Expand Up @@ -699,7 +716,7 @@ def existing_or_new_singleton_class(name)
singleton = T.cast(self[full_singleton_name]&.first, T.nilable(Entry::SingletonClass))

unless singleton
attached_ancestor = T.must(self[name]&.first)
attached_ancestor = self[name]&.first #: as !nil

singleton = Entry::SingletonClass.new(
[full_singleton_name],
Expand Down Expand Up @@ -732,7 +749,8 @@ def linearized_attached_ancestors(name)
name_parts = name.split("::")

if name_parts.last&.start_with?("<Class:")
attached_name = T.must(name_parts[0..-2]).join("::")
attached_name = name_parts[0..-2] #: as !nil
.join("::")
linearized_ancestors_of(attached_name)
else
linearized_ancestors_of(name)
Expand All @@ -755,7 +773,8 @@ def run_included_hooks(fully_qualified_name, nesting)
resolved_modules = resolve(operation.module_name, nesting)
next unless resolved_modules

module_name = T.must(resolved_modules.first).name
module_name = resolved_modules.first #: as !nil
.name

# Then we grab any hooks registered for that module
hooks = @included_hooks[module_name]
Expand All @@ -778,7 +797,8 @@ def linearize_mixins(ancestors, namespace_entries, nesting)
resolved_module = resolve(operation.module_name, nesting)
next unless resolved_module

module_fully_qualified_name = T.must(resolved_module.first).name
module_fully_qualified_name = resolved_module.first #: as !nil
.name

case operation
when Entry::Prepend
Expand All @@ -790,13 +810,11 @@ def linearize_mixins(ancestors, namespace_entries, nesting)
# When there are duplicate prepended modules, we have to insert the new prepends after the existing ones. For
# example, if the current ancestors are `["A", "Foo"]` and we try to prepend `["A", "B"]`, then `"B"` has to
# be inserted after `"A`
uniq_prepends = linearized_prepends - T.must(ancestors[0...main_namespace_index])
prepended_ancestors = ancestors[0...main_namespace_index] #: as !nil
uniq_prepends = linearized_prepends - prepended_ancestors
insert_position = linearized_prepends.length - uniq_prepends.length

T.unsafe(ancestors).insert(
insert_position,
*(linearized_prepends - T.must(ancestors[0...main_namespace_index])),
)
T.unsafe(ancestors).insert(insert_position, *uniq_prepends)

main_namespace_index += linearized_prepends.length
when Entry::Include
Expand Down Expand Up @@ -833,7 +851,7 @@ def linearize_superclass( # rubocop:disable Metrics/ParameterLists
if superclass
# If the user makes a mistake and creates a class that inherits from itself, this method would throw a stack
# error. We need to ensure that this isn't the case
parent_class = T.must(superclass.parent_class)
parent_class = superclass.parent_class #: as !nil

resolved_parent_class = resolve(parent_class, nesting)
parent_class_name = resolved_parent_class&.first&.name
Expand Down Expand Up @@ -888,11 +906,12 @@ def resolve_alias(entry, seen_names)
target = resolve(entry.target, entry.nesting, seen_names)
return entry unless target

target_name = T.must(target.first).name
target_name = target.first #: as !nil
.name
resolved_alias = Entry::ConstantAlias.new(target_name, entry)

# Replace the UnresolvedAlias by a resolved one so that we don't have to do this again later
original_entries = T.must(@entries[alias_name])
original_entries = @entries[alias_name] #: as !nil
original_entries.delete(entry)
original_entries << resolved_alias

Expand All @@ -904,7 +923,8 @@ def resolve_alias(entry, seen_names)
#: (String name, Array[String] nesting, Array[String] seen_names) -> Array[(Entry::Namespace | Entry::ConstantAlias | Entry::UnresolvedConstantAlias)]?
def lookup_enclosing_scopes(name, nesting, seen_names)
nesting.length.downto(1) do |i|
namespace = T.must(nesting[0...i]).join("::")
namespace = nesting[0...i] #: as !nil
.join("::")

# If we find an entry with `full_name` directly, then we can already return it, even if it contains aliases -
# because the user might be trying to jump to the alias definition.
Expand All @@ -928,7 +948,9 @@ def lookup_ancestor_chain(name, nesting, seen_names)
namespace_entries = resolve(nesting_parts.join("::"), [], seen_names)
return unless namespace_entries

ancestors = nesting_parts.empty? ? [] : linearized_ancestors_of(T.must(namespace_entries.first).name)
namespace_name = namespace_entries.first #: as !nil
.name
ancestors = nesting_parts.empty? ? [] : linearized_ancestors_of(namespace_name)

ancestors.each do |ancestor_name|
entries = direct_or_aliased_constant("#{ancestor_name}::#{constant_name}", seen_names)
Expand All @@ -952,15 +974,18 @@ def inherited_constant_completion_candidates(name, nesting)
end
return [] unless namespace_entries

ancestors = linearized_ancestors_of(T.must(namespace_entries.first).name)
namespace_name = namespace_entries.first #: as !nil
.name
ancestors = linearized_ancestors_of(namespace_name)
candidates = ancestors.flat_map do |ancestor_name|
@entries_tree.search("#{ancestor_name}::#{constant_name}")
end

# For candidates with the same name, we must only show the first entry in the inheritance chain, since that's the
# one the user will be referring to in completion
completion_items = candidates.each_with_object({}) do |entries, hash|
*parts, short_name = T.must(entries.first).name.split("::")
*parts, short_name = entries.first #: as !nil
.name.split("::")
namespace_name = parts.join("::")
ancestor_index = ancestors.index(namespace_name)
existing_entry, existing_entry_index = hash[short_name]
Expand Down Expand Up @@ -1027,8 +1052,11 @@ def resolve_method_alias(entry, receiver_name, seen_names)
target_method_entries = resolve_method(entry.old_name, receiver_name, seen_names)
return entry unless target_method_entries

resolved_alias = Entry::MethodAlias.new(T.must(target_method_entries.first), entry)
original_entries = T.must(@entries[new_name])
resolved_alias = Entry::MethodAlias.new(
target_method_entries.first, #: as !nil
entry,
)
original_entries = @entries[new_name] #: as !nil
original_entries.delete(entry)
original_entries << resolved_alias
resolved_alias
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_indexer/lib/ruby_indexer/prefix_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def delete(key)
return unless node

# Remove the node from the tree and then go up the parents to remove any of them with empty children
parent = T.must(node.parent) #: Node[Value]?
parent = node.parent #: Node[Value]?

while parent
parent.children.delete(node.key)
Expand Down
6 changes: 5 additions & 1 deletion lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,11 @@ def on_instance_variable_target_node_enter(node)
#: (Prism::CallNode node) -> void
def on_call_node_enter(node)
if @target.is_a?(MethodTarget) && (name = node.name.to_s) == @target.method_name
@references << Reference.new(name, T.must(node.message_loc), declaration: false)
@references << Reference.new(
name,
node.message_loc, #: as !nil
declaration: false,
)
end
end

Expand Down
Loading
Loading