Sha256: d9cf8850ef465fc6cc67c06c4424c147a07a65b0323cfce41557ae8b76f37205

Contents?: true

Size: 1.25 KB

Versions: 16

Compression:

Stored size: 1.25 KB

Contents

module Ruby2JS
  class Converter

    # (begin
    #   (...)
    #   (...))

    handle :begin do |*statements|
      state = @state
      props = false

      statements.map! do |statement|
        case statement and statement.type
        when :defs, :defp
          props = true
          @ast = statement
          transform_defs(*statement.children)
        when :prop
          props = true
          statement
        else
          statement
        end
      end

      if props
        combine_properties(statements) if props
        statements.compact!
      end

      statements.map{ |statement| parse statement, state }.join(@sep)
    end

    def combine_properties(body)
      for i in 0...body.length-1
        next unless body[i] and body[i].type == :prop
        for j in i+1...body.length
          break unless body[j] and body[j].type == :prop
          if body[i].children[0] == body[j].children[0]
            merge = Hash[(body[i].children[1].to_a+body[j].children[1].to_a).
              group_by {|name, value| name.to_s}.map {|name, values|
              [name, values.map(&:last).reduce(:merge)]}]
            body[j] = s(:prop, body[j].children[0], merge)
            body[i] = nil
            break
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ruby2js-1.15.1 lib/ruby2js/converter/begin.rb
ruby2js-1.15.0 lib/ruby2js/converter/begin.rb
ruby2js-1.14.1 lib/ruby2js/converter/begin.rb
ruby2js-1.14.0 lib/ruby2js/converter/begin.rb
ruby2js-1.13.1 lib/ruby2js/converter/begin.rb
ruby2js-1.13.0 lib/ruby2js/converter/begin.rb
ruby2js-1.12.2 lib/ruby2js/converter/begin.rb
ruby2js-1.12.1 lib/ruby2js/converter/begin.rb
ruby2js-1.12.0 lib/ruby2js/converter/begin.rb
ruby2js-1.11.1 lib/ruby2js/converter/begin.rb
ruby2js-1.11.0 lib/ruby2js/converter/begin.rb
ruby2js-1.10.0 lib/ruby2js/converter/begin.rb
ruby2js-1.9.3 lib/ruby2js/converter/begin.rb
ruby2js-1.9.2 lib/ruby2js/converter/begin.rb
ruby2js-1.9.1 lib/ruby2js/converter/begin.rb
ruby2js-1.9.0 lib/ruby2js/converter/begin.rb