Sha256: 07348cbf0070549ee56e5f6dab3ea5f49875209f82248f77a9239f38801f5b4d

Contents?: true

Size: 974 Bytes

Versions: 16

Compression:

Stored size: 974 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
            push "#{name} = $post_args"
          else
            push "#{name} = $post_args.splice(0, $post_args.length - #{args_to_keep})"
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
opal-1.8.3.rc1 lib/opal/nodes/args/extract_restarg.rb
opal-1.8.2 lib/opal/nodes/args/extract_restarg.rb
opal-1.8.1 lib/opal/nodes/args/extract_restarg.rb
opal-1.8.0 lib/opal/nodes/args/extract_restarg.rb
opal-1.8.0.beta1 lib/opal/nodes/args/extract_restarg.rb
opal-1.7.4 lib/opal/nodes/args/extract_restarg.rb
opal-1.8.0.alpha1 lib/opal/nodes/args/extract_restarg.rb
opal-1.7.3 lib/opal/nodes/args/extract_restarg.rb
opal-1.7.2 lib/opal/nodes/args/extract_restarg.rb
opal-1.7.1 lib/opal/nodes/args/extract_restarg.rb
opal-1.7.0 lib/opal/nodes/args/extract_restarg.rb
opal-1.7.0.rc1 lib/opal/nodes/args/extract_restarg.rb
opal-1.6.1 lib/opal/nodes/args/extract_restarg.rb
opal-1.6.0 lib/opal/nodes/args/extract_restarg.rb
opal-1.6.0.rc1 lib/opal/nodes/args/extract_restarg.rb
opal-1.6.0.alpha1 lib/opal/nodes/args/extract_restarg.rb