Sha256: e59de7600e727838205504f5d4fd7a81f824ac4e037a77f271228865f190143b

Contents?: true

Size: 1.71 KB

Versions: 63

Compression:

Stored size: 1.71 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

      parse_all(*statements, state: 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]
            # relocate property comment to first method
            [body[i], body[j]].each do |node|
              unless @comments[node].empty?
                node.children[1].values.first.each do |key, value| 
                  if [:get, :set].include? key and Parser::AST::Node === value
                    @comments[value] = @comments[node]
                    break
                  end
                end
              end
            end

            # merge properties
            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

63 entries across 63 versions & 1 rubygems

Version Path
ruby2js-3.6.1 lib/ruby2js/converter/begin.rb
ruby2js-3.6.0 lib/ruby2js/converter/begin.rb
ruby2js-3.5.3 lib/ruby2js/converter/begin.rb
ruby2js-3.5.2 lib/ruby2js/converter/begin.rb
ruby2js-3.5.1 lib/ruby2js/converter/begin.rb
ruby2js-3.5.0 lib/ruby2js/converter/begin.rb
ruby2js-3.4.0 lib/ruby2js/converter/begin.rb
ruby2js-3.3.6 lib/ruby2js/converter/begin.rb
ruby2js-3.3.5 lib/ruby2js/converter/begin.rb
ruby2js-3.3.4 lib/ruby2js/converter/begin.rb
ruby2js-3.3.3 lib/ruby2js/converter/begin.rb
ruby2js-3.3.2 lib/ruby2js/converter/begin.rb
ruby2js-3.3.1 lib/ruby2js/converter/begin.rb
ruby2js-3.3.0 lib/ruby2js/converter/begin.rb
ruby2js-3.2.0 lib/ruby2js/converter/begin.rb
ruby2js-3.1.2 lib/ruby2js/converter/begin.rb
ruby2js-3.1.1 lib/ruby2js/converter/begin.rb
ruby2js-3.1.0 lib/ruby2js/converter/begin.rb
ruby2js-3.0.15 lib/ruby2js/converter/begin.rb
ruby2js-3.0.14 lib/ruby2js/converter/begin.rb