Class: Opal::Rewriters::Base

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

Defined Under Namespace

Classes: DummyLocation

Constant Summary collapse

DUMMY_LOCATION =
DummyLocation.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_nodeObject

Store the current node for reporting.



110
111
112
# File 'lib/opal/rewriters/base.rb', line 110

def current_node
  @current_node
end

Class Method Details

.s(type, *children) ⇒ Object



49
50
51
# File 'lib/opal/rewriters/base.rb', line 49

def self.s(type, *children)
  ::Opal::AST::Node.new(type, children, location: DUMMY_LOCATION)
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.



83
84
85
86
# 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) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# 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!Object

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



135
136
137
# File 'lib/opal/rewriters/base.rb', line 135

def dynamic!
  @dynamic_cache_result = true
end

#error(msg) ⇒ Object

This is called when a rewriting error occurs.



121
122
123
124
125
# 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) ⇒ Object



127
128
129
130
131
# 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) ⇒ 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.



69
70
71
72
# 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) ⇒ Object

Intercept the main call and assign current node.



113
114
115
116
117
118
# 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) ⇒ Object



44
45
46
47
# 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) ⇒ Object



88
89
90
91
92
93
94
95
96
# 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