Sha256: a55cf84295615393758daf5018f65e5085243f6bd14bc1c7e4cd08e36ef2d1b9

Contents?: true

Size: 1.53 KB

Versions: 19

Compression:

Stored size: 1.53 KB

Contents

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

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

    # Parse, but drop the :top node
    def ast_of(source)
      parse(source).children.first
    end
  end

  module DSL
    def use_only_described_rewriter!
      around(:each) do |e|
        Opal::Rewriter.disable(except: described_class) { e.run }
      end
    end
  end

  include Common

  def self.included(klass)
    klass.extend(Common)
    klass.extend(DSL)
  end

  def rewriter
    described_class.new
  end

  def rewritten(ast = input)
    rewriter.process(ast)
  end

  alias rewrite rewritten
  alias processed rewritten

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

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

  def parse_without_rewriting(source)
    Opal::Rewriter.disable { parse(source) }
  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
    rewritten = parse(from_source)
    expected = parse(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
    rewritten = parse(from_source).children.first

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

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
opal-1.8.3.rc1 spec/support/rewriters_helper.rb
opal-1.8.2 spec/support/rewriters_helper.rb
opal-1.8.1 spec/support/rewriters_helper.rb
opal-1.8.0 spec/support/rewriters_helper.rb
opal-1.8.0.beta1 spec/support/rewriters_helper.rb
opal-1.7.4 spec/support/rewriters_helper.rb
opal-1.8.0.alpha1 spec/support/rewriters_helper.rb
opal-1.7.3 spec/support/rewriters_helper.rb
opal-1.7.2 spec/support/rewriters_helper.rb
opal-1.7.1 spec/support/rewriters_helper.rb
opal-1.7.0 spec/support/rewriters_helper.rb
opal-1.7.0.rc1 spec/support/rewriters_helper.rb
opal-1.6.1 spec/support/rewriters_helper.rb
opal-1.6.0 spec/support/rewriters_helper.rb
opal-1.6.0.rc1 spec/support/rewriters_helper.rb
opal-1.6.0.alpha1 spec/support/rewriters_helper.rb
opal-1.5.1 spec/support/rewriters_helper.rb
opal-1.5.0 spec/support/rewriters_helper.rb
opal-1.5.0.rc1 spec/support/rewriters_helper.rb