Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a5a0710

Browse files
committed
Capture alias methods in index
1 parent 4864692 commit a5a0710

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

lib/syntax_tree/index.rb

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ def initialize(nesting, name, location, comments)
6868
end
6969
end
7070

71+
# This entry represents a method definition that was created using the alias
72+
# keyword.
73+
class AliasMethodDefinition
74+
attr_reader :nesting, :name, :location, :comments
75+
76+
def initialize(nesting, name, location, comments)
77+
@nesting = nesting
78+
@name = name
79+
@location = location
80+
@comments = comments
81+
end
82+
end
83+
7184
# When you're using the instruction sequence backend, this class is used to
7285
# lazily parse comments out of the source code.
7386
class FileComments
@@ -297,7 +310,7 @@ def index_iseq(iseq, file_comments)
297310
EntryComments.new(file_comments, location)
298311
)
299312
when :definesmethod
300-
if current_iseq[13][index - 1] != [:putself]
313+
if insns[index - 1] != [:putself]
301314
raise NotImplementedError,
302315
"singleton method with non-self receiver"
303316
end
@@ -309,6 +322,24 @@ def index_iseq(iseq, file_comments)
309322
location,
310323
EntryComments.new(file_comments, location)
311324
)
325+
when :opt_send_without_block, :send
326+
if insn[1][:mid] == :"core#set_method_alias"
327+
# Now we have to validate that the alias is happening with a
328+
# non-interpolated value. To do this we'll match the specific
329+
# pattern we're expecting.
330+
values = insns[(index - 4)...index].map { |insn| insn.is_a?(Array) ? insn[0] : insn }
331+
next if values != %i[putspecialobject putspecialobject putobject putobject]
332+
333+
# Now that we know it's in the structure we want it, we can use
334+
# the values of the putobject to determine the alias.
335+
location = Location.new(line, 0)
336+
results << AliasMethodDefinition.new(
337+
current_nesting,
338+
insns[index - 2][1],
339+
location,
340+
EntryComments.new(file_comments, location)
341+
)
342+
end
312343
end
313344
end
314345
end
@@ -331,6 +362,20 @@ def initialize
331362
end
332363

333364
visit_methods do
365+
def visit_alias(node)
366+
if node.left.is_a?(SymbolLiteral) && node.right.is_a?(SymbolLiteral)
367+
location =
368+
Location.new(node.location.start_line, node.location.start_column)
369+
370+
results << AliasMethodDefinition.new(
371+
nesting.dup,
372+
node.left.value.value.to_sym,
373+
location,
374+
comments_for(node)
375+
)
376+
end
377+
end
378+
334379
def visit_class(node)
335380
names = visit(node.constant)
336381
nesting << names

test/index_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ def test_singleton_method_comments
139139
end
140140
end
141141

142+
def test_alias_method
143+
index_each("alias foo bar") do |entry|
144+
assert_equal :foo, entry.name
145+
assert_empty entry.nesting
146+
end
147+
end
148+
142149
def test_this_file
143150
entries = Index.index_file(__FILE__, backend: Index::ParserBackend.new)
144151

0 commit comments

Comments
 (0)