Sha256: 570f79e1ffe7fb1fbc4dd9417589964d6cf82e8af52b5a7197945de31046529b

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 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_depth, block_this, block_hash = @block_depth, @block_this, false
          left, right = node.children

          if Hash === right or right.type == :block
            @block_depth, block_hash = 0, true
          end

          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
          if block_hash
            @block_depth = block_depth
            @block_this = block_this
          end
        end
      end

      pairs.flatten!

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby2js-1.15.1 lib/ruby2js/converter/hash.rb
ruby2js-1.15.0 lib/ruby2js/converter/hash.rb