Module: Ruby2JS::Filter::CJS

Includes:
SEXP
Defined in:
lib/ruby2js/filter/cjs.rb

Instance Method Summary collapse

Methods included from SEXP

#S, #s

Instance Method Details

#on_block(node) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ruby2js/filter/cjs.rb', line 58

def on_block(node)
  child = node.children[0]
  unless child.type == :send and child.children[0..1] == [nil, :export]
    return super 
  end

  send = child.children[2]
  unless send.type == :send and send.children[0..1] == [nil, :default]
    return super 
  end

  if send.children[2] == s(:send, nil, :proc)
    node.updated(:send, [
      s(:attr, nil, :module),
      :exports=,
      s(:block, s(:send, nil, :proc),
      *process_all(node.children[1..-1]))
    ])
  elsif send.children[2] == s(:send, nil, :async, s(:send, nil, :proc))
    node.updated(:send, [
      s(:attr, nil, :module),
      :exports=,
      s(:send, nil, :async,
        s(:block, s(:send, nil, :proc),
        *process_all(node.children[1..-1])))
    ])
  else
    super
  end
end

#on_send(node) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby2js/filter/cjs.rb', line 8

def on_send(node)
  return super unless node.children[1] == :export

  if node.children[2].type == :def
    fn = node.children[2]
    node.updated(nil, [
      s(:attr, nil, :exports),
      fn.children[0].to_s + '=',
      s(:block, s(:send, nil, :proc), *process_all(fn.children[1..-1]))
    ])

  elsif node.children[2].type == :lvasgn
    assign = node.children[2]
    node.updated(nil, [
      s(:attr, nil, :exports),
      assign.children[0].to_s + '=',
      *assign.children[1..-1]
    ])

  elsif \
    node.children[2].type == :send and
    node.children[2].children[0..1] == [nil, :async] and
    node.children[2].children[2].type == :def
  then
    fn = node.children[2].children[2]
    node.updated(nil, [
      s(:attr, nil, :exports),
      fn.children[0].to_s + '=',
      s(:send, nil, :async,
        s(:block, s(:send, nil, :proc),
        *process_all(fn.children[1..-1])))
    ])

  elsif \
    node.children[2].type == :send and
    node.children[2].children[0..1] == [nil, :default]
  then
    node = node.children[2]

    node.updated(nil, [
      s(:attr, nil, :module),
      :exports=,
      node.children[2]
    ])

  else
    super
  end
end