@@ -68,6 +68,19 @@ def initialize(nesting, name, location, comments)
68
68
end
69
69
end
70
70
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
+
71
84
# When you're using the instruction sequence backend, this class is used to
72
85
# lazily parse comments out of the source code.
73
86
class FileComments
@@ -297,7 +310,7 @@ def index_iseq(iseq, file_comments)
297
310
EntryComments . new ( file_comments , location )
298
311
)
299
312
when :definesmethod
300
- if current_iseq [ 13 ] [ index - 1 ] != [ :putself ]
313
+ if insns [ index - 1 ] != [ :putself ]
301
314
raise NotImplementedError ,
302
315
"singleton method with non-self receiver"
303
316
end
@@ -309,6 +322,24 @@ def index_iseq(iseq, file_comments)
309
322
location ,
310
323
EntryComments . new ( file_comments , location )
311
324
)
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
312
343
end
313
344
end
314
345
end
@@ -331,6 +362,20 @@ def initialize
331
362
end
332
363
333
364
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
+
334
379
def visit_class ( node )
335
380
names = visit ( node . constant )
336
381
nesting << names
0 commit comments