Ruby generator support for "begin" blocks

This commit is contained in:
Yorick Peterse 2015-07-27 00:28:07 +02:00
parent 92b43a7500
commit 04aa8f6546
4 changed files with 48 additions and 0 deletions

View File

@ -52,6 +52,22 @@ module Oga
"#{var_names.join(', ')} = #{val_str}" "#{var_names.join(', ')} = #{val_str}"
end end
##
# Processes a `begin` node.
#
# @param [Oga::Ruby::Node] ast
# @return [String]
#
def on_begin(ast)
body = process(ast.to_a[0])
<<-EOF
begin
#{body}
end
EOF
end
## ##
# Processes an equality node. # Processes an equality node.
# #

View File

@ -104,6 +104,15 @@ module Oga
Node.new(:block, [self, args, yield]) Node.new(:block, [self, args, yield])
end end
##
# Wraps the current node in a `begin` node.
#
# @return [Oga::Ruby::Node]
#
def wrap
Node.new(:begin, [self])
end
## ##
# Wraps the current node in an if statement node. # Wraps the current node in an if statement node.
# #

View File

@ -37,6 +37,19 @@ describe Oga::Ruby::Generator do
end end
end end
describe '#on_begin' do
it 'returns a String' do
number = Oga::Ruby::Node.new(:lit, %w{10})
node = Oga::Ruby::Node.new(:begin, [number])
@generator.on_begin(node).should == <<-EOF
begin
10
end
EOF
end
end
describe '#on_eq' do describe '#on_eq' do
it 'returns a String' do it 'returns a String' do
var = Oga::Ruby::Node.new(:lit, %w{number}) var = Oga::Ruby::Node.new(:lit, %w{number})

View File

@ -88,6 +88,16 @@ describe Oga::Ruby::Node do
end end
end end
describe '#wrap' do
it 'returns a begin Node' do
number = described_class.new(:lit, %w{10})
wrapped = number.wrap
wrapped.type.should == :begin
wrapped.to_a.should == [number]
end
end
describe '#if_true' do describe '#if_true' do
it 'returns an if statement Node' do it 'returns an if statement Node' do
condition = described_class.new(:lit, %w{number}) condition = described_class.new(:lit, %w{number})