class Opal::Rewriters::Base

Constants

DUMMY_LOCATION

Attributes

current_node[RW]

Store the current node for reporting.

Public Class Methods

s(type, *children) click to toggle source
# File lib/opal/rewriters/base.rb, line 49
def self.s(type, *children)
  ::Opal::AST::Node.new(type, children, location: DUMMY_LOCATION)
end

Public Instance Methods

append_to_body(body, node) click to toggle source

Appends given node to body node.

Supports body to be one of:

  1. nil - empty body

  2. s(:begin) / s(:kwbegin) - multiline body

  3. s(:anything_else) - singleline body

Returns a new body with node injected as a last statement.

# File lib/opal/rewriters/base.rb, line 83
def append_to_body(body, node)
  stmts = stmts_of(body) + stmts_of(node)
  begin_with_stmts(stmts)
end
begin_with_stmts(stmts) click to toggle source
# File lib/opal/rewriters/base.rb, line 98
def begin_with_stmts(stmts)
  case stmts.length
  when 0
    nil
  when 1
    stmts[0]
  else
    s(:begin, *stmts)
  end
end
dynamic!() click to toggle source

Called when a given transformation is deemed to be dynamic, so that cache is conditionally disabled for a given file.

# File lib/opal/rewriters/base.rb, line 135
def dynamic!
  @dynamic_cache_result = true
end
error(msg) click to toggle source

This is called when a rewriting error occurs.

# File lib/opal/rewriters/base.rb, line 121
def error(msg)
  error = ::Opal::RewritingError.new(msg)
  error.location = current_node.loc if current_node
  raise error
end
on_top(node) click to toggle source
# File lib/opal/rewriters/base.rb, line 127
def on_top(node)
  node = process_regular_node(node)
  node.meta[:dynamic_cache_result] = true if @dynamic_cache_result
  node
end
prepend_to_body(body, node) click to toggle source

Prepends given node to body node.

Supports body to be one of:

  1. nil - empty body

  2. s(:begin) / s(:kwbegin) - multiline body

  3. s(:anything_else) - singleline body

Returns a new body with node injected as a first statement.

# File lib/opal/rewriters/base.rb, line 69
def prepend_to_body(body, node)
  stmts = stmts_of(node) + stmts_of(body)
  begin_with_stmts(stmts)
end
process(node) click to toggle source

Intercept the main call and assign current node.

Calls superclass method
# File lib/opal/rewriters/base.rb, line 113
def process(node)
  self.current_node = node
  super
ensure
  self.current_node = nil
end
s(type, *children) click to toggle source
# File lib/opal/rewriters/base.rb, line 44
def s(type, *children)
  loc = current_node ? current_node.loc : DUMMY_LOCATION
  ::Opal::AST::Node.new(type, children, location: loc)
end
stmts_of(node) click to toggle source
# File lib/opal/rewriters/base.rb, line 88
def stmts_of(node)
  if node.nil?
    []
  elsif %i[begin kwbegin].include?(node.type)
    node.children
  else
    [node]
  end
end