Permalink
Browse files

Add support for rescue on methods

  • Loading branch information...
Markus Schirp
Markus Schirp committed Aug 20, 2012
1 parent 54f7899 commit 2cddd7fb9929cce9e9b2ee17c284c4680c25d7ea
Showing with 165 additions and 4 deletions.
  1. +65 −4 lib/to_source/visitor.rb
  2. +100 −0 spec/unit/to_source/visitor/class_methods/run_spec.rb
View
@@ -54,6 +54,9 @@ def dispatch(node)
name = node.node_name
name = "#{name}_def" if %w[ class module ].include?(name)
__send__(name, node)
+ rescue NoMethodError => exception
+ node.ascii_graph
+ raise exception.message
end
# Emit element assignment
@@ -73,6 +76,64 @@ def element_assignment(node)
dispatch(value)
end
+ # Emit rescue
+ #
+ # @param [Rubinius::AST::Node] node
+ #
+ # @return [undefined]
+ #
+ # @api private
+ #
+ def rescue(node)
+ body(node.body)
+ dispatch(node.rescue)
+ end
+
+ # Emit rescue condition
+ #
+ # @param [Rubinius::AST::Node] node
+ #
+ # @return [undefined]
+ #
+ # @api private
+ #
+ def rescue_condition(node)
+ emit('rescue')
+ if node.conditions
+ body = node.conditions.body
+ first = body.first
+ unless body.one? and first.kind_of?(Rubinius::AST::ConstantAccess) and first.name == :StandardError
+ emit(' ')
+ array_body(body)
+ end
+ end
+
+ if node.splat
+ emit(',') if node.conditions
+ emit(' ')
+ dispatch(node.splat)
+ end
+
+ if node.assignment
+ emit(' => ')
+ emit(node.assignment.name)
+ end
+ nl
+ body(node.body)
+ end
+
+ # Emit rescue splat
+ #
+ # @param [Rubinius::AST::Node] node
+ #
+ # @return [undefined]
+ #
+ # @api private
+ #
+ def rescue_splat(node)
+ emit('*')
+ dispatch(node.value)
+ end
# Emit ensure
#
@@ -120,14 +181,14 @@ def body(node)
when Rubinius::AST::EmptyBody
node
when Rubinius::AST::Block
- # Hack to correctly indent ensure
- if node.array.one? && node.array.first.kind_of?(Rubinius::AST::Ensure)
+ # Hack to correctly indent ensure or rescue
+ noindent = [Rubinius::AST::Ensure, Rubinius::AST::Rescue]
+ if node.array.one? && noindent.include?(node.array.first.class)
@indentation-=1
dispatch(node)
return
- else
- node
end
+ node
else
Rubinius::AST::Block.new(node.line, [node])
end
@@ -546,6 +546,106 @@ def foo
end
end
+ context 'rescue' do
+ context 'without rescue condition' do
+ assert_source <<-RUBY
+ def foo
+ bar
+ rescue
+ baz
+ end
+ RUBY
+ end
+
+ context 'with rescue condition' do
+ context 'without assignment' do
+ assert_source <<-RUBY
+ def foo
+ bar
+ rescue SomeError
+ baz
+ end
+ RUBY
+ end
+
+ context 'with assignment' do
+ assert_source <<-RUBY
+ def foo
+ bar
+ rescue SomeError => exception
+ baz
+ end
+ RUBY
+ end
+ end
+
+ context 'with multivalued rescue condition' do
+ context 'without assignment' do
+ assert_source <<-RUBY
+ def foo
+ bar
+ rescue SomeError, SomeOtherError
+ baz
+ end
+ RUBY
+ end
+
+ context 'with assignment' do
+ assert_source <<-RUBY
+ def foo
+ bar
+ rescue SomeError, SomeOther => exception
+ baz
+ end
+ RUBY
+ end
+ end
+
+ context 'with normal and splat condition' do
+ context 'without assignment' do
+ assert_source <<-RUBY
+ def foo
+ bar
+ rescue SomeError, *bar
+ baz
+ end
+ RUBY
+ end
+
+ context 'with assignment' do
+ assert_source <<-RUBY
+ def foo
+ bar
+ rescue SomeError, *bar => exception
+ baz
+ end
+ RUBY
+ end
+ end
+
+ context 'with splat condition' do
+ context 'without assignment' do
+ assert_source <<-RUBY
+ def foo
+ bar
+ rescue *bar
+ baz
+ end
+ RUBY
+ end
+
+ context 'with assignment' do
+ assert_source <<-RUBY
+ def foo
+ bar
+ rescue *bar => exception
+ baz
+ end
+ RUBY
+ end
+ end
+ end
+
context 'ensure' do
assert_source <<-RUBY
def foo

0 comments on commit 2cddd7f

Please sign in to comment.