Skip to content

Commit

Permalink
fix dynamic delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed May 30, 2023
1 parent 8a8298c commit c159a32
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/ripper-tags/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ def on_assoc_new(key, value)
end

def on_delegate(*args)
method_names = args.select { |arg| arg.first.is_a? String }
options = args.select { |arg| arg.first.is_a?(Array) && arg.first.first == :assoc }.flatten(1)
method_names = args.select { |arg| arg.is_a?(Array) && arg.first.is_a?(String) }
options = args.select { |arg| arg.is_a?(Array) && arg.first.is_a?(Array) && arg.first.first == :assoc }.flatten(1)
options = Hash[options.map { |_assoc, key, val| [key[0], val[0]] if key }.compact]

target = options["to:"] || options["to"] # When using hashrocket syntax there is no ':'
Expand Down
18 changes: 3 additions & 15 deletions test/test_ripper_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,9 @@ def test_invalid_delegate
class C
delegate
delegate [1, 2]
%i(test).each do |m|
delegate m, :"\#{m}=", to: :object
end
end
EOC
assert_equal 1, tags.count
Expand Down Expand Up @@ -846,19 +849,4 @@ def included_method; end
expected = ["Object#test_method", "M", "M#included_method", "M#cache", "M#cache="]
assert_equal expected, tags.map { |t| t[:full_name] }
end

def test_multi_argument_delegate
tags = extract(<<-EOC)
class C
def object = Object.new
delegate :thing, :other, to: :object
%i(test).each do |m|
delegate m, :"\#{m}=", to: :object
end
end
EOC

expected = ["C", "C#object", "C#thing", "C#other", "C#test", "C#test="].sort
assert_equal expected, tags.map { |t| t[:full_name] }.sort
end
end

0 comments on commit c159a32

Please sign in to comment.