Sha256: 7e6081a86fad116e7b82d826450ebd8981d305ef8feced23c7c1b86a4b8407f1

Contents?: true

Size: 1.22 KB

Versions: 7

Compression:

Stored size: 1.22 KB

Contents

module Ruby2JS
  class Converter

    # (hash
    #   (pair
    #     (sym :name)
    #     (str "value")))

    handle :hash do |*pairs|
      pairs.map! do |node|
        raise NotImplementedError, "kwsplat" if node.type == :kwsplat

        begin
          block_this, block_depth = @block_this, @block_depth
          @block_this, @block_depth = false, 0

          left, right = node.children
          if left.type == :prop
            result = []
            if right[:get]
              result << "get #{left.children[0]}#{
                parse(right[:get]).sub(/^function/,'')}"
            end
            if right[:set]
              result << "set #{left.children[0]}#{
                parse(right[:set]).sub(/^function/,'')}"
            end
            result
          else
            key = parse left
            key = $1 if key =~ /\A"([a-zA-Z_$][a-zA-Z_$0-9]*)"\Z/
            "#{key}: #{parse right}"
          end

        ensure
          @block_this, @block_depth = block_this, block_depth
        end
      end

      pairs.flatten!

      if pairs.map {|item| item.length+2}.reduce(&:+).to_i < @width-10
        "{#{ pairs.join(', ') }}"
      else
        "{#@nl#{ pairs.join(",#@ws") }#@nl}"
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ruby2js-1.12.1 lib/ruby2js/converter/hash.rb
ruby2js-1.12.0 lib/ruby2js/converter/hash.rb
ruby2js-1.11.1 lib/ruby2js/converter/hash.rb
ruby2js-1.11.0 lib/ruby2js/converter/hash.rb
ruby2js-1.10.0 lib/ruby2js/converter/hash.rb
ruby2js-1.9.3 lib/ruby2js/converter/hash.rb
ruby2js-1.9.2 lib/ruby2js/converter/hash.rb