Sha256: 03c3f566655b4b61125e309e63f5cd06b7d8e986a2bbe8bd379c584791f6fb6c

Contents?: true

Size: 1.28 KB

Versions: 40

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

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

40 entries across 40 versions & 1 rubygems

Version Path
opal-1.8.3.rc1 lib/opal/nodes/array.rb
opal-1.8.2 lib/opal/nodes/array.rb
opal-1.8.1 lib/opal/nodes/array.rb
opal-1.8.0 lib/opal/nodes/array.rb
opal-1.8.0.beta1 lib/opal/nodes/array.rb
opal-1.7.4 lib/opal/nodes/array.rb
opal-1.8.0.alpha1 lib/opal/nodes/array.rb
opal-1.7.3 lib/opal/nodes/array.rb
opal-1.7.2 lib/opal/nodes/array.rb
opal-1.7.1 lib/opal/nodes/array.rb
opal-1.7.0 lib/opal/nodes/array.rb
opal-1.7.0.rc1 lib/opal/nodes/array.rb
opal-1.6.1 lib/opal/nodes/array.rb
opal-1.6.0 lib/opal/nodes/array.rb
opal-1.6.0.rc1 lib/opal/nodes/array.rb
opal-1.6.0.alpha1 lib/opal/nodes/array.rb
opal-1.5.1 lib/opal/nodes/array.rb
opal-1.5.0 lib/opal/nodes/array.rb
opal-1.5.0.rc1 lib/opal/nodes/array.rb
opal-1.4.1 lib/opal/nodes/array.rb