Sha256: 3e1740e59d82aff57f3d7c6586348a18a43e666b86054ca6248dd5bf3a9c7615

Contents?: true

Size: 1.57 KB

Versions: 47

Compression:

Stored size: 1.57 KB

Contents

module Ruby2JS
  class Converter

    # (module
    #   (const nil :A)
    #   (...)

    handle :module do |name, *body|
      symbols = [] 

      while body.length == 1 and body.first.type == :begin
        body = body.first.children
      end

      visibility = :public
      omit = []

      body.each do |node|
        if node.type == :send and node.children.first == nil
          if [:public, :private, :protected].include? node.children[1]
            if node.children.length == 2
              visibility = node.children[1]
              omit << node
            elsif node.children[1] == :public
              omit << node
              node.children[2..-1].each do |sym|
                symbols << sym.children.first if sym.type == :sym 
              end
            end
          end
        end

        next unless visibility == :public

        if node.type == :casgn and node.children.first == nil
          symbols << node.children[1]
        elsif node.type == :def
          symbols << node.children.first
        elsif node.type == :class and node.children.first.children.first == nil
          symbols << node.children.first.children.last
        end
      end

      body = body - omit + [s(:return, s(:hash, 
        *symbols.map {|sym| s(:pair, s(:sym, sym), s(:lvar, sym))}))]

      body = s(:send, s(:block, s(:send, nil, :proc), s(:args),
        s(:begin, *body)), :[])
      if name.children.first == nil
        parse s(:lvasgn, name.children.last, body)
      else
        parse s(:send, name.children.first, "#{name.children.last}=", body)
      end
    end
  end
end

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
ruby2js-2.1.14 lib/ruby2js/converter/module.rb
ruby2js-2.1.13 lib/ruby2js/converter/module.rb
ruby2js-2.1.12 lib/ruby2js/converter/module.rb
ruby2js-2.1.11 lib/ruby2js/converter/module.rb
ruby2js-2.1.10 lib/ruby2js/converter/module.rb
ruby2js-2.1.9 lib/ruby2js/converter/module.rb
ruby2js-2.1.8 lib/ruby2js/converter/module.rb
ruby2js-2.1.7 lib/ruby2js/converter/module.rb
ruby2js-2.1.6 lib/ruby2js/converter/module.rb
ruby2js-2.1.5 lib/ruby2js/converter/module.rb
ruby2js-2.1.4 lib/ruby2js/converter/module.rb
ruby2js-2.1.3 lib/ruby2js/converter/module.rb
ruby2js-2.1.2 lib/ruby2js/converter/module.rb
ruby2js-2.1.1 lib/ruby2js/converter/module.rb
ruby2js-2.1.0 lib/ruby2js/converter/module.rb
ruby2js-2.0.18 lib/ruby2js/converter/module.rb
ruby2js-2.0.17 lib/ruby2js/converter/module.rb
ruby2js-2.0.16 lib/ruby2js/converter/module.rb
ruby2js-2.0.15 lib/ruby2js/converter/module.rb
ruby2js-2.0.14 lib/ruby2js/converter/module.rb