Sha256: 5248864c64617f58d507ae126e4ccbed9f90798df00d021f4c6539072f44b060

Contents?: true

Size: 1.59 KB

Versions: 10

Compression:

Stored size: 1.59 KB

Contents

require 'lib/spec_helper'
require 'support/rewriters_helper'
require 'opal/rewriters/forward_args'

RSpec.describe Opal::Rewriters::ForwardArgs do
  include RewritersHelper
  extend  RewritersHelper

  before(:each) { Opal::Rewriters::ForRewriter.reset_tmp_counter! }

  correct_names = proc do |ast|
    case ast
    when Opal::AST::Node
      ast.children.map do |i|
        correct_names.(i)
      end.yield_self { |children| s(ast.type, *children) }
    when :fwd_rest
      "$fwd_rest"
    when :fwd_block
      "$fwd_block"
    else
      ast
    end
  end

  include_examples 'it rewrites source-to-AST', <<~ENDSOURCE, correct_names.(parse(<<~ENDDEST))
    def forward(...)
      other(...)
    end
  ENDSOURCE
    def forward(*fwd_rest, &fwd_block)
      other(*fwd_rest, &fwd_block)
    end
  ENDDEST

  include_examples 'it rewrites source-to-AST', <<~ENDSOURCE, correct_names.(parse(<<~ENDDEST))
    def forward(first_arg, ...)
      other(first_arg, second_arg, ...)
      other(other_arg, ...)
      other(...)
    end
  ENDSOURCE
    def forward(first_arg, *fwd_rest, &fwd_block)
      other(first_arg, second_arg, *fwd_rest, &fwd_block)
      other(other_arg, *fwd_rest, &fwd_block)
      other(*fwd_rest, &fwd_block)
    end
  ENDDEST

  # Not supported by the parser (nor by the rewriter which would have to rearrange the arguments)

  # include_examples 'it rewrites source-to-AST', <<~ENDSOURCE, correct_names.(parse(<<~ENDDEST))
  #   def forward(a:, ...)
  #     other(...)
  #   end
  # ENDSOURCE
  #   def forward(*fwd_rest, a:, &fwd_block)
  #     other(*fwd_rest, &fwd_block)
  #   end
  # ENDDEST
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
opal-1.4.1 spec/lib/rewriters/forward_args_spec.rb
opal-1.4.0 spec/lib/rewriters/forward_args_spec.rb
opal-1.4.0.alpha1 spec/lib/rewriters/forward_args_spec.rb
opal-1.3.2 spec/lib/rewriters/forward_args_spec.rb
opal-1.3.1 spec/lib/rewriters/forward_args_spec.rb
opal-1.3.0 spec/lib/rewriters/forward_args_spec.rb
opal-1.3.0.rc1 spec/lib/rewriters/forward_args_spec.rb
opal-1.3.0.alpha1 spec/lib/rewriters/forward_args_spec.rb
opal-1.2.0 spec/lib/rewriters/forward_args_spec.rb
opal-1.2.0.beta1 spec/lib/rewriters/forward_args_spec.rb