Sha256: 2fec93cd49f3add75078633ab27ee8ff2b33b8432008b8f2a9cd7d5283aaf643

Contents?: true

Size: 1.25 KB

Versions: 43

Compression:

Stored size: 1.25 KB

Contents

require 'opal/nodes/base'

module Opal
  module Nodes
    class ArrayNode < Base
      handle :array

      def compile
        return push('[]') if children.empty?

        code, work = [], []

        children.each do |child|
          splat = child.type == :splat
          part  = expr(child)

          if splat
            if work.empty?
              if code.empty?
                code << fragment("[].concat(") << part << fragment(")")
              else
                code << fragment(".concat(") << part << fragment(")")
              end
            else
              if code.empty?
                code << fragment("[") << work << fragment("]")
              else
                code << fragment(".concat([") << work << fragment("])")
              end

              code << fragment(".concat(") << part << fragment(")")
            end
            work = []
          else
            work << fragment(", ") unless work.empty?
            work << part
          end
        end

        unless work.empty?
          join = [fragment("["), work, fragment("]")]

          if code.empty?
            code = join
          else
            code.push([fragment(".concat("), join, fragment(")")])
          end
        end

        push code
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 3 rubygems

Version Path
opal-0.10.6 lib/opal/nodes/array.rb
opal-0.10.6.beta lib/opal/nodes/array.rb
opal-0.10.5 lib/opal/nodes/array.rb
opal-0.10.4 lib/opal/nodes/array.rb
opal-0.10.3 lib/opal/nodes/array.rb
opal-0.10.2 lib/opal/nodes/array.rb
opal-0.10.1 lib/opal/nodes/array.rb
opal-0.10.0 lib/opal/nodes/array.rb
opal-0.10.0.rc2 lib/opal/nodes/array.rb
opal-0.9.4 lib/opal/nodes/array.rb
opal-0.9.3 lib/opal/nodes/array.rb
opal-0.10.0.rc1 lib/opal/nodes/array.rb
opal-0.10.0.beta5 lib/opal/nodes/array.rb
opal-0.10.0.beta4 lib/opal/nodes/array.rb
opal-0.10.0.beta3 lib/opal/nodes/array.rb
opal-0.10.0.beta2 lib/opal/nodes/array.rb
opal-0.10.0.beta1 lib/opal/nodes/array.rb
opal-0.9.2 lib/opal/nodes/array.rb
opal-0.9.0 lib/opal/nodes/array.rb
opal-0.9.0.rc1 lib/opal/nodes/array.rb