Skip to content

Commit

Permalink
Rename constant alias class names (#2361)
Browse files Browse the repository at this point in the history
- Alias should be ConstantAlias
- UnresolvedAlias should be UnresolvedConstantAlias

This is to match MethodAlias and UnresolvedMethodAlias.
  • Loading branch information
st0012 authored Jul 25, 2024
1 parent 77de05f commit b1f677f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 51 deletions.
6 changes: 3 additions & 3 deletions lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,17 +504,17 @@ def add_constant(node, name, value = nil)
@index.add(
case value
when Prism::ConstantReadNode, Prism::ConstantPathNode
Entry::UnresolvedAlias.new(value.slice, @stack.dup, name, @file_path, node.location, comments)
Entry::UnresolvedConstantAlias.new(value.slice, @stack.dup, name, @file_path, node.location, comments)
when Prism::ConstantWriteNode, Prism::ConstantAndWriteNode, Prism::ConstantOrWriteNode,
Prism::ConstantOperatorWriteNode

# If the right hand side is another constant assignment, we need to visit it because that constant has to be
# indexed too
Entry::UnresolvedAlias.new(value.name.to_s, @stack.dup, name, @file_path, node.location, comments)
Entry::UnresolvedConstantAlias.new(value.name.to_s, @stack.dup, name, @file_path, node.location, comments)
when Prism::ConstantPathWriteNode, Prism::ConstantPathOrWriteNode, Prism::ConstantPathOperatorWriteNode,
Prism::ConstantPathAndWriteNode

Entry::UnresolvedAlias.new(value.target.slice, @stack.dup, name, @file_path, node.location, comments)
Entry::UnresolvedConstantAlias.new(value.target.slice, @stack.dup, name, @file_path, node.location, comments)
else
Entry::Constant.new(name, @file_path, node.location, comments)
end,
Expand Down
6 changes: 3 additions & 3 deletions lib/ruby_indexer/lib/ruby_indexer/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ def initialize(name, file_path, location, name_location, comments, signatures, v
# All aliases are inserted as UnresolvedAlias in the index first and then we lazily resolve them to the correct
# target in [rdoc-ref:Index#resolve]. If the right hand side contains a constant that doesn't exist, then it's not
# possible to resolve the alias and it will remain an UnresolvedAlias until the right hand side constant exists
class UnresolvedAlias < Entry
class UnresolvedConstantAlias < Entry
extend T::Sig

sig { returns(String) }
Expand Down Expand Up @@ -439,13 +439,13 @@ def initialize(target, nesting, name, file_path, location, comments) # rubocop:d
end

# Alias represents a resolved alias, which points to an existing constant target
class Alias < Entry
class ConstantAlias < Entry
extend T::Sig

sig { returns(String) }
attr_reader :target

sig { params(target: String, unresolved_alias: UnresolvedAlias).void }
sig { params(target: String, unresolved_alias: UnresolvedConstantAlias).void }
def initialize(target, unresolved_alias)
super(unresolved_alias.name, unresolved_alias.file_path, unresolved_alias.location, unresolved_alias.comments)

Expand Down
44 changes: 22 additions & 22 deletions lib/ruby_indexer/lib/ruby_indexer/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def search_require_paths(query)
name: String,
).returns(T.nilable(T::Array[T.any(
Entry::Namespace,
Entry::Alias,
Entry::UnresolvedAlias,
Entry::ConstantAlias,
Entry::UnresolvedConstantAlias,
Entry::Constant,
)]))
end
Expand All @@ -105,8 +105,8 @@ def first_unqualified_const(name)
entries,
T.nilable(T::Array[T.any(
Entry::Namespace,
Entry::Alias,
Entry::UnresolvedAlias,
Entry::ConstantAlias,
Entry::UnresolvedConstantAlias,
Entry::Constant,
)]),
)
Expand Down Expand Up @@ -230,8 +230,8 @@ def method_completion_candidates(name, receiver_name)
seen_names: T::Array[String],
).returns(T.nilable(T::Array[T.any(
Entry::Namespace,
Entry::Alias,
Entry::UnresolvedAlias,
Entry::ConstantAlias,
Entry::UnresolvedConstantAlias,
)]))
end
def resolve(name, nesting, seen_names = [])
Expand Down Expand Up @@ -334,13 +334,13 @@ def follow_aliased_namespace(name, seen_names = [])
entry = @entries[current_name]&.first

case entry
when Entry::Alias
when Entry::ConstantAlias
target = entry.target
return follow_aliased_namespace("#{target}::#{real_parts.join("::")}", seen_names)
when Entry::UnresolvedAlias
when Entry::UnresolvedConstantAlias
resolved = resolve_alias(entry, seen_names)

if resolved.is_a?(Entry::UnresolvedAlias)
if resolved.is_a?(Entry::UnresolvedConstantAlias)
raise UnresolvableAliasError, "The constant #{resolved.name} is an alias to a non existing constant"
end

Expand Down Expand Up @@ -439,7 +439,7 @@ def linearized_ancestors_of(fully_qualified_name)
case entry
when Entry::Namespace
entry
when Entry::Alias
when Entry::ConstantAlias
self[entry.target]&.grep(Entry::Namespace)
end
end.flatten
Expand Down Expand Up @@ -698,9 +698,9 @@ def linearize_superclass( # rubocop:disable Metrics/ParameterLists
# that doesn't exist, then we return the same UnresolvedAlias
sig do
params(
entry: Entry::UnresolvedAlias,
entry: Entry::UnresolvedConstantAlias,
seen_names: T::Array[String],
).returns(T.any(Entry::Alias, Entry::UnresolvedAlias))
).returns(T.any(Entry::ConstantAlias, Entry::UnresolvedConstantAlias))
end
def resolve_alias(entry, seen_names)
alias_name = entry.name
Expand All @@ -712,7 +712,7 @@ def resolve_alias(entry, seen_names)
return entry unless target

target_name = T.must(target.first).name
resolved_alias = Entry::Alias.new(target_name, entry)
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])
Expand All @@ -731,8 +731,8 @@ def resolve_alias(entry, seen_names)
seen_names: T::Array[String],
).returns(T.nilable(T::Array[T.any(
Entry::Namespace,
Entry::Alias,
Entry::UnresolvedAlias,
Entry::ConstantAlias,
Entry::UnresolvedConstantAlias,
)]))
end
def lookup_enclosing_scopes(name, nesting, seen_names)
Expand Down Expand Up @@ -760,8 +760,8 @@ def lookup_enclosing_scopes(name, nesting, seen_names)
seen_names: T::Array[String],
).returns(T.nilable(T::Array[T.any(
Entry::Namespace,
Entry::Alias,
Entry::UnresolvedAlias,
Entry::ConstantAlias,
Entry::UnresolvedConstantAlias,
)]))
end
def lookup_ancestor_chain(name, nesting, seen_names)
Expand Down Expand Up @@ -821,20 +821,20 @@ def build_non_redundant_full_name(name, nesting)
).returns(
T.nilable(T::Array[T.any(
Entry::Namespace,
Entry::Alias,
Entry::UnresolvedAlias,
Entry::ConstantAlias,
Entry::UnresolvedConstantAlias,
)]),
)
end
def direct_or_aliased_constant(full_name, seen_names)
entries = @entries[full_name] || @entries[follow_aliased_namespace(full_name)]

T.cast(
entries&.map { |e| e.is_a?(Entry::UnresolvedAlias) ? resolve_alias(e, seen_names) : e },
entries&.map { |e| e.is_a?(Entry::UnresolvedConstantAlias) ? resolve_alias(e, seen_names) : e },
T.nilable(T::Array[T.any(
Entry::Namespace,
Entry::Alias,
Entry::UnresolvedAlias,
Entry::ConstantAlias,
Entry::UnresolvedConstantAlias,
)]),
)
end
Expand Down
34 changes: 17 additions & 17 deletions lib/ruby_indexer/test/constant_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ module C
RUBY

unresolve_entry = @index["A::FIRST"].first
assert_instance_of(Entry::UnresolvedAlias, unresolve_entry)
assert_instance_of(Entry::UnresolvedConstantAlias, unresolve_entry)
assert_equal(["A"], unresolve_entry.nesting)
assert_equal("B::C", unresolve_entry.target)

resolved_entry = @index.resolve("A::FIRST", []).first
assert_instance_of(Entry::Alias, resolved_entry)
assert_instance_of(Entry::ConstantAlias, resolved_entry)
assert_equal("A::B::C", resolved_entry.target)
end

Expand All @@ -226,20 +226,20 @@ module Other
RUBY

unresolve_entry = @index["A::ALIAS"].first
assert_instance_of(Entry::UnresolvedAlias, unresolve_entry)
assert_instance_of(Entry::UnresolvedConstantAlias, unresolve_entry)
assert_equal(["A"], unresolve_entry.nesting)
assert_equal("B", unresolve_entry.target)

resolved_entry = @index.resolve("ALIAS", ["A"]).first
assert_instance_of(Entry::Alias, resolved_entry)
assert_instance_of(Entry::ConstantAlias, resolved_entry)
assert_equal("A::B", resolved_entry.target)

resolved_entry = @index.resolve("ALIAS::C", ["A"]).first
assert_instance_of(Entry::Module, resolved_entry)
assert_equal("A::B::C", resolved_entry.name)

unresolve_entry = @index["Other::ONE_MORE"].first
assert_instance_of(Entry::UnresolvedAlias, unresolve_entry)
assert_instance_of(Entry::UnresolvedConstantAlias, unresolve_entry)
assert_equal(["Other"], unresolve_entry.nesting)
assert_equal("A::ALIAS", unresolve_entry.target)

Expand All @@ -259,51 +259,51 @@ module A

# B and C
unresolve_entry = @index["A::B"].first
assert_instance_of(Entry::UnresolvedAlias, unresolve_entry)
assert_instance_of(Entry::UnresolvedConstantAlias, unresolve_entry)
assert_equal(["A"], unresolve_entry.nesting)
assert_equal("C", unresolve_entry.target)

resolved_entry = @index.resolve("A::B", []).first
assert_instance_of(Entry::Alias, resolved_entry)
assert_instance_of(Entry::ConstantAlias, resolved_entry)
assert_equal("A::C", resolved_entry.target)

constant = @index["A::C"].first
assert_instance_of(Entry::Constant, constant)

# D and E
unresolve_entry = @index["A::D"].first
assert_instance_of(Entry::UnresolvedAlias, unresolve_entry)
assert_instance_of(Entry::UnresolvedConstantAlias, unresolve_entry)
assert_equal(["A"], unresolve_entry.nesting)
assert_equal("E", unresolve_entry.target)

resolved_entry = @index.resolve("A::D", []).first
assert_instance_of(Entry::Alias, resolved_entry)
assert_instance_of(Entry::ConstantAlias, resolved_entry)
assert_equal("A::E", resolved_entry.target)

# F and G::H
unresolve_entry = @index["A::F"].first
assert_instance_of(Entry::UnresolvedAlias, unresolve_entry)
assert_instance_of(Entry::UnresolvedConstantAlias, unresolve_entry)
assert_equal(["A"], unresolve_entry.nesting)
assert_equal("G::H", unresolve_entry.target)

resolved_entry = @index.resolve("A::F", []).first
assert_instance_of(Entry::Alias, resolved_entry)
assert_instance_of(Entry::ConstantAlias, resolved_entry)
assert_equal("A::G::H", resolved_entry.target)

# I::J, K::L and M
unresolve_entry = @index["A::I::J"].first
assert_instance_of(Entry::UnresolvedAlias, unresolve_entry)
assert_instance_of(Entry::UnresolvedConstantAlias, unresolve_entry)
assert_equal(["A"], unresolve_entry.nesting)
assert_equal("K::L", unresolve_entry.target)

resolved_entry = @index.resolve("A::I::J", []).first
assert_instance_of(Entry::Alias, resolved_entry)
assert_instance_of(Entry::ConstantAlias, resolved_entry)
assert_equal("A::K::L", resolved_entry.target)

# When we are resolving A::I::J, we invoke `resolve("K::L", ["A"])`, which recursively resolves A::K::L too.
# Therefore, both A::I::J and A::K::L point to A::M by the end of the previous resolve invocation
resolved_entry = @index["A::K::L"].first
assert_instance_of(Entry::Alias, resolved_entry)
assert_instance_of(Entry::ConstantAlias, resolved_entry)
assert_equal("A::M", resolved_entry.target)

constant = @index["A::M"].first
Expand Down Expand Up @@ -344,11 +344,11 @@ module Real
RUBY

assert_entry("A::B", Entry::Constant, "/fake/path/foo.rb:1-2:1-3")
assert_entry("A::C", Entry::UnresolvedAlias, "/fake/path/foo.rb:1-5:1-6")
assert_entry("A::D::E", Entry::UnresolvedAlias, "/fake/path/foo.rb:2-2:2-6")
assert_entry("A::C", Entry::UnresolvedConstantAlias, "/fake/path/foo.rb:1-5:1-6")
assert_entry("A::D::E", Entry::UnresolvedConstantAlias, "/fake/path/foo.rb:2-2:2-6")
assert_entry("A::F::G", Entry::Constant, "/fake/path/foo.rb:2-8:2-12")
assert_entry("A::H", Entry::Constant, "/fake/path/foo.rb:3-2:3-3")
assert_entry("A::I::J", Entry::UnresolvedAlias, "/fake/path/foo.rb:3-5:3-9")
assert_entry("A::I::J", Entry::UnresolvedConstantAlias, "/fake/path/foo.rb:3-5:3-9")
assert_entry("A::K", Entry::Constant, "/fake/path/foo.rb:4-2:4-3")
assert_entry("A::L", Entry::Constant, "/fake/path/foo.rb:4-5:4-6")
end
Expand Down
12 changes: 6 additions & 6 deletions lib/ruby_indexer/test/index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class Bar < self
entry = @index.resolve("BAZ", ["Foo", "Bar"]).first
refute_nil(entry)

assert_instance_of(Entry::UnresolvedAlias, entry)
assert_instance_of(Entry::UnresolvedConstantAlias, entry)
end

def test_visitor_does_not_visit_unnecessary_nodes
Expand Down Expand Up @@ -1015,11 +1015,11 @@ module Namespace

foo_entry = T.must(@index.resolve("FOO", ["Namespace"])&.first)
assert_equal(2, foo_entry.location.start_line)
assert_instance_of(Entry::Alias, foo_entry)
assert_instance_of(Entry::ConstantAlias, foo_entry)

bar_entry = T.must(@index.resolve("BAR", ["Namespace"])&.first)
assert_equal(3, bar_entry.location.start_line)
assert_instance_of(Entry::Alias, bar_entry)
assert_instance_of(Entry::ConstantAlias, bar_entry)
end

def test_resolving_circular_alias_three_levels
Expand All @@ -1033,15 +1033,15 @@ module Namespace

foo_entry = T.must(@index.resolve("FOO", ["Namespace"])&.first)
assert_equal(2, foo_entry.location.start_line)
assert_instance_of(Entry::Alias, foo_entry)
assert_instance_of(Entry::ConstantAlias, foo_entry)

bar_entry = T.must(@index.resolve("BAR", ["Namespace"])&.first)
assert_equal(3, bar_entry.location.start_line)
assert_instance_of(Entry::Alias, bar_entry)
assert_instance_of(Entry::ConstantAlias, bar_entry)

baz_entry = T.must(@index.resolve("BAZ", ["Namespace"])&.first)
assert_equal(4, baz_entry.location.start_line)
assert_instance_of(Entry::Alias, baz_entry)
assert_instance_of(Entry::ConstantAlias, baz_entry)
end

def test_resolving_constants_in_aliased_namespace
Expand Down

0 comments on commit b1f677f

Please sign in to comment.