require 'opal/nodes/base'

module Opal
  module Nodes
    # FIXME: needs rewrite
    class ArglistNode < Base
      handle :arglist

      def compile
        code, work = [], []

        children.each do |current|
          splat = current.first == :splat
          arg = expr(current)

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

              code << fragment(".concat(Opal.to_a(") << arg << fragment("))")
            end

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

        unless work.empty?
          join = work

          if code.empty?
            code = join
          else
            code << fragment(".concat(Opal.to_a(") << join << fragment("))")
          end
        end

        push(*code)
      end
    end
  end
end

require 'opal/nodes/base'

module Opal
  module Nodes
    # FIXME: needs rewrite
    class ArglistNode < Base
      handle :arglist

      def compile
        code, work = [], []

        children.each do |current|
          splat = current.first == :splat
          arg = expr(current)

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

              code << fragment(".concat(") << arg << fragment(")")
            end

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

        unless work.empty?
          join = work

          if code.empty?
            code = join
          else
            code << fragment(".concat(") << join << fragment(")")
          end
        end

        push(*code)
      end
    end
  end
end

Remove previous bug-fix for splatting.
Remove previous bug-fix for splatting.

This is currently causing to emit `Opal.to_a(Opal.to_a(..))` for splats,
since the issue was then fixed downstream in the splat node handler.
