class Opal::Rewriters::Base
def self.s(type, *children)
def self.s(type, *children) ::Opal::AST::Node.new(type, children, location: DUMMY_LOCATION) end
def append_to_body(body, node)
Returns a new body with +node+ injected as a last statement.
3. s(:anything_else) - singleline body
2. s(:begin) / s(:kwbegin) - multiline body
1. nil - empty body
Supports +body+ to be one of:
Appends given +node+ to +body+ node.
def append_to_body(body, node) stmts = stmts_of(body) + stmts_of(node) begin_with_stmts(stmts) end
def begin_with_stmts(stmts)
def begin_with_stmts(stmts) case stmts.length when 0 nil when 1 stmts[0] else s(:begin, *stmts) end end
def dynamic!
Called when a given transformation is deemed to be dynamic, so
def dynamic! @dynamic_cache_result = true end
def error(msg)
def error(msg) error = ::Opal::RewritingError.new(msg) error.location = current_node.loc if current_node raise error end
def on_top(node)
def on_top(node) node = process_regular_node(node) node.meta[:dynamic_cache_result] = true if @dynamic_cache_result node end
def prepend_to_body(body, node)
Returns a new body with +node+ injected as a first statement.
3. s(:anything_else) - singleline body
2. s(:begin) / s(:kwbegin) - multiline body
1. nil - empty body
Supports +body+ to be one of:
Prepends given +node+ to +body+ node.
def prepend_to_body(body, node) stmts = stmts_of(node) + stmts_of(body) begin_with_stmts(stmts) end
def process(node)
def process(node) self.current_node = node super ensure self.current_node = nil end
def s(type, *children)
def s(type, *children) loc = current_node ? current_node.loc : DUMMY_LOCATION ::Opal::AST::Node.new(type, children, location: loc) end
def stmts_of(node)
def stmts_of(node) if node.nil? [] elsif %i[begin kwbegin].include?(node.type) node.children else [node] end end