Class: Opal::Rewriters::Base

Inherits:
Parser::AST::Processor
  • Object
show all
Defined in:
opal/lib/opal/rewriters/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.s(type, *children) ⇒ Object



12
13
14
# File 'opal/lib/opal/rewriters/base.rb', line 12

def self.s(type, *children)
  ::Opal::AST::Node.new(type, children)
end

Instance Method Details

#append_to_body(body, node) ⇒ Object

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.



52
53
54
55
56
57
58
59
60
# File 'opal/lib/opal/rewriters/base.rb', line 52

def append_to_body(body, node)
  if body.nil?
    node
  elsif [:begin, :kwbegin].include?(body.type)
    body.updated(nil, [*body, node])
  else
    s(:begin, body, node)
  end
end

#prepend_to_body(body, node) ⇒ Object

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.



33
34
35
36
37
38
39
40
41
# File 'opal/lib/opal/rewriters/base.rb', line 33

def prepend_to_body(body, node)
  if body.nil?
    node
  elsif [:begin, :kwbegin].include?(body.type)
    body.updated(nil, [node, *body])
  else
    s(:begin, node, body)
  end
end

#s(type, *children) ⇒ Object



8
9
10
# File 'opal/lib/opal/rewriters/base.rb', line 8

def s(type, *children)
  ::Opal::AST::Node.new(type, children)
end