Class: RubyNext::Language::Rewriters::RescueWithinBlock

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby-next/language/rewriters/2.5/rescue_within_block.rb

Constant Summary collapse

NAME =
"rescue-within-block"
SYNTAX_PROBE =
"lambda do
  raise 'err'
rescue
  $! # => #<RuntimeError: err>
end.call"
MIN_SUPPORTED_VERSION =
Gem::Version.new("2.5.0")

Instance Attribute Summary

Attributes inherited from Base

#locals

Instance Method Summary collapse

Methods inherited from Base

ast?, #initialize, #s

Methods inherited from Abstract

ast?, #initialize, text?, unsupported_syntax?, unsupported_version?

Constructor Details

This class inherits a constructor from RubyNext::Language::Rewriters::Base

Instance Method Details

#on_block(block_node) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby-next/language/rewriters/2.5/rescue_within_block.rb', line 16

def on_block(block_node)
  exception_node = block_node.children.find do |node|
    node && (node.type == :rescue || node.type == :ensure)
  end

  return super(block_node) unless exception_node

  context.track! self

  insert_before(exception_node.loc.expression, "begin;")
  insert_after(exception_node.loc.expression, ";end")

  new_children = block_node.children.map do |child|
    next s(:kwbegin, exception_node) if child == exception_node

    child
  end

  process(
    block_node.updated(:block, new_children)
  )
end