module Ruby2JS::Filter::CJS
Public Instance Methods
on_block(node)
click to toggle source
Calls superclass method
# 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)
click to toggle source
Calls superclass method
# 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)
click to toggle source
Calls superclass method
# File lib/ruby2js/filter/cjs.rb, line 10 def options=(options) super @cjs_autoexports = !@disable_autoexports && options[:autoexports] end
process(node)
click to toggle source
Calls superclass method
# 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