class Opal::Nodes::RescueNode

def body_code

def body_code
  body_code = (body.type == :resbody ? s(:nil) : body)
  body_code = compiler.returns(body_code) unless stmt?
  body_code
end

def compile

def compile
  scope.rescue_else_sexp = children[1..-1].detect { |sexp| sexp && sexp.type != :resbody }
  has_rescue_handlers = false
  if handle_rescue_else_manually?
    line "var $no_errors = true;"
  end
  push "try {"
  indent do
    line stmt(body_code)
  end
  line "} catch ($err) {"
  indent do
    if has_rescue_else?
      line "$no_errors = false;"
    end
    children[1..-1].each_with_index do |child, idx|
      # counting only rescue, ignoring rescue-else statement
      if child && child.type == :resbody
        has_rescue_handlers = true
        push " else " unless idx == 0
        line process(child, @level)
      end
    end
    # if no resbodys capture our error, then rethrow
    push " else { throw $err; }"
  end
  line "}"
  if handle_rescue_else_manually?
    # here we must add 'finally' explicitly
    push "finally {"
    indent do
      line "if ($no_errors) { "
      indent do
        line stmt(rescue_else_code)
      end
      line "}"
    end
    push "}"
  end
  # Wrap a try{} catch{} into a function
  # when it's an expression
  # or when there's a method call after begin;rescue;end
  wrap '(function() { ', '})()' if expr? or recv?
end

def handle_rescue_else_manually?


wrapping current rescue.
Returns true when there's no 'ensure' statement
def handle_rescue_else_manually?
  !scope.in_ensure? && scope.has_rescue_else?
end

def rescue_else_code

def rescue_else_code
  rescue_else_code = scope.rescue_else_sexp
  rescue_else_code = compiler.returns(rescue_else_code) unless stmt?
  rescue_else_code
end