Sha256: 97c05778a3275a02c6ba9f1065d9df91521f211b8a3c0d7f3d66c5001f2bb585

Contents?: true

Size: 976 Bytes

Versions: 11

Compression:

Stored size: 976 Bytes

Contents

# frozen_string_literal: true

require 'opal/nodes/base'

module Opal
  module Nodes
    module Args
      # This node is responsible for extracting a splat argument from post-arguments
      #
      # args_to_keep is the number of required post-arguments
      #
      #   def m(*a, b, c, d); end
      # becomes something like:
      #   a = post_args[0..-3]
      #   post_args = post_args[-3..-1]
      #
      class ExtractRestarg < Base
        handle :extract_restarg
        children :name, :args_to_keep

        def compile
          # def m(*)
          # arguments are assigned to `$rest_arg` for super call
          name = self.name || '$rest_arg'

          add_temp name

          if args_to_keep == 0
            # no post-args, we are free to grab everything
            line "#{name} = $post_args;"
          else
            line "#{name} = $post_args.splice(0, $post_args.length - #{args_to_keep});"
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
opal-1.5.1 lib/opal/nodes/args/extract_restarg.rb
opal-1.5.0 lib/opal/nodes/args/extract_restarg.rb
opal-1.5.0.rc1 lib/opal/nodes/args/extract_restarg.rb
opal-1.4.1 lib/opal/nodes/args/extract_restarg.rb
opal-1.4.0 lib/opal/nodes/args/extract_restarg.rb
opal-1.4.0.alpha1 lib/opal/nodes/args/extract_restarg.rb
opal-1.3.2 lib/opal/nodes/args/extract_restarg.rb
opal-1.3.1 lib/opal/nodes/args/extract_restarg.rb
opal-1.3.0 lib/opal/nodes/args/extract_restarg.rb
opal-1.3.0.rc1 lib/opal/nodes/args/extract_restarg.rb
opal-1.3.0.alpha1 lib/opal/nodes/args/extract_restarg.rb