Class: Opal::Rewriters::InlineArgs

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

Overview

Converts

def m( a, b = 1, *c, d, e:, f: 1, **g, &blk ) end

To something like

def m( a, , , , ) blk = $post_args = arguments[1..-1] $kwargs = $post_args.pop a = ? $post_args.shift : 1 c = ? $post_args[0..-1] : [] d = $post_args.last e = $kwargs.delete(:e) f = $kwargs.delete(:f) || 1 g = $kwargs.except(:e, :f) end

Defined Under Namespace

Classes: Initializer

Constant Summary

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, #dynamic!, #error, #on_top, #prepend_to_body, #process, s, #s, #stmts_of

Instance Method Details

#on_def(node) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'opal/lib/opal/rewriters/inline_args.rb', line 28

def on_def(node)
  node = super(node)
  mid, args, body = *node

  body ||= s(:nil) # prevent returning initialization statement

  initializer = Initializer.new(args, type: :def)
  inline_args = args.updated(nil, initializer.inline)
  body = prepend_to_body(body, initializer.initialization)

  node.updated(nil, [mid, inline_args, body])
end

#on_defs(node) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'opal/lib/opal/rewriters/inline_args.rb', line 41

def on_defs(node)
  node = super(node)
  recv, mid, args, body = *node

  body ||= s(:nil) # prevent returning initialization statement

  initializer = Initializer.new(args, type: :defs)
  inline_args = args.updated(nil, initializer.inline)
  body = prepend_to_body(body, initializer.initialization)

  node.updated(nil, [recv, mid, inline_args, body])
end

#on_iter(node) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'opal/lib/opal/rewriters/inline_args.rb', line 54

def on_iter(node)
  node = super(node)
  args, body = *node

  body ||= s(:nil) # prevent returning initialization statement

  initializer = Initializer.new(args, type: :iter)
  inline_args = args.updated(nil, initializer.inline)
  body = prepend_to_body(body, initializer.initialization)

  node.updated(nil, [inline_args, body])
end