Class: Opal::Rewriters::ExplicitWriterReturn

Inherits:
Base
  • Object
show all
Defined in:
opal/lib/opal/rewriters/explicit_writer_return.rb

Constant Summary collapse

TMP_NAME =
'$writer'
GET_ARGS_NODE =
s(:lvar, TMP_NAME)
RETURN_ARGS_NODE =
s(:jsattr,
  GET_ARGS_NODE,
  s(:send, s(:jsattr, GET_ARGS_NODE, s(:str, 'length')), :-, s(:int, 1)),
)

Constants inherited from Base

Base::DUMMY_LOCATION

Instance Attribute Summary

Attributes inherited from Base

#current_node

Instance Method Summary collapse

Methods inherited from Base

#append_to_body, #begin_with_stmts, #error, #prepend_to_body, #process, #s, s, #stmts_of

Constructor Details

#initializeExplicitWriterReturn

Returns a new instance of ExplicitWriterReturn.



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

def initialize
  @in_masgn = false
end

Instance Method Details

#on_masgn(node) ⇒ Object

Multiple assignment is handled by Opal::Nodes::MassAssignNode

For example, "self.a, self.b = 1, 2" parses to: s(:masgn, s(:mlhs, s(:send, s(:self), :a=), s(:send, s(:self), :b=)), s(:array, s(:int, 1), s(:int, 2)))

And this AST rewriter skips this node.



51
52
53
54
55
56
# File 'opal/lib/opal/rewriters/explicit_writer_return.rb', line 51

def on_masgn(node)
  @in_masgn = true
  result = super
  @in_masgn = false
  result
end

#on_send(node) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'opal/lib/opal/rewriters/explicit_writer_return.rb', line 19

def on_send(node)
  return super if @in_masgn

  recv, method_name, *args = *node

  if method_name.to_s =~ /#{REGEXP_START}\w+=#{REGEXP_END}/ || method_name.to_s == '[]='
    set_args_node = s(:lvasgn, TMP_NAME, s(:array, *process_all(args)))

    s(:begin,
      set_args_node,
      node.updated(nil, [recv, method_name, s(:splat, GET_ARGS_NODE)]),
      RETURN_ARGS_NODE,
    )
  else
    super
  end
end