Sha256: 801849fd2e1c743df542e35c176ba9555ccbfd831b7b5b0b0b3bc064354d4555

Contents?: true

Size: 1.57 KB

Versions: 34

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, :lambda), 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

34 entries across 34 versions & 1 rubygems

Version Path
ruby2js-2.0.6 lib/ruby2js/converter/module.rb
ruby2js-2.0.5 lib/ruby2js/converter/module.rb
ruby2js-2.0.4 lib/ruby2js/converter/module.rb
ruby2js-2.0.3 lib/ruby2js/converter/module.rb
ruby2js-2.0.2 lib/ruby2js/converter/module.rb
ruby2js-2.0.1 lib/ruby2js/converter/module.rb
ruby2js-2.0.0 lib/ruby2js/converter/module.rb
ruby2js-1.15.1 lib/ruby2js/converter/module.rb
ruby2js-1.15.0 lib/ruby2js/converter/module.rb
ruby2js-1.14.1 lib/ruby2js/converter/module.rb
ruby2js-1.14.0 lib/ruby2js/converter/module.rb
ruby2js-1.13.1 lib/ruby2js/converter/module.rb
ruby2js-1.13.0 lib/ruby2js/converter/module.rb
ruby2js-1.12.2 lib/ruby2js/converter/module.rb
ruby2js-1.12.1 lib/ruby2js/converter/module.rb
ruby2js-1.12.0 lib/ruby2js/converter/module.rb
ruby2js-1.11.1 lib/ruby2js/converter/module.rb
ruby2js-1.11.0 lib/ruby2js/converter/module.rb
ruby2js-1.10.0 lib/ruby2js/converter/module.rb
ruby2js-1.9.3 lib/ruby2js/converter/module.rb