Sha256: 74e8762fd8c4b550188343e366bea184c748325d3beb8e8eedb0a9ca30821595

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

module RewritersHelper
  def s(type, *children)
    ::Opal::AST::Node.new(type, children)
  end

  def rewritten(ast)
    described_class.new.process(ast)
  end

  alias :rewrite :rewritten

  def expect_rewritten(ast)
    expect(rewritten(ast))
  end

  def expect_no_rewriting_for(ast)
    expect_rewritten(ast).to eq(ast)
  end

  def parse(source)
    buffer = Opal::Parser::SourceBuffer.new('(eval)')
    buffer.source = source
    parser = Opal::Parser.default_parser
    parser.parse(buffer)
  end

  alias :ast_of :parse

  def parse_without_rewriting(source)
    buffer = Opal::Parser::SourceBuffer.new('(eval)')
    buffer.source = source
    parser = Parser::Ruby31.new
    parser.parse(buffer)
  end
end

RSpec.shared_examples 'it rewrites source-to-source' do |from_source, to_source|
  it "rewrites source #{from_source} to source #{to_source}" do
    initial = ast_of(from_source)
    rewritten = self.rewritten(initial)
    expected = ast_of(to_source)

    expect(rewritten).to eq(expected)
  end
end

RSpec.shared_examples 'it rewrites source-to-AST' do |from_source, to_ast|
  it "rewrites source #{from_source} to AST #{to_ast}" do
    initial = ast_of(from_source)
    rewritten = self.rewritten(initial)

    expect(rewritten).to eq(to_ast)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
opal-1.4.1 spec/support/rewriters_helper.rb
opal-1.4.0 spec/support/rewriters_helper.rb
opal-1.4.0.alpha1 spec/support/rewriters_helper.rb