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

Gh1940/alias support #1951

Open
wants to merge 3 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 55 additions & 0 deletions lib/ruby_indexer/lib/ruby_indexer/collector.rb
Expand Up @@ -65,6 +65,8 @@ def collect(node)
handle_call_node(node_or_event)
when Prism::DefNode
handle_def_node(node_or_event)
when Prism::AliasMethodNode
handle_alias_node(node_or_event)
when LEAVE_EVENT
@stack.pop
end
Expand Down Expand Up @@ -152,6 +154,8 @@ def handle_call_node(node)
handle_attribute(node, reader: false, writer: true)
when :attr_accessor
handle_attribute(node, reader: true, writer: true)
when :alias_method
handle_alias_method(node)
when :include
handle_include(node)
when :prepend
Expand Down Expand Up @@ -185,6 +189,57 @@ def handle_def_node(node)
end
end

sig { params(node: Prism::AliasMethodNode).void }
def handle_alias_node(node)
method_name = node.new_name.slice
comments = collect_comments(node)
@index << Entry::UnresolvedMethodAlias.new(
method_name,
node.old_name.slice,
@current_owner,
@file_path,
node.new_name.location,
comments,
)
end

sig { params(node: Prism::CallNode).void }
def handle_alias_method(node)
arguments = node.arguments&.arguments
return unless arguments

new_name, old_name = arguments
return unless new_name && old_name

new_name_value = case new_name
when Prism::StringNode
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would unescaped allow us to avoid this conditional?

ruby-lsp(main):028> Prism.parse("alias foo").value.statements.body.first.new_name.unescaped
=> "foo"
ruby-lsp(main):029> Prism.parse("alias :foo").value.statements.body.first.new_name.unescaped
=> "foo"

new_name.content
when Prism::SymbolNode
new_name.value
end

return unless new_name_value

old_name_value = case old_name
when Prism::StringNode
old_name.content
when Prism::SymbolNode
old_name.value
end

return unless old_name_value

comments = collect_comments(node)
@index << Entry::UnresolvedMethodAlias.new(
new_name_value,
old_name_value,
@current_owner,
@file_path,
new_name.location,
comments,
)
end

sig { params(node: Prism::CallNode).void }
def handle_private_constant(node)
arguments = node.arguments&.arguments
Expand Down
28 changes: 28 additions & 0 deletions lib/ruby_indexer/lib/ruby_indexer/entry.rb
Expand Up @@ -380,5 +380,33 @@ def initialize(target, unresolved_alias)
@target = target
end
end

class UnresolvedMethodAlias < Entry
extend T::Sig

sig { returns(String) }
attr_reader :new_name, :old_name

sig { returns(T.nilable(Entry::Namespace)) }
attr_reader :owner

sig do
params(
new_name: String,
old_name: String,
owner: T.nilable(Entry::Namespace),
file_path: String,
location: T.any(Prism::Location, RubyIndexer::Location),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is RubyIndexer::Location needed here? It typechecks without it, so if we do need it we may be missing a test.

comments: T::Array[String],
).void
end
def initialize(new_name, old_name, owner, file_path, location, comments) # rubocop:disable Metrics/ParameterLists
super(new_name, file_path, location, comments)

@new_name = new_name
@old_name = old_name
@owner = owner
end
end
end
end
17 changes: 17 additions & 0 deletions lib/ruby_indexer/test/method_test.rb
Expand Up @@ -285,5 +285,22 @@ class Foo

assert_no_entry("bar")
end

def test_keeps_track_of_aliases
index(<<~RUBY)
class Foo
alias whatever to_s
alias_method :foo, :to_a
alias_method "bar", "to_a"
alias_method baz, :to_a
alias_method :baz
end
RUBY

assert_entry("whatever", Entry::UnresolvedMethodAlias, "/fake/path/foo.rb:1-8:1-16")
assert_entry("foo", Entry::UnresolvedMethodAlias, "/fake/path/foo.rb:2-15:2-19")
assert_entry("bar", Entry::UnresolvedMethodAlias, "/fake/path/foo.rb:3-15:3-20")
assert_equal(4, @index.instance_variable_get(:@entries).length)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be worth a short explanation of where the 4 comes from.

end
end
end
1 change: 1 addition & 0 deletions lib/ruby_indexer/test/test_case.rb
Expand Up @@ -17,6 +17,7 @@ def index(source)

def assert_entry(expected_name, type, expected_location)
entries = @index[expected_name]
refute_nil(entries, "Expected #{expected_name} to be indexed")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introduced a refute_nil before we refute empty for better error messages

refute_empty(entries, "Expected #{expected_name} to be indexed")

entry = entries.first
Expand Down