class Opal::Nodes::ArityCheckNode

Public Class Methods

new(*) click to toggle source
Calls superclass method
# File lib/opal/nodes/args/arity_check.rb, line 12
def initialize(*)
  super

  arguments = Rewriters::Arguments.new(args_node.children)

  @args      = arguments.args
  @optargs   = arguments.optargs
  @restarg   = arguments.restarg
  @postargs  = arguments.postargs
  @kwargs    = arguments.kwargs
  @kwoptargs = arguments.kwoptargs
  @kwrestarg = arguments.kwrestarg
  @kwnilarg  = arguments.kwnilarg
end

Public Instance Methods

all_args() click to toggle source
# File lib/opal/nodes/args/arity_check.rb, line 44
def all_args
  @all_args ||= [*@args, *@optargs, @restarg, *@postargs, *kwargs].compact
end
arity() click to toggle source
# File lib/opal/nodes/args/arity_check.rb, line 76
def arity
  if @restarg || @optargs.any? || has_only_optional_kwargs?
    negative_arity
  else
    positive_arity
  end
end
arity_checks() click to toggle source

Returns an array of JS conditions for raising and argument error caused by arity check

# File lib/opal/nodes/args/arity_check.rb, line 50
def arity_checks
  return @arity_checks if defined?(@arity_checks)

  arity = all_args.size
  arity -= @optargs.size

  arity -= 1 if @restarg

  arity -= kwargs.size

  arity = -arity - 1 if !@optargs.empty? || !kwargs.empty? || @restarg

  @arity_checks = []

  if arity < 0 # splat or opt args
    min_arity = -(arity + 1)
    max_arity = all_args.size
    @arity_checks << "$arity < #{min_arity}" if min_arity > 0
    @arity_checks << "$arity > #{max_arity}" unless @restarg
  else
    @arity_checks << "$arity !== #{arity}"
  end

  @arity_checks
end
compile() click to toggle source
# File lib/opal/nodes/args/arity_check.rb, line 27
def compile
  scope.arity = arity

  return unless compiler.arity_check?

  unless arity_checks.empty?
    helper :ac
    meth = scope.mid.to_s.inspect
    line 'var $arity = arguments.length;'
    push " if (#{arity_checks.join(' || ')}) { $ac($arity, #{arity}, this, #{meth}); }"
  end
end
has_only_optional_kwargs?() click to toggle source
# File lib/opal/nodes/args/arity_check.rb, line 109
def has_only_optional_kwargs?
  kwargs.any? && kwargs.all? { |arg| %i[kwoptarg kwrestarg].include?(arg.type) }
end
has_required_kwargs?() click to toggle source
# File lib/opal/nodes/args/arity_check.rb, line 113
def has_required_kwargs?
  kwargs.any? { |arg| arg.type == :kwarg }
end
kwargs() click to toggle source
# File lib/opal/nodes/args/arity_check.rb, line 40
def kwargs
  [*@kwargs, *@kwoptargs, @kwrestarg].compact
end
negative_arity() click to toggle source
# File lib/opal/nodes/args/arity_check.rb, line 84
def negative_arity
  required_plain_args = all_args.select do |arg|
    %i[arg mlhs].include?(arg.type)
  end

  result = required_plain_args.size

  if has_required_kwargs?
    result += 1
  end

  result = -result - 1

  result
end
positive_arity() click to toggle source
# File lib/opal/nodes/args/arity_check.rb, line 100
def positive_arity
  result = all_args.size

  result -= kwargs.size
  result += 1 if kwargs.any?

  result
end