Ruby::Generator support for #[] methods

This commit is contained in:
Yorick Peterse 2015-07-12 01:39:57 +02:00
parent 17f6b3a3bb
commit cf2405998b
2 changed files with 15 additions and 4 deletions

View File

@ -126,15 +126,17 @@ end
def on_send(ast)
receiver, name, *args = *ast
call = name.dup
call = name
brackets = name == '[]'
unless args.empty?
arg_strs = args.map { |arg| process(arg) }
call = "#{call}(#{arg_strs.join(', ')})"
arg_str = args.map { |arg| process(arg) }.join(', ')
call = brackets ? "[#{arg_str}]" : "#{call}(#{arg_str})"
end
if receiver
call = "#{process(receiver)}.#{call}"
rec_str = process(receiver)
call = brackets ? "#{rec_str}#{call}" : "#{rec_str}.#{call}"
end
call

View File

@ -104,6 +104,15 @@ end
@generator.on_send(node).should == 'number.foobar(10)'
end
end
describe 'when calling #[]' do
it 'returns a correctly formatted String' do
arg = Oga::Ruby::Node.new(:lit, %w{10})
node = Oga::Ruby::Node.new(:lit, %w{number})[arg]
@generator.on_send(node).should == 'number[10]'
end
end
end
describe '#on_block' do