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



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/ruby2js/filter/cjs.rb', line 158

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



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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ruby2js/filter/cjs.rb', line 63

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 + '=',
      *process_all(assign.children[1..-1])
    ])

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

  elsif node.children[2].type == :class
    assign = node.children[2]
    if assign.children[0].children[0] != nil
      node
    elsif assign.children[1] == nil
      node.updated(nil, [
        s(:attr, nil, :exports),
        assign.children[0].children[1].to_s + '=',
        s(:block, s(:send, s(:const, nil, :Class), :new),
        s(:args), *process_all(assign.children[2..-1]))
      ])
    else
      node.updated(nil, [
        s(:attr, nil, :exports),
        assign.children[0].children[1].to_s + '=',
        s(:block, s(:send, s(:const, nil, :Class), :new,
        assign.children[1]), s(:args), 
        *process_all(assign.children[2..-1]))
      ])
    end

  elsif node.children[2].type == :module
    assign = node.children[2]
    if assign.children[0].children[0] != nil
      node
    else
      node.updated(nil, [
        s(:attr, nil, :exports),
        assign.children[0].children[1].to_s + '=',
        s(:class_module, nil, nil, *process_all(assign.children[1..-1]))
      ])
    end

  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=,
      process(node.children[2])
    ])

  else
    super
  end
end

#options=(options) ⇒ Object



10
11
12
13
# File 'lib/ruby2js/filter/cjs.rb', line 10

def options=(options)
  super
  @cjs_autoexports = !@disable_autoexports && options[:autoexports]
end

#process(node) ⇒ Object



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
57
58
59
60
61
# File 'lib/ruby2js/filter/cjs.rb', line 15

def process(node)
  return super unless @cjs_autoexports

  list = [node]
  while list.length == 1 and list.first.type == :begin
    list = list.first.children.dup
  end

  replaced = []
  list.map! do |child|
    replacement = child

    if [:module, :class].include? child.type and
      child.children.first.type == :const and
      child.children.first.children.first == nil \
    then
      replacement = s(:send, nil, :export, child)
    elsif child.type == :casgn and child.children.first == nil
      replacement = s(:send, nil, :export, child)
    elsif child.type == :def
      replacement = s(:send, nil, :export, child)
    end

    if replacement != child
      replaced << replacement
      @comments[replacement] = @comments[child] if @comments[child]
    end

    replacement
  end

  if replaced.length == 1 and @cjs_autoexports == :default
    list.map! do |child|
      if child == replaced.first
        replacement = s(:send, nil, :export, s(:send, nil, :default,
          *child.children[2..-1]))
        @comments[replacement] = @comments[child] if @comments[child]
        replacement
      else
        child
      end
    end
  end

  @cjs_autoexports = false
  process s(:begin, *list)
end